Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
message_callback.cc
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <algorithm>
17#include <memory>
18#include <ostream>
19#include <string>
20#include <vector>
21
22#include "absl/base/thread_annotations.h"
23#include "absl/log/die_if_null.h"
24#include "absl/strings/string_view.h"
25#include "absl/synchronization/mutex.h"
26#include "absl/types/span.h"
27#include "google/protobuf/repeated_field.h"
28#include "google/protobuf/repeated_ptr_field.h"
31
33namespace {
34
35class PrinterMessageCallbackImpl {
36 public:
37 PrinterMessageCallbackImpl(std::ostream& output_stream,
38 const absl::string_view prefix)
39 : output_stream_(output_stream), prefix_(prefix) {}
40
41 void Call(absl::Span<const std::string> messages) {
42 const absl::MutexLock lock(mutex_);
43 for (const std::string& message : messages) {
44 output_stream_ << prefix_ << message << '\n';
45 }
46 output_stream_.flush();
47 }
48
49 private:
50 absl::Mutex mutex_;
51 std::ostream& output_stream_ ABSL_GUARDED_BY(mutex_);
52 const std::string prefix_;
53};
54
55void PushBack(absl::Span<const std::string> messages,
56 std::vector<std::string>* const sink) {
57 sink->insert(sink->end(), messages.begin(), messages.end());
58}
59
60void PushBack(absl::Span<const std::string> messages,
61 google::protobuf::RepeatedPtrField<std::string>* const sink) {
62 std::copy(messages.begin(), messages.end(),
63 google::protobuf::RepeatedFieldBackInserter(sink));
64}
65
66template <typename Sink>
67class VectorLikeMessageCallbackImpl {
68 public:
69 explicit VectorLikeMessageCallbackImpl(Sink* const sink)
70 : sink_(ABSL_DIE_IF_NULL(sink)) {}
71
72 void Call(absl::Span<const std::string> messages) {
73 const absl::MutexLock lock(mutex_);
74 PushBack(messages, sink_);
75 }
76
77 private:
78 absl::Mutex mutex_;
79 Sink* const sink_;
80};
81
82} // namespace
83
84MessageCallback PrinterMessageCallback(std::ostream& output_stream,
85 const absl::string_view prefix) {
86 // Here we must use an std::shared_ptr since std::function requires that its
87 // input is copyable. And PrinterMessageCallbackImpl can't be copyable since
88 // it uses an absl::Mutex that is not.
89 const auto impl =
90 std::make_shared<PrinterMessageCallbackImpl>(output_stream, prefix);
91 return [=](absl::Span<const std::string> messages) { impl->Call(messages); };
92}
93
94MessageCallback InfoLoggerMessageCallback(const absl::string_view prefix,
95 const absl::SourceLocation loc) {
96 return [=](absl::Span<const std::string> messages) {
97 for (const std::string& message : messages) {
98 LOG(INFO).AtLocation(loc.file_name(), loc.line()) << prefix << message;
99 }
100 };
101}
102
103MessageCallback VLoggerMessageCallback(int level, absl::string_view prefix,
105 return [=](absl::Span<const std::string> messages) {
106 for (const std::string& message : messages) {
107 VLOG(level).AtLocation(loc.file_name(), loc.line()) << prefix << message;
108 }
109 };
110}
111
112MessageCallback VectorMessageCallback(std::vector<std::string>* sink) {
113 CHECK(sink != nullptr);
114 // Here we must use an std::shared_ptr since std::function requires that its
115 // input is copyable. And VectorMessageCallbackImpl can't be copyable since it
116 // uses an absl::Mutex that is not.
117 const auto impl =
118 std::make_shared<VectorLikeMessageCallbackImpl<std::vector<std::string>>>(
119 sink);
120 return [=](absl::Span<const std::string> messages) { impl->Call(messages); };
121}
122
124 google::protobuf::RepeatedPtrField<std::string>* sink) {
125 CHECK(sink != nullptr);
126 // Here we must use an std::shared_ptr since std::function requires that its
127 // input is copyable. And VectorMessageCallbackImpl can't be copyable since
128 // it uses an absl::Mutex that is not.
129 const auto impl = std::make_shared<VectorLikeMessageCallbackImpl<
130 google::protobuf::RepeatedPtrField<std::string>>>(sink);
131 return [=](absl::Span<const std::string> messages) { impl->Call(messages); };
132}
133
134} // namespace operations_research::math_opt
constexpr std::uint_least32_t line() const
constexpr const char * file_name() const
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)