Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
1// Copyright 2010-2025 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 <cstdint>
18#include <functional>
19#include <string>
20#include <vector>
21
22#include "absl/strings/str_cat.h" // IWYU pragma: export
23#include "ortools/base/timer.h"
24
25namespace operations_research {
26
27// Custom logger class. It allows passing callbacks to process log messages.
28//
29// If no callbacks have been added, all logging will use the standard logging
30// facilities. As soon as one callback is added, it is disabled. Unless
31// ForceStandardLogging() has been called.
32//
33// Note that the callbacks will get the message unchanged. No '\n' will be
34// added.
35//
36// Important: This class is currently not thread-safe, it is easy to add a mutex
37// if needed. In CP-SAT, we currently make sure all access to this class do not
38// happen concurrently.
40 public:
42
43 // Enables all logging.
44 //
45 // Note that this is used by the logging macro, but it actually do not
46 // disable logging if LogInfo() is called directly.
47 void EnableLogging(bool enable) { is_enabled_ = enable; }
48
49 // Returns true iff logging is enabled.
50 bool LoggingIsEnabled() const { return is_enabled_; }
51
52 // Should all messages be displayed on stdout ?
53 void SetLogToStdOut(bool enable) { log_to_stdout_ = enable; }
54
55 // Add a callback listening to all information messages.
56 //
57 // They will be run synchronously when LogInfo() is called.
59 std::function<void(const std::string& message)> callback);
60
61 // Removes all callbacks registered via AddInfoLoggingCallback().
63
64 // Returns the number of registered callbacks.
65 int NumInfoLoggingCallbacks() const { return info_callbacks_.size(); }
66
67 // Logs a given information message and dispatch it to all callbacks.
68 void LogInfo(const char* source_filename, int source_line,
69 const std::string& message);
70
71 // Facility to avoid having multi megabytes logs when it brings little
72 // benefits. Logs with the same id will be kept under an average of
73 // throttling_rate_ logs per second.
75 void ThrottledLog(int id, const std::string& message);
76
77 // To not loose the last message of a throttled log, we keep it in memory and
78 // when this function is called we flush logs whose rate is now under the
79 // limit.
80 void FlushPendingThrottledLogs(bool ignore_rates = false);
81
82 private:
83 struct ThrottlingData {
84 int64_t num_displayed_logs = 0;
85 int64_t num_last_skipped_logs = 0;
86 std::string last_skipped_message;
87
88 void UpdateWhenDisplayed() {
89 num_displayed_logs++;
90 num_last_skipped_logs = 0;
91 last_skipped_message = "";
92 }
93 };
94 bool RateIsOk(const ThrottlingData& data);
95
96 bool is_enabled_ = false;
97 bool log_to_stdout_ = false;
98 std::vector<std::function<void(const std::string& message)>> info_callbacks_;
99
100 // TODO(user): Expose? for now we never change this. We start throttling after
101 // throttling_threshold_ logs of a given id, and we enforce a fixed logging
102 // rate afterwards, so that latter burst can still be seen.
103 const int throttling_threshold_ = 20;
104 const double throttling_rate_ = 1.0;
105
106 WallTimer timer_;
107 std::vector<ThrottlingData> id_to_throttling_data_;
108};
109
110#define SOLVER_LOG(logger, ...) \
111 if ((logger)->LoggingIsEnabled()) \
112 (logger)->LogInfo(__FILE__, __LINE__, absl::StrCat(__VA_ARGS__))
113
114} // namespace operations_research
115
116#endif // OR_TOOLS_UTIL_LOGGING_H_
void EnableLogging(bool enable)
Definition logging.h:47
void ThrottledLog(int id, const std::string &message)
Definition logging.cc:61
void SetLogToStdOut(bool enable)
Should all messages be displayed on stdout ?
Definition logging.h:53
void FlushPendingThrottledLogs(bool ignore_rates=false)
Definition logging.cc:79
bool LoggingIsEnabled() const
Returns true iff logging is enabled.
Definition logging.h:50
void AddInfoLoggingCallback(std::function< void(const std::string &message)> callback)
Definition logging.cc:29
void ClearInfoLoggingCallbacks()
Removes all callbacks registered via AddInfoLoggingCallback().
Definition logging.cc:34
int NumInfoLoggingCallbacks() const
Returns the number of registered callbacks.
Definition logging.h:65
void LogInfo(const char *source_filename, int source_line, const std::string &message)
Logs a given information message and dispatch it to all callbacks.
Definition logging.cc:36
In SWIG mode, we don't want anything besides these top-level includes.