Veethree D7 SDK
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1
5#ifndef LOG_H
6#define LOG_H
7
8#ifndef DOXYGEN_SKIP_THIS
9// tell spdlog that it's supposed to use an external libfmt rather
10// than its own bundled copy
11#ifndef SPDLOG_FMT_EXTERNAL
12#define SPDLOG_FMT_EXTERNAL
13#endif
14#endif // DOXYGEN_SKIP_THIS
15
16#include <spdlog/common.h>
17#include <spdlog/spdlog.h>
18
19#include <functional>
20#include <map>
21#include <nlohmann/json.hpp>
22#include <string>
23#include <utility>
24
29
33enum class module : uint8_t {
43 // Add new modules here
44 max
45};
46
133namespace vsdk::diag {
142class log {
143public:
147 static log* instance();
148
153 void load_config_file(const std::string& path);
154
159 void load_config_json(const nlohmann::json& cfg);
160
165
166 log(const log&) = delete;
167 log& operator=(const log&) = delete;
168 log(log&&) = delete;
169 log& operator=(log&&) = delete;
170 ~log() noexcept = default;
171
178 using log_func = std::function<
179 void(spdlog::source_loc, spdlog::level::level_enum, fmt::string_view, fmt::format_args)>;
180
189 template<typename... Args>
190 static void logger(module module,
191 spdlog::source_loc loc,
192 spdlog::level::level_enum lvl,
193 fmt::format_string<Args...> fmt,
194 const Args&... args)
195 {
196 // https://github.com/fmtlib/fmt/issues/3443
197 vlogger(module, loc, lvl, fmt, fmt::make_format_args(args...));
198 }
199
200private:
208 log();
209
214 static void set_log_level(const std::string& level);
215
222 log_func make_logger_func(module module, spdlog::level::level_enum lvl);
223
230 void setup_loggers();
231
237
238 static std::string to_string(module module);
239
248 static void vlogger(module module,
249 spdlog::source_loc loc,
250 spdlog::level::level_enum lvl,
251 fmt::string_view fmt,
252 fmt::format_args args)
253 {
254 log::instance()->_loggers[{ module, lvl }](loc, lvl, fmt, args);
255 }
256
257 std::mutex _config_mutex;
258 std::vector<std::string> _enabled_modules;
259 bool _is_enabled = false;
260 std::map<std::pair<module, spdlog::level::level_enum>, log_func>
261 _loggers;
262};
263
269#define LOG_TRACE(module, ...) \
270 vsdk::diag::log::logger(module, \
271 spdlog::source_loc{ __FILE__, __LINE__, SPDLOG_FUNCTION }, \
272 spdlog::level::trace, \
273 ##__VA_ARGS__)
274
280#define LOG_DEBUG(module, ...) \
281 vsdk::diag::log::logger(module, \
282 spdlog::source_loc{ __FILE__, __LINE__, SPDLOG_FUNCTION }, \
283 spdlog::level::debug, \
284 ##__VA_ARGS__)
285
291#define LOG_INFO(module, ...) \
292 vsdk::diag::log::logger(module, \
293 spdlog::source_loc{ __FILE__, __LINE__, SPDLOG_FUNCTION }, \
294 spdlog::level::info, \
295 ##__VA_ARGS__)
296
302#define LOG_WARN(module, ...) \
303 vsdk::diag::log::logger(module, \
304 spdlog::source_loc{ __FILE__, __LINE__, SPDLOG_FUNCTION }, \
305 spdlog::level::warn, \
306 ##__VA_ARGS__)
307
313#define LOG_ERROR(module, ...) \
314 vsdk::diag::log::logger(module, \
315 spdlog::source_loc{ __FILE__, __LINE__, SPDLOG_FUNCTION }, \
316 spdlog::level::err, \
317 ##__VA_ARGS__)
318
324#define LOG_CRITICAL(module, ...) \
325 vsdk::diag::log::logger(module, \
326 spdlog::source_loc{ __FILE__, __LINE__, SPDLOG_FUNCTION }, \
327 spdlog::level::critical, \
328 ##__VA_ARGS__)
329
330} // namespace vsdk::diag
331
333
334#endif // LOG_H
logging with module-based filtering
Definition log.h:142
static void logger(module module, spdlog::source_loc loc, spdlog::level::level_enum lvl, fmt::format_string< Args... > fmt, const Args &... args)
Templated wrapper for type-safe logging.
Definition log.h:190
void load_config_file(const std::string &path)
Load configuration from JSON file.
void clear_config()
Remove the current configuration, disabling any logging.
std::function< void(spdlog::source_loc, spdlog::level::level_enum, fmt::string_view, fmt::format_args)> log_func
Function type for logging operations.
Definition log.h:178
void load_config_json(const nlohmann::json &cfg)
Load configuration from JSON object.
static log * instance()
Get a pointer to the singleton instance.
module
Enumeration of available logging modules.
Definition log.h:33
@ analog
Analog module.
Definition log.h:38
@ system
System module.
Definition log.h:39
@ digital_out
Digital output module.
Definition log.h:37
@ network
Network module.
Definition log.h:40
@ pwm_out
PWM output module.
Definition log.h:35
@ comms
Comms module.
Definition log.h:41
@ digital_in
Digital input module.
Definition log.h:36
@ pwm_in
PWM input module.
Definition log.h:34
@ ct2klib
CT2KLib Compatibility module.
Definition log.h:42