PcapPlusPlus  21.05+
Logger.h
Go to the documentation of this file.
1 #ifndef PCAPPP_LOGGER
2 #define PCAPPP_LOGGER
3 
4 #include <stdio.h>
5 #include <iostream>
6 #include <sstream>
7 #include <iomanip>
8 #include <stdint.h>
9 
10 #ifndef LOG_MODULE
11 #define LOG_MODULE UndefinedLogModule
12 #endif
13 
15 
20 namespace pcpp
21 {
22 
26  enum LogModule
27  {
28  UndefinedLogModule,
72  NumOfLogModules
73  };
74 
93  class Logger
94  {
95  public:
96 
100  enum LogLevel
101  {
105  };
106 
116  typedef void (*LogPrinter)(LogLevel logLevel, const std::string& logMessage, const std::string& file, const std::string& method, const int line);
117 
123  static std::string logLevelAsString(LogLevel logLevel);
124 
130  LogLevel getLogLevel(LogModule module) { return m_LogModulesArray[module]; }
131 
137  void setLogLevel(LogModule module, LogLevel level) { m_LogModulesArray[module] = level; }
138 
144  bool isDebugEnabled(LogModule module) const { return m_LogModulesArray[module] == Debug; }
145 
150  void setAllModlesToLogLevel(LogLevel level) { for (int i=1; i<NumOfLogModules; i++) m_LogModulesArray[i] = level; }
151 
156  void setLogPrinter(LogPrinter printer) { m_LogPrinter = printer; }
157 
161  void resetLogPrinter() { m_LogPrinter = &defaultLogPrinter; }
162 
166  std::string getLastError() { return m_LastError; }
167 
171  void suppressLogs() { m_LogsEnabled = false; }
172 
176  void enableLogs() { m_LogsEnabled = true; }
177 
182  bool logsEnabled() const { return m_LogsEnabled; }
183 
187  inline void internalPrintLogMessage(LogLevel logLevel, const std::string& logMessage, const std::string& file, const std::string& method, const int line);
188 
194  static Logger& getInstance()
195  {
196  static Logger instance;
197  return instance;
198  }
199  private:
200  bool m_LogsEnabled;
201  Logger::LogLevel m_LogModulesArray[NumOfLogModules];
202  LogPrinter m_LogPrinter;
203  std::string m_LastError;
204 
205  // private c'tor - this class is a singleton
206  Logger();
207 
208  static void defaultLogPrinter(LogLevel logLevel, const std::string& logMessage, const std::string& file, const std::string& method, const int line);
209  };
210 
211 #define LOG_DEBUG(message) do { \
212  if (pcpp::Logger::getInstance().logsEnabled() && pcpp::Logger::getInstance().isDebugEnabled(LOG_MODULE)) { \
213  std::ostringstream logStream; \
214  logStream << message; \
215  pcpp::Logger::getInstance().internalPrintLogMessage(pcpp::Logger::Debug, logStream.str(), __FILE__, __FUNCTION__, __LINE__); \
216  } \
217  } while(0)
218 
219 #define LOG_ERROR(message) do { \
220  std::ostringstream logStream; \
221  logStream << message; \
222  pcpp::Logger::getInstance().internalPrintLogMessage(pcpp::Logger::Error, logStream.str(), __FILE__, __FUNCTION__, __LINE__); \
223  } while (0)
224 
225 void Logger::internalPrintLogMessage(LogLevel logLevel, const std::string& logMessage, const std::string& file, const std::string& method, const int line)
226 {
227  if (logLevel == Logger::Error)
228  {
229  m_LastError = logMessage;
230  }
231  if (m_LogsEnabled)
232  {
233  m_LogPrinter(logLevel, logMessage, file, method, line);
234  }
235 }
236 
237 } // namespace pcpp
238 
239 #endif /* PCAPPP_LOGGER */
The main namespace for the PcapPlusPlus lib.
LogLevel
Definition: Logger.h:100
IcmpLayer module (Packet++)
Definition: Logger.h:47
Definition: Logger.h:93
static Logger & getInstance()
Definition: Logger.h:194
PfRingDevice module (Pcap++)
Definition: Logger.h:67
void setLogLevel(LogModule module, LogLevel level)
Definition: Logger.h:137
bool isDebugEnabled(LogModule module) const
Definition: Logger.h:144
LogModule
Definition: Logger.h:26
SdpLayer module (Packet++)
Definition: Logger.h:55
SllLayer module (Packet++)
Definition: Logger.h:50
Packet module (Packet++)
Definition: Logger.h:33
RadiusLayer module (Packet++)
Definition: Logger.h:56
std::string getLastError()
Definition: Logger.h:166
DhcpV6Layer module (Packet++)
Definition: Logger.h:52
MBufRawPacket module (Pcap++)
Definition: Logger.h:68
PayloadLayer module (Packet++)
Definition: Logger.h:39
VlanLayer module (Packet++)
Definition: Logger.h:42
IP Utils module (Common++)
Definition: Logger.h:29
IPReassembly module (Packet++)
Definition: Logger.h:61
HttpLayer module (Packet++)
Definition: Logger.h:43
LogLevel getLogLevel(LogModule module)
Definition: Logger.h:130
bool logsEnabled() const
Definition: Logger.h:182
Error log level.
Definition: Logger.h:102
SipLayer module (Packet++)
Definition: Logger.h:54
IgmpLayer module (Packet++)
Definition: Logger.h:53
Info log level.
Definition: Logger.h:103
DnsLayer module (Packet++)
Definition: Logger.h:45
RawPacket module (Packet++)
Definition: Logger.h:32
SSHLayer module (Packet++)
Definition: Logger.h:59
PcapLiveDevice module (Pcap++)
Definition: Logger.h:65
IPSecLayers module (Packet++)
Definition: Logger.h:62
GtpLayer module (Packet++)
Definition: Logger.h:57
DpdkDevice module (Pcap++)
Definition: Logger.h:69
void setLogPrinter(LogPrinter printer)
Definition: Logger.h:156
MplsLayer module (Packet++)
Definition: Logger.h:46
IPv4Layer module (Packet++)
Definition: Logger.h:37
FileDevice module (Pcap++)
Definition: Logger.h:66
Generic Utils (Common++)
Definition: Logger.h:31
static std::string logLevelAsString(LogLevel logLevel)
NetworkUtils module (Pcap++)
Definition: Logger.h:71
SSLLayer module (Packet++)
Definition: Logger.h:49
TcpLayer module (Packet++)
Definition: Logger.h:40
KniDevice module (Pcap++)
Definition: Logger.h:70
void internalPrintLogMessage(LogLevel logLevel, const std::string &logMessage, const std::string &file, const std::string &method, const int line)
Definition: Logger.h:225
GreLayer module (Packet++)
Definition: Logger.h:48
WinPcapLiveDevice module (Pcap++)
Definition: Logger.h:63
WinPcapRemoteDevice module (Pcap++)
Definition: Logger.h:64
void setAllModlesToLogLevel(LogLevel level)
Definition: Logger.h:150
TcpReassembly module (Packet++)
Definition: Logger.h:60
Layer module (Packet++)
Definition: Logger.h:34
ArpLayer module (Packet++)
Definition: Logger.h:35
EthLayer module (Packet++)
Definition: Logger.h:36
void enableLogs()
Definition: Logger.h:176
void resetLogPrinter()
Definition: Logger.h:161
UdpLayer module (Packet++)
Definition: Logger.h:41
GtpLayer module (Packet++)
Definition: Logger.h:58
PPPoELayer module (Packet++)
Definition: Logger.h:44
DhcpLayer module (Packet++)
Definition: Logger.h:51
void(* LogPrinter)(LogLevel logLevel, const std::string &logMessage, const std::string &file, const std::string &method, const int line)
Definition: Logger.h:116
IPv6Layer module (Packet++)
Definition: Logger.h:38
void suppressLogs()
Definition: Logger.h:171
Debug log level.
Definition: Logger.h:104
Table printer module (Common++)
Definition: Logger.h:30