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