Logging.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_LOGGER_HPP
12 #define CUBBYFLOW_LOGGER_HPP
13 
14 #include <sstream>
15 
16 namespace CubbyFlow
17 {
20 enum class LogLevel : uint8_t
21 {
22  All = 0,
23  Debug = 1,
24  Info = 2,
25  Warn = 3,
26  Error = 4,
27  Off = 5
28 };
29 
36 class Logger final
37 {
38  public:
40  explicit Logger(LogLevel level);
41 
43  Logger(const Logger&) = delete;
44 
46  Logger(Logger&&) noexcept = delete;
47 
49  ~Logger();
50 
52  Logger& operator=(const Logger&) = delete;
53 
55  Logger& operator=(Logger&&) noexcept = delete;
56 
58  template <typename T>
59  const Logger& operator<<(const T& x) const
60  {
61  m_buffer << x;
62  return *this;
63  }
64 
65  private:
66  LogLevel m_level;
67  mutable std::stringstream m_buffer{};
68 };
69 
71 class Logging
72 {
73  public:
75  static void SetInfoStream(std::ostream* stream);
76 
78  static void SetWarnStream(std::ostream* stream);
79 
81  static void SetErrorStream(std::ostream* stream);
82 
84  static void SetDebugStream(std::ostream* stream);
85 
87  static void SetAllStream(std::ostream* stream);
88 
90  static std::string GetHeader(LogLevel level);
91 
93  static void SetLevel(LogLevel level);
94 
96  static void Mute();
97 
99  static void Unmute();
100 };
101 
103 extern Logger infoLogger;
104 
106 extern Logger warnLogger;
107 
109 extern Logger errorLogger;
110 
112 extern Logger debugLogger;
113 
114 #define CUBBYFLOW_INFO \
115  (Logger(LogLevel::Info) \
116  << Logging::GetHeader(LogLevel::Info) << "[" << __FILE__ << ":" \
117  << __LINE__ << " (" << __func__ << ")] ")
118 #define CUBBYFLOW_WARN \
119  (Logger(LogLevel::Warn) \
120  << Logging::GetHeader(LogLevel::Warn) << "[" << __FILE__ << ":" \
121  << __LINE__ << " (" << __func__ << ")] ")
122 #define CUBBYFLOW_ERROR \
123  (Logger(LogLevel::Error) \
124  << Logging::GetHeader(LogLevel::Error) << "[" << __FILE__ << ":" \
125  << __LINE__ << " (" << __func__ << ")] ")
126 #define CUBBYFLOW_DEBUG \
127  (Logger(LogLevel::Debug) \
128  << Logging::GetHeader(LogLevel::Debug) << "[" << __FILE__ << ":" \
129  << __LINE__ << " (" << __func__ << ")] ")
130 } // namespace CubbyFlow
131 
132 #endif
Logger errorLogger
Error-level logger.
Super simple logger implementation.
Definition: Logging.hpp:36
Helper class for logging.
Definition: Logging.hpp:71
Logger warnLogger
Warn-level logger.
Definition: pybind11Utils.hpp:20
const Logger & operator<<(const T &x) const
Writes a value to the buffer stream.
Definition: Logging.hpp:59
Logger infoLogger
Info-level logger.
LogLevel
Definition: Logging.hpp:20
Logger debugLogger
Debug-level logger.