kanzi::AbstractLogger Class Referenceabstract

Inherit from this class to implement logger. More...

#include <kanzi/core/log/abstract_logger.hpp>

Inheritance diagram for kanzi::AbstractLogger:
kanzi::AndroidLogger kanzi::CoutLogger kanzi::detail::ChainedLogger kanzi::detail::StringLogger kanzi::Win32DebugLogger

Public Member Functions

void write (LogLevel level, string_view levelName, string_view categoryName, string_view fileName, size_t lineNumber, string_view message)
 This function is called by Kanzi to store the message to the log. More...
 
virtual ~AbstractLogger ()
 Destructor. More...
 

Protected Member Functions

virtual void writeOverride (LogLevel level, string_view levelName, string_view categoryName, string_view fileName, size_t lineNumber, string_view message)=0
 Implement this function in inherited class to store the message to the log. More...
 

Detailed Description

Inherit from this class to implement logger.

Your logger class should override writeOverride function. Kanzi invokes writeOverride function when the message is ready to be written to the log. Your implementation of writeOverride function is responsible for final processing and storing of the log message.

The macro kzLog must be used to write the log messages using custom logger. If you want to use custom logger to write all the application log messages then register the logger within Default Logger (see kanzi::DefaultLogger).

There are several loggers implemented. See Default loggers.

Examples

To implement logger:

// Use this logger to store log messages in a container for later retrieval.
//
// This class inherits from kanzi::AbstractLogger and implements writeOverride() function. It stores logs in the container.
// The logs could be retrieved with get() function.
class SimpleLogger : public AbstractLogger
{
public:
// Kanzi calls this function to write message to the log.
//
// This function stores the log message in a container. To retreive all log messages use get() function.
// Every log message along with message text itself includes the following information:
// - the log level
// - the log category
// - if an error is reported, the file name and the line number where error occured.
//
// \param level The log level of the message.
// \param levelName The string representation of the log level.
// \param categoryName The string representation of the log category.
// \param fileName The file name where the message originated.
// \param lineNumber The line number where the message originated.
// \param message The message.
virtual void writeOverride(LogLevel level, string_view levelName, string_view categoryName, string_view fileName, size_t lineNumber, string_view message)
{
// Start the log with the level name.
string logMessage(levelName);
// Seperate the log level and the log category with a colon.
logMessage += ':';
// Aadd category name.
logMessage.append(categoryName.data(), categoryName.length());
if (level == LogLevelError)
{
// The error is reported.
// Add the file name and the line number to the message to show the place in a code where the error was reported.
logMessage.append(fileName.data(), fileName.length());
// Seperate the file name and the line number with a dot.
logMessage += '.';
logMessage += to_string(lineNumber);
}
// Finally, add an angle brace and the message text.
logMessage += "> ";
logMessage.append(message.data(), message.length());
// Append the log message to the log vector.
m_log.push_back(logMessage);
}
// Get log vector reference.
vector<string>& getLog()
{
return m_log;
}
private:
// Log vector.
vector<string> m_log;
};

To use logger:

// SimpleLogger inherits from kanzi::AbstractLogger and implements writeOverride.
SimpleLogger simpleLogger;
// Write log message using Simple Logger.
kzLog(simpleLogger, KZ_LOG_LEVEL_INFO, KZ_LOG_CATEGORY_GENERIC, ("Lets log 1 + 2 = {}.", 1 + 2));
// Prints to console: info:generic> Lets log 1 + 2 = 3.
std::cout << simpleLogger.getLog()[0] << std::endl;

Constructor & Destructor Documentation

virtual kanzi::AbstractLogger::~AbstractLogger ( )
inlinevirtual

Destructor.

Member Function Documentation

void kanzi::AbstractLogger::write ( LogLevel  level,
string_view  levelName,
string_view  categoryName,
string_view  fileName,
size_t  lineNumber,
string_view  message 
)
inline

This function is called by Kanzi to store the message to the log.

This function calls writeOverride() function implemented in inherited class to store the log message.

Parameters
levelThe log level of the message.
levelNameThe string representation of the log level.
categoryNameThe string representation of the log category.
fileNameThe file name where the log message is originated.
lineNumberThe line number where the log message is originated.
messageThe log message.
virtual void kanzi::AbstractLogger::writeOverride ( LogLevel  level,
string_view  levelName,
string_view  categoryName,
string_view  fileName,
size_t  lineNumber,
string_view  message 
)
protectedpure virtual

Implement this function in inherited class to store the message to the log.

It is transparent to Kanzi how this function stores the messages to the log.

Parameters
levelThe log level of the message.
levelNameThe string representation of the log level.
categoryNameThe string representation of the log category.
fileNameThe file name where the log message is originated.
lineNumberThe line number where the log message is originated.
messageThe log message.

Implemented in kanzi::detail::StringLogger, kanzi::detail::ChainedLogger, kanzi::CoutLogger, kanzi::Win32DebugLogger, and kanzi::AndroidLogger.


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