VOX
A little voxel engine
Loading...
Searching...
No Matches
logger.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "ft_format.hpp"
4
5#include <iostream>
6#include <fstream>
7#include <sstream>
8#include <ctime>
9#include <iomanip>
10#include <filesystem>
11#include <memory>
12#include <mutex>
13
14#define LOG_CRITICAL(...) logger << Logger::Level::CRITICAL << __VA_ARGS__ << std::endl
15#define LOG_ERROR(...) logger << Logger::Level::ERROR << __VA_ARGS__ << std::endl
16#define LOG_WARNING(...) logger << Logger::Level::WARNING << __VA_ARGS__ << std::endl
17#define LOG_INFO(...) logger << Logger::Level::INFO << __VA_ARGS__ << std::endl
18#define LOG_DEBUG(...) logger << Logger::Level::DEBUG << __VA_ARGS__ << std::endl
19#define LOG_TRACE(...) logger << Logger::Level::TRACE << __VA_ARGS__ << std::endl
20
21#define LOG_CRITICAL_FMT(fmt, ...) logger << Logger::Level::CRITICAL << ft_format(fmt, __VA_ARGS__) << std::endl
22#define LOG_ERROR_FMT(fmt, ...) logger << Logger::Level::ERROR << ft_format(fmt, __VA_ARGS__) << std::endl
23#define LOG_WARNING_FMT(fmt, ...) logger << Logger::Level::WARNING << ft_format(fmt, __VA_ARGS__) << std::endl
24#define LOG_INFO_FMT(fmt, ...) logger << Logger::Level::INFO << ft_format(fmt, __VA_ARGS__) << std::endl
25#define LOG_DEBUG_FMT(fmt, ...) logger << Logger::Level::DEBUG << ft_format(fmt, __VA_ARGS__) << std::endl
26#define LOG_TRACE_FMT(fmt, ...) logger << Logger::Level::TRACE << ft_format(fmt, __VA_ARGS__) << std::endl
27
28// #define LOG_CRITICAL_FMT(fmt, ...) logger << Logger::Level::CRITICAL << fmt << std::endl, __VA_ARGS__
29
33class FileOutput: public std::ofstream
34{
35
36public:
37
45 FileOutput(const std::filesystem::path & path);
46
50 FileOutput(FileOutput &&other);
51
56
57};
58
62class Logger
63{
64
65public:
66
70 enum Level
71 {
73 ERROR = 1,
75 INFO = 3,
76 DEBUG = 4,
77 TRACE = 5,
78 MAX = 6
79 };
80
81
85 Logger();
86
94 Logger(const std::filesystem::path & path);
95
103 void configure(const std::filesystem::path & path);
104
110 void setLevel(Level level);
111
115 Level level() const;
116
122 void setTimestamp(bool enabled);
123
132
138 Logger & operator<<(std::ostream & (*manipulator)(std::ostream &));
139
145 template <typename T>
146 Logger & operator<<(T const & arg)
147 {
148 m_current_msg << arg;
149 return *this;
150 }
151
152private:
153
157 bool m_file_initialized = false;
158
162 std::stringstream m_current_msg;
163
167 std::unique_ptr<FileOutput> m_log_files[static_cast<int>(Level::MAX)];
168
172 Level m_min_console_level = Level::TRACE;
173
177 Level m_next_msg_level = Level::INFO;
178
182 bool m_timestamp_enabled = true;
183
184 std::mutex m_mutex;
185
189 inline static std::string const m_level_to_string[static_cast<uint32_t>(MAX)] =
190 {
191 "[CRITICAL]:",
192 "[ERROR] :",
193 "[WARNING] :",
194 "[INFO] :",
195 "[DEBUG] :",
196 "[TRACE] :"
197 };
198
202 inline static std::string const m_level_to_color[static_cast<uint32_t>(MAX)] =
203 {
204 "\033[33;41;1m",
205 "\033[31m",
206 "\033[33m",
207 "\033[32m",
208 "\033[34m",
209 "\033[35m"
210 };
211
215 void _flush();
216
220 FileOutput & _logFile(Level level) const;
221
225 void _writeToConsole(std::string const & message);
226
230 void _writeToFile(std::string const & message);
231
235 std::string _levelHeader(Level level, bool color = true);
236
240 std::string _timestamp();
241
242};
243
244extern Logger logger;
A class that represents a file used for output with RAII.
Definition: logger.hpp:34
~FileOutput()
Destroy the FileOutput object and close the file.
Definition: logger.cpp:23
A class for logging messages to the console and to files.
Definition: logger.hpp:63
Logger & operator<<(T const &arg)
Transfer a argument to the stringstream buffer waiting to be flushed.
Definition: logger.hpp:146
Logger & operator<<(Level level)
Set the message level.
Definition: logger.cpp:70
void configure(const std::filesystem::path &path)
Open 5 different files for logging. If the files exist, they will be overwritten.
Definition: logger.cpp:44
void setTimestamp(bool enabled)
Enable or disable timestamps.
Definition: logger.cpp:65
void setLevel(Level level)
Set the minimum level of messages to log to the console.
Definition: logger.cpp:55
Logger()
Construct a new Logger object.
Definition: logger.cpp:37
Level
The different levels of logging.
Definition: logger.hpp:71
@ ERROR
Definition: logger.hpp:73
@ CRITICAL
Definition: logger.hpp:72
@ INFO
Definition: logger.hpp:75
@ TRACE
Definition: logger.hpp:77
@ MAX
Definition: logger.hpp:78
@ DEBUG
Definition: logger.hpp:76
@ WARNING
Definition: logger.hpp:74
Level level() const
Return the minimum level of messages to log to the console.
Definition: logger.cpp:60
Logger logger
Definition: logger.cpp:35