PcapPlusPlus  22.11
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,
83  NumOfLogModules
84  };
85 
104  class Logger
105  {
106  public:
110  enum LogLevel
111  {
115  };
116 
126  typedef void (*LogPrinter)(LogLevel logLevel, const std::string& logMessage, const std::string& file, const std::string& method, const int line);
127 
133  static std::string logLevelAsString(LogLevel logLevel);
134 
140  LogLevel getLogLevel(LogModule module) { return m_LogModulesArray[module]; }
141 
147  void setLogLevel(LogModule module, LogLevel level) { m_LogModulesArray[module] = level; }
148 
154  bool isDebugEnabled(LogModule module) const { return m_LogModulesArray[module] == Debug; }
155 
160  void setAllModlesToLogLevel(LogLevel level) { for (int i=1; i<NumOfLogModules; i++) m_LogModulesArray[i] = level; }
161 
166  void setLogPrinter(LogPrinter printer) { m_LogPrinter = printer; }
167 
171  void resetLogPrinter() { m_LogPrinter = &defaultLogPrinter; }
172 
176  std::string getLastError() { return m_LastError; }
177 
181  void suppressLogs() { m_LogsEnabled = false; }
182 
186  void enableLogs() { m_LogsEnabled = true; }
187 
192  bool logsEnabled() const { return m_LogsEnabled; }
193 
194  template<class T>
195  Logger& operator<<(const T& msg)
196  {
197  (*m_LogStream) << msg;
198  return *this;
199  }
200 
201  std::ostringstream * internalCreateLogStream();
202 
206  void internalPrintLogMessage(std::ostringstream* logStream, Logger::LogLevel logLevel, const char* file, const char* method, int line);
207 
213  static Logger& getInstance()
214  {
215  static Logger instance;
216  return instance;
217  }
218  private:
219  bool m_LogsEnabled;
220  Logger::LogLevel m_LogModulesArray[NumOfLogModules];
221  LogPrinter m_LogPrinter;
222  std::string m_LastError;
223  std::ostringstream* m_LogStream;
224 
225  // private c'tor - this class is a singleton
226  Logger();
227 
228  static void defaultLogPrinter(LogLevel logLevel, const std::string& logMessage, const std::string& file, const std::string& method, const int line);
229  };
230 
231 #define PCPP_LOG_DEBUG(message) do \
232  { \
233  if (pcpp::Logger::getInstance().logsEnabled() && pcpp::Logger::getInstance().isDebugEnabled(LOG_MODULE)) \
234  { \
235  std::ostringstream* sstream = pcpp::Logger::getInstance().internalCreateLogStream(); \
236  (*sstream) << message; \
237  pcpp::Logger::getInstance().internalPrintLogMessage(sstream, pcpp::Logger::Debug, __FILE__, __FUNCTION__, __LINE__); \
238  } \
239  } while(0)
240 
241 #define PCPP_LOG_ERROR(message) do \
242  { \
243  std::ostringstream* sstream = pcpp::Logger::getInstance().internalCreateLogStream(); \
244  (*sstream) << message; \
245  pcpp::Logger::getInstance().internalPrintLogMessage(sstream, pcpp::Logger::Error, __FILE__, __FUNCTION__, __LINE__); \
246  } while (0)
247 
248 } // namespace pcpp
249 
250 #endif /* PCAPPP_LOGGER */
The main namespace for the PcapPlusPlus lib.
LogLevel
Definition: Logger.h:110
IcmpLayer module (Packet++)
Definition: Logger.h:47
Definition: Logger.h:104
static Logger & getInstance()
Definition: Logger.h:213
PfRingDevice module (Pcap++)
Definition: Logger.h:78
void setLogLevel(LogModule module, LogLevel level)
Definition: Logger.h:147
bool isDebugEnabled(LogModule module) const
Definition: Logger.h:154
LogModule
Definition: Logger.h:26
SdpLayer module (Packet++)
Definition: Logger.h:56
SllLayer module (Packet++)
Definition: Logger.h:51
TelnetLayer module (Packet++)
Definition: Logger.h:65
IcmpV6Layer module (Packet++)
Definition: Logger.h:48
Packet module (Packet++)
Definition: Logger.h:33
RadiusLayer module (Packet++)
Definition: Logger.h:57
std::string getLastError()
Definition: Logger.h:176
DhcpV6Layer module (Packet++)
Definition: Logger.h:53
MBufRawPacket module (Pcap++)
Definition: Logger.h:79
NdpLayer module (Packet++)
Definition: Logger.h:69
PayloadLayer module (Packet++)
Definition: Logger.h:39
VlanLayer module (Packet++)
Definition: Logger.h:42
IP Utils module (Common++)
Definition: Logger.h:29
NtpLayer module (Packet++)
Definition: Logger.h:64
IPReassembly module (Packet++)
Definition: Logger.h:62
WakeOnLanLayer module (Packet++)
Definition: Logger.h:73
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:140
bool logsEnabled() const
Definition: Logger.h:192
Error log level.
Definition: Logger.h:112
SipLayer module (Packet++)
Definition: Logger.h:55
SingleCommandTextProtocol module (Packet++)
Definition: Logger.h:68
IgmpLayer module (Packet++)
Definition: Logger.h:54
Info log level.
Definition: Logger.h:113
DnsLayer module (Packet++)
Definition: Logger.h:45
RawPacket module (Packet++)
Definition: Logger.h:32
SSHLayer module (Packet++)
Definition: Logger.h:60
PcapLiveDevice module (Pcap++)
Definition: Logger.h:76
IPSecLayers module (Packet++)
Definition: Logger.h:63
SomeIpSdLayer module (Packet++)
Definition: Logger.h:72
StpLayer module (Packet++)
Definition: Logger.h:66
GtpLayer module (Packet++)
Definition: Logger.h:58
DpdkDevice module (Pcap++)
Definition: Logger.h:80
void setLogPrinter(LogPrinter printer)
Definition: Logger.h:166
MplsLayer module (Packet++)
Definition: Logger.h:46
SomeIpLayer module (Packet++)
Definition: Logger.h:71
IPv4Layer module (Packet++)
Definition: Logger.h:37
LLCLayer module (Packet++)
Definition: Logger.h:67
FileDevice module (Pcap++)
Definition: Logger.h:77
Generic Utils (Common++)
Definition: Logger.h:31
static std::string logLevelAsString(LogLevel logLevel)
NetworkUtils module (Pcap++)
Definition: Logger.h:82
SSLLayer module (Packet++)
Definition: Logger.h:50
TcpLayer module (Packet++)
Definition: Logger.h:40
KniDevice module (Pcap++)
Definition: Logger.h:81
GreLayer module (Packet++)
Definition: Logger.h:49
WinPcapLiveDevice module (Pcap++)
Definition: Logger.h:74
WinPcapRemoteDevice module (Pcap++)
Definition: Logger.h:75
void setAllModlesToLogLevel(LogLevel level)
Definition: Logger.h:160
TcpReassembly module (Packet++)
Definition: Logger.h:61
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:186
void resetLogPrinter()
Definition: Logger.h:171
UdpLayer module (Packet++)
Definition: Logger.h:41
GtpLayer module (Packet++)
Definition: Logger.h:59
PPPoELayer module (Packet++)
Definition: Logger.h:44
FtpLayer module (Packet++)
Definition: Logger.h:70
DhcpLayer module (Packet++)
Definition: Logger.h:52
void(* LogPrinter)(LogLevel logLevel, const std::string &logMessage, const std::string &file, const std::string &method, const int line)
Definition: Logger.h:126
IPv6Layer module (Packet++)
Definition: Logger.h:38
void suppressLogs()
Definition: Logger.h:181
Debug log level.
Definition: Logger.h:114
Table printer module (Common++)
Definition: Logger.h:30