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,
58  NumOfLogModules
59  };
60 
75  class LoggerPP
76  {
77  public:
81  enum LogLevel
82  {
85  };
86 
92  void setLogLevel(LogModule module, LogLevel level) { m_LogModulesArray[module] = level; }
93 
98  void setAllModlesToLogLevel(LogLevel level) { for (int i=1; i<NumOfLogModules; i++) m_LogModulesArray[i] = level; }
99 
105  inline bool isDebugEnabled(LogModule module) { return m_LogModulesArray[module] == Debug; }
106 
114  inline LogLevel* getLogModulesArr() { return m_LogModulesArray; }
115 
120  inline bool isErrorStringSet() { return m_ErrorString != NULL; }
121 
126  inline char* getErrorString() { return m_ErrorString; }
127 
134  void setErrorString(char* errString, int len) { m_ErrorString = errString; m_ErrorStringLen = len; }
135 
140  inline int getErrorStringLength() { return m_ErrorStringLen; }
141 
145  void supressErrors() { m_SuppressErrors = true; }
146 
150  void enableErrors() { m_SuppressErrors = false; }
151 
156  inline bool isSupressErrors() { return m_SuppressErrors; }
157 
163  static inline LoggerPP& getInstance()
164  {
165  static LoggerPP instance;
166  return instance;
167  }
168  private:
169  char* m_ErrorString;
170  int m_ErrorStringLen;
171  bool m_SuppressErrors;
172  LoggerPP::LogLevel m_LogModulesArray[NumOfLogModules];
173  LoggerPP();
174  };
175 
176 #define LOG_DEBUG(format, ...) do { \
177  if(pcpp::LoggerPP::getInstance().isDebugEnabled(LOG_MODULE)) { \
178  printf("[%-35s: %-25s: line:%-4d] " format "\n", __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
179  } \
180  } while(0)
181 
182 #define LOG_ERROR(format, ...) do { \
183  if (!pcpp::LoggerPP::getInstance().isSupressErrors()) {\
184  if(pcpp::LoggerPP::getInstance().isErrorStringSet()) \
185  snprintf(pcpp::LoggerPP::getInstance().getErrorString(), pcpp::LoggerPP::getInstance().getErrorStringLength(), format "\n", ## __VA_ARGS__); \
186  else \
187  fprintf(stderr, format "\n", ## __VA_ARGS__); \
188  } \
189  } while (0)
190 
191 #define IS_DEBUG pcpp::LoggerPP::getInstance().isDebugEnabled(LOG_MODULE)
192 
193 } // namespace pcpp
194 
195 #endif /* PCAPPP_LOGGER */
static LoggerPP & getInstance()
Definition: Logger.h:163
PayloadLayer module (Packet++)
Definition: Logger.h:34
int getErrorStringLength()
Definition: Logger.h:140
IcmpLayer module (Packet++)
Definition: Logger.h:42
LogLevel
Definition: Logger.h:81
PPPoELayer module (Packet++)
Definition: Logger.h:39
void enableErrors()
Definition: Logger.h:150
PfRingDevice module (Pcap++)
Definition: Logger.h:55
void setErrorString(char *errString, int len)
Definition: Logger.h:134
HttpLayer module (Packet++)
Definition: Logger.h:38
IPv6Layer module (Packet++)
Definition: Logger.h:33
FileDevice module (Pcap++)
Definition: Logger.h:54
UdpLayer module (Packet++)
Definition: Logger.h:36
bool isErrorStringSet()
Definition: Logger.h:120
SdpLayer module (Packet++)
Definition: Logger.h:49
TcpLayer module (Packet++)
Definition: Logger.h:35
DpdkDevice module (Pcap++)
Definition: Logger.h:56
EthLayer module (Packet++)
Definition: Logger.h:31
DnsLayer module (Packet++)
Definition: Logger.h:40
TcpReassembly module (Packet++)
Definition: Logger.h:50
WinPcapRemoteDevice module (Pcap++)
Definition: Logger.h:52
void supressErrors()
Definition: Logger.h:145
IP Utils module (Common++)
Definition: Logger.h:26
bool isDebugEnabled(LogModule module)
Definition: Logger.h:105
SSLLayer module (Packet++)
Definition: Logger.h:44
Debug log level.
Definition: Logger.h:84
SllLayer module (Packet++)
Definition: Logger.h:45
RawPacket module (Packet++)
Definition: Logger.h:27
LogModule
Definition: Logger.h:23
DhcpLayer module (Packet++)
Definition: Logger.h:46
void setAllModlesToLogLevel(LogLevel level)
Definition: Logger.h:98
ArpLayer module (Packet++)
Definition: Logger.h:30
NetworkUtils module (Pcap++)
Definition: Logger.h:57
Packet module (Packet++)
Definition: Logger.h:28
void setLogLevel(LogModule module, LogLevel level)
Definition: Logger.h:92
bool isSupressErrors()
Definition: Logger.h:156
char * getErrorString()
Definition: Logger.h:126
MplsLayer module (Packet++)
Definition: Logger.h:41
Normal log level.
Definition: Logger.h:83
GreLayer module (Packet++)
Definition: Logger.h:43
Layer module (Packet++)
Definition: Logger.h:29
The main namespace for the PcapPlusPlus lib.
Definition: Logger.h:75
IgmpLayer module (Packet++)
Definition: Logger.h:47
LogLevel * getLogModulesArr()
Definition: Logger.h:114
IPv4Layer module (Packet++)
Definition: Logger.h:32
PcapLiveDevice module (Pcap++)
Definition: Logger.h:53
WinPcapLiveDevice module (Pcap++)
Definition: Logger.h:51
SipLayer module (Packet++)
Definition: Logger.h:48
VlanLayer module (Packet++)
Definition: Logger.h:37