Google OR-Tools v9.11
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-2024 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 "google/protobuf/repeated_field.h"
26#include "google/protobuf/repeated_ptr_field.h"
29
31namespace {
32
33class PrinterMessageCallbackImpl {
34 public:
35 PrinterMessageCallbackImpl(std::ostream& output_stream,
36 const absl::string_view prefix)
37 : output_stream_(output_stream), prefix_(prefix) {}
38
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';
43 }
44 output_stream_.flush();
45 }
46
47 private:
48 absl::Mutex mutex_;
49 std::ostream& output_stream_ ABSL_GUARDED_BY(mutex_);
50 const std::string prefix_;
51};
52
53void PushBack(const std::vector<std::string>& messages,
54 std::vector<std::string>* const sink) {
55 sink->insert(sink->end(), messages.begin(), messages.end());
56}
57
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));
62}
63
64template <typename Sink>
65class VectorLikeMessageCallbackImpl {
66 public:
67 explicit VectorLikeMessageCallbackImpl(Sink* const sink)
68 : sink_(ABSL_DIE_IF_NULL(sink)) {}
69
70 void Call(const std::vector<std::string>& messages) {
71 const absl::MutexLock lock(&mutex_);
72 PushBack(messages, sink_);
73 }
74
75 private:
76 absl::Mutex mutex_;
77 Sink* const sink_;
78};
79
80} // namespace
81
82MessageCallback PrinterMessageCallback(std::ostream& output_stream,
83 const absl::string_view prefix) {
84 // Here we must use an std::shared_ptr since std::function requires that its
85 // input is copyable. And PrinterMessageCallbackImpl can't be copyable since
86 // it uses an absl::Mutex that is not.
87 const auto impl =
88 std::make_shared<PrinterMessageCallbackImpl>(output_stream, prefix);
89 return
90 [=](const std::vector<std::string>& messages) { impl->Call(messages); };
91}
92
93MessageCallback InfoLoggerMessageCallback(const absl::string_view prefix,
94 const absl::SourceLocation loc) {
95 return [=](const std::vector<std::string>& messages) {
96 for (const std::string& message : messages) {
97 LOG(INFO).AtLocation(loc.file_name(), loc.line()) << prefix << message;
98 }
99 };
100}
101
102MessageCallback VLoggerMessageCallback(int level, absl::string_view prefix,
104 return [=](const std::vector<std::string>& messages) {
105 for (const std::string& message : messages) {
106 VLOG(level).AtLocation(loc.file_name(), loc.line()) << prefix << message;
107 }
108 };
109}
110
111MessageCallback VectorMessageCallback(std::vector<std::string>* sink) {
112 CHECK(sink != nullptr);
113 // Here we must use an std::shared_ptr since std::function requires that its
114 // input is copyable. And VectorMessageCallbackImpl can't be copyable since it
115 // uses an absl::Mutex that is not.
116 const auto impl =
117 std::make_shared<VectorLikeMessageCallbackImpl<std::vector<std::string>>>(
118 sink);
119 return
120 [=](const std::vector<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
132 [=](const std::vector<std::string>& messages) { impl->Call(messages); };
133}
134
135} // 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)
std::string message
Definition trace.cc:397