Google OR-Tools v9.15
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-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
15
16#include <algorithm>
17#include <cstdint>
18#include <functional>
19#include <iostream>
20#include <ostream>
21#include <string>
22#include <utility>
23
24#include "absl/strings/str_cat.h"
25#include "absl/strings/str_format.h"
26#include "absl/strings/str_join.h"
27
28namespace operations_research {
29
30std::string FormatCounter(int64_t num) {
31 std::string s = absl::StrCat(num);
32 std::string out;
33 const int size = s.size();
34 for (int i = 0; i < size; ++i) {
35 if (i > 0 && (size - i) % 3 == 0) {
36 out.push_back('\'');
37 }
38 out.push_back(s[i]);
39 }
40 return out;
41}
42
43SolverLogger::SolverLogger() { timer_.Start(); }
44
46 std::function<void(const std::string& message)> callback) {
47 info_callbacks_.push_back(std::move(callback));
48}
49
50void SolverLogger::ClearInfoLoggingCallbacks() { info_callbacks_.clear(); }
51
52void SolverLogger::LogInfo(const char* source_filename, int source_line,
53 const std::string& message) {
54 if (log_to_stdout_) {
55 std::cout << message << std::endl;
56 }
57
58 for (const auto& callback : info_callbacks_) {
59 callback(message);
60 }
61}
62
64 const int id = id_to_throttling_data_.size();
65 id_to_throttling_data_.resize(id + 1);
66 return id;
67}
68
69bool SolverLogger::RateIsOk(const ThrottlingData& data) {
70 const double time = std::max(1.0, timer_.Get());
71 const double rate =
72 static_cast<double>(data.num_displayed_logs - throttling_threshold_) /
73 time;
74 return rate < throttling_rate_;
75}
76
77void SolverLogger::ThrottledLog(int id, const std::string& message) {
78 if (!is_enabled_) return;
79 ThrottlingData& data = id_to_throttling_data_[id];
80 if (RateIsOk(data)) {
81 if (data.num_last_skipped_logs > 0) {
82 LogInfo("", 0,
83 absl::StrCat(message,
84 " [skipped_logs=", data.num_last_skipped_logs, "]"));
85 } else {
86 LogInfo("", 0, message);
87 }
88 data.UpdateWhenDisplayed();
89 } else {
90 data.num_last_skipped_logs++;
91 data.last_skipped_message = message;
92 }
93}
94
96 if (!is_enabled_) return;
97
98 // TODO(user): If this is called too often, we could optimize it and do
99 // nothing if there are no skipped logs.
100 for (int id = 0; id < id_to_throttling_data_.size(); ++id) {
101 ThrottlingData& data = id_to_throttling_data_[id];
102 if (data.num_last_skipped_logs == 0) continue;
103 if (ignore_rates || RateIsOk(data)) {
104 // Note the -1 since we didn't skip the last log in the end.
105 LogInfo("", 0,
106 absl::StrCat(data.last_skipped_message, " [skipped_logs=",
107 data.num_last_skipped_logs - 1, "]"));
108 data.UpdateWhenDisplayed();
109 }
110 }
111}
112
114 time_limit_->AdvanceDeterministicTime(work_);
115 const double dtime =
116 time_limit_->GetElapsedDeterministicTime() - dtime_at_start_;
117
118 std::string counter_string;
119 for (const auto& [counter_name, count] : counters_) {
120 absl::StrAppend(&counter_string, " #", counter_name, "=",
121 FormatCounter(count));
122 }
123
124 // We use absl::Seconds() to get a nicer display.
125 if (override_logging_ ? log_when_override_ : logger_->LoggingIsEnabled()) {
126 logger_->LogInfo(
127 __FILE__, __LINE__,
128 absl::StrCat(absl::StrFormat(" %.2es", timer_.Get()),
129 absl::StrFormat(" %.2ed", dtime),
130 (WorkLimitIsReached() ? " *" : " "), "[", name_, "]",
131 counter_string, " ", absl::StrJoin(extra_infos_, " ")));
132 }
133}
134
135} // namespace operations_research
double Get() const
Definition timer.h:44
void ThrottledLog(int id, const std::string &message)
Definition logging.cc:77
void FlushPendingThrottledLogs(bool ignore_rates=false)
Definition logging.cc:95
void AddInfoLoggingCallback(std::function< void(const std::string &message)> callback)
Definition logging.cc:45
void LogInfo(const char *source_filename, int source_line, const std::string &message)
Definition logging.cc:52
OR-Tools root namespace.
std::string FormatCounter(int64_t num)
Definition logging.cc:30