Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
message_callback_data.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 <cstddef>
17#include <optional>
18#include <string>
19#include <utility>
20#include <vector>
21
22#include "absl/strings/str_cat.h"
23#include "absl/strings/string_view.h"
24
25namespace operations_research {
26namespace math_opt {
27
28std::vector<std::string> MessageCallbackData::Parse(
29 const absl::string_view message) {
30 std::vector<std::string> strings;
31
32 // Iterate on all complete lines (lines ending with a '\n').
33 absl::string_view remainder = message;
34 for (std::size_t end = 0; end = remainder.find('\n'), end != remainder.npos;
35 remainder = remainder.substr(end + 1)) {
36 const auto line = remainder.substr(0, end);
37 if (!unfinished_line_.empty()) {
38 std::string new_message = std::move(unfinished_line_);
39 unfinished_line_.clear();
40 absl::StrAppend(&new_message, line);
41 strings.push_back(std::move(new_message));
42 } else {
43 strings.emplace_back(line);
44 }
45 }
46
47 // At the end of the loop, the remainder may contain the last unfinished line.
48 // This could be the first line too if the entire message does not contain
49 // '\n'.
50 absl::StrAppend(&unfinished_line_, remainder);
51
52 return strings;
53}
54
55std::vector<std::string> MessageCallbackData::Flush() {
56 if (unfinished_line_.empty()) {
57 return {};
58 }
59
60 std::vector<std::string> strings = {std::move(unfinished_line_)};
61 unfinished_line_.clear();
62 return strings;
63}
64
66 SolverInterface::MessageCallback user_message_callback)
67 : user_message_callback_(std::move(user_message_callback)) {}
68
71 return;
72 }
73 std::vector<std::string> messages;
74 {
75 absl::MutexLock lock(&mutex_);
76 messages = message_callback_data_.Parse(message);
77 }
78 // Do not hold lock during callback to user code.
79 if (!messages.empty()) {
80 user_message_callback_(messages);
81 }
82}
83
86 return;
87 }
88 std::vector<std::string> messages;
89 {
90 absl::MutexLock lock(&mutex_);
91 messages = message_callback_data_.Flush();
92 }
93 // Do not hold lock during callback to user code.
94 if (!messages.empty()) {
95 user_message_callback_(messages);
96 }
97}
98
99} // namespace math_opt
100} // namespace operations_research
BufferedMessageCallback(SolverInterface::MessageCallback user_message_callback)
std::vector< std::string > Parse(absl::string_view message)
std::function< void(const std::vector< std::string > &)> MessageCallback
In SWIG mode, we don't want anything besides these top-level includes.
STL namespace.
#include "ortools/base/logging.h"
Definition case.cc:32
int line
std::optional< int64_t > end
std::string message
Definition trace.cc:397