ClickHouse/base/common/logger_useful.h

48 lines
2.6 KiB
C++
Raw Normal View History

#pragma once
/// Macros for convenient usage of Poco logger.
2020-05-22 10:58:29 +00:00
#include <fmt/format.h>
2020-05-23 16:53:51 +00:00
#include <fmt/ostream.h>
#include <Poco/Logger.h>
2019-07-10 12:19:17 +00:00
#include <Poco/Message.h>
2019-07-16 08:36:50 +00:00
#include <Poco/Version.h>
2019-07-09 10:39:05 +00:00
#include <Common/CurrentThread.h>
2020-05-22 10:58:29 +00:00
/// TODO Remove this.
using Poco::Logger;
2019-07-10 12:19:17 +00:00
using Poco::Message;
2019-07-08 11:41:54 +00:00
using DB::LogsLevel;
2019-07-09 10:39:05 +00:00
using DB::CurrentThread;
2019-07-08 11:41:54 +00:00
/// Logs a message to a specified logger with that level.
2020-05-23 16:42:16 +00:00
#define LOG_IMPL_FORMATTED(logger, priority, PRIORITY, ...) do \
2020-05-22 10:58:29 +00:00
{ \
const bool is_clients_log = (CurrentThread::getGroup() != nullptr) && \
(CurrentThread::getGroup()->client_logs_level >= (priority)); \
if ((logger)->is((PRIORITY)) || is_clients_log) \
{ \
2020-05-23 16:42:16 +00:00
std::string formatted_message = fmt::format(__VA_ARGS__); \
2020-05-22 10:58:29 +00:00
if (auto channel = (logger)->getChannel()) \
{ \
std::string file_function; \
file_function += __FILE__; \
file_function += "; "; \
file_function += __PRETTY_FUNCTION__; \
Message poco_message((logger)->name(), formatted_message, \
(PRIORITY), file_function.c_str(), __LINE__); \
channel->log(poco_message); \
} \
} \
} while (false)
2020-05-23 16:42:16 +00:00
#define LOG_TRACE_FORMATTED(logger, ...) LOG_IMPL_FORMATTED(logger, LogsLevel::trace, Message::PRIO_TRACE, __VA_ARGS__)
#define LOG_DEBUG_FORMATTED(logger, ...) LOG_IMPL_FORMATTED(logger, LogsLevel::debug, Message::PRIO_DEBUG, __VA_ARGS__)
#define LOG_INFO_FORMATTED(logger, ...) LOG_IMPL_FORMATTED(logger, LogsLevel::information, Message::PRIO_INFORMATION, __VA_ARGS__)
#define LOG_WARNING_FORMATTED(logger, ...) LOG_IMPL_FORMATTED(logger, LogsLevel::warning, Message::PRIO_WARNING, __VA_ARGS__)
#define LOG_ERROR_FORMATTED(logger, ...) LOG_IMPL_FORMATTED(logger, LogsLevel::error, Message::PRIO_ERROR, __VA_ARGS__)
#define LOG_FATAL_FORMATTED(logger, ...) LOG_IMPL_FORMATTED(logger, LogsLevel::error, Message::PRIO_FATAL, __VA_ARGS__)