TAMSVIZ
Visualization and annotation tool for ROS
log.h
1 // TAMSVIZ
2 // (c) 2020 Philipp Ruppel
3 
4 #pragma once
5 
6 #include <iostream>
7 #include <sstream>
8 
9 #include "event.h"
10 
11 enum class LogLevel {
12  Debug,
13  Info,
14  Warn,
15  Error,
16  Fatal,
17  Success,
18 };
19 
20 class LogMessage {
21  LogLevel _level;
22  const char *_message;
23  const char *_file;
24  size_t _line;
25  const char *_function;
26  const char *_node;
27 
28 public:
29  LogMessage(const LogLevel &level, const char *message, const char *file,
30  size_t line, const char *function, const char *node)
31  : _level(level), _message(message), _file(file), _line(line),
32  _function(function), _node(node) {}
33  LogMessage(const LogMessage &) = delete;
34  LogMessage &operator=(const LogMessage &) = delete;
35  LogLevel level() const { return _level; }
36  const char *message() const { return _message; }
37  const char *file() const { return _file; }
38  size_t line() const { return _line; }
39  const char *function() const { return _function; }
40  const char *node() const { return _node; }
41 };
42 
43 struct LogManager {
44  LogManager();
46  static LogManager &instance();
47 };
48 
49 #define LOG_IMPL(level, condition, ...) \
50  if (condition) { \
51  std::ostringstream log_ostream_internal; /* TODO: replace */ \
52  log_ostream_internal << __VA_ARGS__; \
53  LogManager::instance().received( \
54  LogMessage(level, log_ostream_internal.str().c_str(), __FILE__, \
55  __LINE__, __PRETTY_FUNCTION__, "")); \
56  }
57 
58 #define LOG_THROTTLE_IMPL(interval) \
59  ([]() { \
60  static std::chrono::time_point<std::chrono::steady_clock> l; \
61  auto t = std::chrono::steady_clock::now(); \
62  if (t - l > std::chrono::duration<double>(interval)) { \
63  l = t; \
64  return true; \
65  } else { \
66  return false; \
67  } \
68  }())
69 
70 #define LOG_DEBUG(...) LOG_IMPL(LogLevel::Debug, 1, __VA_ARGS__)
71 #define LOG_INFO(...) LOG_IMPL(LogLevel::Info, 1, __VA_ARGS__)
72 #define LOG_WARN(...) LOG_IMPL(LogLevel::Warn, 1, __VA_ARGS__)
73 #define LOG_ERROR(...) LOG_IMPL(LogLevel::Error, 1, __VA_ARGS__)
74 #define LOG_FATAL(...) LOG_IMPL(LogLevel::Fatal, 1, __VA_ARGS__)
75 #define LOG_SUCCESS(...) LOG_IMPL(LogLevel::Success, 1, __VA_ARGS__)
76 
77 #define LOG_DEBUG_THROTTLE(interval, ...) \
78  LOG_IMPL(LogLevel::Debug, LOG_THROTTLE_IMPL(interval), __VA_ARGS__)
79 #define LOG_INFO_THROTTLE(interval, ...) \
80  LOG_IMPL(LogLevel::Info, LOG_THROTTLE_IMPL(interval), __VA_ARGS__)
81 #define LOG_WARN_THROTTLE(interval, ...) \
82  LOG_IMPL(LogLevel::Warn, LOG_THROTTLE_IMPL(interval), __VA_ARGS__)
83 #define LOG_ERROR_THROTTLE(interval, ...) \
84  LOG_IMPL(LogLevel::Error, LOG_THROTTLE_IMPL(interval), __VA_ARGS__)
85 #define LOG_FATAL_THROTTLE(interval, ...) \
86  LOG_IMPL(LogLevel::Fatal, LOG_THROTTLE_IMPL(interval), __VA_ARGS__)
87 #define LOG_SUCCESS_THROTTLE(interval, ...) \
88  LOG_IMPL(LogLevel::Success, LOG_THROTTLE_IMPL(interval), __VA_ARGS__)
Definition: log.h:43
Definition: log.h:20