Mountain
1.0.0
Simple C++ 2D Game Framework
|
Static class used to log messages to the console and/or a file. More...
#include <logger.hpp>
Public Types | |
enum | LogLevel : uint8_t { LogLevel::Debug, LogLevel::Verbose, LogLevel::Info, LogLevel::Warning, LogLevel::Error, LogLevel::Fatal } |
Describes the severity of a log. More... | |
Static Public Member Functions | |
template<Concepts::FormattableT... Args> | |
static void | Log (LogLevel level, const std::string &format, Args &&... args) |
Logs a message using the specified format string, arguments and LogLevel. More... | |
template<Concepts::FormattableT... Args> | |
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. More... | |
template<Concepts::FormattableT... Args> | |
static void | LogVerbose (const std::string &format, Args &&... args) |
Logs a debug message using the specified format string and arguments. More... | |
template<Concepts::FormattableT... Args> | |
static void | LogInfo (const std::string &format, Args &&... args) |
Logs an information message using the specified format string and arguments. More... | |
template<Concepts::FormattableT... Args> | |
static void | LogWarning (const std::string &format, Args &&... args) |
Logs a warning message using the specified format string and arguments. More... | |
template<Concepts::FormattableT... Args> | |
static void | LogError (const std::string &format, Args &&... args) |
Logs an error message using the specified format string and arguments. More... | |
template<Concepts::FormattableT... Args> | |
static void | LogFatal (const std::string &format, Args &&... args) |
Logs a fatal error message using the specified format string and arguments. More... | |
static MOUNTAIN_API void | OpenFile (const std::filesystem::path &filepath) |
Opens a file for logging. More... | |
static MOUNTAIN_API void | OpenDefaultFile () |
Opens the default log file. More... | |
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. | |
static MOUNTAIN_API void | Synchronize () |
Synchronizes the calling thread with the logger one, and makes sure all logs have been printed before returning. | |
static MOUNTAIN_API void | Start () |
Starts the logger. More... | |
static MOUNTAIN_API void | Stop () |
Synchronizes the threads and stops the logger. More... | |
static MOUNTAIN_APIdecltype(m_Logs) const & | GetLogList () |
static MOUNTAIN_API void | Clear () |
Static Public Attributes | |
static MOUNTAIN_API LogLevel | minimumConsoleLevel |
The minimum necessary LogLevel for a log to be printed in the console. More... | |
static MOUNTAIN_API LogLevel | minimumFileLevel = LogLevel::Info |
The minimum necessary LogLevel for a log to be printed in the log file. More... | |
Static class used to log messages to the console and/or a file.
Thread-safe logger that starts logging once Logger::Start()
gets called, which is already done in Game::Game()
. You can synchronize the calling thread with the logger one at any time by calling Logger::Synchronize
. The logger needs to be stopped by calling Logger::Stop()
, which is also already done in Game::~Game()
.
By default, the logger doesn't log to a file. This can be changed by either calling Logger::OpenDefaultFile
or Logger::OpenFile
. You can also stop logging to the file whenever you want by calling Logger::CloseFile
.
You can change at any time the minimum LogLevel for either the console or the file by respectively setting Logger::minimumConsoleLevel
or Logger::minimumFileLevel
to a different value.
The most generic way of logging is by using the Logger::Log
function, which allows you to pass a LogLevel to describe the severity of the log. Shortcuts are also available through the use of Logger::LogDebug
, Logger::LogVerbose
, Logger::LogInfo
, Logger::LogWarning
, Logger::LogError
and Logger::LogFatal
. Those functions take a format string and format parameters to follow the usage of std::format. This means that any new parameter type that is directly printed must satisfy the requirements of the std::formattable concept (defined a Concepts::Formattable in the Mountain namespace), and therefore needs to implement its own version of the std::formatter struct.
All logs are preceded by their timestamp (the exact time at which the Logger::Log
function was called) and a string representation of their LogLevel. A typical log looks like the following:
Also, debug logs automatically specify the file and line at which the log was made.
Definition at line 47 of file logger.hpp.
|
strong |
Describes the severity of a log.
Definition at line 53 of file logger.hpp.
|
static |
Logs a message using the specified format string, arguments and LogLevel.
Args | The format arguments types. These are generally automatically deducted by the compiler and often don't need to be manually specified. Also, they must satisfy the Concepts::Formattable concept requirements. |
level | The log severity. |
format | The format string to log. |
args | The arguments to replace the format string with. |
|
static |
Logs a temporary debug message using the current file, line, specified format string and arguments.
This function shouldn't be used directly. To print a temporary debug log message, instead use DEBUG_LOG.
|
static |
Logs an error message using the specified format string and arguments.
|
static |
Logs a fatal error message using the specified format string and arguments.
|
static |
Logs an information message using the specified format string and arguments.
|
static |
Logs a debug message using the specified format string and arguments.
|
static |
Logs a warning message using the specified format string and arguments.
|
static |
Opens the default log file.
The default log file is an autogenerated one with a name corresponding to the current date and the .log file extension. An example of a default log file:
2024-02-13.log
. If this is called multiple times in a day (by launching the program multiple times during the same day for example), this appends to the already existing log file.
|
static |
Opens a file for logging.
If a file is already open for logging e.g. if Logger::HasFileOpen returns true
, this overwrites it with the new file.
filepath | The file to open and to log into. |
|
static |
Starts the logger.
This function is called automatically when the Game is constructed. After a call to this function, you can use the Log functions.
This function doesn't do anything if the logger has already been started.
|
static |
Synchronizes the threads and stops the logger.
This function is called automatically when the Game is destroyed. After a call to this function, logger function calls won't do anything.
This function doesn't do anything if the logger has already been stopped.
|
inlinestatic |
The minimum necessary LogLevel for a log to be printed in the console.
Defaults to LogLevel::Debug in a debug build, or LogLevel::Info otherwise.
Definition at line 121 of file logger.hpp.
|
inlinestatic |
The minimum necessary LogLevel for a log to be printed in the log file.
Defaults to LogLevel::Info.
Definition at line 131 of file logger.hpp.