22#include "absl/base/thread_annotations.h"
23#include "absl/strings/string_view.h"
24#include "absl/synchronization/mutex.h"
25#include "google/protobuf/repeated_field.h"
26#include "google/protobuf/repeated_ptr_field.h"
33class PrinterMessageCallbackImpl {
35 PrinterMessageCallbackImpl(std::ostream& output_stream,
36 const absl::string_view prefix)
37 : output_stream_(output_stream), prefix_(prefix) {}
39 void Call(
const std::vector<std::string>& messages) {
40 const absl::MutexLock lock(&mutex_);
41 for (
const std::string&
message : messages) {
42 output_stream_ << prefix_ <<
message <<
'\n';
44 output_stream_.flush();
49 std::ostream& output_stream_ ABSL_GUARDED_BY(mutex_);
50 const std::string prefix_;
53void PushBack(
const std::vector<std::string>& messages,
54 std::vector<std::string>*
const sink) {
55 sink->insert(sink->end(), messages.begin(), messages.end());
58void PushBack(
const std::vector<std::string>& messages,
59 google::protobuf::RepeatedPtrField<std::string>*
const sink) {
60 std::copy(messages.begin(), messages.end(),
61 google::protobuf::RepeatedFieldBackInserter(sink));
64template <
typename Sink>
65class VectorLikeMessageCallbackImpl {
67 explicit VectorLikeMessageCallbackImpl(Sink*
const sink)
68 : sink_(ABSL_DIE_IF_NULL(sink)) {}
70 void Call(
const std::vector<std::string>& messages) {
71 const absl::MutexLock lock(&mutex_);
72 PushBack(messages, sink_);
83 const absl::string_view prefix) {
88 std::make_shared<PrinterMessageCallbackImpl>(output_stream, prefix);
90 [=](
const std::vector<std::string>& messages) { impl->Call(messages); };
95 return [=](
const std::vector<std::string>& messages) {
96 for (
const std::string&
message : messages) {
104 return [=](
const std::vector<std::string>& messages) {
105 for (
const std::string&
message : messages) {
112 CHECK(sink !=
nullptr);
117 std::make_shared<VectorLikeMessageCallbackImpl<std::vector<std::string>>>(
120 [=](
const std::vector<std::string>& messages) { impl->Call(messages); };
124 google::protobuf::RepeatedPtrField<std::string>* sink) {
125 CHECK(sink !=
nullptr);
129 const auto impl = std::make_shared<VectorLikeMessageCallbackImpl<
130 google::protobuf::RepeatedPtrField<std::string>>>(sink);
132 [=](
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)