2021-01-14 16:20:33 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-10-27 23:10:39 +00:00
|
|
|
#include <libnuraft/nuraft.hxx>
|
2022-04-27 15:05:45 +00:00
|
|
|
#include <Common/logger_useful.h>
|
2021-02-10 09:28:53 +00:00
|
|
|
#include <Core/SettingsEnums.h>
|
2021-01-14 16:20:33 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class LoggerWrapper : public nuraft::logger
|
|
|
|
{
|
2021-02-19 14:13:29 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
static inline const std::unordered_map<LogsLevel, Poco::Message::Priority> LEVELS =
|
|
|
|
{
|
2024-01-04 07:59:44 +00:00
|
|
|
{LogsLevel::test, Poco::Message::Priority::PRIO_TEST},
|
2021-02-19 14:13:29 +00:00
|
|
|
{LogsLevel::trace, Poco::Message::Priority::PRIO_TRACE},
|
|
|
|
{LogsLevel::debug, Poco::Message::Priority::PRIO_DEBUG},
|
|
|
|
{LogsLevel::information, Poco::Message::PRIO_INFORMATION},
|
|
|
|
{LogsLevel::warning, Poco::Message::PRIO_WARNING},
|
|
|
|
{LogsLevel::error, Poco::Message::PRIO_ERROR},
|
|
|
|
{LogsLevel::fatal, Poco::Message::PRIO_FATAL}
|
|
|
|
};
|
|
|
|
static inline const int LEVEL_MAX = static_cast<int>(LogsLevel::trace);
|
|
|
|
static inline const int LEVEL_MIN = static_cast<int>(LogsLevel::none);
|
|
|
|
|
2021-01-14 20:43:52 +00:00
|
|
|
public:
|
2021-02-10 09:28:53 +00:00
|
|
|
LoggerWrapper(const std::string & name, LogsLevel level_)
|
2024-01-23 17:04:50 +00:00
|
|
|
: log(getLogger(name))
|
2021-02-19 14:13:29 +00:00
|
|
|
, level(level_)
|
2021-01-25 10:19:02 +00:00
|
|
|
{
|
2021-02-19 14:13:29 +00:00
|
|
|
log->setLevel(static_cast<int>(LEVELS.at(level)));
|
2021-01-25 10:19:02 +00:00
|
|
|
}
|
2021-01-14 16:20:33 +00:00
|
|
|
|
|
|
|
void put_details(
|
2021-01-26 08:17:19 +00:00
|
|
|
int level_,
|
2021-01-14 16:20:33 +00:00
|
|
|
const char * /* source_file */,
|
|
|
|
const char * /* func_name */,
|
|
|
|
size_t /* line_number */,
|
|
|
|
const std::string & msg) override
|
|
|
|
{
|
2021-02-19 14:13:29 +00:00
|
|
|
LogsLevel db_level = static_cast<LogsLevel>(level_);
|
Use fmt::runtime() for LOG_* for non constexpr
Here is oneliner:
$ gg 'LOG_\(DEBUG\|TRACE\|INFO\|TEST\|WARNING\|ERROR\|FATAL\)([^,]*, [a-zA-Z]' -- :*.cpp :*.h | cut -d: -f1 | sort -u | xargs -r sed -E -i 's#(LOG_[A-Z]*)\(([^,]*), ([A-Za-z][^,)]*)#\1(\2, fmt::runtime(\3)#'
Note, that I tried to do this with coccinelle (tool for semantic
patchin), but it cannot parse C++:
$ cat fmt.cocci
@@
expression log;
expression var;
@@
-LOG_DEBUG(log, var)
+LOG_DEBUG(log, fmt::runtime(var))
I've also tried to use some macros/templates magic to do this implicitly
in logger_useful.h, but I failed to do so, and apparently it is not
possible for now.
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
v2: manual fixes
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
2022-02-01 09:10:27 +00:00
|
|
|
LOG_IMPL(log, db_level, LEVELS.at(db_level), fmt::runtime(msg));
|
2021-01-14 16:20:33 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 08:17:19 +00:00
|
|
|
void set_level(int level_) override
|
2021-01-14 16:20:33 +00:00
|
|
|
{
|
2021-02-19 14:13:29 +00:00
|
|
|
level_ = std::min(LEVEL_MAX, std::max(LEVEL_MIN, level_));
|
|
|
|
level = static_cast<LogsLevel>(level_);
|
|
|
|
log->setLevel(static_cast<int>(LEVELS.at(level)));
|
2021-01-14 16:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int get_level() override
|
|
|
|
{
|
2021-02-19 14:13:29 +00:00
|
|
|
LogsLevel lvl = level;
|
|
|
|
return static_cast<int>(lvl);
|
2021-01-14 16:20:33 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 20:43:52 +00:00
|
|
|
private:
|
2024-01-23 17:04:50 +00:00
|
|
|
LoggerPtr log;
|
2021-02-19 14:13:29 +00:00
|
|
|
std::atomic<LogsLevel> level;
|
2021-01-14 16:20:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|