Files
ortools-clone/ortools/math_opt/cpp/message_callback.h

100 lines
3.5 KiB
C
Raw Permalink Normal View History

2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 Google LLC
2022-01-31 18:44:25 +01:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2022-09-05 18:30:25 +02:00
// IWYU pragma: private, include "ortools/math_opt/cpp/math_opt.h"
// IWYU pragma: friend "ortools/math_opt/cpp/.*"
2025-11-05 11:34:49 +01:00
#ifndef ORTOOLS_MATH_OPT_CPP_MESSAGE_CALLBACK_H_
#define ORTOOLS_MATH_OPT_CPP_MESSAGE_CALLBACK_H_
2022-01-31 18:44:25 +01:00
#include <functional>
#include <iostream>
2023-04-03 18:22:22 +02:00
#include <ostream>
2022-01-31 18:44:25 +01:00
#include <string>
2022-07-22 14:35:40 +02:00
#include <vector>
2022-01-31 18:44:25 +01:00
#include "absl/strings/string_view.h"
2023-05-31 14:27:19 +02:00
#include "google/protobuf/repeated_ptr_field.h"
2022-01-31 18:44:25 +01:00
#include "ortools/base/source_location.h"
namespace operations_research::math_opt {
// Callback function for messages callback sent by the solver.
//
// Each message represents a single output line from the solver, and each
// message does not contain any '\n' character in it.
//
// Thread-safety: a callback may be called concurrently from multiple
// threads. The users is expected to use proper synchronization primitives to
// deal with that.
using MessageCallback = std::function<void(const std::vector<std::string>&)>;
// Returns a message callback function that prints its output to the given
// output stream, prefixing each line with the given prefix.
//
// For each call to the returned message callback, the output_stream is flushed.
//
// Usage:
//
// SolveArguments args;
// args.message_callback = PrinterMessageCallback(std::cerr, "solver logs> ");
MessageCallback PrinterMessageCallback(std::ostream& output_stream = std::cout,
absl::string_view prefix = "");
2024-02-21 18:28:57 +01:00
// Returns a message callback function that prints each line to LOG(INFO),
// prefixing each line with the given prefix.
//
// Usage:
//
// SolveArguments args;
// args.message_callback = InfoLoggerMessageCallback("[solver] ");
MessageCallback InfoLoggerMessageCallback(
absl::string_view prefix = "",
absl::SourceLocation loc = absl::SourceLocation::current());
// Returns a message callback function that prints each line to VLOG(level),
// prefixing each line with the given prefix.
//
// Usage:
//
// SolveArguments args;
// args.message_callback = VLoggerMessageCallback(1, "[solver] ");
MessageCallback VLoggerMessageCallback(
int level, absl::string_view prefix = "",
absl::SourceLocation loc = absl::SourceLocation::current());
2023-04-03 18:22:22 +02:00
// Returns a message callback function that aggregates all messages in the
// provided vector.
//
// Usage:
//
// std::vector<std::string> msgs;
// SolveArguments args;
// args.message_callback = VectorMessageCallback(&msgs);
MessageCallback VectorMessageCallback(std::vector<std::string>* sink);
2023-05-31 14:27:19 +02:00
// Returns a message callback function that aggregates all messages in the
// provided RepeatedPtrField.
//
// Usage:
//
// google::protobuf::RepeatedPtrField<std::string> msgs;
// SolveArguments args;
// args.message_callback = RepeatedPtrFieldMessageCallback(&msgs);
MessageCallback RepeatedPtrFieldMessageCallback(
google::protobuf::RepeatedPtrField<std::string>* sink);
2022-01-31 18:44:25 +01:00
} // namespace operations_research::math_opt
2025-11-05 11:34:49 +01:00
#endif // ORTOOLS_MATH_OPT_CPP_MESSAGE_CALLBACK_H_