Google OR-Tools v9.9
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
22#include "absl/strings/str_cat.h"
23
24namespace operations_research {
25
27
29 std::function<void(const std::string& message)> callback) {
30 info_callbacks_.push_back(callback);
31}
32
33void SolverLogger::ClearInfoLoggingCallbacks() { info_callbacks_.clear(); }
34
35void SolverLogger::LogInfo(const char* source_filename, int source_line,
36 const std::string& message) {
37 if (log_to_stdout_) {
38 std::cout << message << std::endl;
39 }
40
41 for (const auto& callback : info_callbacks_) {
43 }
44}
45
47 const int id = id_to_throttling_data_.size();
48 id_to_throttling_data_.resize(id + 1);
49 return id;
50}
51
52bool SolverLogger::RateIsOk(const ThrottlingData& data) {
53 const double time = std::max(1.0, timer_.Get());
54 const double rate =
55 static_cast<double>(data.num_displayed_logs - throttling_threshold_) /
56 time;
57 return rate < throttling_rate_;
58}
59
60void SolverLogger::ThrottledLog(int id, const std::string& message) {
61 if (!is_enabled_) return;
62 ThrottlingData& data = id_to_throttling_data_[id];
63 if (RateIsOk(data)) {
64 if (data.num_last_skipped_logs > 0) {
65 LogInfo("", 0,
66 absl::StrCat(message,
67 " [skipped_logs=", data.num_last_skipped_logs, "]"));
68 } else {
69 LogInfo("", 0, message);
70 }
71 data.UpdateWhenDisplayed();
72 } else {
73 data.num_last_skipped_logs++;
74 data.last_skipped_message = message;
75 }
76}
77
79 if (!is_enabled_) return;
80
81 // TODO(user): If this is called too often, we could optimize it and do
82 // nothing if there are no skipped logs.
83 for (int id = 0; id < id_to_throttling_data_.size(); ++id) {
84 ThrottlingData& data = id_to_throttling_data_[id];
85 if (data.num_last_skipped_logs == 0) continue;
86 if (ignore_rates || RateIsOk(data)) {
87 // Note the -1 since we didn't skip the last log in the end.
88 LogInfo("", 0,
89 absl::StrCat(data.last_skipped_message, " [skipped_logs=",
90 data.num_last_skipped_logs - 1, "]"));
91 data.UpdateWhenDisplayed();
92 }
93 }
94}
95
96} // 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:60
void FlushPendingThrottledLogs(bool ignore_rates=false)
Definition logging.cc:78
void AddInfoLoggingCallback(std::function< void(const std::string &message)> callback)
Definition logging.cc:28
void ClearInfoLoggingCallbacks()
Removes all callbacks registered via AddInfoLoggingCallback().
Definition logging.cc:33
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:35
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