OR-Tools  9.0
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 
21 namespace 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.
33 class SolverLogger {
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  // Logs a given information message and dispatch it to all callbacks.
54  void LogInfo(const char* source_filename, int source_line,
55  const std::string& message);
56 
57  private:
58  bool is_enabled_ = false;
59  bool log_to_stdout_ = false;
60  std::vector<std::function<void(const std::string& message)>> info_callbacks_;
61 };
62 
63 #define SOLVER_LOG(logger, ...) \
64  if ((logger)->LoggingIsEnabled()) \
65  (logger)->LogInfo(__FILE__, __LINE__, absl::StrCat(__VA_ARGS__))
66 
67 } // namespace operations_research
68 
69 #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:28
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