PcapPlusPlus  21.05
pcpp::Logger Class Reference

#include <Logger.h>

Public Types

enum  LogLevel { Error, Info, Debug }
 
typedef void(* LogPrinter) (LogLevel logLevel, const std::string &logMessage, const std::string &file, const std::string &method, const int line)
 

Public Member Functions

LogLevel getLogLevel (LogModule module)
 
void setLogLevel (LogModule module, LogLevel level)
 
bool isDebugEnabled (LogModule module) const
 
void setAllModlesToLogLevel (LogLevel level)
 
void setLogPrinter (LogPrinter printer)
 
void resetLogPrinter ()
 
std::string getLastError ()
 
void suppressLogs ()
 
void enableLogs ()
 
bool logsEnabled () const
 
void internalPrintLogMessage (LogLevel logLevel, const std::string &logMessage, const std::string &file, const std::string &method, const int line)
 

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

pcpp::Logger::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

Member Enumeration Documentation

◆ LogLevel

An enum representing the log level. Currently 3 log levels are supported: Error, Info and Debug. Info is the default log level

Enumerator
Error 

Error log level.

Info 

Info log level.

Debug 

Debug log level.

Member Function Documentation

◆ enableLogs()

void pcpp::Logger::enableLogs ( )
inline

Enable logs in all PcapPlusPlus modules

◆ 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 ( )
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

◆ internalPrintLogMessage()

void pcpp::Logger::internalPrintLogMessage ( LogLevel  logLevel,
const std::string &  logMessage,
const std::string &  file,
const std::string &  method,
const int  line 
)
inline

An internal method to print log messages. Shouldn't be used externally.

◆ 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

◆ resetLogPrinter()

void pcpp::Logger::resetLogPrinter ( )
inline

Set the log printer back to the default printer

◆ setAllModlesToLogLevel()

void pcpp::Logger::setAllModlesToLogLevel ( 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

◆ suppressLogs()

void pcpp::Logger::suppressLogs ( )
inline

Suppress logs in all PcapPlusPlus modules