OR-Tools  9.6
message_callback.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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 
14 // IWYU pragma: private, include "ortools/math_opt/cpp/math_opt.h"
15 // IWYU pragma: friend "ortools/math_opt/cpp/.*"
16 
17 #ifndef OR_TOOLS_MATH_OPT_CPP_MESSAGE_CALLBACK_H_
18 #define OR_TOOLS_MATH_OPT_CPP_MESSAGE_CALLBACK_H_
19 
20 #include <functional>
21 #include <iostream>
22 #include <string>
23 #include <vector>
24 
25 #include "absl/strings/string_view.h"
27 
29 
30 // Callback function for messages callback sent by the solver.
31 //
32 // Each message represents a single output line from the solver, and each
33 // message does not contain any '\n' character in it.
34 //
35 // Thread-safety: a callback may be called concurrently from multiple
36 // threads. The users is expected to use proper synchronization primitives to
37 // deal with that.
38 using MessageCallback = std::function<void(const std::vector<std::string>&)>;
39 
40 // Returns a message callback function that prints its output to the given
41 // output stream, prefixing each line with the given prefix.
42 //
43 // For each call to the returned message callback, the output_stream is flushed.
44 //
45 // Usage:
46 //
47 // SolveArguments args;
48 // args.message_callback = PrinterMessageCallback(std::cerr, "solver logs> ");
49 MessageCallback PrinterMessageCallback(std::ostream& output_stream = std::cout,
50  absl::string_view prefix = "");
51 
52 } // namespace operations_research::math_opt
53 
54 #endif // OR_TOOLS_MATH_OPT_CPP_MESSAGE_CALLBACK_H_
MessageCallback PrinterMessageCallback(std::ostream &output_stream, const absl::string_view prefix)
std::function< void(const std::vector< std::string > &)> MessageCallback