Google OR-Tools v9.12
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/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"
30
32namespace {
33
34class PrinterMessageCallbackImpl {
35 public:
36 PrinterMessageCallbackImpl(std::ostream& output_stream,
37 const absl::string_view prefix)
38 : output_stream_(output_stream), prefix_(prefix) {}
39
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';
44 }
45 output_stream_.flush();
46 }
47
48 private:
49 absl::Mutex mutex_;
50 std::ostream& output_stream_ ABSL_GUARDED_BY(mutex_);
51 const std::string prefix_;
52};
53
54void PushBack(absl::Span<const std::string> messages,
55 std::vector<std::string>* const sink) {
56 sink->insert(sink->end(), messages.begin(), messages.end());
57}
58
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));
63}
64
65template <typename Sink>
66class VectorLikeMessageCallbackImpl {
67 public:
68 explicit VectorLikeMessageCallbackImpl(Sink* const sink)
69 : sink_(ABSL_DIE_IF_NULL(sink)) {}
70
71 void Call(const std::vector<std::string>& messages) {
72 const absl::MutexLock lock(&mutex_);
73 PushBack(messages, sink_);
74 }
75
76 private:
77 absl::Mutex mutex_;
78 Sink* const sink_;
79};
80
81} // namespace
82
83MessageCallback PrinterMessageCallback(std::ostream& output_stream,
84 const absl::string_view prefix) {
85 // Here we must use an std::shared_ptr since std::function requires that its
86 // input is copyable. And PrinterMessageCallbackImpl can't be copyable since
87 // it uses an absl::Mutex that is not.
88 const auto impl =
89 std::make_shared<PrinterMessageCallbackImpl>(output_stream, prefix);
90 return
91 [=](const std::vector<std::string>& messages) { impl->Call(messages); };
92}
93
94MessageCallback InfoLoggerMessageCallback(const absl::string_view prefix,
95 const absl::SourceLocation loc) {
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;
99 }
100 };
101}
102
103MessageCallback VLoggerMessageCallback(int level, absl::string_view prefix,
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;
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
121 [=](const std::vector<std::string>& messages) { impl->Call(messages); };
122}
123
125 google::protobuf::RepeatedPtrField<std::string>* sink) {
126 CHECK(sink != nullptr);
127 // Here we must use an std::shared_ptr since std::function requires that its
128 // input is copyable. And VectorMessageCallbackImpl can't be copyable since
129 // it uses an absl::Mutex that is not.
130 const auto impl = std::make_shared<VectorLikeMessageCallbackImpl<
131 google::protobuf::RepeatedPtrField<std::string>>>(sink);
132 return
133 [=](const std::vector<std::string>& messages) { impl->Call(messages); };
134}
135
136} // namespace operations_research::math_opt
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.
Definition gurobi_isv.cc:28
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)