PcapPlusPlus
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,
60  NumOfLogModules
61  };
62 
77  class LoggerPP
78  {
79  public:
83  enum LogLevel
84  {
87  };
88 
94  void setLogLevel(LogModule module, LogLevel level) { m_LogModulesArray[module] = level; }
95 
100  void setAllModlesToLogLevel(LogLevel level) { for (int i=1; i<NumOfLogModules; i++) m_LogModulesArray[i] = level; }
101 
107  inline bool isDebugEnabled(LogModule module) { return m_LogModulesArray[module] == Debug; }
108 
116  inline LogLevel* getLogModulesArr() { return m_LogModulesArray; }
117 
122  inline bool isErrorStringSet() { return m_ErrorString != NULL; }
123 
128  inline char* getErrorString() { return m_ErrorString; }
129 
136  void setErrorString(char* errString, int len) { m_ErrorString = errString; m_ErrorStringLen = len; }
137 
142  inline int getErrorStringLength() { return m_ErrorStringLen; }
143 
147  void supressErrors() { m_SuppressErrors = true; }
148 
152  void enableErrors() { m_SuppressErrors = false; }
153 
158  inline bool isSupressErrors() { return m_SuppressErrors; }
159 
165  static inline LoggerPP& getInstance()
166  {
167  static LoggerPP instance;
168  return instance;
169  }
170  private:
171  char* m_ErrorString;
172  int m_ErrorStringLen;
173  bool m_SuppressErrors;
174  LoggerPP::LogLevel m_LogModulesArray[NumOfLogModules];
175  LoggerPP();
176  };
177 
178 #define LOG_DEBUG(format, ...) do { \
179  if(pcpp::LoggerPP::getInstance().isDebugEnabled(LOG_MODULE)) { \
180  printf("[%-35s: %-25s: line:%-4d] " format "\n", __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
181  } \
182  } while(0)
183 
184 #define LOG_ERROR(format, ...) do { \
185  if (!pcpp::LoggerPP::getInstance().isSupressErrors()) {\
186  if(pcpp::LoggerPP::getInstance().isErrorStringSet()) \
187  snprintf(pcpp::LoggerPP::getInstance().getErrorString(), pcpp::LoggerPP::getInstance().getErrorStringLength(), format "\n", ## __VA_ARGS__); \
188  else \
189  fprintf(stderr, format "\n", ## __VA_ARGS__); \
190  } \
191  } while (0)
192 
193 #define IS_DEBUG pcpp::LoggerPP::getInstance().isDebugEnabled(LOG_MODULE)
194 
195 } // namespace pcpp
196 
197 #endif /* PCAPPP_LOGGER */
WinPcapRemoteDevice module (Pcap++)
Definition: Logger.h:54
PayloadLayer module (Packet++)
Definition: Logger.h:35
bool isErrorStringSet()
Definition: Logger.h:122
DhcpLayer module (Packet++)
Definition: Logger.h:47
SllLayer module (Packet++)
Definition: Logger.h:46
void setErrorString(char *errString, int len)
Definition: Logger.h:136
SSLLayer module (Packet++)
Definition: Logger.h:45
Layer module (Packet++)
Definition: Logger.h:30
PcapLiveDevice module (Pcap++)
Definition: Logger.h:55
IP Utils module (Common++)
Definition: Logger.h:26
IcmpLayer module (Packet++)
Definition: Logger.h:43
IgmpLayer module (Packet++)
Definition: Logger.h:48
PfRingDevice module (Pcap++)
Definition: Logger.h:57
The main namespace for the PcapPlusPlus lib.
void setLogLevel(LogModule module, LogLevel level)
Definition: Logger.h:94
DnsLayer module (Packet++)
Definition: Logger.h:41
PPPoELayer module (Packet++)
Definition: Logger.h:40
bool isSupressErrors()
Definition: Logger.h:158
TcpReassembly module (Packet++)
Definition: Logger.h:51
LogModule
Definition: Logger.h:23
Table printer module (Common++)
Definition: Logger.h:27
IPReassembly module (Packet++)
Definition: Logger.h:52
Debug log level.
Definition: Logger.h:86
void setAllModlesToLogLevel(LogLevel level)
Definition: Logger.h:100
TcpLayer module (Packet++)
Definition: Logger.h:36
VlanLayer module (Packet++)
Definition: Logger.h:38
Normal log level.
Definition: Logger.h:85
MplsLayer module (Packet++)
Definition: Logger.h:42
LogLevel
Definition: Logger.h:83
GreLayer module (Packet++)
Definition: Logger.h:44
void supressErrors()
Definition: Logger.h:147
RawPacket module (Packet++)
Definition: Logger.h:28
Definition: Logger.h:77
UdpLayer module (Packet++)
Definition: Logger.h:37
Packet module (Packet++)
Definition: Logger.h:29
HttpLayer module (Packet++)
Definition: Logger.h:39
IPv4Layer module (Packet++)
Definition: Logger.h:33
ArpLayer module (Packet++)
Definition: Logger.h:31
DpdkDevice module (Pcap++)
Definition: Logger.h:58
bool isDebugEnabled(LogModule module)
Definition: Logger.h:107
EthLayer module (Packet++)
Definition: Logger.h:32
void enableErrors()
Definition: Logger.h:152
FileDevice module (Pcap++)
Definition: Logger.h:56
SipLayer module (Packet++)
Definition: Logger.h:49
NetworkUtils module (Pcap++)
Definition: Logger.h:59
WinPcapLiveDevice module (Pcap++)
Definition: Logger.h:53
SdpLayer module (Packet++)
Definition: Logger.h:50
IPv6Layer module (Packet++)
Definition: Logger.h:34
char * getErrorString()
Definition: Logger.h:128
int getErrorStringLength()
Definition: Logger.h:142
LogLevel * getLogModulesArr()
Definition: Logger.h:116
static LoggerPP & getInstance()
Definition: Logger.h:165