Mountain  1.0.0
Simple C++ 2D Game Framework
Mountain::Logger Class Referencefinal

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...
 

Detailed Description

Static class used to log messages to the console and/or a file.

Requirements

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().

Options

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.

Usage

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.

Example

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:

[11:26:05.751] [INFO] Starting logging to file.

Also, debug logs automatically specify the file and line at which the log was made.

Definition at line 47 of file logger.hpp.

Member Enumeration Documentation

◆ LogLevel

enum Mountain::Logger::LogLevel : uint8_t
strong

Describes the severity of a log.

Enumerator
Debug 

Log intended for temporary debugging only.

Preceded by '[TEMP DEBUG]' and appears green in the console. Temporary debug logs are not printed in the log file by default, and they are only printed in the console if in a debug build.

Verbose 

Log intended for debugging only.

Preceded by '[DEBUG]' and appears gray in the console. Verbose logs are not printed in the log file by default, and they are only printed in the console if in a debug build.

Info 

Log intended for general information.

Preceded by '[INFO]' and appears white in the console. Info logs are not printed in the log file by default.

Warning 

Log intended for warnings.

Preceded by '[WARN]' and appears orange in the console.

Error 

Log intended for errors.

Preceded by '[ERROR]' and appears red in the console.

Fatal 

Log intended for fatal errors.

Preceded by '[FATAL]' and appears red and bold in the console. After such a log, the program is not intended to continue to function normally and should instead exit ASAP.

Definition at line 53 of file logger.hpp.

Member Function Documentation

◆ Log()

template<Concepts::FormattableT... Args>
static void Mountain::Logger::Log ( LogLevel  level,
const std::string &  format,
Args &&...  args 
)
static

Logs a message using the specified format string, arguments and LogLevel.

Template Parameters
ArgsThe 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.
Parameters
levelThe log severity.
formatThe format string to log.
argsThe arguments to replace the format string with.
See also
The standard format specification

◆ LogDebug()

template<Concepts::FormattableT... Args>
static void Mountain::Logger::LogDebug ( const std::string &  format,
const char_t *  file,
int32_t  line,
Args &&...  args 
)
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.

See also
Log
LogLevel::Debug

◆ LogError()

template<Concepts::FormattableT... Args>
static void Mountain::Logger::LogError ( const std::string &  format,
Args &&...  args 
)
static

Logs an error message using the specified format string and arguments.

See also
Log

◆ LogFatal()

template<Concepts::FormattableT... Args>
static void Mountain::Logger::LogFatal ( const std::string &  format,
Args &&...  args 
)
static

Logs a fatal error message using the specified format string and arguments.

See also
Log

◆ LogInfo()

template<Concepts::FormattableT... Args>
static void Mountain::Logger::LogInfo ( const std::string &  format,
Args &&...  args 
)
static

Logs an information message using the specified format string and arguments.

See also
Log

◆ LogVerbose()

template<Concepts::FormattableT... Args>
static void Mountain::Logger::LogVerbose ( const std::string &  format,
Args &&...  args 
)
static

Logs a debug message using the specified format string and arguments.

See also
Log

◆ LogWarning()

template<Concepts::FormattableT... Args>
static void Mountain::Logger::LogWarning ( const std::string &  format,
Args &&...  args 
)
static

Logs a warning message using the specified format string and arguments.

See also
Log

◆ OpenDefaultFile()

static MOUNTAIN_API void Mountain::Logger::OpenDefaultFile ( )
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.

◆ OpenFile()

static MOUNTAIN_API void Mountain::Logger::OpenFile ( const std::filesystem::path &  filepath)
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.

Parameters
filepathThe file to open and to log into.

◆ Start()

static MOUNTAIN_API void Mountain::Logger::Start ( )
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.

◆ Stop()

static MOUNTAIN_API void Mountain::Logger::Stop ( )
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.

Member Data Documentation

◆ minimumConsoleLevel

MOUNTAIN_API LogLevel Mountain::Logger::minimumConsoleLevel
inlinestatic
Initial value:

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.

◆ minimumFileLevel

MOUNTAIN_API LogLevel Mountain::Logger::minimumFileLevel = LogLevel::Info
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.


The documentation for this class was generated from the following file: