Mountain  1.0.0
Simple C++ 2D Game Framework
logger.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <condition_variable>
4 #include <filesystem>
5 #include <fstream>
6 #include <thread>
7 
8 #include "Mountain/core.hpp"
11 
14 
16 #define DEBUG_LOG(message, ...) Mountain::Logger::LogDebug(message, __FILE__, __LINE__, __VA_ARGS__)
17 
18 namespace Mountain
19 {
47  class Logger final
48  {
49  STATIC_CLASS(Logger)
50 
51  public:
53  enum class LogLevel : uint8_t
54  {
59  Debug,
64  Verbose,
69  Info,
73  Warning,
77  Error,
82  Fatal
83  };
84 
85  private:
86  struct LogEntry
87  {
88  std::string message;
89  LogLevel level;
90  std::chrono::system_clock::time_point time;
91 
92  bool_t printToConsole, printToFile;
93 
94  std::string file;
95  int32_t line = -1;
96 
97  bool_t sameAsPrevious = false;
98 
99  std::shared_ptr<LogEntry> previousLog = nullptr;
100 
101  MOUNTAIN_API LogEntry();
102 
103  MOUNTAIN_API LogEntry(std::string&& message, LogLevel level);
104 
105  MOUNTAIN_API LogEntry(std::string&& message, LogLevel level, const std::string& file, int32_t line);
106 
107  MOUNTAIN_API LogEntry(std::string&& message, LogLevel level, std::chrono::system_clock::time_point timePoint);
108 
109  MOUNTAIN_API LogEntry(std::string&& message, LogLevel level, std::chrono::system_clock::duration duration);
110 
111  MOUNTAIN_API bool_t operator==(const LogEntry& other) const;
112  };
113 
114  // We thought about using std::list here instead but because the allocations are made on the logger thread anyway we can make it a std::vector
115  MOUNTAIN_API static inline std::vector<std::shared_ptr<LogEntry>> m_Logs;
116 
117  public:
121  MOUNTAIN_API static inline LogLevel minimumConsoleLevel =
122  #ifdef _DEBUG
124  #else
126  #endif
127 
131  MOUNTAIN_API static inline LogLevel minimumFileLevel = LogLevel::Info;
132 
141  template <Concepts::FormattableT... Args>
142  static void Log(LogLevel level, const std::string& format, Args&&... args);
143 
150  template <Concepts::FormattableT... Args>
151  static void LogDebug(const std::string& format, const char_t* file, int32_t line, Args&&... args);
152 
156  template <Concepts::FormattableT... Args>
157  static void LogVerbose(const std::string& format, Args&&... args);
158 
162  template <Concepts::FormattableT... Args>
163  static void LogInfo(const std::string& format, Args&&... args);
164 
168  template <Concepts::FormattableT... Args>
169  static void LogWarning(const std::string& format, Args&&... args);
170 
174  template <Concepts::FormattableT... Args>
175  static void LogError(const std::string& format, Args&&... args);
176 
180  template <Concepts::FormattableT... Args>
181  static void LogFatal(const std::string& format, Args&&... args);
182 
188  MOUNTAIN_API static void OpenFile(const std::filesystem::path& filepath);
189 
196  MOUNTAIN_API static void OpenDefaultFile();
197 
199  MOUNTAIN_API static bool HasFileOpen();
200 
202  MOUNTAIN_API static void CloseFile();
203 
205  MOUNTAIN_API static void Synchronize();
206 
213  MOUNTAIN_API static void Start();
214 
221  MOUNTAIN_API static void Stop();
222 
223  MOUNTAIN_API static const decltype(m_Logs)& GetLogList();
224 
225  MOUNTAIN_API static void Clear();
226 
227  private:
228  MOUNTAIN_API static inline TsQueue<std::shared_ptr<LogEntry>> m_NewLogs;
229 
230  MOUNTAIN_API static inline std::shared_ptr<LogEntry> m_LastLog;
231 
232  MOUNTAIN_API static inline bool_t m_LastLogCollapsed = false;
233 
234  MOUNTAIN_API static inline std::condition_variable m_CondVar;
235 
236  MOUNTAIN_API static void Run();
237 
238  MOUNTAIN_API static inline std::thread m_Thread;
239 
240  MOUNTAIN_API static inline std::mutex m_Mutex;
241 
242  MOUNTAIN_API static inline bool_t m_Running = false;
243 
244  MOUNTAIN_API static inline bool_t m_Synchronizing = false;
245 
246  MOUNTAIN_API static inline std::ofstream m_File;
247 
248  MOUNTAIN_API static inline std::filesystem::path m_Filepath;
249 
250  MOUNTAIN_API static inline uint32_t m_LogIndex = 0;
251 
252  MOUNTAIN_API static void PushLog(const std::shared_ptr<LogEntry>& log);
253 
255  MOUNTAIN_API static void PrintLog(const std::shared_ptr<LogEntry>& log);
256 
258  MOUNTAIN_API static std::pair<std::string, const char_t*> BuildLogPrefix(const std::shared_ptr<LogEntry>& log);
259  };
260 }
261 
262 #include "Mountain/utils/logger.inl"
LogLevel
Describes the severity of a log.
Definition: logger.hpp:53
static void Log(LogLevel level, const std::string &format, Args &&... args)
Logs a message using the specified format string, arguments and LogLevel.
Log intended for general information.
Defines the Mountain::TsQueue class.
Log intended for debugging only.
constexpr bool_t operator==(const Color &c1, const Color &c2)
Compares 2 Color component-wise.
static MOUNTAIN_API void Start()
Starts the logger.
static void LogWarning(const std::string &format, Args &&... args)
Logs a warning message using the specified format string and arguments.
static MOUNTAIN_API LogLevel minimumFileLevel
The minimum necessary LogLevel for a log to be printed in the log file.
Definition: logger.hpp:131
static void LogVerbose(const std::string &format, Args &&... args)
Logs a debug message using the specified format string and arguments.
static MOUNTAIN_API void OpenFile(const std::filesystem::path &filepath)
Opens a file for logging.
Log intended for fatal errors.
Thread-Safe Queue.
Definition: ts_queue.hpp:17
static MOUNTAIN_API void OpenDefaultFile()
Opens the default log file.
static MOUNTAIN_API void Stop()
Synchronizes the threads and stops the logger.
Static class used to log messages to the console and/or a file.
Definition: logger.hpp:47
static void LogFatal(const std::string &format, Args &&... args)
Logs a fatal error message using the specified format string and arguments.
static void LogError(const std::string &format, Args &&... args)
Logs an error message using the specified format string and arguments.
static void LogInfo(const std::string &format, Args &&... args)
Logs an information message using the specified format string and arguments.
Log intended for errors.
static MOUNTAIN_API void Synchronize()
Synchronizes the calling thread with the logger one, and makes sure all logs have been printed before...
Defines the Mountain::Concepts namespace which contains useful concepts used in the engine...
Log intended for warnings.
static MOUNTAIN_API bool HasFileOpen()
Returns whether the logger is already log to a file.
static MOUNTAIN_API void CloseFile()
Closes the current log file.
Log intended for temporary debugging only.
static void LogDebug(const std::string &format, const char_t *file, int32_t line, Args &&... args)
Logs a temporary debug message using the current file, line, specified format string and arguments...
static MOUNTAIN_API LogLevel minimumConsoleLevel
The minimum necessary LogLevel for a log to be printed in the console.
Definition: logger.hpp:121
Contains all declarations of the Mountain Framework.
Definition: audio.hpp:22