22#include "absl/base/thread_annotations.h"
23#include "absl/strings/string_view.h"
24#include "absl/synchronization/mutex.h"
25#include "absl/types/span.h"
26#include "google/protobuf/repeated_field.h"
27#include "google/protobuf/repeated_ptr_field.h"
34class PrinterMessageCallbackImpl {
36 PrinterMessageCallbackImpl(std::ostream& output_stream,
37 const absl::string_view prefix)
38 : output_stream_(output_stream), prefix_(prefix) {}
40 void Call(
const std::vector<std::string>& messages) {
41 const absl::MutexLock lock(&mutex_);
42 for (
const std::string& message : messages) {
43 output_stream_ << prefix_ << message <<
'\n';
45 output_stream_.flush();
50 std::ostream& output_stream_ ABSL_GUARDED_BY(mutex_);
51 const std::string prefix_;
54void PushBack(absl::Span<const std::string> messages,
55 std::vector<std::string>*
const sink) {
56 sink->insert(sink->end(), messages.begin(), messages.end());
59void PushBack(
const std::vector<std::string>& messages,
60 google::protobuf::RepeatedPtrField<std::string>*
const sink) {
61 std::copy(messages.begin(), messages.end(),
62 google::protobuf::RepeatedFieldBackInserter(sink));
65template <
typename Sink>
66class VectorLikeMessageCallbackImpl {
68 explicit VectorLikeMessageCallbackImpl(Sink*
const sink)
69 : sink_(ABSL_DIE_IF_NULL(sink)) {}
71 void Call(
const std::vector<std::string>& messages) {
72 const absl::MutexLock lock(&mutex_);
73 PushBack(messages, sink_);
84 const absl::string_view prefix) {
89 std::make_shared<PrinterMessageCallbackImpl>(output_stream, prefix);
91 [=](
const std::vector<std::string>& messages) { impl->Call(messages); };
96 return [=](
const std::vector<std::string>& messages) {
97 for (
const std::string& message : messages) {
98 LOG(INFO).AtLocation(loc.
file_name(), loc.
line()) << prefix << message;
105 return [=](
const std::vector<std::string>& messages) {
106 for (
const std::string& message : messages) {
107 VLOG(level).AtLocation(loc.
file_name(), loc.
line()) << prefix << message;
113 CHECK(sink !=
nullptr);
118 std::make_shared<VectorLikeMessageCallbackImpl<std::vector<std::string>>>(
121 [=](
const std::vector<std::string>& messages) { impl->Call(messages); };
125 google::protobuf::RepeatedPtrField<std::string>* sink) {
126 CHECK(sink !=
nullptr);
130 const auto impl = std::make_shared<VectorLikeMessageCallbackImpl<
131 google::protobuf::RepeatedPtrField<std::string>>>(sink);
133 [=](
const std::vector<std::string>& messages) { impl->Call(messages); };
constexpr std::uint_least32_t line() const
The line number of the captured source location.
constexpr const char * file_name() const
The file name of the captured source location.
An object oriented wrapper for quadratic constraints in ModelStorage.
std::function< void(const std::vector< std::string > &)> MessageCallback
MessageCallback RepeatedPtrFieldMessageCallback(google::protobuf::RepeatedPtrField< std::string > *sink)
MessageCallback PrinterMessageCallback(std::ostream &output_stream, const absl::string_view prefix)
MessageCallback VLoggerMessageCallback(int level, absl::string_view prefix, absl::SourceLocation loc)
MessageCallback InfoLoggerMessageCallback(const absl::string_view prefix, const absl::SourceLocation loc)
MessageCallback VectorMessageCallback(std::vector< std::string > *sink)