"research/deeplab/deprecated/segmentation_dataset.py" did not exist on "8caa269db25165fdf21e73262921aa31bc595d70"
log.h 8.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * Copyright 2022 Max Planck Institute for Software Systems, and
 * National University of Singapore
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

Jakob Görgen's avatar
Jakob Görgen committed
25
26
#ifndef UTILS_LOG_H_
#define UTILS_LOG_H_
27
28
29
30
31
32
33
34
35
36

#include <stdio.h>

#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <string_view>
#include <unordered_map>
Jakob Görgen's avatar
Jakob Görgen committed
37
#include <utility>
38
39
40
41
42
43

namespace sim_log {

#define SIMLOG 1

enum LogLevel : int {
44
45
46
47
48
  debug   = 1,
  info    = 2,
  warn    = 3,
  error   = 4,
  off     = 5
49
50
51
52
53
54
55
56
57
58
};

class Log;
using LogPtT = std::unique_ptr<Log>;

class Log {
 public:
  FILE *file_ = nullptr;
  const bool is_file_ = false;

Jakob Görgen's avatar
Jakob Görgen committed
59
  explicit Log(FILE *file) : file_(file), is_file_(false) {
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  }

  Log(FILE *file, bool is_file) : file_(file), is_file_(is_file) {
  }

  ~Log() {
    if (file_ != nullptr && is_file_) {
      fclose(file_);
    }
  }

  static LogPtT createLog() {
    FILE *out;
    out = stdout;
    return std::make_unique<Log>(out);
  }

77
  static LogPtT createLog(FILE *out, bool file) {
78
    if (out == nullptr) {
79
80
      fputs("error: FILE* is null, fallback to stdout logging\n", stderr);
      out = stdout;
81
82
83
84
85
86
    }
    return std::make_unique<Log>(out, file);
  }

  static LogPtT createLog(const char *file_path) {
    if (file_path == nullptr) {
87
      fputs("error: file_path is null, fallback to stdout logging\n", stderr);
88
89
90
91
92
93
94
95
96
97
98
99
100
      return sim_log::Log::createLog();
    }

    FILE *file = fopen(file_path, "w");
    return createLog(file, true);
  }
};

class LogRegistry {
  const std::unordered_map<LogLevel, const char *> level_names_{
      {LogLevel::off, "off"},
      {LogLevel::info, "info"},
      {LogLevel::warn, "warn"},
101
      {LogLevel::debug, "debug"},
102
103
104
      {LogLevel::error, "error"}};

  LogLevel level_ = LogLevel::info;
105
  bool enforce_flush_ = false;
106
107

 public:
108
  LogLevel GetLevel() {
109
110
111
112
113
114
115
    return level_;
  }

  void SetLogLevel(LogLevel level) {
    level_ = level;
  }

116
117
118
119
120
121
122
123
  void SetFlush(bool flush) {
    enforce_flush_ = flush;
  }

  bool EnforceFlush() const {
    return enforce_flush_;
  }

124
125
126
  const char *GetRepr(LogLevel level) const {
    auto it = level_names_.find(level);
    if (it == level_names_.end()) {
127
      return "undefined";
128
129
130
131
132
133
134
    }
    return it->second;
  }
};

class Logger {
 private:
135
  inline bool ShouldLog(LogLevel level) const {
136
    auto &registry = Logger::GetRegistry();
137
    return level >= registry.GetLevel() && registry.GetLevel() != LogLevel::off;
138
139
140
141
  }

  template <typename... Args>
  inline void log_internal(LogLevel level, FILE *out, const char *format,
142
                           Args... args) const {
Jakob Görgen's avatar
Jakob Görgen committed
143
    if (!ShouldLog(level)) {
144
145
      return;
    }
146
147
    auto &registry = Logger::GetRegistry();
    fprintf(out, "%s: ", registry.GetRepr(level));
148
    fprintf(out, format, args...);
149
150
151
    if (registry.EnforceFlush()) {
      fflush(out);
    }
152
153
  }

154
155
  inline void log_internal(LogLevel level, FILE *out, const char *to_print)
                           const {
Jakob Görgen's avatar
Jakob Görgen committed
156
    if (!ShouldLog(level)) {
157
158
      return;
    }
159
160
161
162
163
164
    auto &registry = Logger::GetRegistry();
    fprintf(out, "%s: ", registry.GetRepr(level));
    fputs(to_print, out);
    if (registry.EnforceFlush()) {
      fflush(out);
    }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  }

  Logger() = default;

  ~Logger() = default;

 public:
  static Logger &GetLogger() {
    static Logger logger;
    return logger;
  }

  static LogRegistry &GetRegistry() {
    static LogRegistry registry;
    return registry;
  }

182
183
184
185
186
187
188
189
190
191
192
  void Flush(LogPtT &log) const {
    if (log == nullptr || log->file_ == nullptr) {
      return;
    }
    fflush(log->file_);
  }

  void Flush() const {
    fflush(stdout);
  }

193
194
  template <typename... Args>
  inline void log_stdout_f(LogLevel level, const char *format,
195
196
                           const Args &...args) const {
    log_internal(level, stdout, format, args...);
197
198
  }

199
200
  inline void log_stdout(LogLevel level, const char *to_print) const {
    log_internal(level, stdout, to_print);
201
202
203
204
  }

  template <typename... Args>
  void log_f(LogLevel level, LogPtT &log, const char *format,
205
             const Args &...args) const {
206
    if (log->file_ == nullptr) {
207
208
      log_stdout(level, "log file is null. it should not be!\n");
      log_stdout_f(level, format, args...);
209
210
      return;
    }
211
    log_internal(level, log->file_, format, args...);
212
213
  }

214
  void log(LogLevel level, LogPtT &log, const char *to_print) const {
215
    if (log->file_ == nullptr) {
216
217
      log_stdout(level, "log file is null. it should not be!\n");
      log_stdout(level, to_print);
218
219
      return;
    }
220
    log_internal(level, log->file_, to_print);
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  }
};

#ifdef SIMLOG

template <typename... Args>
inline void LogInfo(LogPtT &log, const char *fmt, Args &&...args) {
  sim_log::Logger::GetLogger().log_f(LogLevel::info, log, fmt,
                                     std::forward<Args>(args)...);
}

inline void LogInfo(LogPtT &log, const char *msg) {
  sim_log::Logger::GetLogger().log(LogLevel::info, log, msg);
}

template <typename... Args>
inline void LogInfo(const char *fmt, Args &&...args) {
  sim_log::Logger::GetLogger().log_stdout_f(LogLevel::info, fmt,
                                            std::forward<Args>(args)...);
}

inline void LogInfo(const char *msg) {
  sim_log::Logger::GetLogger().log_stdout(LogLevel::info, msg);
}

template <typename... Args>
inline void LogWarn(LogPtT &log, const char *fmt, Args &&...args) {
  sim_log::Logger::GetLogger().log_f(LogLevel::warn, log, fmt,
                                     std::forward<Args>(args)...);
}

inline void LogWarn(LogPtT &log, const char *msg) {
  sim_log::Logger::GetLogger().log(LogLevel::warn, log, msg);
}

template <typename... Args>
inline void LogWarn(const char *fmt, Args &&...args) {
  sim_log::Logger::GetLogger().log_stdout_f(LogLevel::warn, fmt,
                                            std::forward<Args>(args)...);
}

inline void LogWarn(const char *msg) {
  sim_log::Logger::GetLogger().log_stdout(LogLevel::warn, msg);
}

template <typename... Args>
inline void LogError(LogPtT &log, const char *fmt, Args &&...args) {
  sim_log::Logger::GetLogger().log_f(LogLevel::error, log, fmt,
                                     std::forward<Args>(args)...);
}

inline void LogError(LogPtT &log, const char *msg) {
  sim_log::Logger::GetLogger().log(LogLevel::error, log, msg);
}

template <typename... Args>
inline void LogError(const char *fmt, Args &&...args) {
  sim_log::Logger::GetLogger().log_stdout_f(LogLevel::error, fmt,
                                            std::forward<Args>(args)...);
}

inline void LogError(const char *msg) {
  sim_log::Logger::GetLogger().log_stdout(LogLevel::error, msg);
}

#else

template <typename... Args>
inline void LogInfo(LogPtT &log, const char *fmt, Args &&...args) {
}

inline void LogInfo(LogPtT &log, const char *msg) {
}

template <typename... Args>
inline void LogInfo(const char *fmt, Args &&...args) {
}

inline void LogInfo(const char *msg) {
}

template <typename... Args>
inline void LogWarn(LogPtT &log, const char *fmt, Args &&...args) {
}

inline void LogWarn(LogPtT &log, const char *msg) {
}

template <typename... Args>
inline void LogWarn(const char *fmt, Args &&...args) {
}

inline void LogWarn(const char *msg) {
}

template <typename... Args>
inline void LogError(LogPtT &log, const char *fmt, Args &&...args) {
}

inline void LogError(LogPtT &log, const char *msg) {
}

template <typename... Args>
inline void LogError(const char *fmt, Args &&...args) {
}

inline void LogError(const char *msg) {
}

#endif

}  // namespace sim_log

Jakob Görgen's avatar
Jakob Görgen committed
334
#endif  // UTILS_LOG_H_