Google OR-Tools: ortools/util/logging.cc Source File

1

2

3

4

5

6

7

8

9

10

11

12

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

29

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

44

46 std::function<void(const std::string& message)> callback) {

47 info_callbacks_.push_back(std::move(callback));

48}

49

51

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

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) {

83 absl::StrCat(message,

84 " [skipped_logs=", data.num_last_skipped_logs, "]"));

85 } else {

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

99

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

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, "=",

122 }

123

124

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),

131 counter_string, " ", absl::StrJoin(extra_infos_, " ")));

132 }

133}

134

135}

~PresolveTimer()

Definition logging.cc:113

bool WorkLimitIsReached() const

void ThrottledLog(int id, const std::string &message)

Definition logging.cc:77

SolverLogger()

Definition logging.cc:43

void FlushPendingThrottledLogs(bool ignore_rates=false)

Definition logging.cc:95

int GetNewThrottledId()

Definition logging.cc:63

void AddInfoLoggingCallback(std::function< void(const std::string &message)> callback)

Definition logging.cc:45

void ClearInfoLoggingCallbacks()

Definition logging.cc:50

void LogInfo(const char *source_filename, int source_line, const std::string &message)

Definition logging.cc:52

std::string FormatCounter(int64_t num)

Definition logging.cc:30