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

1

2

3

4

5

6

7

8

9

10

11

12

13

14#ifndef ORTOOLS_UTIL_LOGGING_H_

15#define ORTOOLS_UTIL_LOGGING_H_

16

17#include <cstdint>

18#include <functional>

19#include <string>

20#include <vector>

21

22#include "absl/strings/str_cat.h"

25

27

28

30

31

32

33

34

35

36

37

38

39

40

41

42

44 public:

46

47

48

49

50

52

53

55

56

58

59

60

61

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

64

65

67

68

70

71

72 void LogInfo(const char* source_filename, int source_line,

73 const std::string& message);

74

75

76

77

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

80

81

82

83

85

86 private:

87 struct ThrottlingData {

88 int64_t num_displayed_logs = 0;

89 int64_t num_last_skipped_logs = 0;

90 std::string last_skipped_message;

91

92 void UpdateWhenDisplayed() {

93 num_displayed_logs++;

94 num_last_skipped_logs = 0;

95 last_skipped_message = "";

96 }

97 };

98 bool RateIsOk(const ThrottlingData& data);

99

100 bool is_enabled_ = false;

101 bool log_to_stdout_ = false;

102 std::vector<std::function<void(const std::string& message)>> info_callbacks_;

103

104

105

106

107 const int throttling_threshold_ = 20;

108 const double throttling_rate_ = 1.0;

109

110 WallTimer timer_;

111 std::vector<ThrottlingData> id_to_throttling_data_;

112};

113

114#define SOLVER_LOG(logger, ...) \

115 if ((logger)->LoggingIsEnabled()) \

116 (logger)->LogInfo(__FILE__, __LINE__, absl::StrCat(__VA_ARGS__))

117

118#define FORCED_SOLVER_LOG(logger, ...) \

119 (logger)->LogInfo(__FILE__, __LINE__, absl::StrCat(__VA_ARGS__))

120

121

122

123

124

125

126

128 public:

130 : name_(std::move(name)),

131 dtime_at_start_(time_limit->GetElapsedDeterministicTime()),

132 logger_(logger),

133 time_limit_(time_limit) {

134 timer_.Start();

135 }

136

137

138

139 void AddToWork(double dtime) { work_ += dtime; }

144

145

146

147 void AddCounter(std::string name, int64_t count) {

148 if (count == 0) return;

149 counters_.emplace_back(std::move(name), count);

150 }

151

152

153 void AddMessage(std::string name) { extra_infos_.push_back(std::move(name)); }

154

155

157

158

159

161 override_logging_ = true;

162 log_when_override_ = value;

163 };

164

166 double wtime() const { return timer_.Get(); }

167

168 private:

169 const std::string name_;

170 const double dtime_at_start_;

171

175

176 bool override_logging_ = false;

177 bool log_when_override_ = false;

178 double work_ = 0.0;

179 std::vector<std::pair<std::string, int64_t>> counters_;

180 std::vector<std::string> extra_infos_;

181};

182

183}

184

185#endif

void AddMessage(std::string name)

Definition logging.h:153

void OverrideLogging(bool value)

Definition logging.h:160

double deterministic_time() const

Definition logging.h:165

void TrackSimpleLoop(int size)

Definition logging.h:140

double wtime() const

Definition logging.h:166

void AddCounter(std::string name, int64_t count)

Definition logging.h:147

void AddToWork(double dtime)

Definition logging.h:139

PresolveTimer(std::string name, SolverLogger *logger, TimeLimit *time_limit)

Definition logging.h:129

void TrackFastLoop(int size)

Definition logging.h:142

void TrackHashLookups(int size)

Definition logging.h:141

bool WorkLimitIsReached() const

Definition logging.h:143

void EnableLogging(bool enable)

Definition logging.h:51

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

void SetLogToStdOut(bool enable)

Definition logging.h:57

void FlushPendingThrottledLogs(bool ignore_rates=false)

bool LoggingIsEnabled() const

Definition logging.h:54

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

void ClearInfoLoggingCallbacks()

int NumInfoLoggingCallbacks() const

Definition logging.h:69

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

std::string FormatCounter(int64_t num)