ClickHouse/libs/libcommon/include/common/logger_useful.h

114 lines
4.0 KiB
C++
Raw Normal View History

#pragma once
/// Macros for convenient usage of Poco logger.
#include <sstream>
2019-07-08 16:11:38 +00:00
#include <atomic>
#include <Poco/Logger.h>
2019-07-10 12:19:17 +00:00
#include <Poco/Message.h>
2019-07-08 16:11:38 +00:00
#include <Core/SettingsCommon.h>
2019-07-09 10:39:05 +00:00
#include <Common/CurrentThread.h>
#ifndef QUERY_PREVIEW_LENGTH
#define QUERY_PREVIEW_LENGTH 160
#endif
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.
2019-07-15 15:25:32 +00:00
#if defined(POCO_CLICKHOUSE_PATCH)
#define LOG_TRACE(logger, message) do { \
if ((logger)->trace()) {\
std::stringstream oss_internal_rare; \
oss_internal_rare << message; \
(logger)->trace(oss_internal_rare.str());}} while(false)
#define LOG_DEBUG(logger, message) do { \
if ((logger)->debug()) {\
std::stringstream oss_internal_rare; \
oss_internal_rare << message; \
(logger)->debug(oss_internal_rare.str());}} while(false)
#define LOG_INFO(logger, message) do { \
if ((logger)->information()) {\
std::stringstream oss_internal_rare; \
oss_internal_rare << message; \
(logger)->information(oss_internal_rare.str());}} while(false)
#define LOG_WARNING(logger, message) do { \
if ((logger)->warning()) {\
std::stringstream oss_internal_rare; \
oss_internal_rare << message; \
(logger)->warning(oss_internal_rare.str());}} while(false)
#define LOG_ERROR(logger, message) do { \
if ((logger)->error()) {\
std::stringstream oss_internal_rare; \
oss_internal_rare << message; \
(logger)->error(oss_internal_rare.str());}} while(false)
#else
#define LOG_TRACE(logger, message) do { \
2019-07-10 12:19:17 +00:00
const bool is_clients_log = (CurrentThread::getGroup() != nullptr) && (CurrentThread::getGroup()->client_logs_level >= LogsLevel::trace); \
if ((logger)->trace() || is_clients_log) {\
std::stringstream oss_internal_rare; \
oss_internal_rare << message; \
2019-07-10 12:19:17 +00:00
if (is_clients_log) {\
(logger)->force_log(oss_internal_rare.str(), Message::PRIO_TRACE); \
} else { \
(logger)->trace(oss_internal_rare.str()); \
}}} while(false)
#define LOG_DEBUG(logger, message) do { \
2019-07-10 12:19:17 +00:00
const bool is_clients_log = (CurrentThread::getGroup() != nullptr) && (CurrentThread::getGroup()->client_logs_level >= LogsLevel::debug); \
if ((logger)->debug() || is_clients_log) {\
std::stringstream oss_internal_rare; \
oss_internal_rare << message; \
2019-07-10 12:19:17 +00:00
if (is_clients_log) {\
(logger)->force_log(oss_internal_rare.str(), Message::PRIO_DEBUG); \
} else { \
(logger)->debug(oss_internal_rare.str()); \
}}} while(false)
#define LOG_INFO(logger, message) do { \
2019-07-10 12:19:17 +00:00
const bool is_clients_log = (CurrentThread::getGroup() != nullptr) && (CurrentThread::getGroup()->client_logs_level >= LogsLevel::information); \
if ((logger)->information() || is_clients_log) {\
std::stringstream oss_internal_rare; \
oss_internal_rare << message; \
2019-07-10 12:19:17 +00:00
if (is_clients_log) {\
(logger)->force_log(oss_internal_rare.str(), Message::PRIO_INFORMATION); \
} else { \
(logger)->information(oss_internal_rare.str()); \
}}} while(false)
#define LOG_WARNING(logger, message) do { \
2019-07-10 12:19:17 +00:00
const bool is_clients_log = (CurrentThread::getGroup() != nullptr) && (CurrentThread::getGroup()->client_logs_level >= LogsLevel::warning); \
if ((logger)->warning() || is_clients_log) {\
std::stringstream oss_internal_rare; \
oss_internal_rare << message; \
2019-07-10 12:19:17 +00:00
if (is_clients_log) {\
(logger)->force_log(oss_internal_rare.str(), Message::PRIO_WARNING); \
} else { \
(logger)->warning(oss_internal_rare.str()); \
}}} while(false)
#define LOG_ERROR(logger, message) do { \
2019-07-10 12:19:17 +00:00
const bool is_clients_log = (CurrentThread::getGroup() != nullptr) && (CurrentThread::getGroup()->client_logs_level >= LogsLevel::error); \
if ((logger)->error() || is_clients_log) {\
std::stringstream oss_internal_rare; \
oss_internal_rare << message; \
2019-07-10 12:19:17 +00:00
if (is_clients_log) {\
(logger)->force_log(oss_internal_rare.str(), Message::PRIO_ERROR); \
} else { \
(logger)->error(oss_internal_rare.str()); \
}}} while(false)
2019-07-15 15:25:32 +00:00
#endif