WPILibC++  2019.1.1-beta-1-6-g9f6544f
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Logger.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef WPIUTIL_WPI_LOGGER_H_
9 #define WPIUTIL_WPI_LOGGER_H_
10 
11 #include <functional>
12 
13 #include "wpi/SmallString.h"
14 #include "wpi/raw_ostream.h"
15 
16 namespace wpi {
17 
18 enum LogLevel {
19  WPI_LOG_CRITICAL = 50,
20  WPI_LOG_ERROR = 40,
21  WPI_LOG_WARNING = 30,
22  WPI_LOG_INFO = 20,
23  WPI_LOG_DEBUG = 10,
24  WPI_LOG_DEBUG1 = 9,
25  WPI_LOG_DEBUG2 = 8,
26  WPI_LOG_DEBUG3 = 7,
27  WPI_LOG_DEBUG4 = 6
28 };
29 
30 class Logger {
31  public:
32  using LogFunc = std::function<void(unsigned int level, const char* file,
33  unsigned int line, const char* msg)>;
34 
35  Logger() = default;
36  explicit Logger(const LogFunc& func) : m_func(func) {}
37  Logger(const LogFunc& func, unsigned int min_level)
38  : m_func(func), m_min_level(min_level) {}
39 
40  void SetLogger(LogFunc func) { m_func = func; }
41 
42  void set_min_level(unsigned int level) { m_min_level = level; }
43  unsigned int min_level() const { return m_min_level; }
44 
45  void Log(unsigned int level, const char* file, unsigned int line,
46  const char* msg) {
47  if (!m_func || level < m_min_level) return;
48  m_func(level, file, line, msg);
49  }
50 
51  bool HasLogger() const { return m_func != nullptr; }
52 
53  private:
54  LogFunc m_func;
55  unsigned int m_min_level = 20;
56 };
57 
58 #define WPI_LOG(logger_inst, level, x) \
59  do { \
60  ::wpi::Logger& WPI_logger_ = logger_inst; \
61  if (WPI_logger_.min_level() <= level && WPI_logger_.HasLogger()) { \
62  ::wpi::SmallString<128> log_buf_; \
63  ::wpi::raw_svector_ostream log_os_{log_buf_}; \
64  log_os_ << x; \
65  WPI_logger_.Log(level, __FILE__, __LINE__, log_buf_.c_str()); \
66  } \
67  } while (0)
68 
69 #define WPI_ERROR(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_ERROR, x)
70 #define WPI_WARNING(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_WARNING, x)
71 #define WPI_INFO(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_INFO, x)
72 
73 #ifdef NDEBUG
74 #define WPI_DEBUG(inst, x) \
75  do { \
76  } while (0)
77 #define WPI_DEBUG1(inst, x) \
78  do { \
79  } while (0)
80 #define WPI_DEBUG2(inst, x) \
81  do { \
82  } while (0)
83 #define WPI_DEBUG3(inst, x) \
84  do { \
85  } while (0)
86 #define WPI_DEBUG4(inst, x) \
87  do { \
88  } while (0)
89 #else
90 #define WPI_DEBUG(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG, x)
91 #define WPI_DEBUG1(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG1, x)
92 #define WPI_DEBUG2(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG2, x)
93 #define WPI_DEBUG3(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG3, x)
94 #define WPI_DEBUG4(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG4, x)
95 #endif
96 
97 } // namespace wpi
98 
99 #endif // WPIUTIL_WPI_LOGGER_H_
Definition: Log.h:30
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
Definition: Logger.h:30