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 
49 namespace pcpp
50 {
51 
55  enum LogModule
56  {
57  UndefinedLogModule,
117  NumOfLogModules
118  };
119 
139  class Logger
140  {
141  public:
146  enum LogLevel
147  {
150  Debug
151  };
152 
162  typedef void (*LogPrinter)(LogLevel logLevel, const std::string& logMessage, const std::string& file,
163  const std::string& method, const int line);
164 
170  static std::string logLevelAsString(LogLevel logLevel);
171 
178  {
179  return m_LogModulesArray[module];
180  }
181 
187  void setLogLevel(LogModule module, LogLevel level)
188  {
189  m_LogModulesArray[module] = level;
190  }
191 
197  bool isDebugEnabled(LogModule module) const
198  {
199  return m_LogModulesArray[module] == Debug;
200  }
201 
207  {
208  for (int i = 1; i < NumOfLogModules; i++)
209  m_LogModulesArray[i] = level;
210  }
211 
216  void setLogPrinter(LogPrinter printer)
217  {
218  m_LogPrinter = printer;
219  }
220 
225  {
226  m_LogPrinter = &defaultLogPrinter;
227  }
228 
232  std::string getLastError()
233  {
234  return m_LastError;
235  }
236 
241  {
242  m_LogsEnabled = false;
243  }
244 
248  void enableLogs()
249  {
250  m_LogsEnabled = true;
251  }
252 
257  bool logsEnabled() const
258  {
259  return m_LogsEnabled;
260  }
261 
262  template <class T> Logger& operator<<(const T& msg)
263  {
264  (*m_LogStream) << msg;
265  return *this;
266  }
267 
268  std::ostringstream* internalCreateLogStream();
269 
273  void internalPrintLogMessage(std::ostringstream* logStream, Logger::LogLevel logLevel, const char* file,
274  const char* method, int line);
275 
281  static Logger& getInstance()
282  {
283  static Logger instance;
284  return instance;
285  }
286 
287  private:
288  bool m_LogsEnabled;
289  Logger::LogLevel m_LogModulesArray[NumOfLogModules];
290  LogPrinter m_LogPrinter;
291  std::string m_LastError;
292  std::ostringstream* m_LogStream;
293 
294  // private c'tor - this class is a singleton
295  Logger();
296 
297  static void defaultLogPrinter(LogLevel logLevel, const std::string& logMessage, const std::string& file,
298  const std::string& method, const int line);
299  };
300 } // namespace pcpp
Definition: Logger.h:140
void setLogPrinter(LogPrinter printer)
Definition: Logger.h:216
void enableLogs()
Definition: Logger.h:248
LogLevel
Definition: Logger.h:147
@ Debug
Debug log level.
Definition: Logger.h:150
@ Info
Info log level.
Definition: Logger.h:149
@ Error
Error log level.
Definition: Logger.h:148
void setLogLevel(LogModule module, LogLevel level)
Definition: Logger.h:187
LogLevel getLogLevel(LogModule module)
Definition: Logger.h:177
bool logsEnabled() const
Definition: Logger.h:257
void resetLogPrinter()
Definition: Logger.h:224
static Logger & getInstance()
Definition: Logger.h:281
void internalPrintLogMessage(std::ostringstream *logStream, Logger::LogLevel logLevel, const char *file, const char *method, int line)
void suppressLogs()
Definition: Logger.h:240
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:162
std::string getLastError()
Definition: Logger.h:232
bool isDebugEnabled(LogModule module) const
Definition: Logger.h:197
void setAllModulesToLogLevel(LogLevel level)
Definition: Logger.h:206
The main namespace for the PcapPlusPlus lib.
LogModule
Definition: Logger.h:56
@ PacketLogModuleRawPacket
RawPacket module (Packet++)
Definition: Logger.h:61
@ PacketLogModulePayloadLayer
PayloadLayer module (Packet++)
Definition: Logger.h:69
@ PcapLogModuleXdpDevice
XdpDevice module (Pcap++)
Definition: Logger.h:115
@ PacketLogModuleUdpLayer
UdpLayer module (Packet++)
Definition: Logger.h:71
@ PacketLogModuleTelnetLayer
TelnetLayer module (Packet++)
Definition: Logger.h:97
@ CommonLogModuleTablePrinter
Table printer module (Common++)
Definition: Logger.h:59
@ PacketLogModuleFtpLayer
FtpLayer module (Packet++)
Definition: Logger.h:101
@ PacketLogModuleGreLayer
GreLayer module (Packet++)
Definition: Logger.h:79
@ PacketLogModuleLLCLayer
LLCLayer module (Packet++)
Definition: Logger.h:99
@ PacketLogModuleVlanLayer
VlanLayer module (Packet++)
Definition: Logger.h:72
@ PcapLogModuleKniDevice
KniDevice module (Pcap++)
Definition: Logger.h:114
@ PacketLogModuleSomeIpLayer
SomeIpLayer module (Packet++)
Definition: Logger.h:102
@ PacketLogModuleEthLayer
EthLayer module (Packet++)
Definition: Logger.h:66
@ PacketLogModuleSdpLayer
SdpLayer module (Packet++)
Definition: Logger.h:87
@ PacketLogModuleIPv4Layer
IPv4Layer module (Packet++)
Definition: Logger.h:67
@ PacketLogModuleArpLayer
ArpLayer module (Packet++)
Definition: Logger.h:65
@ PacketLogModuleRadiusLayer
RadiusLayer module (Packet++)
Definition: Logger.h:88
@ PacketLogModuleDhcpV6Layer
DhcpV6Layer module (Packet++)
Definition: Logger.h:84
@ PacketLogModuleSipLayer
SipLayer module (Packet++)
Definition: Logger.h:86
@ PacketLogModuleNflogLayer
NflogLayer module (Packet++)
Definition: Logger.h:82
@ PcapLogModuleRemoteDevice
WinPcapRemoteDevice module (Pcap++)
Definition: Logger.h:108
@ PacketLogModuleNdpLayer
NdpLayer module (Packet++)
Definition: Logger.h:100
@ CommonLogModuleIpUtils
IP Utils module (Common++)
Definition: Logger.h:58
@ PacketLogModuleIcmpLayer
IcmpLayer module (Packet++)
Definition: Logger.h:77
@ PacketLogModuleNtpLayer
NtpLayer module (Packet++)
Definition: Logger.h:96
@ PacketLogModuleVrrpLayer
Vrrp Record module (Packet++)
Definition: Logger.h:92
@ PcapLogModuleFileDevice
FileDevice module (Pcap++)
Definition: Logger.h:110
@ PacketLogModuleGtpLayer
GtpLayer module (Packet++)
Definition: Logger.h:89
@ PacketLogModuleSSHLayer
SSHLayer module (Packet++)
Definition: Logger.h:91
@ PacketLogModuleDhcpLayer
DhcpLayer module (Packet++)
Definition: Logger.h:83
@ PcapLogModuleDpdkDevice
DpdkDevice module (Pcap++)
Definition: Logger.h:113
@ PacketLogModuleWakeOnLanLayer
WakeOnLanLayer module (Packet++)
Definition: Logger.h:104
@ PacketLogModulePPPoELayer
PPPoELayer module (Packet++)
Definition: Logger.h:74
@ PacketLogModuleIPReassembly
IPReassembly module (Packet++)
Definition: Logger.h:94
@ NetworkUtils
NetworkUtils module (Pcap++)
Definition: Logger.h:116
@ PacketLogModuleTcpReassembly
TcpReassembly module (Packet++)
Definition: Logger.h:93
@ PacketLogModuleHttpLayer
HttpLayer module (Packet++)
Definition: Logger.h:73
@ PacketLogModuleWireGuardLayer
WireGuardLayer module (Packet++)
Definition: Logger.h:106
@ PacketLogModuleBgpLayer
GtpLayer module (Packet++)
Definition: Logger.h:90
@ PacketLogModuleSllLayer
SllLayer module (Packet++)
Definition: Logger.h:81
@ CommonLogModuleGenericUtils
Generic Utils (Common++)
Definition: Logger.h:60
@ PacketLogModuleIgmpLayer
IgmpLayer module (Packet++)
Definition: Logger.h:85
@ PacketLogModuleStpLayer
StpLayer module (Packet++)
Definition: Logger.h:98
@ PcapLogModuleWinPcapLiveDevice
WinPcapLiveDevice module (Pcap++)
Definition: Logger.h:107
@ PacketLogModuleIPv6Layer
IPv6Layer module (Packet++)
Definition: Logger.h:68
@ PacketLogModuleMplsLayer
MplsLayer module (Packet++)
Definition: Logger.h:76
@ PacketLogModuleAsn1Codec
Asn1Codec module (Packet++)
Definition: Logger.h:64
@ PacketLogModuleDnsLayer
DnsLayer module (Packet++)
Definition: Logger.h:75
@ PcapLogModulePfRingDevice
PfRingDevice module (Pcap++)
Definition: Logger.h:111
@ PacketLogModuleSSLLayer
SSLLayer module (Packet++)
Definition: Logger.h:80
@ PacketLogModulePacket
Packet module (Packet++)
Definition: Logger.h:62
@ PacketLogModuleSomeIpSdLayer
SomeIpSdLayer module (Packet++)
Definition: Logger.h:103
@ PcapLogModuleMBufRawPacket
MBufRawPacket module (Pcap++)
Definition: Logger.h:112
@ PacketLogModuleLayer
Layer module (Packet++)
Definition: Logger.h:63
@ PacketLogModuleTcpLayer
TcpLayer module (Packet++)
Definition: Logger.h:70
@ PacketLogModuleIcmpV6Layer
IcmpV6Layer module (Packet++)
Definition: Logger.h:78
@ PcapLogModuleLiveDevice
PcapLiveDevice module (Pcap++)
Definition: Logger.h:109
@ PacketLogModuleSmtpLayer
SmtpLayer module (Packet++)
Definition: Logger.h:105
@ PacketLogModuleIPSecLayer
IPSecLayers module (Packet++)
Definition: Logger.h:95