Kanzi  3.9.9
Kanzi Engine C++ API
kanzi::AbstractLogger Class Referenceabstract

To implement a logger, inherit from this class. More...

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

Inheritance diagram for kanzi::AbstractLogger:
[legend]

Public Member Functions

void write (LogLevel level, string_view levelName, string_view categoryName, string_view fileName, size_t lineNumber, string_view message)
 Kanzi calls this function 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
 To store the message to the log, implement this function in the inherited class. More...
 

Detailed Description

To implement a logger, inherit from this class.

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

To write log messages using your custom logger, use the kzLog macro. If you want to use a custom logger to write all the application log messages, register the logger in kanzi::DefaultLogger.

The logging system comes with several loggers. See Default loggers.

Examples

To implement a logger:

// Use this logger to store log messages in a container for later retrieval.
//
// This class inherits from kanzi::AbstractLogger and implements the writeOverride() function. It stores logs in the container.
// To retrieve the logs, use the get() function.
class SimpleLogger : public AbstractLogger
{
public:
// Kanzi calls this function to write messages to the log.
//
// This function stores the log message in a container. To retrieve all log messages, use the get() function.
// Each log message includes:
// - The message text
// - The log level
// - The log category
// - If the message is an error, the file name and the line number where the 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 name of the file in which the message originated.
// \param lineNumber The number of the line on which the message originated.
// \param message The message.
void writeOverride(LogLevel level, string_view levelName, string_view categoryName,
string_view fileName, size_t lineNumber, string_view message) override
{
// Start the log with the level name.
string logMessage(levelName);
// Separate the log level and the log category with a colon.
logMessage += ':';
// Add the category name.
logMessage.append(categoryName.data(), categoryName.length());
if (level == LogLevelError)
{
// The error is reported.
// To show where in the code the error was reported, add the file name and the line number to the message.
logMessage.append(fileName.data(), fileName.length());
// Separate the file name and the line number with a colon.
logMessage += ':';
logMessage += to_string(lineNumber);
}
// Add an angle bracket 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 a logger explicitly:

// SimpleLogger inherits from kanzi::AbstractLogger and implements writeOverride.
SimpleLogger simpleLogger;
// Write a log message using SimpleLogger.
kzLog(simpleLogger, KZ_LOG_LEVEL_INFO, KZ_LOG_CATEGORY_GENERIC, ("Lets log 1 + 2 = {}.", 1 + 2));

To use a logger in logger chain:

// SimpleLogger inherits from kanzi::AbstractLogger and implements writeOverride.
AbstractLoggerUniquePtr simpleLogger(new SimpleLogger());
// Before adding SimpleLogger to the logger chain, get the pointer to it.
// After you add SimpleLogger to the logger chain, the logger chain controls the lifetime of SimpleLogger.
SimpleLogger* simpleLoggerPtr = static_cast<SimpleLogger*>(simpleLogger.get());
// Push SimpleLogger to the logger chain. Now both SimpleLogger and the rest of the loggers handle every logged message.
DefaultLogger::pushLogger(kanzi::move(simpleLogger));
// Write a log message. SimpleLogger handles this message.
kzLogInfo(KZ_LOG_CATEGORY_GENERIC, ("Lets log 1 + 2 = {}.", 1 + 2));

To exclusively redirect log messages to a logger:

// SimpleLogger inherits from kanzi::AbstractLogger and implements writeOverride.
AbstractLoggerUniquePtr simpleLogger(new SimpleLogger());
// Before adding SimpleLogger to the logger chain, get the pointer to it.
// After you add SimpleLogger to the logger chain, the logger chain controls the lifetime of SimpleLogger.
SimpleLogger* simpleLoggerPtr = static_cast<SimpleLogger*>(simpleLogger.get());
// Remove all default loggers from the logger chain. This way you make only SimpleLogger, which you push to the chain later, handle the logged messages.
// Push SimpleLogger to the logger chain. Now only SimpleLogger handles every logged message.
DefaultLogger::pushLogger(kanzi::move(simpleLogger));
// Write a log message. SimpleLogger handles this message.
kzLogInfo(KZ_LOG_CATEGORY_GENERIC, ("Lets log 1 + 2 = {}.", 1 + 2));

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

Kanzi calls this function to store the message to the log.

To store the log message, this function calls the writeOverride() function implemented in the inherited class.

Parameters
levelThe log level of the message.
levelNameThe string representation of the log level.
categoryNameThe string representation of the log category.
fileNameThe name of the file from which the log message originated.
lineNumberThe number of the line in the file from which the log message 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

To store the message to the log, implement this function in the inherited class.

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 name of the file from which the log message originated.
lineNumberThe number of the line in the file from which the log message originated.
messageThe log message.

Implemented in kanzi::QnxLogger, kanzi::CoutLogger, kanzi::Win32DebugLogger, and kanzi::AndroidLogger.


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