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,
62  NumOfLogModules
63  };
64 
79  class LoggerPP
80  {
81  public:
85  enum LogLevel
86  {
89  };
90 
96  void setLogLevel(LogModule module, LogLevel level) { m_LogModulesArray[module] = level; }
97 
102  void setAllModlesToLogLevel(LogLevel level) { for (int i=1; i<NumOfLogModules; i++) m_LogModulesArray[i] = level; }
103 
109  inline bool isDebugEnabled(LogModule module) { return m_LogModulesArray[module] == Debug; }
110 
118  inline LogLevel* getLogModulesArr() { return m_LogModulesArray; }
119 
124  inline bool isErrorStringSet() { return m_ErrorString != NULL; }
125 
130  inline char* getErrorString() { return m_ErrorString; }
131 
138  void setErrorString(char* errString, int len) { m_ErrorString = errString; m_ErrorStringLen = len; }
139 
144  inline int getErrorStringLength() { return m_ErrorStringLen; }
145 
149  void supressErrors() { m_SuppressErrors = true; }
150 
154  void enableErrors() { m_SuppressErrors = false; }
155 
160  inline bool isSupressErrors() { return m_SuppressErrors; }
161 
167  static inline LoggerPP& getInstance()
168  {
169  static LoggerPP instance;
170  return instance;
171  }
172  private:
173  char* m_ErrorString;
174  int m_ErrorStringLen;
175  bool m_SuppressErrors;
176  LoggerPP::LogLevel m_LogModulesArray[NumOfLogModules];
177  LoggerPP();
178  };
179 
180 #define LOG_DEBUG(format, ...) do { \
181  if(pcpp::LoggerPP::getInstance().isDebugEnabled(LOG_MODULE)) { \
182  printf("[%-35s: %-25s: line:%-4d] " format "\n", __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
183  } \
184  } while(0)
185 
186 #define LOG_ERROR(format, ...) do { \
187  if (!pcpp::LoggerPP::getInstance().isSupressErrors()) {\
188  if(pcpp::LoggerPP::getInstance().isErrorStringSet()) \
189  snprintf(pcpp::LoggerPP::getInstance().getErrorString(), pcpp::LoggerPP::getInstance().getErrorStringLength(), format "\n", ## __VA_ARGS__); \
190  else \
191  fprintf(stderr, format "\n", ## __VA_ARGS__); \
192  } \
193  } while (0)
194 
195 #define IS_DEBUG pcpp::LoggerPP::getInstance().isDebugEnabled(LOG_MODULE)
196 
197 } // namespace pcpp
198 
199 #endif /* PCAPPP_LOGGER */
WinPcapRemoteDevice module (Pcap++)
Definition: Logger.h:56
SllLayer module (Packet++)
Definition: Logger.h:47
WinPcapLiveDevice module (Pcap++)
Definition: Logger.h:55
RawPacket module (Packet++)
Definition: Logger.h:29
LogLevel
Definition: Logger.h:85
MplsLayer module (Packet++)
Definition: Logger.h:43
IPReassembly module (Packet++)
Definition: Logger.h:54
bool isErrorStringSet()
Definition: Logger.h:124
void setAllModlesToLogLevel(LogLevel level)
Definition: Logger.h:102
TcpReassembly module (Packet++)
Definition: Logger.h:53
PPPoELayer module (Packet++)
Definition: Logger.h:41
IgmpLayer module (Packet++)
Definition: Logger.h:49
IPv4Layer module (Packet++)
Definition: Logger.h:34
SipLayer module (Packet++)
Definition: Logger.h:50
PfRingDevice module (Pcap++)
Definition: Logger.h:59
HttpLayer module (Packet++)
Definition: Logger.h:40
IP Utils module (Common++)
Definition: Logger.h:26
bool isDebugEnabled(LogModule module)
Definition: Logger.h:109
UdpLayer module (Packet++)
Definition: Logger.h:38
ArpLayer module (Packet++)
Definition: Logger.h:32
Packet module (Packet++)
Definition: Logger.h:30
TcpLayer module (Packet++)
Definition: Logger.h:37
Normal log level.
Definition: Logger.h:87
GreLayer module (Packet++)
Definition: Logger.h:45
void enableErrors()
Definition: Logger.h:154
EthLayer module (Packet++)
Definition: Logger.h:33
Table printer module (Common++)
Definition: Logger.h:27
Debug log level.
Definition: Logger.h:88
PcapLiveDevice module (Pcap++)
Definition: Logger.h:57
DhcpLayer module (Packet++)
Definition: Logger.h:48
VlanLayer module (Packet++)
Definition: Logger.h:39
PayloadLayer module (Packet++)
Definition: Logger.h:36
FileDevice module (Pcap++)
Definition: Logger.h:58
DnsLayer module (Packet++)
Definition: Logger.h:42
IcmpLayer module (Packet++)
Definition: Logger.h:44
LogLevel * getLogModulesArr()
Definition: Logger.h:118
IPv6Layer module (Packet++)
Definition: Logger.h:35
void setErrorString(char *errString, int len)
Definition: Logger.h:138
RadiusLayer module (Packet++)
Definition: Logger.h:52
DpdkDevice module (Pcap++)
Definition: Logger.h:60
Layer module (Packet++)
Definition: Logger.h:31
Definition: Logger.h:79
Generic Utils (Common++)
Definition: Logger.h:28
void setLogLevel(LogModule module, LogLevel level)
Definition: Logger.h:96
void supressErrors()
Definition: Logger.h:149
int getErrorStringLength()
Definition: Logger.h:144
static LoggerPP & getInstance()
Definition: Logger.h:167
NetworkUtils module (Pcap++)
Definition: Logger.h:61
The main namespace for the PcapPlusPlus lib.
bool isSupressErrors()
Definition: Logger.h:160
char * getErrorString()
Definition: Logger.h:130
SdpLayer module (Packet++)
Definition: Logger.h:51
LogModule
Definition: Logger.h:23
SSLLayer module (Packet++)
Definition: Logger.h:46