Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
message_callback.h
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
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 <ostream>
23#include <string>
24#include <vector>
25
26#include "absl/strings/string_view.h"
27#include "google/protobuf/repeated_ptr_field.h"
29
31
32// Callback function for messages callback sent by the solver.
33//
34// Each message represents a single output line from the solver, and each
35// message does not contain any '\n' character in it.
36//
37// Thread-safety: a callback may be called concurrently from multiple
38// threads. The users is expected to use proper synchronization primitives to
39// deal with that.
40using MessageCallback = std::function<void(const std::vector<std::string>&)>;
41
42// Returns a message callback function that prints its output to the given
43// output stream, prefixing each line with the given prefix.
44//
45// For each call to the returned message callback, the output_stream is flushed.
46//
47// Usage:
48//
49// SolveArguments args;
50// args.message_callback = PrinterMessageCallback(std::cerr, "solver logs> ");
51MessageCallback PrinterMessageCallback(std::ostream& output_stream = std::cout,
52 absl::string_view prefix = "");
53
54// Returns a message callback function that prints each line to LOG(INFO),
55// prefixing each line with the given prefix.
56//
57// Usage:
58//
59// SolveArguments args;
60// args.message_callback = InfoLoggerMessageCallback("[solver] ");
62 absl::string_view prefix = "",
64
65// Returns a message callback function that prints each line to VLOG(level),
66// prefixing each line with the given prefix.
67//
68// Usage:
69//
70// SolveArguments args;
71// args.message_callback = VLoggerMessageCallback(1, "[solver] ");
73 int level, absl::string_view prefix = "",
75
76// Returns a message callback function that aggregates all messages in the
77// provided vector.
78//
79// Usage:
80//
81// std::vector<std::string> msgs;
82// SolveArguments args;
83// args.message_callback = VectorMessageCallback(&msgs);
84MessageCallback VectorMessageCallback(std::vector<std::string>* sink);
85
86// Returns a message callback function that aggregates all messages in the
87// provided RepeatedPtrField.
88//
89// Usage:
90//
91// google::protobuf::RepeatedPtrField<std::string> msgs;
92// SolveArguments args;
93// args.message_callback = RepeatedPtrFieldMessageCallback(&msgs);
95 google::protobuf::RepeatedPtrField<std::string>* sink);
96
97} // namespace operations_research::math_opt
98
99#endif // OR_TOOLS_MATH_OPT_CPP_MESSAGE_CALLBACK_H_
static constexpr SourceLocation current()
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)