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