PcapPlusPlus  23.09
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,
85  NumOfLogModules
86  };
87 
106  class Logger
107  {
108  public:
112  enum LogLevel
113  {
117  };
118 
128  typedef void (*LogPrinter)(LogLevel logLevel, const std::string& logMessage, const std::string& file, const std::string& method, const int line);
129 
135  static std::string logLevelAsString(LogLevel logLevel);
136 
142  LogLevel getLogLevel(LogModule module) { return m_LogModulesArray[module]; }
143 
149  void setLogLevel(LogModule module, LogLevel level) { m_LogModulesArray[module] = level; }
150 
156  bool isDebugEnabled(LogModule module) const { return m_LogModulesArray[module] == Debug; }
157 
162  void setAllModulesToLogLevel(LogLevel level) { for (int i=1; i<NumOfLogModules; i++) m_LogModulesArray[i] = level; }
163 
168  void setLogPrinter(LogPrinter printer) { m_LogPrinter = printer; }
169 
173  void resetLogPrinter() { m_LogPrinter = &defaultLogPrinter; }
174 
178  std::string getLastError() { return m_LastError; }
179 
183  void suppressLogs() { m_LogsEnabled = false; }
184 
188  void enableLogs() { m_LogsEnabled = true; }
189 
194  bool logsEnabled() const { return m_LogsEnabled; }
195 
196  template<class T>
197  Logger& operator<<(const T& msg)
198  {
199  (*m_LogStream) << msg;
200  return *this;
201  }
202 
203  std::ostringstream * internalCreateLogStream();
204 
208  void internalPrintLogMessage(std::ostringstream* logStream, Logger::LogLevel logLevel, const char* file, const char* method, int line);
209 
215  static Logger& getInstance()
216  {
217  static Logger instance;
218  return instance;
219  }
220  private:
221  bool m_LogsEnabled;
222  Logger::LogLevel m_LogModulesArray[NumOfLogModules];
223  LogPrinter m_LogPrinter;
224  std::string m_LastError;
225  std::ostringstream* m_LogStream;
226 
227  // private c'tor - this class is a singleton
228  Logger();
229 
230  static void defaultLogPrinter(LogLevel logLevel, const std::string& logMessage, const std::string& file, const std::string& method, const int line);
231  };
232 
233 #define PCPP_LOG_DEBUG(message) do \
234  { \
235  if (pcpp::Logger::getInstance().logsEnabled() && pcpp::Logger::getInstance().isDebugEnabled(LOG_MODULE)) \
236  { \
237  std::ostringstream* sstream = pcpp::Logger::getInstance().internalCreateLogStream(); \
238  (*sstream) << message; \
239  pcpp::Logger::getInstance().internalPrintLogMessage(sstream, pcpp::Logger::Debug, __FILE__, __FUNCTION__, __LINE__); \
240  } \
241  } while(0)
242 
243 #define PCPP_LOG_ERROR(message) do \
244  { \
245  std::ostringstream* sstream = pcpp::Logger::getInstance().internalCreateLogStream(); \
246  (*sstream) << message; \
247  pcpp::Logger::getInstance().internalPrintLogMessage(sstream, pcpp::Logger::Error, __FILE__, __FUNCTION__, __LINE__); \
248  } while (0)
249 
250 } // namespace pcpp
251 
252 #endif /* PCAPPP_LOGGER */
The main namespace for the PcapPlusPlus lib.
LogLevel
Definition: Logger.h:112
IcmpLayer module (Packet++)
Definition: Logger.h:47
Definition: Logger.h:106
static Logger & getInstance()
Definition: Logger.h:215
PfRingDevice module (Pcap++)
Definition: Logger.h:80
void setLogLevel(LogModule module, LogLevel level)
Definition: Logger.h:149
bool isDebugEnabled(LogModule module) const
Definition: Logger.h:156
LogModule
Definition: Logger.h:26
SdpLayer module (Packet++)
Definition: Logger.h:57
SllLayer module (Packet++)
Definition: Logger.h:51
TelnetLayer module (Packet++)
Definition: Logger.h:67
IcmpV6Layer module (Packet++)
Definition: Logger.h:48
Packet module (Packet++)
Definition: Logger.h:33
RadiusLayer module (Packet++)
Definition: Logger.h:58
std::string getLastError()
Definition: Logger.h:178
DhcpV6Layer module (Packet++)
Definition: Logger.h:54
MBufRawPacket module (Pcap++)
Definition: Logger.h:81
NdpLayer module (Packet++)
Definition: Logger.h:71
void setAllModulesToLogLevel(LogLevel level)
Definition: Logger.h:162
PayloadLayer module (Packet++)
Definition: Logger.h:39
VlanLayer module (Packet++)
Definition: Logger.h:42
Vrrp Record module (Packet++)
Definition: Logger.h:62
IP Utils module (Common++)
Definition: Logger.h:29
NtpLayer module (Packet++)
Definition: Logger.h:66
IPReassembly module (Packet++)
Definition: Logger.h:64
NflogLayer module (Packet++)
Definition: Logger.h:52
WakeOnLanLayer module (Packet++)
Definition: Logger.h:75
void internalPrintLogMessage(std::ostringstream *logStream, Logger::LogLevel logLevel, const char *file, const char *method, int line)
HttpLayer module (Packet++)
Definition: Logger.h:43
LogLevel getLogLevel(LogModule module)
Definition: Logger.h:142
bool logsEnabled() const
Definition: Logger.h:194
Error log level.
Definition: Logger.h:114
SipLayer module (Packet++)
Definition: Logger.h:56
SingleCommandTextProtocol module (Packet++)
Definition: Logger.h:70
IgmpLayer module (Packet++)
Definition: Logger.h:55
Info log level.
Definition: Logger.h:115
DnsLayer module (Packet++)
Definition: Logger.h:45
RawPacket module (Packet++)
Definition: Logger.h:32
SSHLayer module (Packet++)
Definition: Logger.h:61
PcapLiveDevice module (Pcap++)
Definition: Logger.h:78
IPSecLayers module (Packet++)
Definition: Logger.h:65
SomeIpSdLayer module (Packet++)
Definition: Logger.h:74
StpLayer module (Packet++)
Definition: Logger.h:68
GtpLayer module (Packet++)
Definition: Logger.h:59
DpdkDevice module (Pcap++)
Definition: Logger.h:82
void setLogPrinter(LogPrinter printer)
Definition: Logger.h:168
MplsLayer module (Packet++)
Definition: Logger.h:46
SomeIpLayer module (Packet++)
Definition: Logger.h:73
IPv4Layer module (Packet++)
Definition: Logger.h:37
LLCLayer module (Packet++)
Definition: Logger.h:69
FileDevice module (Pcap++)
Definition: Logger.h:79
Generic Utils (Common++)
Definition: Logger.h:31
static std::string logLevelAsString(LogLevel logLevel)
NetworkUtils module (Pcap++)
Definition: Logger.h:84
SSLLayer module (Packet++)
Definition: Logger.h:50
TcpLayer module (Packet++)
Definition: Logger.h:40
KniDevice module (Pcap++)
Definition: Logger.h:83
GreLayer module (Packet++)
Definition: Logger.h:49
WinPcapLiveDevice module (Pcap++)
Definition: Logger.h:76
WinPcapRemoteDevice module (Pcap++)
Definition: Logger.h:77
TcpReassembly module (Packet++)
Definition: Logger.h:63
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:188
void resetLogPrinter()
Definition: Logger.h:173
UdpLayer module (Packet++)
Definition: Logger.h:41
GtpLayer module (Packet++)
Definition: Logger.h:60
PPPoELayer module (Packet++)
Definition: Logger.h:44
FtpLayer module (Packet++)
Definition: Logger.h:72
DhcpLayer module (Packet++)
Definition: Logger.h:53
void(* LogPrinter)(LogLevel logLevel, const std::string &logMessage, const std::string &file, const std::string &method, const int line)
Definition: Logger.h:128
IPv6Layer module (Packet++)
Definition: Logger.h:38
void suppressLogs()
Definition: Logger.h:183
Debug log level.
Definition: Logger.h:116
Table printer module (Common++)
Definition: Logger.h:30