PcapPlusPlus  Next
Logger.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdio.h>
4 #include <iostream>
5 #include <sstream>
6 #include <iomanip>
7 #include <stdint.h>
8 
9 #ifndef LOG_MODULE
10 # define LOG_MODULE UndefinedLogModule
11 #endif
12 
13 // Use __FILE_NAME__ to avoid leaking complete full path
14 #ifdef __FILE_NAME__
15 # define PCAPPP_FILENAME __FILE_NAME__
16 #else
17 # define PCAPPP_FILENAME __FILE__
18 #endif
19 
20 #define PCPP_LOG(level, message) \
21  do \
22  { \
23  std::ostringstream* sstream = pcpp::Logger::getInstance().internalCreateLogStream(); \
24  (*sstream) << message; \
25  pcpp::Logger::getInstance().internalPrintLogMessage(sstream, level, PCAPPP_FILENAME, __FUNCTION__, __LINE__); \
26  } while (0)
27 
28 #define PCPP_LOG_DEBUG(message) \
29  do \
30  { \
31  if (pcpp::Logger::getInstance().logsEnabled() && pcpp::Logger::getInstance().isDebugEnabled(LOG_MODULE)) \
32  { \
33  PCPP_LOG(pcpp::Logger::Debug, message); \
34  } \
35  } while (0)
36 
37 #define PCPP_LOG_ERROR(message) \
38  do \
39  { \
40  PCPP_LOG(pcpp::Logger::Error, message); \
41  } while (0)
42 
44 
47 namespace pcpp
48 {
49 
51  enum LogModule
52  {
53  UndefinedLogModule,
113  NumOfLogModules
114  };
115 
133  class Logger
134  {
135  public:
138  enum LogLevel
139  {
142  Debug
143  };
144 
152  typedef void (*LogPrinter)(LogLevel logLevel, const std::string& logMessage, const std::string& file,
153  const std::string& method, const int line);
154 
158  static std::string logLevelAsString(LogLevel logLevel);
159 
164  {
165  return m_LogModulesArray[module];
166  }
167 
171  void setLogLevel(LogModule module, LogLevel level)
172  {
173  m_LogModulesArray[module] = level;
174  }
175 
179  bool isDebugEnabled(LogModule module) const
180  {
181  return m_LogModulesArray[module] == Debug;
182  }
183 
187  {
188  for (int i = 1; i < NumOfLogModules; i++)
189  m_LogModulesArray[i] = level;
190  }
191 
194  void setLogPrinter(LogPrinter printer)
195  {
196  m_LogPrinter = printer;
197  }
198 
201  {
202  m_LogPrinter = &defaultLogPrinter;
203  }
204 
206  std::string getLastError()
207  {
208  return m_LastError;
209  }
210 
213  {
214  m_LogsEnabled = false;
215  }
216 
218  void enableLogs()
219  {
220  m_LogsEnabled = true;
221  }
222 
225  bool logsEnabled() const
226  {
227  return m_LogsEnabled;
228  }
229 
230  template <class T> Logger& operator<<(const T& msg)
231  {
232  (*m_LogStream) << msg;
233  return *this;
234  }
235 
236  std::ostringstream* internalCreateLogStream();
237 
239  void internalPrintLogMessage(std::ostringstream* logStream, Logger::LogLevel logLevel, const char* file,
240  const char* method, int line);
241 
245  static Logger& getInstance()
246  {
247  static Logger instance;
248  return instance;
249  }
250 
251  private:
252  bool m_LogsEnabled;
253  Logger::LogLevel m_LogModulesArray[NumOfLogModules];
254  LogPrinter m_LogPrinter;
255  std::string m_LastError;
256  std::ostringstream* m_LogStream;
257 
258  // private c'tor - this class is a singleton
259  Logger();
260 
261  static void defaultLogPrinter(LogLevel logLevel, const std::string& logMessage, const std::string& file,
262  const std::string& method, const int line);
263  };
264 } // namespace pcpp
Definition: Logger.h:134
void setLogPrinter(LogPrinter printer)
Definition: Logger.h:194
void enableLogs()
Enable logs in all PcapPlusPlus modules.
Definition: Logger.h:218
LogLevel
Definition: Logger.h:139
@ Debug
Debug log level.
Definition: Logger.h:142
@ Info
Info log level.
Definition: Logger.h:141
@ Error
Error log level.
Definition: Logger.h:140
void setLogLevel(LogModule module, LogLevel level)
Definition: Logger.h:171
LogLevel getLogLevel(LogModule module)
Definition: Logger.h:163
bool logsEnabled() const
Definition: Logger.h:225
void resetLogPrinter()
Set the log printer back to the default printer.
Definition: Logger.h:200
static Logger & getInstance()
Definition: Logger.h:245
void internalPrintLogMessage(std::ostringstream *logStream, Logger::LogLevel logLevel, const char *file, const char *method, int line)
An internal method to print log messages. Shouldn't be used externally.
void suppressLogs()
Suppress logs in all PcapPlusPlus modules.
Definition: Logger.h:212
static std::string logLevelAsString(LogLevel logLevel)
void(* LogPrinter)(LogLevel logLevel, const std::string &logMessage, const std::string &file, const std::string &method, const int line)
Definition: Logger.h:152
std::string getLastError()
Definition: Logger.h:206
bool isDebugEnabled(LogModule module) const
Definition: Logger.h:179
void setAllModulesToLogLevel(LogLevel level)
Definition: Logger.h:186
The main namespace for the PcapPlusPlus lib.
LogModule
An enum representing all PcapPlusPlus modules.
Definition: Logger.h:52
@ PacketLogModuleRawPacket
RawPacket module (Packet++)
Definition: Logger.h:57
@ PacketLogModulePayloadLayer
PayloadLayer module (Packet++)
Definition: Logger.h:65
@ PcapLogModuleXdpDevice
XdpDevice module (Pcap++)
Definition: Logger.h:111
@ PacketLogModuleUdpLayer
UdpLayer module (Packet++)
Definition: Logger.h:67
@ PacketLogModuleTelnetLayer
TelnetLayer module (Packet++)
Definition: Logger.h:93
@ CommonLogModuleTablePrinter
Table printer module (Common++)
Definition: Logger.h:55
@ PacketLogModuleFtpLayer
FtpLayer module (Packet++)
Definition: Logger.h:97
@ PacketLogModuleGreLayer
GreLayer module (Packet++)
Definition: Logger.h:75
@ PacketLogModuleLLCLayer
LLCLayer module (Packet++)
Definition: Logger.h:95
@ PacketLogModuleVlanLayer
VlanLayer module (Packet++)
Definition: Logger.h:68
@ PcapLogModuleKniDevice
KniDevice module (Pcap++)
Definition: Logger.h:110
@ PacketLogModuleSomeIpLayer
SomeIpLayer module (Packet++)
Definition: Logger.h:98
@ PacketLogModuleEthLayer
EthLayer module (Packet++)
Definition: Logger.h:62
@ PacketLogModuleSdpLayer
SdpLayer module (Packet++)
Definition: Logger.h:83
@ PacketLogModuleIPv4Layer
IPv4Layer module (Packet++)
Definition: Logger.h:63
@ PacketLogModuleArpLayer
ArpLayer module (Packet++)
Definition: Logger.h:61
@ PacketLogModuleRadiusLayer
RadiusLayer module (Packet++)
Definition: Logger.h:84
@ PacketLogModuleDhcpV6Layer
DhcpV6Layer module (Packet++)
Definition: Logger.h:80
@ PacketLogModuleSipLayer
SipLayer module (Packet++)
Definition: Logger.h:82
@ PacketLogModuleNflogLayer
NflogLayer module (Packet++)
Definition: Logger.h:78
@ PcapLogModuleRemoteDevice
WinPcapRemoteDevice module (Pcap++)
Definition: Logger.h:104
@ PacketLogModuleNdpLayer
NdpLayer module (Packet++)
Definition: Logger.h:96
@ CommonLogModuleIpUtils
IP Utils module (Common++)
Definition: Logger.h:54
@ PacketLogModuleIcmpLayer
IcmpLayer module (Packet++)
Definition: Logger.h:73
@ PacketLogModuleNtpLayer
NtpLayer module (Packet++)
Definition: Logger.h:92
@ PacketLogModuleVrrpLayer
Vrrp Record module (Packet++)
Definition: Logger.h:88
@ PcapLogModuleFileDevice
FileDevice module (Pcap++)
Definition: Logger.h:106
@ PacketLogModuleGtpLayer
GtpLayer module (Packet++)
Definition: Logger.h:85
@ PacketLogModuleSSHLayer
SSHLayer module (Packet++)
Definition: Logger.h:87
@ PacketLogModuleDhcpLayer
DhcpLayer module (Packet++)
Definition: Logger.h:79
@ PcapLogModuleDpdkDevice
DpdkDevice module (Pcap++)
Definition: Logger.h:109
@ PacketLogModuleWakeOnLanLayer
WakeOnLanLayer module (Packet++)
Definition: Logger.h:100
@ PacketLogModulePPPoELayer
PPPoELayer module (Packet++)
Definition: Logger.h:70
@ PacketLogModuleIPReassembly
IPReassembly module (Packet++)
Definition: Logger.h:90
@ NetworkUtils
NetworkUtils module (Pcap++)
Definition: Logger.h:112
@ PacketLogModuleTcpReassembly
TcpReassembly module (Packet++)
Definition: Logger.h:89
@ PacketLogModuleHttpLayer
HttpLayer module (Packet++)
Definition: Logger.h:69
@ PacketLogModuleWireGuardLayer
WireGuardLayer module (Packet++)
Definition: Logger.h:102
@ PacketLogModuleBgpLayer
GtpLayer module (Packet++)
Definition: Logger.h:86
@ PacketLogModuleSllLayer
SllLayer module (Packet++)
Definition: Logger.h:77
@ CommonLogModuleGenericUtils
Generic Utils (Common++)
Definition: Logger.h:56
@ PacketLogModuleIgmpLayer
IgmpLayer module (Packet++)
Definition: Logger.h:81
@ PacketLogModuleStpLayer
StpLayer module (Packet++)
Definition: Logger.h:94
@ PcapLogModuleWinPcapLiveDevice
WinPcapLiveDevice module (Pcap++)
Definition: Logger.h:103
@ PacketLogModuleIPv6Layer
IPv6Layer module (Packet++)
Definition: Logger.h:64
@ PacketLogModuleMplsLayer
MplsLayer module (Packet++)
Definition: Logger.h:72
@ PacketLogModuleAsn1Codec
Asn1Codec module (Packet++)
Definition: Logger.h:60
@ PacketLogModuleDnsLayer
DnsLayer module (Packet++)
Definition: Logger.h:71
@ PcapLogModulePfRingDevice
PfRingDevice module (Pcap++)
Definition: Logger.h:107
@ PacketLogModuleSSLLayer
SSLLayer module (Packet++)
Definition: Logger.h:76
@ PacketLogModulePacket
Packet module (Packet++)
Definition: Logger.h:58
@ PacketLogModuleSomeIpSdLayer
SomeIpSdLayer module (Packet++)
Definition: Logger.h:99
@ PcapLogModuleMBufRawPacket
MBufRawPacket module (Pcap++)
Definition: Logger.h:108
@ PacketLogModuleLayer
Layer module (Packet++)
Definition: Logger.h:59
@ PacketLogModuleTcpLayer
TcpLayer module (Packet++)
Definition: Logger.h:66
@ PacketLogModuleIcmpV6Layer
IcmpV6Layer module (Packet++)
Definition: Logger.h:74
@ PcapLogModuleLiveDevice
PcapLiveDevice module (Pcap++)
Definition: Logger.h:105
@ PacketLogModuleSmtpLayer
SmtpLayer module (Packet++)
Definition: Logger.h:101
@ PacketLogModuleIPSecLayer
IPSecLayers module (Packet++)
Definition: Logger.h:91