OR-Tools  9.3
util/logging.h
Go to the documentation of this file.
1// Copyright 2010-2021 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#ifndef OR_TOOLS_UTIL_LOGGING_H_
15#define OR_TOOLS_UTIL_LOGGING_H_
16
17#include <functional>
18#include <string>
19#include <vector>
20
21namespace operations_research {
22
23// Custom logger class. It allows passing callbacks to process log messages.
24// It also enables separating logs from multiple solvers running concurrently in
25// the same process.
26//
27// If no callbacks have been added, all logging will use the standard logging
28// facilities. As soon as one callback is added, it is disabled. Unless
29// ForceStandardLogging() has been called.
30//
31// Note that the callbacks will get the message unchanged. No '\n' will be
32// added.
34 public:
35 // Enables all logging.
36 //
37 // Note that this is used by the logging macro, but it actually do not
38 // disable logging if LogInfo() is called directly.
39 void EnableLogging(bool enable) { is_enabled_ = enable; }
40
41 // Returns true iff logging is enabled.
42 bool LoggingIsEnabled() const { return is_enabled_; }
43
44 // Should all messages be displayed on stdout ?
45 void SetLogToStdOut(bool enable) { log_to_stdout_ = enable; }
46
47 // Add a callback listening to all information messages.
48 //
49 // They will be run synchronously when LogInfo() is called.
51 std::function<void(const std::string& message)> callback);
52
53 // Removes all callbacks registered via AddInfoLoggingCallback().
55
56 // Returns the number of registered callbacks.
57 int NumInfoLoggingCallbacks() const { return info_callbacks_.size(); }
58
59 // Logs a given information message and dispatch it to all callbacks.
60 void LogInfo(const char* source_filename, int source_line,
61 const std::string& message);
62
63 private:
64 bool is_enabled_ = false;
65 bool log_to_stdout_ = false;
66 std::vector<std::function<void(const std::string& message)>> info_callbacks_;
67};
68
69#define SOLVER_LOG(logger, ...) \
70 if ((logger)->LoggingIsEnabled()) \
71 (logger)->LogInfo(__FILE__, __LINE__, absl::StrCat(__VA_ARGS__))
72
73} // namespace operations_research
74
75#endif // OR_TOOLS_UTIL_LOGGING_H_
void SetLogToStdOut(bool enable)
Definition: util/logging.h:45
void LogInfo(const char *source_filename, int source_line, const std::string &message)
Definition: util/logging.cc:30
void AddInfoLoggingCallback(std::function< void(const std::string &message)> callback)
Definition: util/logging.cc:23
MPCallback * callback
Collection of objects used to extend the Constraint Solver library.
std::string message
Definition: trace.cc:398