Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
core_lib::log::DebugLog< Formatter > Class Template Referencefinal

DebugLog class. More...

#include <DebugLog.h>

Public Member Functions

 DebugLog ()=default
 Default constructor. More...
 
 DebugLog (const std::string &softwareVersion, const std::string &logFolderPath, const std::string &logName, long maxLogSize=5 *BYTES_IN_MEBIBYTE)
 Initialisation constructor. More...
 
 DebugLog (const DebugLog &)=delete
 Copy constructor deleted.
 
DebugLogoperator= (const DebugLog &)=delete
 Copy assignment operator deleted.
 
 DebugLog (DebugLog &&)=delete
 Move constructor deleted.
 
DebugLogoperator= (DebugLog &&)=delete
 Move assignment operator deleted.
 
 ~DebugLog ()
 Destructor.
 
void Instantiate (const std::string &softwareVersion, const std::string &logFolderPath, const std::string &logName, long maxLogSize=5 *BYTES_IN_MEBIBYTE)
 Instantiate a previously default constructed DebugLog object. More...
 
void AddLogMsgLevelFilter (eLogMessageLevel logMessageLevel)
 Add level to filter. More...
 
void RemoveLogMsgLevelFilter (eLogMessageLevel logMessageLevel)
 Remove level from filter. More...
 
void ClearLogMsgLevelFilters ()
 Clear all message levels from filter. More...
 
void AddLogMessage (const std::string &message)
 Add message to the log file. More...
 
void AddLogMessage (const std::string &message, const std::string &file, const std::string &function, int lineNo, eLogMessageLevel logMsgLevel)
 Add message to the log file. More...
 

Private Types

enum  eFileOpenOptions { truncate_file, append_file }
 Enumeration containing file opening options. More...
 
using log_msg_queue = threads::MessageQueueThread< int, dl_private::LogQueueMessage >
 Typedef for message queue thread.
 

Private Member Functions

long MaxLogSize () const
 Get the max log size in bytes.
 
void RegisterLogQueueMessageId ()
 Register the log queue message ID.
 
bool MessageHandler (dl_private::LogQueueMessage &message)
 Method to process message. More...
 
bool IsLogMsgLevelInLookup (eLogMessageLevel logMessageLevel) const
 Is message level in map. More...
 
const std::string & GetLogMsgLevelAsString (eLogMessageLevel logMessageLevel) const
 Get message level as a string. More...
 
bool IsLogMsgLevelFilterSetNoMutex (eLogMessageLevel logMessageLevel) const
 Is message level in filter set (no mutex). More...
 
bool IsLogMsgLevelFilterSet (eLogMessageLevel logMessageLevel) const
 Is message level in filter set (with mutex). More...
 
void OpenOfStream (const std::string &filePath, eFileOpenOptions fileOptions)
 Open file stream. More...
 
void CloseOfStream ()
 Close file stream.
 
void CheckLogFileSize (long requiredSpace)
 Check size of current log file. More...
 
void WriteMessageToLog (dl_private::LogQueueMessage &&logMessage)
 Write log message to file stream. More...
 

Static Private Member Functions

static int MessageDecoder (const dl_private::LogQueueMessage &)
 Method to decode message ID. More...
 

Private Attributes

std::mutex m_mutex
 Mutex to lock access.
 
std::string m_unknownLogMsgLevel {"?"}
 String for unknown message level.
 
std::unordered_map< eLogMessageLevel, std::string > m_logMsgLevelLookup
 Message level string lookup map. More...
 
std::set< eLogMessageLevelm_logMsgFilterSet {}
 Message level filter set.
 
Formatter m_logFormatter {}
 Log formatter object.
 
long m_maxLogSize {5 * BYTES_IN_MEBIBYTE}
 Log file max size.
 
std::ofstream m_ofStream {}
 Output file stream.
 
std::string m_softwareVersion {}
 Software version string.
 
std::string m_logFilePath {}
 Path to current log file.
 
std::string m_oldLogFilePath {}
 Path to old log file.
 
std::unique_ptr< log_msg_queuem_logMsgQueueThread
 Unique_ptr holding message queue thread. More...
 

Detailed Description

template<class Formatter>
class core_lib::log::DebugLog< Formatter >

DebugLog class.

Templated class to perform logging. The class is threaded and thread safe.

The template args comprise a formatter type and which should be a function object with same prototype as DefaultLogFormat::operator()().

The second arg is optional and controls the size at which the log will close and switch to a new file. Only 2 files ever exist the "log".txt and "log"_old.txt. The default log size is 5MiB.

Member Enumeration Documentation

◆ eFileOpenOptions

template<class Formatter >
enum core_lib::log::DebugLog::eFileOpenOptions
private

Enumeration containing file opening options.

Enumerator
truncate_file 

Option to truncate file when opened.

append_file 

Option to append to file when opened.

Constructor & Destructor Documentation

◆ DebugLog() [1/2]

template<class Formatter >
core_lib::log::DebugLog< Formatter >::DebugLog ( )
default

Default constructor.

Create the DebugLog object in a partial state. This is used by the SingletonHolder to create a singleton DebugLog instance. The user should then call Instantiate to complete the setup of the log, otherwise no output or file will be created.

◆ DebugLog() [2/2]

template<class Formatter >
core_lib::log::DebugLog< Formatter >::DebugLog ( const std::string &  softwareVersion,
const std::string &  logFolderPath,
const std::string &  logName,
long  maxLogSize = 5 * BYTES_IN_MEBIBYTE 
)
inline

Initialisation constructor.

Parameters
[in]softwareVersion- Version of software that "owns" the log.
[in]logFolderPath- Folder path (with trailing slash) where log will be created.
[in]logName- File name of log file without extension.
[in]maxLogSize- (Optional) The maximum size for the log file.

Create the DebugLog in given folder with given name. A ".txt" extension is automatically appending to log file's name.

Member Function Documentation

◆ AddLogMessage() [1/2]

template<class Formatter >
void core_lib::log::DebugLog< Formatter >::AddLogMessage ( const std::string &  message)
inline

Add message to the log file.

Parameters
[in]message- Message to add to log.

Add a simple message to the log without any extra properties set, such as file, line no. etc.

◆ AddLogMessage() [2/2]

template<class Formatter >
void core_lib::log::DebugLog< Formatter >::AddLogMessage ( const std::string &  message,
const std::string &  file,
const std::string &  function,
int  lineNo,
eLogMessageLevel  logMsgLevel 
)
inline

Add message to the log file.

Parameters
[in]message- Message to add to log.
[in]file- Source file in which message AddLogMessage was called, e.g. std::string(FILE).
[in]function- Function in source file in which message AddLogMessage was called, e.g. BOOST_CURRENT_FUNCTION.
[in]lineNo- Line number in the source file where AddLogMessage was called, e.g. LINE.
[in]logMsgLevel- Message level.

Add a message to the log with extra properties set, such as file, line no. etc.

◆ AddLogMsgLevelFilter()

template<class Formatter >
void core_lib::log::DebugLog< Formatter >::AddLogMsgLevelFilter ( eLogMessageLevel  logMessageLevel)
inline

Add level to filter.

Parameters
[in]logMessageLevel- Message level to filter out of log.

You can dynamically filter out log message from appearing in the log file based on adding message levels to the filter set. For example, you may want to filter out messages of type eLogMessageLevel::warning. In this case after calling this function with eLogMessageLevel::warning messages of this type will not appear in the log from this point until you later remove the filter.

◆ CheckLogFileSize()

template<class Formatter >
void core_lib::log::DebugLog< Formatter >::CheckLogFileSize ( long  requiredSpace)
inlineprivate

Check size of current log file.

Parameters
[in]requiredSpace- Space required in file to write new message.

◆ ClearLogMsgLevelFilters()

template<class Formatter >
void core_lib::log::DebugLog< Formatter >::ClearLogMsgLevelFilters ( )
inline

Clear all message levels from filter.

Clear the filter set. AFter calling this messages of all levels will once again appear in the log file.

◆ GetLogMsgLevelAsString()

template<class Formatter >
const std::string& core_lib::log::DebugLog< Formatter >::GetLogMsgLevelAsString ( eLogMessageLevel  logMessageLevel) const
inlineprivate

Get message level as a string.

Parameters
[in]logMessageLevel- Message level.
Returns
Message level string.

◆ Instantiate()

template<class Formatter >
void core_lib::log::DebugLog< Formatter >::Instantiate ( const std::string &  softwareVersion,
const std::string &  logFolderPath,
const std::string &  logName,
long  maxLogSize = 5 * BYTES_IN_MEBIBYTE 
)
inline

Instantiate a previously default constructed DebugLog object.

Parameters
[in]softwareVersion- Version of software that "owns" the log.
[in]logFolderPath- Folder path (with trailing slash) where log will be created.
[in]logName- File name of log file without extension.
[in]maxLogSize- (Optional) The maximum size for the log file.

Instantiate the DebugLog in given folder with given name. A ".txt" extension is automatically appending to log file's name. This method should only be used when constructing a DebugLog using the default constructor.

This method throws std::runtime_error exception if object already instantiated.

◆ IsLogMsgLevelFilterSet()

template<class Formatter >
bool core_lib::log::DebugLog< Formatter >::IsLogMsgLevelFilterSet ( eLogMessageLevel  logMessageLevel) const
inlineprivate

Is message level in filter set (with mutex).

Parameters
[in]logMessageLevel- Message level.
Returns
True if message level is found, false otherwise.

◆ IsLogMsgLevelFilterSetNoMutex()

template<class Formatter >
bool core_lib::log::DebugLog< Formatter >::IsLogMsgLevelFilterSetNoMutex ( eLogMessageLevel  logMessageLevel) const
inlineprivate

Is message level in filter set (no mutex).

Parameters
[in]logMessageLevel- Message level.
Returns
True if message level is found, false otherwise.

◆ IsLogMsgLevelInLookup()

template<class Formatter >
bool core_lib::log::DebugLog< Formatter >::IsLogMsgLevelInLookup ( eLogMessageLevel  logMessageLevel) const
inlineprivate

Is message level in map.

Parameters
[in]logMessageLevel- Message level.
Returns
True if message level is found, false otherwise.

◆ MessageDecoder()

template<class Formatter >
static int core_lib::log::DebugLog< Formatter >::MessageDecoder ( const dl_private::LogQueueMessage )
inlinestaticprivate

Method to decode message ID.

Parameters
[in]message- Message to decode.
Returns
Message ID.

◆ MessageHandler()

template<class Formatter >
bool core_lib::log::DebugLog< Formatter >::MessageHandler ( dl_private::LogQueueMessage message)
inlineprivate

Method to process message.

Parameters
[in]message- Message to process.
Returns
LogQueueMessage reference.

◆ OpenOfStream()

template<class Formatter >
void core_lib::log::DebugLog< Formatter >::OpenOfStream ( const std::string &  filePath,
eFileOpenOptions  fileOptions 
)
inlineprivate

Open file stream.

Parameters
[in]filePath- File path.
[in]fileOptions- File options (truncate or append).

◆ RemoveLogMsgLevelFilter()

template<class Formatter >
void core_lib::log::DebugLog< Formatter >::RemoveLogMsgLevelFilter ( eLogMessageLevel  logMessageLevel)
inline

Remove level from filter.

Parameters
[in]logMessageLevel- Message level to remove from filter set.

If you want to make sure messages of a given level appear back in the log file after having called AddLogMsgLevelFilter at some point then call this function to remove the message level from the filter set. After calling this function messages of the specified level will once again appear in the log file.

◆ WriteMessageToLog()

template<class Formatter >
void core_lib::log::DebugLog< Formatter >::WriteMessageToLog ( dl_private::LogQueueMessage &&  logMessage)
inlineprivate

Write log message to file stream.

Parameters
[in]logMessage- Log message, pefectly forwarded.

Member Data Documentation

◆ m_logMsgLevelLookup

template<class Formatter >
std::unordered_map<eLogMessageLevel, std::string> core_lib::log::DebugLog< Formatter >::m_logMsgLevelLookup
private
Initial value:

Message level string lookup map.

◆ m_logMsgQueueThread

template<class Formatter >
std::unique_ptr<log_msg_queue> core_lib::log::DebugLog< Formatter >::m_logMsgQueueThread
private
Initial value:

Unique_ptr holding message queue thread.


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