OpenVPN 3 Core Library
Loading...
Searching...
No Matches
logger.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2024- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12
13#pragma once
14
16
18constexpr int LOG_LEVEL_ERROR = 0;
20constexpr int LOG_LEVEL_INFO = 1;
23constexpr int LOG_LEVEL_VERB = 2;
26constexpr int LOG_LEVEL_DEBUG = 3;
29constexpr int LOG_LEVEL_TRACE = 4;
30
52template <int DEFAULT_LOG_LEVEL, int MAX_LEVEL = LOG_LEVEL_DEBUG>
53class Logger
54{
55 public:
56 static constexpr int max_log_level = std::max(MAX_LEVEL, DEFAULT_LOG_LEVEL);
57 static constexpr int default_log_level = DEFAULT_LOG_LEVEL;
58
59
61 int log_level() const
62 {
63 return current_log_level;
64 }
65
67 void set_log_level(int level)
68 {
69 current_log_level = level;
70 }
71
73 std::string log_prefix() const
74 {
75 return prefix_;
76 }
77
79 void set_log_prefix(const std::string &prefix)
80 {
81 prefix_ = prefix;
82 }
83
89 template <int LEVEL, typename T>
90 void log(T &&msg) const
91 {
92 /* this ensures that the function is empty if MAX_LEVEL excludes this level */
93 if constexpr (max_log_level >= LEVEL)
94 {
95 if (current_log_level >= LEVEL)
96 OPENVPN_LOG(prefix_ << std::forward<T>(msg));
97 }
98 }
99
105 template <typename T>
106 void log_trace(T &&msg) const
107 {
108 log<LOG_LEVEL_TRACE>(std::forward<T>(msg));
109 }
110
116 template <typename T>
117 void log_debug(T &&msg) const
118 {
119 log<LOG_LEVEL_DEBUG>(std::forward<T>(msg));
120 }
121
127 template <typename T>
128 void log_info(T &&msg) const
129 {
130 log<LOG_LEVEL_INFO>(std::forward<T>(msg));
131 }
132
137 template <typename T>
138 void log_verbose(T &&msg) const
139 {
140 log<LOG_LEVEL_VERB>(std::forward<T>(msg));
141 }
142
147 template <typename T>
148 void log_error(T &&msg) const
149 {
150 log<LOG_LEVEL_ERROR>(std::forward<T>(msg));
151 }
152
153 protected:
155 int current_log_level = DEFAULT_LOG_LEVEL;
156 std::string prefix_;
157};
158
163template <int DEFAULT_LOG_LEVEL, int MAX_LEVEL = LOG_LEVEL_TRACE, typename TagT = std::nullptr_t>
165{
166 public:
168 static int log_level()
169 {
170 return log_.log_level();
171 }
172
174 static void set_log_level(int level)
175 {
176 log_.set_log_level(level);
177 }
178
181
182 protected:
184};
185
186
187/* Log helper macros that allow to not instantiate/execute the code that builds the log messsage if the message is
188 * not logged or MAX_LEVEL is not compiled. This are not as nice as using the log_ members methods but are nicer than
189 * other #defines that do not use if constexpr */
190
197#define LOGGER_LOG(VERB, logger, args) \
198 do \
199 { \
200 if constexpr (decltype(logger)::max_log_level >= openvpn::logging::LOG_LEVEL_##VERB) \
201 { \
202 if (logger.log_level() >= openvpn::logging::LOG_LEVEL_##VERB) \
203 { \
204 std::ostringstream _ovpn_log_ss; \
205 _ovpn_log_ss << args; \
206 logger.log_info(_ovpn_log_ss.str()); \
207 } \
208 } \
209 } while (0)
210
211#define LOGGER_LOG_INFO(logger, args) LOGGER_LOG(INFO, logger, args)
212#define LOGGER_LOG_VERBOSE(logger, args) LOGGER_LOG(VERB, logger, args)
213#define LOGGER_LOG_DEBUG(logger, args) LOGGER_LOG(DEBUG, logger, args)
214#define LOGGER_LOG_TRACE(logger, args) LOGGER_LOG(TRACE, logger, args)
215#define LOGGER_LOG_ERROR(logger, args) LOGGER_LOG(ERROR, logger, args)
216
223#define OVPN_LOG_ERROR(args) LOGGER_LOG_ERROR(log_, args)
224#define OVPN_LOG_INFO(args) LOGGER_LOG_INFO(log_, args)
225#define OVPN_LOG_VERBOSE(args) LOGGER_LOG_VERBOSE(log_, args)
226#define OVPN_LOG_DEBUG(args) LOGGER_LOG_DEBUG(log_, args)
227#define OVPN_LOG_TRACE(args) LOGGER_LOG_TRACE(log_, args)
228
229} // namespace openvpn::logging
void log_info(T &&msg) const
Definition logger.hpp:128
static constexpr int default_log_level
Definition logger.hpp:57
static constexpr int max_log_level
Definition logger.hpp:56
void log_error(T &&msg) const
Definition logger.hpp:148
void set_log_level(int level)
set the log level for all loggigng
Definition logger.hpp:67
void log_verbose(T &&msg) const
Definition logger.hpp:138
void log_trace(T &&msg) const
Definition logger.hpp:106
std::string log_prefix() const
return the current prefix all logging
Definition logger.hpp:73
int current_log_level
configured loglevel
Definition logger.hpp:155
int log_level() const
return the current logging level for all logging
Definition logger.hpp:61
void log_debug(T &&msg) const
Definition logger.hpp:117
void set_log_prefix(const std::string &prefix)
set the log prefix for all loggigng
Definition logger.hpp:79
void log(T &&msg) const
Definition logger.hpp:90
static int log_level()
return the current logging level for all logging
Definition logger.hpp:168
static constexpr int max_log_level
Definition logger.hpp:179
static constexpr int default_log_level
Definition logger.hpp:180
static logging::Logger< DEFAULT_LOG_LEVEL, MAX_LEVEL > log_
Definition logger.hpp:183
static void set_log_level(int level)
set the log level for all loggigng
Definition logger.hpp:174
#define OPENVPN_LOG(args)
constexpr int LOG_LEVEL_VERB
Definition logger.hpp:23
constexpr int LOG_LEVEL_ERROR
Definition logger.hpp:18
constexpr int LOG_LEVEL_INFO
Definition logger.hpp:20
constexpr int LOG_LEVEL_DEBUG
Definition logger.hpp:26
constexpr int LOG_LEVEL_TRACE
Definition logger.hpp:29
#define msg(flags,...)