UTILLib 2.0
Common C++ library with utilities.
Loading...
Searching...
No Matches
util::log::LogConfig Class Referencefinal

Singleton class that is used for configure the default logger in an application. More...

#include "util/logconfig."

Public Member Functions

 LogConfig (const LogConfig &)=delete
 LogConfig (LogConfig &&)=delete
LogConfigoperator= (const LogConfig &)=delete
LogConfigoperator= (LogConfig &&)=delete
bool CreateDefaultLogger ()
 Create a logger named 'Default'.
void DeleteLogChain ()
 Clear the list of loggers.
void AddLogger (const std::string &logger_name, std::unique_ptr< ILogger > logger)
 Adds a logger with a unique name.
void AddLogger (const std::string &logger_name, LogType type, const std::vector< std::string > &arg_list)
 Adds a predefined logger with a unique name.
void DeleteLogger (const std::string &logger_name)
 Deletes a logger.
ILoggerGetLogger (const std::string &logger_name) const
 Returns a logger by name.
std::string GetLogFile (const std::string &logger_name="Default") const
 Returns a log file.
void AddLogMessage (const LogMessage &message) const
 Adds a log message.
void Type (LogType log_type)
 Sets the type of default logger.
LogType Type () const
 Returns the type of default logger.
void BaseName (const std::string &base_name)
 Sets the base name (stem) of the log file.
const std::string & BaseName () const
 Returns the base name.
void Enabled (bool enabled)
 Turns off or on the loggers.
bool Enabled () const
 Returns true if the loggers are enabled.
void RootDir (const std::string &root_dir)
 Sets the root directory.
const std::string & RootDir () const
 Return the root directory.
void SubDir (const std::string &sub_dir)
 Sets the sub directory.
const std::string & SubDir () const
 Returns the sub directory.
void ApplicationName (const std::string &app_name)
 Sets the default application name.
const std::string & ApplicationName ()
 Returns the default application name.

Static Public Member Functions

static LogConfigInstance ()
 Returns this class (singleton).

Detailed Description

The class is used to configure the default logger for an application. There is basically 2 default logger to choose between. For large applications, it's recommended to log messages onto a file while simpler applications typically those without a GUI, may log all message onto the cout stream.

Note that you may add many loggers to the application. Normally is the default logger good enough but more advanced system may add more logger interfaces.

Usage (snippets from mdfviewer.cpp):

// Set up the log file.
// The log file will be in %TEMP%/report_server/mdf_viewer.log
auto& log_config = LogConfig::Instance();
log_config.Type(LogType::LogToFile);
log_config.SubDir("report_server");
log_config.BaseName("mdf_viewer");
log_config.CreateDefaultLogger();
LOG_INFO() << "Log File created. Path: " << log_config.GetLogFile();
static LogConfig & Instance()
Returns this class (singleton).
#define LOG_INFO()
Info log stream.
Definition logstream.h:27
@ LogToFile
Log to file.
Definition ilogger.h:24

Member Function Documentation

◆ AddLogger() [1/2]

void util::log::LogConfig::AddLogger ( const std::string & logger_name,
LogType type,
const std::vector< std::string > & arg_list )

Adds a new logger to the logger list. The logger should use a unique name and any existing logger with the same name, will be removed. Note that logger names is case insensitive.

Parameters
[in]logger_nameUnique name of the logger.
[in]typeType of logger.
[in]arg_listArgument list.

◆ AddLogger() [2/2]

void util::log::LogConfig::AddLogger ( const std::string & logger_name,
std::unique_ptr< ILogger > logger )

Adds a known logger to the logger list. The logger should use a unique name and any existing logger with the same name, will be removed. Note that logger names is case insensitive.

Parameters
[in]logger_nameUnique name of the logger.
[in]loggerSmart pointer to the new logger.

◆ AddLogMessage()

void util::log::LogConfig::AddLogMessage ( const LogMessage & message) const

Adds a log message to all the loggers. Note that all log messages uses this function.

Parameters
[in]messageMessage to log.

◆ ApplicationName() [1/2]

const std::string & util::log::LogConfig::ApplicationName ( )
inlinenodiscard

The application name property is used by some typical log related objects.

Returns
Application name.

◆ ApplicationName() [2/2]

void util::log::LogConfig::ApplicationName ( const std::string & app_name)
inline

Some logger includes application name in their message logs. The application name may not contain any spaces. UTF8 characters should be avoided.

Parameters
app_nameApplication name

◆ BaseName()

void util::log::LogConfig::BaseName ( const std::string & base_name)

Sets the base name (stem) of the file name. Don't add any path or extension. Note that the log file backup older log files by adding a number at the end of the base name so avoid using numbers at the end of the base name. Don't use spaces in file name. Use underscore instead.

Parameters
[in]base_nameTypical the application name is used as base name.

◆ CreateDefaultLogger()

bool util::log::LogConfig::CreateDefaultLogger ( )

Create the default logger according to the settings done earlier. The logger name is 'Default'.

Returns
True on success.

◆ DeleteLogChain()

void util::log::LogConfig::DeleteLogChain ( )

Remove all loggers from the application.

◆ DeleteLogger()

void util::log::LogConfig::DeleteLogger ( const std::string & logger_name)

Deletes a logger by its name.

Parameters
logger_nameName of logger. Note case insensitive.

◆ GetLogFile()

std::string util::log::LogConfig::GetLogFile ( const std::string & logger_name = "Default") const

Return the log file if any exist.

Parameters
[in]logger_nameName of the logger.
Returns
Returns the full path to its log file or empty string if no one exist.

◆ GetLogger()

ILogger * util::log::LogConfig::GetLogger ( const std::string & logger_name) const

Returns a logger bu searching of its name.

Parameters
[in]logger_nameName of the logger.
Returns
Pointer to the logger. Do not delete the pointer.

◆ Instance()

LogConfig & util::log::LogConfig::Instance ( )
static

The log configuration is a singleton object that defines the default logger as well as holding the actual list of loggers a.k.a. log chain.

Returns
The singleton log configuration

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