PcapPlusPlus  Next
pcpp::Logger Class Reference

#include <Logger.h>

Public Types

using LogPrinter = std::add_pointer< void(LogLevel logLevel, const std::string &logMessage, const std::string &file, const std::string &method, const int line)>::type
 

Public Member Functions

LogLevel getLogLevel (LogModule module)
 
void setLogLevel (LogModule module, LogLevel level)
 
bool isDebugEnabled (LogModule module) const
 
bool shouldLog (LogLevel level, LogModule module) const
 Check whether a log level should be emitted by the logger. More...
 
void setAllModulesToLogLevel (LogLevel level)
 
void setLogPrinter (LogPrinter printer)
 
void resetLogPrinter ()
 Set the log printer back to the default printer.
 
std::string getLastError () const
 
void suppressLogs ()
 Suppress logs in all PcapPlusPlus modules.
 
void enableLogs ()
 Enable logs in all PcapPlusPlus modules.
 
bool logsEnabled () const
 
void useContextPooling (bool enabled, std::size_t preallocate=2, std::size_t maxPoolSize=10)
 Controls if the logger should use a pool of LogContext objects. More...
 
std::unique_ptr< internal::LogContextcreateLogContext ()
 Creates a new LogContext with Info level and no source. More...
 
std::unique_ptr< internal::LogContextcreateLogContext (LogLevel level, LogSource const &source={})
 Creates a new LogContext with the given level and source. More...
 
void emit (LogSource const &source, LogLevel level, std::string const &message)
 Directly emits a log message bypassing all level checks. More...
 
void emit (std::unique_ptr< internal::LogContext > message)
 Directly emits a log message bypassing all level checks. More...
 

Static Public Member Functions

static std::string logLevelAsString (LogLevel logLevel)
 
static LoggergetInstance ()
 

Detailed Description

PcapPlusPlus logger manager. PcapPlusPlus uses this logger to output both error and debug logs. There are currently 3 log levels: Logger::Error, Logger::Info and Logger::Debug.

PcapPlusPlus is divided into modules (described in LogModule enum). The user can set the log level got each module or to all modules at once. The default is Logger::Info which outputs only error messages. Changing log level for modules can be done dynamically while the application is running.

The logger also exposes a method to retrieve the last error log message.

Logs are printed to console by default in a certain format. The user can set a different print function to change the format or to print to other media (such as files, etc.).

PcapPlusPlus logger is a singleton which can be reached from anywhere in the code.

Note: Logger::Info level logs are currently only used in DPDK devices to set DPDK log level to RTE_LOG_NOTICE.

Member Typedef Documentation

◆ LogPrinter

Log printer callback. Used for printing the logs in a custom way.

Parameters
[in]logLevelThe log level for this log message
[in]logMessageThe log message
[in]fileThe source file in PcapPlusPlus code the log message is coming from
[in]methodThe method in PcapPlusPlus code the log message is coming from
[in]lineThe line in PcapPlusPlus code the log message is coming from
Remarks
The printer callback should support being called from multiple threads simultaneously.

Member Function Documentation

◆ createLogContext() [1/2]

std::unique_ptr<internal::LogContext> pcpp::Logger::createLogContext ( )

Creates a new LogContext with Info level and no source.

Returns
A new LogContext.

◆ createLogContext() [2/2]

std::unique_ptr<internal::LogContext> pcpp::Logger::createLogContext ( LogLevel  level,
LogSource const &  source = {} 
)

Creates a new LogContext with the given level and source.

Parameters
levelThe log level for this message.
sourceThe log source.
Returns
A new LogContext.

◆ emit() [1/2]

void pcpp::Logger::emit ( LogSource const &  source,
LogLevel  level,
std::string const &  message 
)

Directly emits a log message bypassing all level checks.

Parameters
sourceThe log source.
levelThe log level for this message. This is only used for the log printer.
messageThe log message.

◆ emit() [2/2]

void pcpp::Logger::emit ( std::unique_ptr< internal::LogContext message)

Directly emits a log message bypassing all level checks.

Parameters
messageThe log message.

◆ getInstance()

static Logger& pcpp::Logger::getInstance ( )
inlinestatic

Get access to Logger singleton

Returns
a pointer to the Logger singleton

◆ getLastError()

std::string pcpp::Logger::getLastError ( ) const
inline
Returns
Get the last error message

◆ getLogLevel()

LogLevel pcpp::Logger::getLogLevel ( LogModule  module)
inline

Get the log level for a certain module

Parameters
[in]modulePcapPlusPlus module
Returns
The log level set for this module

◆ isDebugEnabled()

bool pcpp::Logger::isDebugEnabled ( LogModule  module) const
inline

Check whether a certain module is set to debug log level

Parameters
[in]modulePcapPlusPlus module
Returns
True if this module log level is "debug". False otherwise

◆ logLevelAsString()

static std::string pcpp::Logger::logLevelAsString ( LogLevel  logLevel)
static

A static method for converting the log level enum to a string.

Parameters
[in]logLevelA log level enum
Returns
The log level as a string

◆ logsEnabled()

bool pcpp::Logger::logsEnabled ( ) const
inline

Get an indication if logs are currently enabled.

Returns
True if logs are currently enabled, false otherwise

◆ setAllModulesToLogLevel()

void pcpp::Logger::setAllModulesToLogLevel ( LogLevel  level)
inline

Set all PcapPlusPlus modules to a certain log level

Parameters
[in]levelThe log level to set all modules to

◆ setLogLevel()

void pcpp::Logger::setLogLevel ( LogModule  module,
LogLevel  level 
)
inline

Set the log level for a certain PcapPlusPlus module

Parameters
[in]modulePcapPlusPlus module
[in]levelThe log level to set the module to

◆ setLogPrinter()

void pcpp::Logger::setLogPrinter ( LogPrinter  printer)
inline

Set a custom log printer.

Parameters
[in]printerA log printer function that will be called for every log message

◆ shouldLog()

bool pcpp::Logger::shouldLog ( LogLevel  level,
LogModule  module 
) const
inline

Check whether a log level should be emitted by the logger.

Parameters
levelThe level of the log message.
modulePcapPlusPlus module
Returns
True if the message should be emitted. False otherwise.

◆ useContextPooling()

void pcpp::Logger::useContextPooling ( bool  enabled,
std::size_t  preallocate = 2,
std::size_t  maxPoolSize = 10 
)
inline

Controls if the logger should use a pool of LogContext objects.

If enabled is set to false, preallocate and maxPoolSize are ignored.

Parameters
enabledTrue to enable context pooling, false to disable.
preallocateThe number of LogContext objects to preallocate in the pool.
maxPoolSizeThe maximum number of LogContext objects to keep in the pool.
Remarks
Disabling the pooling clears the pool.