Jet  v1.3.3
logging.h
Go to the documentation of this file.
1 // Copyright (c) 2018 Doyub Kim
2 //
3 // I am making my contributions/submissions to this project solely in my
4 // personal capacity and am not conveying any rights to any intellectual
5 // property of any third parties.
6 
7 #ifndef INCLUDE_JET_LOGGING_H_
8 #define INCLUDE_JET_LOGGING_H_
9 
10 #include <iostream>
11 #include <sstream>
12 #include <string>
13 
14 namespace jet {
15 
18 enum class LoggingLevel : uint8_t {
19  All = 0,
20  Debug = 1,
21  Info = 2,
22  Warn = 3,
23  Error = 4,
24  Off = 5
25 };
26 
33 class Logger final {
34  public:
36  explicit Logger(LoggingLevel level);
37 
40 
42  template <typename T>
43  const Logger& operator<<(const T& x) const {
44  _buffer << x;
45  return *this;
46  }
47 
48  private:
49  LoggingLevel _level;
50  mutable std::stringstream _buffer;
51 };
52 
54 class Logging {
55  public:
57  static void setInfoStream(std::ostream* strm);
58 
60  static void setWarnStream(std::ostream* strm);
61 
63  static void setErrorStream(std::ostream* strm);
64 
66  static void setDebugStream(std::ostream* strm);
67 
69  static void setAllStream(std::ostream* strm);
70 
72  static std::string getHeader(LoggingLevel level);
73 
75  static void setLevel(LoggingLevel level);
76 
78  static void mute();
79 
81  static void unmute();
82 };
83 
85 extern Logger infoLogger;
86 
88 extern Logger warnLogger;
89 
91 extern Logger errorLogger;
92 
94 extern Logger debugLogger;
95 
96 #define JET_INFO \
97  (Logger(LoggingLevel::Info) << Logging::getHeader(LoggingLevel::Info) \
98  << "[" << __FILE__ << ":" << __LINE__ << " (" << __func__ << ")] ")
99 #define JET_WARN \
100  (Logger(LoggingLevel::Warn) << Logging::getHeader(LoggingLevel::Warn) \
101  << "[" << __FILE__ << ":" << __LINE__ << " (" << __func__ << ")] ")
102 #define JET_ERROR \
103  (Logger(LoggingLevel::Error) << Logging::getHeader(LoggingLevel::Error) \
104  << "[" << __FILE__ << ":" << __LINE__ << " (" << __func__ << ")] ")
105 #define JET_DEBUG \
106  (Logger(LoggingLevel::Debug) << Logging::getHeader(LoggingLevel::Debug) \
107  << "[" << __FILE__ << ":" << __LINE__ << " (" << __func__ << ")] ")
108 
109 } // namespace jet
110 
111 #endif // INCLUDE_JET_LOGGING_H_
jet::Logger::Logger
Logger(LoggingLevel level)
Constructs a logger with logging level.
jet::LoggingLevel::Off
@ Off
jet::LoggingLevel::Debug
@ Debug
jet::Logging::unmute
static void unmute()
Un-mutes the logger.
jet::Logging::mute
static void mute()
Mutes the logger.
jet::Logging::getHeader
static std::string getHeader(LoggingLevel level)
Returns the header string.
jet::LoggingLevel::Error
@ Error
jet::infoLogger
Logger infoLogger
Info-level logger.
jet
Definition: advection_solver2.h:18
jet::errorLogger
Logger errorLogger
Error-level logger.
jet::Logging::setWarnStream
static void setWarnStream(std::ostream *strm)
Sets the output stream for the warning level logs.
jet::Logger
Super simple logger implementation.
Definition: logging.h:33
jet::Logger::~Logger
~Logger()
Destructor.
jet::Logging::setLevel
static void setLevel(LoggingLevel level)
Sets the logging level.
jet::Logger::operator<<
const Logger & operator<<(const T &x) const
Writes a value to the buffer stream.
Definition: logging.h:43
jet::Logging::setAllStream
static void setAllStream(std::ostream *strm)
Sets the output stream for all the log levelss.
jet::warnLogger
Logger warnLogger
Warn-level logger.
jet::Logging::setErrorStream
static void setErrorStream(std::ostream *strm)
Sets the output stream for the error level logs.
jet::Logging
Helper class for logging.
Definition: logging.h:54
jet::debugLogger
Logger debugLogger
Debug-level logger.
jet::LoggingLevel::Warn
@ Warn
jet::Logging::setDebugStream
static void setDebugStream(std::ostream *strm)
Sets the output stream for the debug level logs.
jet::LoggingLevel::Info
@ Info
jet::LoggingLevel::All
@ All
jet::Logging::setInfoStream
static void setInfoStream(std::ostream *strm)
Sets the output stream for the info level logs.
jet::LoggingLevel
LoggingLevel
Definition: logging.h:18