Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
logging.cc
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
15
16#include <algorithm>
17#include <functional>
18#include <iostream>
19#include <ostream>
20#include <string>
21#include <utility>
22
23#include "absl/strings/str_cat.h"
24
25namespace operations_research {
26
28
30 std::function<void(const std::string& message)> callback) {
31 info_callbacks_.push_back(std::move(callback));
32}
33
34void SolverLogger::ClearInfoLoggingCallbacks() { info_callbacks_.clear(); }
35
36void SolverLogger::LogInfo(const char* source_filename, int source_line,
37 const std::string& message) {
38 if (log_to_stdout_) {
39 std::cout << message << std::endl;
40 }
41
42 for (const auto& callback : info_callbacks_) {
44 }
45}
46
48 const int id = id_to_throttling_data_.size();
49 id_to_throttling_data_.resize(id + 1);
50 return id;
51}
52
53bool SolverLogger::RateIsOk(const ThrottlingData& data) {
54 const double time = std::max(1.0, timer_.Get());
55 const double rate =
56 static_cast<double>(data.num_displayed_logs - throttling_threshold_) /
57 time;
58 return rate < throttling_rate_;
59}
60
61void SolverLogger::ThrottledLog(int id, const std::string& message) {
62 if (!is_enabled_) return;
63 ThrottlingData& data = id_to_throttling_data_[id];
64 if (RateIsOk(data)) {
65 if (data.num_last_skipped_logs > 0) {
66 LogInfo("", 0,
67 absl::StrCat(message,
68 " [skipped_logs=", data.num_last_skipped_logs, "]"));
69 } else {
70 LogInfo("", 0, message);
71 }
72 data.UpdateWhenDisplayed();
73 } else {
74 data.num_last_skipped_logs++;
75 data.last_skipped_message = message;
76 }
77}
78
80 if (!is_enabled_) return;
81
82 // TODO(user): If this is called too often, we could optimize it and do
83 // nothing if there are no skipped logs.
84 for (int id = 0; id < id_to_throttling_data_.size(); ++id) {
85 ThrottlingData& data = id_to_throttling_data_[id];
86 if (data.num_last_skipped_logs == 0) continue;
87 if (ignore_rates || RateIsOk(data)) {
88 // Note the -1 since we didn't skip the last log in the end.
89 LogInfo("", 0,
90 absl::StrCat(data.last_skipped_message, " [skipped_logs=",
91 data.num_last_skipped_logs - 1, "]"));
92 data.UpdateWhenDisplayed();
93 }
94 }
95}
96
97} // namespace operations_research
double Get() const
Definition timer.h:46
void Start()
When Start() is called multiple times, only the most recent is used.
Definition timer.h:32
void ThrottledLog(int id, const std::string &message)
Definition logging.cc:61
void FlushPendingThrottledLogs(bool ignore_rates=false)
Definition logging.cc:79
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
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.
int64_t time
Definition resource.cc:1708
std::string message
Definition trace.cc:397