AtilaCalculatorSoftware  2.3.4
C++ Interface for Atila fem resources files
Logger Class Reference

Logging interface that stores logs, allow logfile writing, also contains other output methods. More...

#include <logger.h>

Public Member Functions

 Logger (const Logger &logger)
 Copy constructor of Logger. More...
 
 ~Logger ()
 Logger destructor. More...
 
Loggeroperator= (const Logger &logger)
 Copy operator of Logger. More...
 
size_t log (const std::string &message, LogLevel level=Debug, time_t timestamp=time(nullptr))
 Add a log entry in the current logger, automatically create log meta data. More...
 

Static Public Member Functions

static std::string getEncodedLogLevel (LogLevel level)
 Get the string format of a given logging level. More...
 
static std::string formatInt (const int &value, int size)
 Format integer value to string fitting a given size. More...
 
static std::string formatEncodedLogLevel (const std::string &str, LogLevel level)
 Replace the level% variable by the given logging level. More...
 
static std::string formatTime (std::string str, time_t timestamp, tm *t)
 
static std::string format (LogMetaData metaData, const std::string &message, const std::string &format=Logger::getLoggingFormat())
 Get the formatted string of a log entry. More...
 
static LoggergetInstance ()
 Retrieve the current instance of logger or create a new one. More...
 
static LogEntriesgetLogs ()
 Get the log entries. More...
 
static std::string getLoggingFormat ()
 
static std::string getDefaultLoggingFormat ()
 
static void setVerbosity (bool verbosity)
 Enable or disable Logger verbosity. More...
 
static void setVerboseLevel (LogLevel level)
 Set the verbosity level, will show all logs above the given level (including itself) More...
 
static void setVerboseLevelRange (LogLevel lowest, LogLevel highest)
 Set the verbosity level range, won't show any log out of this range. More...
 
static void setVerboseLevels (std::set< LogLevel > levels)
 Set the verbose levels manually, will only show elements that have the LogLevel in the set. More...
 
static void logToFile (const std::string &filename, bool legacy=true)
 Writes incoming logs to the given file. More...
 
static void setFileLogging (bool value)
 If set on true will enable file logging. More...
 
static void setLoggingFormat (std::string format)
 Set the logging formatting string of the logger. More...
 
static size_t trace_s (const std::string &message)
 Log a TRACE entry from the given string into the logger. More...
 
template<typename... TArgs>
static size_t trace (TArgs const &... args)
 Concatenate and log the given elements as an TRACE entry. More...
 
static size_t debug_s (const std::string &message)
 Log a DEBUG entry from the given string into the logger. More...
 
template<typename... TArgs>
static size_t debug (TArgs const &... args)
 Concatenate and log the given elements as an DEBUG entry. More...
 
static size_t info_s (const std::string &message)
 Log an INFO entry from the given string into the logger. More...
 
template<typename... TArgs>
static size_t info (TArgs const &... args)
 Concatenate and log the given elements as an INFO entry. More...
 
static size_t warn_s (const std::string &message)
 Log a WARN entry from the given string into the logger. More...
 
template<typename... TArgs>
static size_t warn (TArgs const &... args)
 Concatenate and log the given elements as an WARN entry. More...
 
static size_t error_s (const std::string &message)
 Log an ERROR entry from the given string into the logger. More...
 
template<typename... TArgs>
static size_t error (TArgs const &... args)
 Concatenate and log the given elements as an ERROR entry. More...
 
static size_t fatal_s (const std::string &message)
 Log a FATAL entry from the given string into the logger. More...
 
template<typename... TArgs>
static size_t fatal (TArgs const &... args)
 Concatenate and log the given elements as an FATAL entry. More...
 
static size_t addEntryListener (entry_listener listener)
 Add an entry listener in the entry listeners vector. More...
 

Private Member Functions

 Logger ()
 Logger Singleton private constructor. More...
 

Private Attributes

LogEntriesentries
 Nonformatted raw log entries. More...
 
bool verbose
 If set on true, will show log messages in console. More...
 
std::set< LogLevelverbosityLevels
 Allowed logging levels on verbose (in console) More...
 
bool fileLogging
 If set on true, will write log messages into file. More...
 
std::ofstream loggingFile
 Output file stream for file writing log messages. More...
 
std::vector< entry_listenerlisteners
 List of listeners that will trigger on new entry. More...
 

Static Private Attributes

static LogTimeMode timeMode = TimeLocal
 Time mode of the log entries (Local or UTC - absolute) Default value is TimeLocal. More...
 
static std::string logFormat
 Current log formatting string, if empty will use defaultLogFormat. More...
 
static std::string defaultLogFormat
 Default log formatting string. More...
 
static Loggerinstance = nullptr
 Logger instance. More...
 

Detailed Description

Logging interface that stores logs, allow logfile writing, also contains other output methods.

Constructor & Destructor Documentation

◆ Logger() [1/2]

Logger::Logger ( const Logger logger)

Copy constructor of Logger.

Parameters
loggerLogger to copy
Note
If the source logger is destroyed, the entries vector of the copy will reset as it is a referenced pointer

◆ ~Logger()

Logger::~Logger ( )

Logger destructor.

◆ Logger() [2/2]

Logger::Logger ( )
private

Logger Singleton private constructor.

Member Function Documentation

◆ addEntryListener()

size_t Logger::addEntryListener ( entry_listener  listener)
static

Add an entry listener in the entry listeners vector.

Parameters
listenerListener that will trigger on any new entry
Returns
The id of the listener

◆ debug()

template<typename... TArgs>
static size_t Logger::debug ( TArgs const &...  args)
inlinestatic

Concatenate and log the given elements as an DEBUG entry.

Template Parameters
TArgsTypes of the given elements
Parameters
argsElements to log (string, int, float, ...), will be concatenate
Returns
The id of the create entry

◆ debug_s()

size_t Logger::debug_s ( const std::string &  message)
static

Log a DEBUG entry from the given string into the logger.

Parameters
messageMessage to log
Returns
The id of the current entry

◆ error()

template<typename... TArgs>
static size_t Logger::error ( TArgs const &...  args)
inlinestatic

Concatenate and log the given elements as an ERROR entry.

Template Parameters
TArgsTypes of the given elements
Parameters
argsElements to log (string, int, float, ...), will be concatenate
Returns
The id of the create entry

◆ error_s()

size_t Logger::error_s ( const std::string &  message)
static

Log an ERROR entry from the given string into the logger.

Parameters
messageMessage to log
Returns
The id of the current entry

◆ fatal()

template<typename... TArgs>
static size_t Logger::fatal ( TArgs const &...  args)
inlinestatic

Concatenate and log the given elements as an FATAL entry.

Template Parameters
TArgsTypes of the given elements
Parameters
argsElements to log (string, int, float, ...), will be concatenate
Returns
The id of the create entry

◆ fatal_s()

size_t Logger::fatal_s ( const std::string &  message)
static

Log a FATAL entry from the given string into the logger.

Parameters
messageMessage to log
Returns
The id of the current entry

◆ format()

std::string Logger::format ( LogMetaData  metaData,
const std::string &  message,
const std::string &  format = Logger::getLoggingFormat() 
)
static

Get the formatted string of a log entry.

Parameters
metaDataMeta data of the log entry
messageMessage of the log entry
formatThe format to apply to the log entry
Returns
The string formatted log entry

◆ formatEncodedLogLevel()

std::string Logger::formatEncodedLogLevel ( const std::string &  str,
LogLevel  level 
)
static

Replace the level% variable by the given logging level.

Parameters
strThe base string
levelThe logging level
Returns
The string with the variable replaced

◆ formatInt()

std::string Logger::formatInt ( const int &  value,
int  size 
)
static

Format integer value to string fitting a given size.

Parameters
valueInteger value to format
sizeSize of the string
Returns
The formatted string

◆ formatTime()

std::string Logger::formatTime ( std::string  str,
time_t  timestamp,
tm *  t 
)
static

the dt:x% variables by the given time values

Parameters
strThe base string
tThe time values
Returns
The string with the variable replaced

◆ getDefaultLoggingFormat()

std::string Logger::getDefaultLoggingFormat ( )
static
Returns
The default logging formatting string
See also
defaultLogFormat

◆ getEncodedLogLevel()

std::string Logger::getEncodedLogLevel ( LogLevel  level)
static

Get the string format of a given logging level.

Parameters
levelThe logging level
Returns
The string formatted logging level

◆ getInstance()

Logger * Logger::getInstance ( )
static

Retrieve the current instance of logger or create a new one.

Returns
The Logger object instance reference

◆ getLoggingFormat()

std::string Logger::getLoggingFormat ( )
static
Returns
The current logging formatting string
See also
logFormat

◆ getLogs()

LogEntries * Logger::getLogs ( )
static

Get the log entries.

Returns
A pointer to the log entries container

◆ info()

template<typename... TArgs>
static size_t Logger::info ( TArgs const &...  args)
inlinestatic

Concatenate and log the given elements as an INFO entry.

Template Parameters
TArgsTypes of the given elements
Parameters
argsElements to log (string, int, float, ...), will be concatenate
Returns
The id of the create entry

◆ info_s()

size_t Logger::info_s ( const std::string &  message)
static

Log an INFO entry from the given string into the logger.

Parameters
messageMessage to log
Returns
The id of the current entry

◆ log()

size_t Logger::log ( const std::string &  message,
LogLevel  level = Debug,
time_t  timestamp = time(nullptr) 
)

Add a log entry in the current logger, automatically create log meta data.

Parameters
messageMessage to log
levelLogging level (default = Debug)
timestampLogging timestamp
Returns
The id of the current entry

◆ logToFile()

void Logger::logToFile ( const std::string &  filename,
bool  legacy = true 
)
static

Writes incoming logs to the given file.

Parameters
filenamePath to the log file
legacyIf set on true, will write all previous logs

◆ operator=()

Logger & Logger::operator= ( const Logger logger)

Copy operator of Logger.

Parameters
loggerLogger to copy
Note
If the source logger is destroyed, the entries vector of the copy will reset as it is a referenced pointer

◆ setFileLogging()

void Logger::setFileLogging ( bool  value)
static

If set on true will enable file logging.

◆ setLoggingFormat()

void Logger::setLoggingFormat ( std::string  format)
static

Set the logging formatting string of the logger.

Parameters
formatFormat of the log entries
See also
logFormat

◆ setVerboseLevel()

void Logger::setVerboseLevel ( LogLevel  level)
static

Set the verbosity level, will show all logs above the given level (including itself)

Parameters
levelLower level of verbosity

◆ setVerboseLevelRange()

void Logger::setVerboseLevelRange ( LogLevel  lowest,
LogLevel  highest 
)
static

Set the verbosity level range, won't show any log out of this range.

Parameters
lowestLowest verbosity level of the range
highestHighest verbosity level of the range

◆ setVerboseLevels()

void Logger::setVerboseLevels ( std::set< LogLevel levels)
static

Set the verbose levels manually, will only show elements that have the LogLevel in the set.

Parameters
levelsSet of allowed logging levels in verbose

◆ setVerbosity()

void Logger::setVerbosity ( bool  verbosity)
static

Enable or disable Logger verbosity.

◆ trace()

template<typename... TArgs>
static size_t Logger::trace ( TArgs const &...  args)
inlinestatic

Concatenate and log the given elements as an TRACE entry.

Template Parameters
TArgsTypes of the given elements
Parameters
argsElements to log (string, int, float, ...), will be concatenate
Returns
The id of the create entry

◆ trace_s()

size_t Logger::trace_s ( const std::string &  message)
static

Log a TRACE entry from the given string into the logger.

Parameters
messageMessage to log
Returns
The id of the current entry

◆ warn()

template<typename... TArgs>
static size_t Logger::warn ( TArgs const &...  args)
inlinestatic

Concatenate and log the given elements as an WARN entry.

Template Parameters
TArgsTypes of the given elements
Parameters
argsElements to log (string, int, float, ...), will be concatenate
Returns
The id of the create entry

◆ warn_s()

size_t Logger::warn_s ( const std::string &  message)
static

Log a WARN entry from the given string into the logger.

Parameters
messageMessage to log
Returns
The id of the current entry

Member Data Documentation

◆ defaultLogFormat

std::string Logger::defaultLogFormat
staticprivate
Initial value:
=
"[%dt:year%-%dt:mon%-%dt:mday%|%dt:hour%:%dt:min%:%dt:sec%][%level%]%message%"

Default log formatting string.

See also
logFormat

◆ entries

LogEntries* Logger::entries
private

Nonformatted raw log entries.

Note
As LogEntries and Logger as self including classes, we have to use a reference instead of a classic use (as LogEntries is "incomplete")

◆ fileLogging

bool Logger::fileLogging
private

If set on true, will write log messages into file.

◆ instance

Logger * Logger::instance = nullptr
staticprivate

Logger instance.

◆ listeners

std::vector<entry_listener> Logger::listeners
private

List of listeners that will trigger on new entry.

◆ logFormat

std::string Logger::logFormat
staticprivate

Current log formatting string, if empty will use defaultLogFormat.

Variables:

  • dt%: Hardcoded timestamp (eg: "1548405000")
  • dt:sec%: Timestamp second ([0;59])
  • dt:min%: Timestamp minute ([0;59])
  • dt:hour%: Timestamp hour ([0:23])
  • dt:mday%: Timestamp day of the month ([1:31])
  • dt:mon%: Timestamp month of the year ([1:12])
  • dt:year%: Timestamp year (> 1900)
  • dt%:wday%: Timestamp day of the week ([0;6])
  • dt:yday%: Timestamp day of the year ([0;365])
  • level%: Logging level (eg: "WARN")
  • message%: Logging message (eg: "Loading /share/file.res resource file")

All dt variables are related to the ctime library tm structure

See also
https://www.cplusplus.com/reference/ctime/tm/

Default value is an empty string

See also
defaultLogFormat

◆ loggingFile

std::ofstream Logger::loggingFile
private

Output file stream for file writing log messages.

◆ timeMode

LogTimeMode Logger::timeMode = TimeLocal
staticprivate

Time mode of the log entries (Local or UTC - absolute) Default value is TimeLocal.

◆ verbose

bool Logger::verbose
private

If set on true, will show log messages in console.

◆ verbosityLevels

std::set<LogLevel> Logger::verbosityLevels
private

Allowed logging levels on verbose (in console)


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