Logging.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Logging.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Super simple logger implementation.
6 > Created Time: 2017/04/23
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_LOGGER_H
10 #define CUBBYFLOW_LOGGER_H
11 
12 #include <sstream>
13 
14 namespace CubbyFlow
15 {
18  enum class LogLevel : uint8_t
19  {
20  All = 0,
21  Debug = 1,
22  Info = 2,
23  Warn = 3,
24  Error = 4,
25  Off = 5
26  };
27 
34  class Logger final
35  {
36  public:
38  explicit Logger(LogLevel level);
39 
41  ~Logger();
42 
44  template <typename T>
45  const Logger& operator<<(const T& x) const
46  {
47  m_buffer << x;
48  return *this;
49  }
50 
51  private:
52  LogLevel m_level;
53  mutable std::stringstream m_buffer;
54  };
55 
57  class Logging
58  {
59  public:
61  static void SetInfoStream(std::ostream* stream);
62 
64  static void SetWarnStream(std::ostream* stream);
65 
67  static void SetErrorStream(std::ostream* stream);
68 
70  static void SetDebugStream(std::ostream* stream);
71 
73  static void SetAllStream(std::ostream* stream);
74 
76  static std::string GetHeader(LogLevel level);
77 
79  static void SetLevel(LogLevel level);
80 
82  static void Mute();
83 
85  static void Unmute();
86  };
87 
89  extern Logger infoLogger;
90 
92  extern Logger warnLogger;
93 
95  extern Logger errorLogger;
96 
98  extern Logger debugLogger;
99 
100  #define CUBBYFLOW_INFO \
101  (Logger(LogLevel::Info) << Logging::GetHeader(LogLevel::Info) \
102  << "[" << __FILE__ << ":" << __LINE__ << " (" << __func__ << ")] ")
103  #define CUBBYFLOW_WARN \
104  (Logger(LogLevel::Warn) << Logging::GetHeader(LogLevel::Warn) \
105  << "[" << __FILE__ << ":" << __LINE__ << " (" << __func__ << ")] ")
106  #define CUBBYFLOW_ERROR \
107  (Logger(LogLevel::Error) << Logging::GetHeader(LogLevel::Error) \
108  << "[" << __FILE__ << ":" << __LINE__ << " (" << __func__ << ")] ")
109  #define CUBBYFLOW_DEBUG \
110  (Logger(LogLevel::Debug) << Logging::GetHeader(LogLevel::Debug) \
111  << "[" << __FILE__ << ":" << __LINE__ << " (" << __func__ << ")] ")
112 }
113 
114 #endif
static void SetInfoStream(std::ostream *stream)
Sets the output stream for the info level logs.
Logger errorLogger
Error-level logger.
Super simple logger implementation.
Definition: Logging.h:34
Logger(LogLevel level)
Constructs a logger with logging level.
Helper class for logging.
Definition: Logging.h:57
Logger warnLogger
Warn-level logger.
static void SetLevel(LogLevel level)
Sets the log level.
static void Unmute()
Un-mutes the logger.
Definition: pybind11Utils.h:24
const Logger & operator<<(const T &x) const
Writes a value to the buffer stream.
Definition: Logging.h:45
static void Mute()
Mutes the logger.
static void SetWarnStream(std::ostream *stream)
Sets the output stream for the warning level logs.
static void SetDebugStream(std::ostream *stream)
Sets the output stream for the debug level logs.
Logger infoLogger
Info-level logger.
LogLevel
Definition: Logging.h:18
static void SetErrorStream(std::ostream *stream)
Sets the output stream for the error level logs.
static std::string GetHeader(LogLevel level)
Returns the header string.
static void SetAllStream(std::ostream *stream)
Sets the output stream for all the log levels.
~Logger()
Destructor.
Logger debugLogger
Debug-level logger.