PcapPlusPlus  20.08+
Logger.h
Go to the documentation of this file.
1 #ifndef PCAPPP_LOGGER
2 #define PCAPPP_LOGGER
3 
4 #include <stdio.h>
5 #include <stdint.h>
6 
7 #ifndef LOG_MODULE
8 #define LOG_MODULE UndefinedLogModule
9 #endif
10 
12 
17 namespace pcpp
18 {
19 
23  enum LogModule
24  {
25  UndefinedLogModule,
67  NumOfLogModules
68  };
69 
84  class LoggerPP
85  {
86  public:
90  enum LogLevel
91  {
94  };
95 
101  void setLogLevel(LogModule module, LogLevel level) { m_LogModulesArray[module] = level; }
102 
107  void setAllModlesToLogLevel(LogLevel level) { for (int i=1; i<NumOfLogModules; i++) m_LogModulesArray[i] = level; }
108 
114  bool isDebugEnabled(LogModule module) const { return m_LogModulesArray[module] == Debug; }
115 
123  const LogLevel* getLogModulesArr() const { return m_LogModulesArray; }
124 
129  bool isErrorStringSet() const { return m_ErrorString != NULL; }
130 
135  char* getErrorString() const { return m_ErrorString; }
136 
143  void setErrorString(char* errString, int len) { m_ErrorString = errString; m_ErrorStringLen = len; }
144 
149  int getErrorStringLength() const { return m_ErrorStringLen; }
150 
154  void supressErrors() { m_SuppressErrors = true; }
155 
159  void enableErrors() { m_SuppressErrors = false; }
160 
165  bool isSupressErrors() const { return m_SuppressErrors; }
166 
173  {
174  static LoggerPP instance;
175  return instance;
176  }
177  private:
178  char* m_ErrorString;
179  int m_ErrorStringLen;
180  bool m_SuppressErrors;
181  LoggerPP::LogLevel m_LogModulesArray[NumOfLogModules];
182  LoggerPP();
183  };
184 
185 #define LOG_DEBUG(format, ...) do { \
186  if(pcpp::LoggerPP::getInstance().isDebugEnabled(LOG_MODULE)) { \
187  printf("[%-35s: %-25s: line:%-4d] " format "\n", __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
188  } \
189  } while(0)
190 
191 #define LOG_ERROR(format, ...) do { \
192  if (!pcpp::LoggerPP::getInstance().isSupressErrors()) {\
193  if(pcpp::LoggerPP::getInstance().isErrorStringSet()) \
194  snprintf(pcpp::LoggerPP::getInstance().getErrorString(), pcpp::LoggerPP::getInstance().getErrorStringLength(), format, ## __VA_ARGS__); \
195  else \
196  fprintf(stderr, format "\n", ## __VA_ARGS__); \
197  } \
198  } while (0)
199 
200 #define IS_DEBUG pcpp::LoggerPP::getInstance().isDebugEnabled(LOG_MODULE)
201 
202 } // namespace pcpp
203 
204 #endif /* PCAPPP_LOGGER */
The main namespace for the PcapPlusPlus lib.
IcmpLayer module (Packet++)
Definition: Logger.h:44
PfRingDevice module (Pcap++)
Definition: Logger.h:62
static LoggerPP & getInstance()
Definition: Logger.h:172
LogModule
Definition: Logger.h:23
SdpLayer module (Packet++)
Definition: Logger.h:51
SllLayer module (Packet++)
Definition: Logger.h:47
Packet module (Packet++)
Definition: Logger.h:30
RadiusLayer module (Packet++)
Definition: Logger.h:52
MBufRawPacket module (Pcap++)
Definition: Logger.h:63
bool isSupressErrors() const
Definition: Logger.h:165
const LogLevel * getLogModulesArr() const
Definition: Logger.h:123
PayloadLayer module (Packet++)
Definition: Logger.h:36
void enableErrors()
Definition: Logger.h:159
VlanLayer module (Packet++)
Definition: Logger.h:39
IP Utils module (Common++)
Definition: Logger.h:26
void setLogLevel(LogModule module, LogLevel level)
Definition: Logger.h:101
IPReassembly module (Packet++)
Definition: Logger.h:57
LogLevel
Definition: Logger.h:90
HttpLayer module (Packet++)
Definition: Logger.h:40
bool isErrorStringSet() const
Definition: Logger.h:129
SipLayer module (Packet++)
Definition: Logger.h:50
void setErrorString(char *errString, int len)
Definition: Logger.h:143
IgmpLayer module (Packet++)
Definition: Logger.h:49
Debug log level.
Definition: Logger.h:93
DnsLayer module (Packet++)
Definition: Logger.h:42
RawPacket module (Packet++)
Definition: Logger.h:29
SSHLayer module (Packet++)
Definition: Logger.h:55
PcapLiveDevice module (Pcap++)
Definition: Logger.h:60
GtpLayer module (Packet++)
Definition: Logger.h:53
DpdkDevice module (Pcap++)
Definition: Logger.h:64
Normal log level.
Definition: Logger.h:92
MplsLayer module (Packet++)
Definition: Logger.h:43
IPv4Layer module (Packet++)
Definition: Logger.h:34
FileDevice module (Pcap++)
Definition: Logger.h:61
Generic Utils (Common++)
Definition: Logger.h:28
bool isDebugEnabled(LogModule module) const
Definition: Logger.h:114
NetworkUtils module (Pcap++)
Definition: Logger.h:66
SSLLayer module (Packet++)
Definition: Logger.h:46
TcpLayer module (Packet++)
Definition: Logger.h:37
KniDevice module (Pcap++)
Definition: Logger.h:65
void supressErrors()
Definition: Logger.h:154
GreLayer module (Packet++)
Definition: Logger.h:45
WinPcapLiveDevice module (Pcap++)
Definition: Logger.h:58
WinPcapRemoteDevice module (Pcap++)
Definition: Logger.h:59
void setAllModlesToLogLevel(LogLevel level)
Definition: Logger.h:107
TcpReassembly module (Packet++)
Definition: Logger.h:56
Layer module (Packet++)
Definition: Logger.h:31
ArpLayer module (Packet++)
Definition: Logger.h:32
int getErrorStringLength() const
Definition: Logger.h:149
EthLayer module (Packet++)
Definition: Logger.h:33
UdpLayer module (Packet++)
Definition: Logger.h:38
GtpLayer module (Packet++)
Definition: Logger.h:54
PPPoELayer module (Packet++)
Definition: Logger.h:41
Definition: Logger.h:84
DhcpLayer module (Packet++)
Definition: Logger.h:48
IPv6Layer module (Packet++)
Definition: Logger.h:35
char * getErrorString() const
Definition: Logger.h:135
Table printer module (Common++)
Definition: Logger.h:27