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::Win32DebugLogger

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

Inherit from this class to implement logger.

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 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

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::CoutLogger, kanzi::Win32DebugLogger, and kanzi::AndroidLogger.


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