2015-09-29 19:21:02 +00:00
|
|
|
#pragma once
|
2017-02-02 14:19:13 +00:00
|
|
|
|
|
|
|
/// Macros for convenient usage of Poco logger.
|
2015-09-29 19:21:02 +00:00
|
|
|
|
2020-05-22 10:58:29 +00:00
|
|
|
#include <fmt/format.h>
|
2015-09-29 19:21:02 +00:00
|
|
|
#include <Poco/Logger.h>
|
2019-07-10 12:19:17 +00:00
|
|
|
#include <Poco/Message.h>
|
2019-07-09 10:39:05 +00:00
|
|
|
#include <Common/CurrentThread.h>
|
2023-04-22 12:55:25 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
2023-01-16 22:20:33 +00:00
|
|
|
#include <Common/LoggingFormatStringHelpers.h>
|
|
|
|
|
|
|
|
namespace Poco { class Logger; }
|
2015-09-29 19:21:02 +00:00
|
|
|
|
2023-01-13 19:34:31 +00:00
|
|
|
|
|
|
|
#define LogToStr(x, y) std::make_unique<LogToStrImpl>(x, y)
|
2023-03-13 23:23:36 +00:00
|
|
|
#define LogFrequencyLimiter(x, y) std::make_unique<LogFrequencyLimiterIml>(x, y)
|
2015-09-29 19:21:02 +00:00
|
|
|
|
2023-06-23 21:02:30 +00:00
|
|
|
using LogSeriesLimiterPtr = std::shared_ptr<LogSeriesLimiter>;
|
|
|
|
|
2020-05-29 00:09:12 +00:00
|
|
|
namespace
|
|
|
|
{
|
2023-05-05 08:54:11 +00:00
|
|
|
[[maybe_unused]] const ::Poco::Logger * getLogger(const ::Poco::Logger * logger) { return logger; }
|
|
|
|
[[maybe_unused]] const ::Poco::Logger * getLogger(const std::atomic<::Poco::Logger *> & logger) { return logger.load(); }
|
|
|
|
[[maybe_unused]] std::unique_ptr<LogToStrImpl> getLogger(std::unique_ptr<LogToStrImpl> && logger) { return logger; }
|
|
|
|
[[maybe_unused]] std::unique_ptr<LogFrequencyLimiterIml> getLogger(std::unique_ptr<LogFrequencyLimiterIml> && logger) { return logger; }
|
2023-06-23 21:02:30 +00:00
|
|
|
[[maybe_unused]] LogSeriesLimiterPtr getLogger(LogSeriesLimiterPtr & logger) { return logger; }
|
2022-07-14 13:00:11 +00:00
|
|
|
}
|
2020-05-29 00:09:12 +00:00
|
|
|
|
2022-12-23 18:40:29 +00:00
|
|
|
#define LOG_IMPL_FIRST_ARG(X, ...) X
|
2022-12-23 14:06:30 +00:00
|
|
|
|
2015-09-29 19:21:02 +00:00
|
|
|
/// Logs a message to a specified logger with that level.
|
2020-05-29 00:09:12 +00:00
|
|
|
/// If more than one argument is provided,
|
2023-04-22 12:55:25 +00:00
|
|
|
/// the first argument is interpreted as a template with {}-substitutions
|
|
|
|
/// and the latter arguments are treated as values to substitute.
|
|
|
|
/// If only one argument is provided, it is treated as a message without substitutions.
|
2015-09-29 19:21:02 +00:00
|
|
|
|
2020-05-29 00:09:12 +00:00
|
|
|
#define LOG_IMPL(logger, priority, PRIORITY, ...) do \
|
2020-05-22 10:58:29 +00:00
|
|
|
{ \
|
2022-07-14 13:00:11 +00:00
|
|
|
auto _logger = ::getLogger(logger); \
|
|
|
|
const bool _is_clients_log = (DB::CurrentThread::getGroup() != nullptr) && \
|
2023-04-22 12:55:25 +00:00
|
|
|
(DB::CurrentThread::get().getClientLogsLevel() >= (priority)); \
|
2023-01-13 19:34:31 +00:00
|
|
|
if (_is_clients_log || _logger->is((PRIORITY))) \
|
2020-05-22 10:58:29 +00:00
|
|
|
{ \
|
2020-05-29 00:09:12 +00:00
|
|
|
std::string formatted_message = numArgs(__VA_ARGS__) > 1 ? fmt::format(__VA_ARGS__) : firstArg(__VA_ARGS__); \
|
2023-01-23 22:16:16 +00:00
|
|
|
formatStringCheckArgsNum(__VA_ARGS__); \
|
2022-07-14 13:00:11 +00:00
|
|
|
if (auto _channel = _logger->getChannel()) \
|
2020-05-22 10:58:29 +00:00
|
|
|
{ \
|
|
|
|
std::string file_function; \
|
|
|
|
file_function += __FILE__; \
|
|
|
|
file_function += "; "; \
|
|
|
|
file_function += __PRETTY_FUNCTION__; \
|
2022-07-14 13:00:11 +00:00
|
|
|
Poco::Message poco_message(_logger->name(), formatted_message, \
|
2023-04-22 12:55:25 +00:00
|
|
|
(PRIORITY), file_function.c_str(), __LINE__, tryGetStaticFormatString(LOG_IMPL_FIRST_ARG(__VA_ARGS__))); \
|
2022-12-23 18:40:29 +00:00
|
|
|
_channel->log(poco_message); \
|
|
|
|
} \
|
2023-04-22 12:55:25 +00:00
|
|
|
ProfileEvents::incrementForLogMessage(PRIORITY); \
|
2022-12-23 18:40:29 +00:00
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
2020-05-22 10:58:29 +00:00
|
|
|
|
2021-09-03 10:07:40 +00:00
|
|
|
#define LOG_TEST(logger, ...) LOG_IMPL(logger, DB::LogsLevel::test, Poco::Message::PRIO_TEST, __VA_ARGS__)
|
2020-05-30 21:57:37 +00:00
|
|
|
#define LOG_TRACE(logger, ...) LOG_IMPL(logger, DB::LogsLevel::trace, Poco::Message::PRIO_TRACE, __VA_ARGS__)
|
|
|
|
#define LOG_DEBUG(logger, ...) LOG_IMPL(logger, DB::LogsLevel::debug, Poco::Message::PRIO_DEBUG, __VA_ARGS__)
|
|
|
|
#define LOG_INFO(logger, ...) LOG_IMPL(logger, DB::LogsLevel::information, Poco::Message::PRIO_INFORMATION, __VA_ARGS__)
|
|
|
|
#define LOG_WARNING(logger, ...) LOG_IMPL(logger, DB::LogsLevel::warning, Poco::Message::PRIO_WARNING, __VA_ARGS__)
|
|
|
|
#define LOG_ERROR(logger, ...) LOG_IMPL(logger, DB::LogsLevel::error, Poco::Message::PRIO_ERROR, __VA_ARGS__)
|
|
|
|
#define LOG_FATAL(logger, ...) LOG_IMPL(logger, DB::LogsLevel::error, Poco::Message::PRIO_FATAL, __VA_ARGS__)
|