/src/PcapPlusPlus/Common++/header/Logger.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #include <cstdio> |
4 | | #include <cstdint> |
5 | | #include <memory> |
6 | | #include <array> |
7 | | #include <mutex> |
8 | | #include <ostream> |
9 | | #include <sstream> |
10 | | #include <functional> |
11 | | #include "DeprecationUtils.h" |
12 | | #include "ObjectPool.h" |
13 | | |
14 | | #ifndef LOG_MODULE |
15 | 27.9M | # define LOG_MODULE UndefinedLogModule |
16 | | #endif |
17 | | |
18 | | // Use __FILE_NAME__ to avoid leaking complete full path |
19 | | #ifdef __FILE_NAME__ |
20 | 133k | # define PCAPPP_FILENAME __FILE_NAME__ |
21 | | #else |
22 | | # define PCAPPP_FILENAME __FILE__ |
23 | | #endif |
24 | | |
25 | | /// @file |
26 | | |
27 | | // Compile time log levels. |
28 | | // Allows for conditional removal of unwanted log calls at compile time. |
29 | | #define PCPP_LOG_LEVEL_OFF 0 |
30 | | #define PCPP_LOG_LEVEL_ERROR 1 |
31 | | #define PCPP_LOG_LEVEL_WARN 2 |
32 | | #define PCPP_LOG_LEVEL_INFO 3 |
33 | | #define PCPP_LOG_LEVEL_DEBUG 4 |
34 | | |
35 | | // All log messages built via a PCPP_LOG_* macro below the PCPP_ACTIVE_LOG_LEVEL will be removed at compile time. |
36 | | // Uses the PCPP_ACTIVE_LOG_LEVEL if it is defined, otherwise defaults to PCAP_LOG_LEVEL_DEBUG |
37 | | #ifndef PCPP_ACTIVE_LOG_LEVEL |
38 | | # define PCPP_ACTIVE_LOG_LEVEL PCPP_LOG_LEVEL_DEBUG |
39 | | #endif // !PCPP_ACTIVE_LOG_LEVEL |
40 | | |
41 | | /// @namespace pcpp |
42 | | /// @brief The main namespace for the PcapPlusPlus lib |
43 | | namespace pcpp |
44 | | { |
45 | | /// Cross-platform and thread-safe version of strerror |
46 | | /// @param errnum Value of errno |
47 | | /// @return String representation of the error number |
48 | | std::string getErrorString(int errnum); |
49 | | |
50 | | /// An enum representing all PcapPlusPlus modules |
51 | | enum LogModule : uint8_t |
52 | | { |
53 | | UndefinedLogModule, |
54 | | CommonLogModuleIpUtils, ///< IP Utils module (Common++) |
55 | | CommonLogModuleTablePrinter, ///< Table printer module (Common++) |
56 | | CommonLogModuleGenericUtils, ///< Generic Utils (Common++) |
57 | | PacketLogModuleRawPacket, ///< RawPacket module (Packet++) |
58 | | PacketLogModulePacket, ///< Packet module (Packet++) |
59 | | PacketLogModuleLayer, ///< Layer module (Packet++) |
60 | | PacketLogModuleAsn1Codec, ///< Asn1Codec module (Packet++) |
61 | | PacketLogModuleArpLayer, ///< ArpLayer module (Packet++) |
62 | | PacketLogModuleEthLayer, ///< EthLayer module (Packet++) |
63 | | PacketLogModuleIPv4Layer, ///< IPv4Layer module (Packet++) |
64 | | PacketLogModuleIPv6Layer, ///< IPv6Layer module (Packet++) |
65 | | PacketLogModulePayloadLayer, ///< PayloadLayer module (Packet++) |
66 | | PacketLogModuleTcpLayer, ///< TcpLayer module (Packet++) |
67 | | PacketLogModuleUdpLayer, ///< UdpLayer module (Packet++) |
68 | | PacketLogModuleVlanLayer, ///< VlanLayer module (Packet++) |
69 | | PacketLogModuleHttpLayer, ///< HttpLayer module (Packet++) |
70 | | PacketLogModulePPPoELayer, ///< PPPoELayer module (Packet++) |
71 | | PacketLogModuleDnsLayer, ///< DnsLayer module (Packet++) |
72 | | PacketLogModuleMplsLayer, ///< MplsLayer module (Packet++) |
73 | | PacketLogModuleIcmpLayer, ///< IcmpLayer module (Packet++) |
74 | | PacketLogModuleIcmpV6Layer, ///< IcmpV6Layer module (Packet++) |
75 | | PacketLogModuleGreLayer, ///< GreLayer module (Packet++) |
76 | | PacketLogModuleSSLLayer, ///< SSLLayer module (Packet++) |
77 | | PacketLogModuleSllLayer, ///< SllLayer module (Packet++) |
78 | | PacketLogModuleSll2Layer, ///< Sll2Layer module (Packet++) |
79 | | PacketLogModuleNflogLayer, ///< NflogLayer module (Packet++) |
80 | | PacketLogModuleDhcpLayer, ///< DhcpLayer module (Packet++) |
81 | | PacketLogModuleDhcpV6Layer, ///< DhcpV6Layer module (Packet++) |
82 | | PacketLogModuleIgmpLayer, ///< IgmpLayer module (Packet++) |
83 | | PacketLogModuleSipLayer, ///< SipLayer module (Packet++) |
84 | | PacketLogModuleSdpLayer, ///< SdpLayer module (Packet++) |
85 | | PacketLogModuleRadiusLayer, ///< RadiusLayer module (Packet++) |
86 | | PacketLogModuleGtpLayer, ///< GtpLayer module (Packet++) |
87 | | PacketLogModuleBgpLayer, ///< GtpLayer module (Packet++) |
88 | | PacketLogModuleSSHLayer, ///< SSHLayer module (Packet++) |
89 | | PacketLogModuleVrrpLayer, ///< Vrrp Record module (Packet++) |
90 | | PacketLogModuleTcpReassembly, ///< TcpReassembly module (Packet++) |
91 | | PacketLogModuleIPReassembly, ///< IPReassembly module (Packet++) |
92 | | PacketLogModuleIPSecLayer, ///< IPSecLayers module (Packet++) |
93 | | PacketLogModuleNtpLayer, ///< NtpLayer module (Packet++) |
94 | | PacketLogModuleTelnetLayer, ///< TelnetLayer module (Packet++) |
95 | | PacketLogModuleStpLayer, ///< StpLayer module (Packet++) |
96 | | PacketLogModuleLLCLayer, ///< LLCLayer module (Packet++) |
97 | | PacketLogModuleNdpLayer, ///< NdpLayer module (Packet++) |
98 | | PacketLogModuleFtpLayer, ///< FtpLayer module (Packet++) |
99 | | PacketLogModuleSomeIpLayer, ///< SomeIpLayer module (Packet++) |
100 | | PacketLogModuleSomeIpSdLayer, ///< SomeIpSdLayer module (Packet++) |
101 | | PacketLogModuleWakeOnLanLayer, ///< WakeOnLanLayer module (Packet++) |
102 | | PacketLogModuleSmtpLayer, ///< SmtpLayer module (Packet++) |
103 | | PacketLogModuleWireGuardLayer, ///< WireGuardLayer module (Packet++) |
104 | | PacketLogModuleDoIpLayer, ///< DoipLayer module (Packet++) |
105 | | PacketLogModuleS7CommLayer, ///< S7CommLayer module (Packet++) |
106 | | PcapLogModuleWinPcapLiveDevice, ///< WinPcapLiveDevice module (Pcap++) |
107 | | PcapLogModuleRemoteDevice, ///< WinPcapRemoteDevice module (Pcap++) |
108 | | PcapLogModuleLiveDevice, ///< PcapLiveDevice module (Pcap++) |
109 | | PcapLogModuleFileDevice, ///< FileDevice module (Pcap++) |
110 | | PcapLogModulePfRingDevice, ///< PfRingDevice module (Pcap++) |
111 | | PcapLogModuleMBufRawPacket, ///< MBufRawPacket module (Pcap++) |
112 | | PcapLogModuleDpdkDevice, ///< DpdkDevice module (Pcap++) |
113 | | PcapLogModuleKniDevice, ///< KniDevice module (Pcap++) |
114 | | PcapLogModuleXdpDevice, ///< XdpDevice module (Pcap++) |
115 | | PcapLogModuleNetworkUtils, ///< Network Utils module (Pcap++) |
116 | | NumOfLogModules |
117 | | }; |
118 | | |
119 | | /// @struct LogSource |
120 | | /// Represents the source of a log message. |
121 | | /// Contains information about the source file, function, line number, and the log module. |
122 | | struct LogSource |
123 | | { |
124 | | /// Default constructor for LogSource. |
125 | 2 | constexpr LogSource() = default; |
126 | | |
127 | | /// Constructor for LogSource with only the log module. |
128 | | /// @param logModule The log module. |
129 | | explicit constexpr LogSource(LogModule logModule) : logModule(logModule) |
130 | 0 | {} |
131 | | |
132 | | /// Constructor for LogSource with all parameters. |
133 | | /// @param logModule The log module. |
134 | | /// @param file The source file. |
135 | | /// @param function The source function. |
136 | | /// @param line The line number. |
137 | | constexpr LogSource(LogModule logModule, const char* file, const char* function, int line) |
138 | 133k | : file(file), function(function), line(line), logModule(logModule) |
139 | 133k | {} |
140 | | |
141 | | const char* file = nullptr; /**< The source file. */ |
142 | | const char* function = nullptr; /**< The source function. */ |
143 | | int line = 0; /**< The line number. */ |
144 | | LogModule logModule = UndefinedLogModule; /**< The log module. */ |
145 | | }; |
146 | | |
147 | | /// An enum representing the log level. Currently 4 log levels are supported: Off, Error, Info and Debug. Info is |
148 | | /// the default log level |
149 | | enum class LogLevel : uint8_t |
150 | | { |
151 | | Off = PCPP_LOG_LEVEL_OFF, ///< No log messages are emitted. |
152 | | Error = PCPP_LOG_LEVEL_ERROR, ///< Error level logs are emitted. |
153 | | Warn = PCPP_LOG_LEVEL_WARN, ///< Warning level logs and above are emitted. |
154 | | Info = PCPP_LOG_LEVEL_INFO, ///< Info level logs and above are emitted. |
155 | | Debug = PCPP_LOG_LEVEL_DEBUG ///< Debug level logs and above are emitted. |
156 | | }; |
157 | | |
158 | | inline std::ostream& operator<<(std::ostream& ostr, LogLevel lvl) |
159 | 0 | { |
160 | 0 | return ostr << static_cast<std::underlying_type_t<LogLevel>>(lvl); |
161 | 0 | } |
162 | | |
163 | | // Forward declaration |
164 | | class Logger; |
165 | | |
166 | | namespace internal |
167 | | { |
168 | | /// @class LogContext |
169 | | /// @brief A context encapsulating the details of a single log message to be passed to the Logger. |
170 | | class LogContext |
171 | | { |
172 | | public: |
173 | | friend class pcpp::Logger; |
174 | | |
175 | | /// @brief Creates a context with an empty message with Info level and no source. |
176 | 2 | LogContext() = default; |
177 | | |
178 | | /// @brief Creates a context with an empty message with the given level and source. |
179 | | /// @param level The log level for this message. |
180 | | /// @param source The log source. |
181 | 0 | explicit LogContext(LogLevel level, LogSource const& source = {}) : m_Source(source), m_Level(level) |
182 | 0 | {} |
183 | | |
184 | | /// @brief Initializes the context with an empty message and the given level and source. |
185 | | /// @param level The log level for this message. |
186 | | /// @param source The log source. |
187 | | void init(LogLevel level, LogSource const& source) |
188 | 133k | { |
189 | 133k | m_Source = source; |
190 | 133k | m_Level = level; |
191 | 133k | m_Stream.clear(); |
192 | 133k | m_Stream.str({}); |
193 | 133k | } |
194 | | |
195 | | /// @brief Appends to the message. |
196 | | /// @param value The value to append. |
197 | | /// @return A reference to this context. |
198 | | template <class T> LogContext& operator<<(T const& value) |
199 | 288k | { |
200 | 288k | m_Stream << value; |
201 | 288k | return *this; |
202 | 288k | } pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [52]>(char const (&) [52]) Line | Count | Source | 199 | 411 | { | 200 | 411 | m_Stream << value; | 201 | 411 | return *this; | 202 | 411 | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 199 | 1.42k | { | 200 | 1.42k | m_Stream << value; | 201 | 1.42k | return *this; | 202 | 1.42k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [2]>(char const (&) [2]) Line | Count | Source | 199 | 1.30k | { | 200 | 1.30k | m_Stream << value; | 201 | 1.30k | return *this; | 202 | 1.30k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char const*>(char const* const&) Line | Count | Source | 199 | 2 | { | 200 | 2 | m_Stream << value; | 201 | 2 | return *this; | 202 | 2 | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [20]>(char const (&) [20]) Line | Count | Source | 199 | 1.30k | { | 200 | 1.30k | m_Stream << value; | 201 | 1.30k | return *this; | 202 | 1.30k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [46]>(char const (&) [46]) Line | Count | Source | 199 | 971 | { | 200 | 971 | m_Stream << value; | 201 | 971 | return *this; | 202 | 971 | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [29]>(char const (&) [29]) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [86]>(char const (&) [86]) Line | Count | Source | 199 | 345 | { | 200 | 345 | m_Stream << value; | 201 | 345 | return *this; | 202 | 345 | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <std::__1::ios_base& (std::__1::ios_base&)>(std::__1::ios_base& ( const&)(std::__1::ios_base&)) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <unsigned int>(unsigned int const&) Line | Count | Source | 199 | 10.3k | { | 200 | 10.3k | m_Stream << value; | 201 | 10.3k | return *this; | 202 | 10.3k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [32]>(char const (&) [32]) Line | Count | Source | 199 | 101 | { | 200 | 101 | m_Stream << value; | 201 | 101 | return *this; | 202 | 101 | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [26]>(char const (&) [26]) Line | Count | Source | 199 | 1.00k | { | 200 | 1.00k | m_Stream << value; | 201 | 1.00k | return *this; | 202 | 1.00k | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [28]>(char const (&) [28]) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [31]>(char const (&) [31]) Line | Count | Source | 199 | 12.9k | { | 200 | 12.9k | m_Stream << value; | 201 | 12.9k | return *this; | 202 | 12.9k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [24]>(char const (&) [24]) Line | Count | Source | 199 | 6.11k | { | 200 | 6.11k | m_Stream << value; | 201 | 6.11k | return *this; | 202 | 6.11k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [23]>(char const (&) [23]) Line | Count | Source | 199 | 426 | { | 200 | 426 | m_Stream << value; | 201 | 426 | return *this; | 202 | 426 | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [27]>(char const (&) [27]) Line | Count | Source | 199 | 304 | { | 200 | 304 | m_Stream << value; | 201 | 304 | return *this; | 202 | 304 | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [37]>(char const (&) [37]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [22]>(char const (&) [22]) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [25]>(char const (&) [25]) Line | Count | Source | 199 | 65.0k | { | 200 | 65.0k | m_Stream << value; | 201 | 65.0k | return *this; | 202 | 65.0k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [50]>(char const (&) [50]) Line | Count | Source | 199 | 1.02k | { | 200 | 1.02k | m_Stream << value; | 201 | 1.02k | return *this; | 202 | 1.02k | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [71]>(char const (&) [71]) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [77]>(char const (&) [77]) Line | Count | Source | 199 | 18 | { | 200 | 18 | m_Stream << value; | 201 | 18 | return *this; | 202 | 18 | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [30]>(char const (&) [30]) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [70]>(char const (&) [70]) Line | Count | Source | 199 | 292 | { | 200 | 292 | m_Stream << value; | 201 | 292 | return *this; | 202 | 292 | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [17]>(char const (&) [17]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [49]>(char const (&) [49]) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [39]>(char const (&) [39]) Line | Count | Source | 199 | 814 | { | 200 | 814 | m_Stream << value; | 201 | 814 | return *this; | 202 | 814 | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [48]>(char const (&) [48]) Line | Count | Source | 199 | 2 | { | 200 | 2 | m_Stream << value; | 201 | 2 | return *this; | 202 | 2 | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [56]>(char const (&) [56]) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [21]>(char const (&) [21]) Line | Count | Source | 199 | 161 | { | 200 | 161 | m_Stream << value; | 201 | 161 | return *this; | 202 | 161 | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [13]>(char const (&) [13]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [47]>(char const (&) [47]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [54]>(char const (&) [54]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [18]>(char const (&) [18]) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [44]>(char const (&) [44]) Line | Count | Source | 199 | 2.34k | { | 200 | 2.34k | m_Stream << value; | 201 | 2.34k | return *this; | 202 | 2.34k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [33]>(char const (&) [33]) Line | Count | Source | 199 | 6 | { | 200 | 6 | m_Stream << value; | 201 | 6 | return *this; | 202 | 6 | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [59]>(char const (&) [59]) Line | Count | Source | 199 | 506 | { | 200 | 506 | m_Stream << value; | 201 | 506 | return *this; | 202 | 506 | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [45]>(char const (&) [45]) Line | Count | Source | 199 | 2.71k | { | 200 | 2.71k | m_Stream << value; | 201 | 2.71k | return *this; | 202 | 2.71k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [36]>(char const (&) [36]) Line | Count | Source | 199 | 626 | { | 200 | 626 | m_Stream << value; | 201 | 626 | return *this; | 202 | 626 | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char>(char const&) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [15]>(char const (&) [15]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [14]>(char const (&) [14]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <unsigned short>(unsigned short const&) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [72]>(char const (&) [72]) Line | Count | Source | 199 | 297 | { | 200 | 297 | m_Stream << value; | 201 | 297 | return *this; | 202 | 297 | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <int>(int const&) Line | Count | Source | 199 | 70.4k | { | 200 | 70.4k | m_Stream << value; | 201 | 70.4k | return *this; | 202 | 70.4k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [142]>(char const (&) [142]) Line | Count | Source | 199 | 868 | { | 200 | 868 | m_Stream << value; | 201 | 868 | return *this; | 202 | 868 | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [76]>(char const (&) [76]) Line | Count | Source | 199 | 51 | { | 200 | 51 | m_Stream << value; | 201 | 51 | return *this; | 202 | 51 | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [85]>(char const (&) [85]) Line | Count | Source | 199 | 2.47k | { | 200 | 2.47k | m_Stream << value; | 201 | 2.47k | return *this; | 202 | 2.47k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [75]>(char const (&) [75]) Line | Count | Source | 199 | 704 | { | 200 | 704 | m_Stream << value; | 201 | 704 | return *this; | 202 | 704 | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [84]>(char const (&) [84]) Line | Count | Source | 199 | 1.54k | { | 200 | 1.54k | m_Stream << value; | 201 | 1.54k | return *this; | 202 | 1.54k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [69]>(char const (&) [69]) Line | Count | Source | 199 | 690 | { | 200 | 690 | m_Stream << value; | 201 | 690 | return *this; | 202 | 690 | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [73]>(char const (&) [73]) Line | Count | Source | 199 | 16 | { | 200 | 16 | m_Stream << value; | 201 | 16 | return *this; | 202 | 16 | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [83]>(char const (&) [83]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [87]>(char const (&) [87]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [57]>(char const (&) [57]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [92]>(char const (&) [92]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [82]>(char const (&) [82]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <unsigned long>(unsigned long const&) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [8]>(char const (&) [8]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <pcpp::DhcpV6OptionType>(pcpp::DhcpV6OptionType const&) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [98]>(char const (&) [98]) Line | Count | Source | 199 | 10.7k | { | 200 | 10.7k | m_Stream << value; | 201 | 10.7k | return *this; | 202 | 10.7k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [43]>(char const (&) [43]) Line | Count | Source | 199 | 18.9k | { | 200 | 18.9k | m_Stream << value; | 201 | 18.9k | return *this; | 202 | 18.9k | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [16]>(char const (&) [16]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [35]>(char const (&) [35]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [19]>(char const (&) [19]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [96]>(char const (&) [96]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [51]>(char const (&) [51]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [60]>(char const (&) [60]) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [64]>(char const (&) [64]) Line | Count | Source | 199 | 509 | { | 200 | 509 | m_Stream << value; | 201 | 509 | return *this; | 202 | 509 | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [12]>(char const (&) [12]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [53]>(char const (&) [53]) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [38]>(char const (&) [38]) Line | Count | Source | 199 | 1.58k | { | 200 | 1.58k | m_Stream << value; | 201 | 1.58k | return *this; | 202 | 1.58k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [40]>(char const (&) [40]) Line | Count | Source | 199 | 65.0k | { | 200 | 65.0k | m_Stream << value; | 201 | 65.0k | return *this; | 202 | 65.0k | } |
pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [41]>(char const (&) [41]) Line | Count | Source | 199 | 2.34k | { | 200 | 2.34k | m_Stream << value; | 201 | 2.34k | return *this; | 202 | 2.34k | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [42]>(char const (&) [42]) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [141]>(char const (&) [141]) Line | Count | Source | 199 | 612 | { | 200 | 612 | m_Stream << value; | 201 | 612 | return *this; | 202 | 612 | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [68]>(char const (&) [68]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [122]>(char const (&) [122]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [138]>(char const (&) [138]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [103]>(char const (&) [103]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [95]>(char const (&) [95]) pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [67]>(char const (&) [67]) Line | Count | Source | 199 | 566 | { | 200 | 566 | m_Stream << value; | 201 | 566 | return *this; | 202 | 566 | } |
Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [7]>(char const (&) [7]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [81]>(char const (&) [81]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [91]>(char const (&) [91]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [5]>(char const (&) [5]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <unsigned char>(unsigned char const&) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <pcpp::IPAddress::AddressType>(pcpp::IPAddress::AddressType const&) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [9]>(char const (&) [9]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <pcpp::IPAddress>(pcpp::IPAddress const&) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [65]>(char const (&) [65]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [108]>(char const (&) [108]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [34]>(char const (&) [34]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [55]>(char const (&) [55]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [74]>(char const (&) [74]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [10]>(char const (&) [10]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [58]>(char const (&) [58]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [63]>(char const (&) [63]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [1]>(char const (&) [1]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [4]>(char const (&) [4]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [6]>(char const (&) [6]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [3]>(char const (&) [3]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [61]>(char const (&) [61]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [117]>(char const (&) [117]) Unexecuted instantiation: pcpp::internal::LogContext& pcpp::internal::LogContext::operator<< <char [88]>(char const (&) [88]) |
203 | | |
204 | | private: |
205 | | std::ostringstream m_Stream; |
206 | | LogSource m_Source; |
207 | | LogLevel m_Level = LogLevel::Info; |
208 | | }; |
209 | | } // namespace internal |
210 | | |
211 | | /// @class Logger |
212 | | /// PcapPlusPlus logger manager. |
213 | | /// PcapPlusPlus uses this logger to output both error and debug logs. |
214 | | /// There are currently 3 log levels: Logger#Error, Logger#Info and Logger#Debug. |
215 | | /// |
216 | | /// PcapPlusPlus is divided into modules (described in #LogModule enum). The user can set the log level got each |
217 | | /// module or to all modules at once. The default is Logger#Info which outputs only error messages. Changing log |
218 | | /// level for modules can be done dynamically while the application is running. |
219 | | /// |
220 | | /// The logger also exposes a method to retrieve the last error log message. |
221 | | /// |
222 | | /// Logs are printed to console by default in a certain format. The user can set a different print function to |
223 | | /// change the format or to print to other media (such as files, etc.). |
224 | | /// |
225 | | /// PcapPlusPlus logger is a singleton which can be reached from anywhere in the code. |
226 | | /// |
227 | | /// Note: Logger#Info level logs are currently only used in DPDK devices to set DPDK log level to RTE_LOG_NOTICE. |
228 | | class Logger |
229 | | { |
230 | | public: |
231 | | Logger(const Logger&) = delete; |
232 | | Logger& operator=(const Logger&) = delete; |
233 | | |
234 | | // Deprecated, Use the LogLevel in the pcpp namespace instead. |
235 | | using LogLevel = pcpp::LogLevel; |
236 | | PCPP_DEPRECATED("Use the LogLevel in the pcpp namespace instead.") |
237 | | static const LogLevel Error = LogLevel::Error; |
238 | | PCPP_DEPRECATED("Use the LogLevel in the pcpp namespace instead.") |
239 | | static const LogLevel Info = LogLevel::Info; |
240 | | PCPP_DEPRECATED("Use the LogLevel in the pcpp namespace instead.") |
241 | | static const LogLevel Debug = LogLevel::Debug; |
242 | | |
243 | | /// @typedef LogPrinter |
244 | | /// Log printer callback. Used for printing the logs in a custom way. |
245 | | /// @param[in] logLevel The log level for this log message |
246 | | /// @param[in] logMessage The log message |
247 | | /// @param[in] file The source file in PcapPlusPlus code the log message is coming from |
248 | | /// @param[in] method The method in PcapPlusPlus code the log message is coming from |
249 | | /// @param[in] line The line in PcapPlusPlus code the log message is coming from |
250 | | /// @remarks The printer callback should support being called from multiple threads simultaneously. |
251 | | using LogPrinter = std::function<void(LogLevel logLevel, const std::string& logMessage, const std::string& file, |
252 | | const std::string& method, const int line)>; |
253 | | |
254 | | /// A static method for converting the log level enum to a string. |
255 | | /// @param[in] logLevel A log level enum |
256 | | /// @return The log level as a string |
257 | | static std::string logLevelAsString(LogLevel logLevel); |
258 | | |
259 | | /// Get the log level for a certain module |
260 | | /// @param[in] module PcapPlusPlus module |
261 | | /// @return The log level set for this module |
262 | | LogLevel getLogLevel(LogModule module) |
263 | 0 | { |
264 | 0 | return m_LogModulesArray[module]; |
265 | 0 | } |
266 | | |
267 | | /// Set the log level for a certain PcapPlusPlus module |
268 | | /// @param[in] module PcapPlusPlus module |
269 | | /// @param[in] level The log level to set the module to |
270 | | void setLogLevel(LogModule module, LogLevel level) |
271 | 0 | { |
272 | 0 | m_LogModulesArray[module] = level; |
273 | 0 | } |
274 | | |
275 | | /// Check whether a certain module is set to debug log level |
276 | | /// @param[in] module PcapPlusPlus module |
277 | | /// @return True if this module log level is "debug". False otherwise |
278 | | bool isDebugEnabled(LogModule module) const |
279 | 98.4k | { |
280 | 98.4k | return m_LogModulesArray[module] == LogLevel::Debug; |
281 | 98.4k | } |
282 | | |
283 | | /// @brief Check whether a log level should be emitted by the logger. |
284 | | /// @param level The level of the log message. |
285 | | /// @param module PcapPlusPlus module |
286 | | /// @return True if the message should be emitted. False otherwise. |
287 | | bool shouldLog(LogLevel level, LogModule module) const |
288 | 28.5M | { |
289 | 28.5M | return level != LogLevel::Off && m_LogModulesArray[module] >= level; |
290 | 28.5M | } |
291 | | |
292 | | /// Set all PcapPlusPlus modules to a certain log level |
293 | | /// @param[in] level The log level to set all modules to |
294 | | void setAllModulesToLogLevel(LogLevel level) |
295 | 0 | { |
296 | 0 | for (int i = 1; i < NumOfLogModules; i++) |
297 | 0 | { |
298 | 0 | m_LogModulesArray[i] = level; |
299 | 0 | } |
300 | 0 | } |
301 | | |
302 | | /// Set a custom log printer. |
303 | | /// @param[in] printer A log printer function that will be called for every log message |
304 | | void setLogPrinter(LogPrinter printer) |
305 | 0 | { |
306 | 0 | m_LogPrinter = printer; |
307 | 0 | } |
308 | | |
309 | | /// Set the log printer back to the default printer |
310 | | void resetLogPrinter(); |
311 | | |
312 | | /// @return Get the last error message |
313 | | std::string getLastError() const |
314 | 0 | { |
315 | 0 | return { m_LastError.data() }; |
316 | 0 | } |
317 | | |
318 | | /// Suppress logs in all PcapPlusPlus modules |
319 | | void suppressLogs() |
320 | 2.26k | { |
321 | 2.26k | m_LogsEnabled = false; |
322 | 2.26k | } |
323 | | |
324 | | /// Enable logs in all PcapPlusPlus modules |
325 | | void enableLogs() |
326 | 0 | { |
327 | 0 | m_LogsEnabled = true; |
328 | 0 | } |
329 | | |
330 | | /// Get an indication if logs are currently enabled. |
331 | | /// @return True if logs are currently enabled, false otherwise |
332 | | bool logsEnabled() const |
333 | 0 | { |
334 | 0 | return m_LogsEnabled; |
335 | 0 | } |
336 | | |
337 | | /// @brief Controls if the logger should use a pool of LogContext objects. |
338 | | /// |
339 | | /// If enabled is set to false, preallocate and maxPoolSize are ignored. |
340 | | /// @param enabled True to enable context pooling, false to disable. |
341 | | /// @param preallocate The number of LogContext objects to preallocate in the pool. |
342 | | /// @param maxPoolSize The maximum number of LogContext objects to keep in the pool. |
343 | | /// @remarks Disabling the pooling clears the pool. |
344 | | void useContextPooling(bool enabled, std::size_t preallocate = 2, std::size_t maxPoolSize = 10) |
345 | 0 | { |
346 | 0 | m_UseContextPooling = enabled; |
347 | 0 |
|
348 | 0 | if (m_UseContextPooling) |
349 | 0 | { |
350 | 0 | m_LogContextPool.setMaxSize(maxPoolSize); |
351 | 0 |
|
352 | 0 | if (preallocate > 0) |
353 | 0 | { |
354 | 0 | m_LogContextPool.preallocate(preallocate); |
355 | 0 | } |
356 | 0 | } |
357 | 0 | else |
358 | 0 | { |
359 | 0 | // Clear the pool if we're disabling pooling. |
360 | 0 | m_LogContextPool.clear(); |
361 | 0 | } |
362 | 0 | } |
363 | | |
364 | | /// Get access to Logger singleton |
365 | | /// @todo: make this singleton thread-safe/ |
366 | | /// @return a pointer to the Logger singleton |
367 | | static Logger& getInstance() |
368 | 28.6M | { |
369 | 28.6M | static Logger instance; |
370 | 28.6M | return instance; |
371 | 28.6M | } |
372 | | |
373 | | /// @brief Creates a new LogContext with Info level and no source. |
374 | | /// @return A new LogContext. |
375 | | std::unique_ptr<internal::LogContext> createLogContext(); |
376 | | |
377 | | /// @brief Creates a new LogContext with the given level and source. |
378 | | /// @param level The log level for this message. |
379 | | /// @param source The log source. |
380 | | /// @return A new LogContext. |
381 | | std::unique_ptr<internal::LogContext> createLogContext(LogLevel level, LogSource const& source = {}); |
382 | | |
383 | | /// @brief Directly emits a log message bypassing all level checks. |
384 | | /// @param source The log source. |
385 | | /// @param level The log level for this message. This is only used for the log printer. |
386 | | /// @param message The log message. |
387 | | void emit(LogSource const& source, LogLevel level, std::string const& message); |
388 | | |
389 | | /// @brief Directly emits a log message bypassing all level checks. |
390 | | /// @param message The log message. |
391 | | void emit(std::unique_ptr<internal::LogContext> message); |
392 | | |
393 | | private: |
394 | | bool m_LogsEnabled; |
395 | | std::array<LogLevel, NumOfLogModules> m_LogModulesArray{}; |
396 | | LogPrinter m_LogPrinter; |
397 | | |
398 | | // Using an array instead of std::string because MinGW has issues with TLS destruction. |
399 | | // The array should be large enough to hold most PCPP errors without cutoffs. |
400 | | static thread_local std::array<char, 255> m_LastError; |
401 | | |
402 | | bool m_UseContextPooling = true; |
403 | | // Keep a maximum of 10 LogContext objects in the pool. |
404 | | internal::DynamicObjectPool<internal::LogContext> m_LogContextPool{ 10, 2 }; |
405 | | |
406 | | // private c'tor - this class is a singleton |
407 | | Logger(); |
408 | | }; |
409 | | |
410 | | } // namespace pcpp |
411 | | |
412 | | #define PCPP_LOG(level, message) \ |
413 | 28.5M | do \ |
414 | 28.5M | { \ |
415 | 28.5M | auto& logger = pcpp::Logger::getInstance(); \ |
416 | 28.5M | if (logger.shouldLog(level, LOG_MODULE)) \ |
417 | 28.5M | { \ |
418 | 133k | auto ctx = \ |
419 | 133k | logger.createLogContext(level, pcpp::LogSource(LOG_MODULE, PCAPPP_FILENAME, __FUNCTION__, __LINE__)); \ |
420 | 133k | (*ctx) << message; \ |
421 | 133k | logger.emit(std::move(ctx)); \ |
422 | 133k | } \ |
423 | 28.5M | } while (0) |
424 | | |
425 | | #if PCPP_ACTIVE_LOG_LEVEL >= PCPP_LOG_LEVEL_DEBUG |
426 | 28.4M | # define PCPP_LOG_DEBUG(message) PCPP_LOG(pcpp::LogLevel::Debug, message) |
427 | | #else |
428 | | # define PCPP_LOG_DEBUG(message) (void)0 |
429 | | #endif |
430 | | |
431 | | #if PCPP_ACTIVE_LOG_LEVEL >= PCPP_LOG_LEVEL_WARN |
432 | 5.16k | # define PCPP_LOG_WARN(message) PCPP_LOG(pcpp::LogLevel::Warn, message) |
433 | | #else |
434 | | # define PCPP_LOG_WARN(message) (void)0 |
435 | | #endif |
436 | | |
437 | | #if PCPP_ACTIVE_LOG_LEVEL >= PCPP_LOG_LEVEL_INFO |
438 | | # define PCPP_LOG_INFO(message) PCPP_LOG(pcpp::LogLevel::Info, message) |
439 | | #else |
440 | | # define PCPP_LOG_INFO(message) (void)0 |
441 | | #endif |
442 | | |
443 | | #if PCPP_ACTIVE_LOG_LEVEL >= PCPP_LOG_LEVEL_ERROR |
444 | 128k | # define PCPP_LOG_ERROR(message) PCPP_LOG(pcpp::LogLevel::Error, message) |
445 | | #else |
446 | | # define PCPP_LOG_ERROR(message) (void)0 |
447 | | #endif |