veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
logger.hpp
1// Copyright (c) 2019-2022 Xenios SEZC
2// https://www.veriblock.org
3// Distributed under the MIT software license, see the accompanying
4// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef ALT_INTEGRATION_LOGGER_LOGGER_HPP
7#define ALT_INTEGRATION_LOGGER_LOGGER_HPP
8
9#include <memory>
10#include <string>
11
12#include "fmt.hpp"
13
28namespace altintegration {
29
31enum class LogLevel { debug, info, warn, error, critical, off };
32
48struct Logger {
49 virtual ~Logger() = default;
50
51 virtual void log(LogLevel, const std::string&) {}
52
53 LogLevel level = LogLevel::off;
54};
55
60void SetLogger(std::unique_ptr<Logger> lgr, LogLevel log_lvl = LogLevel::info);
61
63template <typename L>
64void SetLogger(LogLevel log_lvl = LogLevel::info) {
65 SetLogger(std::unique_ptr<L>(new L()), log_lvl);
66}
67
69std::string LevelToString(LogLevel l);
72LogLevel StringToLevel(const std::string&);
73
74#ifndef VERIBLOCK_POP_LOGGER_DISABLED
75
76#ifndef VBK_LOG_FORMAT
78#define VBK_LOG_FORMAT(format, ...) \
79 fmt::sprintf(std::string("%s: ") + format, __func__, ##__VA_ARGS__)
80#endif
81
82#ifdef VBK_FUZZING_UNSAFE_FOR_PRODUCTION
85#define VBK_LOG(...)
86#else
87
88#define VBK_LOG(lvl, format, ...) \
89 /* GCOVR_EXCL_START */ \
90 do { \
91 auto& logger = altintegration::GetLogger(); \
92 if (logger.level <= lvl) { \
93 logger.log(lvl, VBK_LOG_FORMAT(format, ##__VA_ARGS__)); \
94 } \
95 } while (false) /* GCOVR_EXCL_STOP */
96#endif
97
98// clang-format off
100#define VBK_LOG_DEBUG(format, ...) VBK_LOG(altintegration::LogLevel::debug, format, ##__VA_ARGS__)
102#define VBK_LOG_INFO(format, ...) VBK_LOG(altintegration::LogLevel::info, format, ##__VA_ARGS__)
104#define VBK_LOG_WARN(format, ...) VBK_LOG(altintegration::LogLevel::warn, format, ##__VA_ARGS__)
106#define VBK_LOG_ERROR(format, ...) VBK_LOG(altintegration::LogLevel::error, format, ##__VA_ARGS__)
108#define VBK_LOG_CRITICAL(format, ...) VBK_LOG(altintegration::LogLevel::critical, format, ##__VA_ARGS__)
109// clang-format on
110
111#else // !VERIBLOCK_POP_LOGGER_DISABLED
112
114#define VBK_LOG_DEBUG(...)
116#define VBK_LOG_INFO(...)
118#define VBK_LOG_WARN(...)
120#define VBK_LOG_ERROR(...)
122#define VBK_LOG_CRITICAL(...)
123
124#endif // VERIBLOCK_POP_LOGGER_DISABLED
125
126/* GCOVR_EXCL_START */
127template <typename S, typename... Args>
128inline std::string format(const S& format_str, Args&&... args) {
129 try {
130 return fmt::format(format_str, (Args &&) args...);
131 } catch (const fmt::format_error&) {
132 VBK_LOG_WARN("invalid string formatting, str: %s", format_str);
133 }
134 return "";
135}
136/* GCOVR_EXCL_STOP */
137
138} // namespace altintegration
139
140#endif // ALT_INTEGRATION_LOGGER_LOGGER_HPP
Defines logging helpers.
Definition: block.hpp:14
std::string LevelToString(LogLevel l)
convert loglevel to string
LogLevel
Log level.
Definition: logger.hpp:31
LogLevel StringToLevel(const std::string &)
convert string to loglevel
void SetLogger(std::unique_ptr< Logger > lgr, LogLevel log_lvl=LogLevel::info)
setter for global logger instance
Logger & GetLogger()
getter for global logger instance
An interface for logger.
Definition: logger.hpp:48