fmt-style logging, part 1

This commit is contained in:
Alexey Milovidov 2020-05-22 13:58:29 +03:00
parent 08c7277d2b
commit 35d79e1252
8 changed files with 68 additions and 14 deletions

3
.gitmodules vendored
View File

@ -157,3 +157,6 @@
[submodule "contrib/openldap"] [submodule "contrib/openldap"]
path = contrib/openldap path = contrib/openldap
url = https://github.com/openldap/openldap.git url = https://github.com/openldap/openldap.git
[submodule "contrib/fmtlib"]
path = contrib/fmtlib
url = https://github.com/fmtlib/fmt.git

View File

@ -79,6 +79,7 @@ target_link_libraries (common
Poco::Util Poco::Util
Poco::Foundation Poco::Foundation
replxx replxx
fmt
PRIVATE PRIVATE
cctz cctz

View File

@ -3,15 +3,14 @@
/// Macros for convenient usage of Poco logger. /// Macros for convenient usage of Poco logger.
#include <sstream> #include <sstream>
#include <fmt/format.h>
#include <Poco/Logger.h> #include <Poco/Logger.h>
#include <Poco/Message.h> #include <Poco/Message.h>
#include <Poco/Version.h> #include <Poco/Version.h>
#include <Common/CurrentThread.h> #include <Common/CurrentThread.h>
#ifndef QUERY_PREVIEW_LENGTH
#define QUERY_PREVIEW_LENGTH 160
#endif
/// TODO Remove this.
using Poco::Logger; using Poco::Logger;
using Poco::Message; using Poco::Message;
using DB::LogsLevel; using DB::LogsLevel;
@ -19,7 +18,7 @@ using DB::CurrentThread;
/// Logs a message to a specified logger with that level. /// Logs a message to a specified logger with that level.
#define LOG_SIMPLE(logger, message, priority, PRIORITY) do \ #define LOG_IMPL(logger, priority, PRIORITY, message) do \
{ \ { \
const bool is_clients_log = (CurrentThread::getGroup() != nullptr) && \ const bool is_clients_log = (CurrentThread::getGroup() != nullptr) && \
(CurrentThread::getGroup()->client_logs_level >= (priority)); \ (CurrentThread::getGroup()->client_logs_level >= (priority)); \
@ -41,10 +40,38 @@ using DB::CurrentThread;
} while (false) } while (false)
#define LOG_TRACE(logger, message) LOG_SIMPLE(logger, message, LogsLevel::trace, Message::PRIO_TRACE) #define LOG_TRACE(logger, message) LOG_IMPL(logger, LogsLevel::trace, Message::PRIO_TRACE, message)
#define LOG_DEBUG(logger, message) LOG_SIMPLE(logger, message, LogsLevel::debug, Message::PRIO_DEBUG) #define LOG_DEBUG(logger, message) LOG_IMPL(logger, LogsLevel::debug, Message::PRIO_DEBUG, message)
#define LOG_INFO(logger, message) LOG_SIMPLE(logger, message, LogsLevel::information, Message::PRIO_INFORMATION) #define LOG_INFO(logger, message) LOG_IMPL(logger, LogsLevel::information, Message::PRIO_INFORMATION, message)
#define LOG_WARNING(logger, message) LOG_SIMPLE(logger, message, LogsLevel::warning, Message::PRIO_WARNING) #define LOG_WARNING(logger, message) LOG_IMPL(logger, LogsLevel::warning, Message::PRIO_WARNING, message)
#define LOG_ERROR(logger, message) LOG_SIMPLE(logger, message, LogsLevel::error, Message::PRIO_ERROR) #define LOG_ERROR(logger, message) LOG_IMPL(logger, LogsLevel::error, Message::PRIO_ERROR, message)
#define LOG_FATAL(logger, message) LOG_SIMPLE(logger, message, LogsLevel::error, Message::PRIO_FATAL) #define LOG_FATAL(logger, message) LOG_IMPL(logger, LogsLevel::error, Message::PRIO_FATAL, message)
#define LOG_IMPL_FORMATTED(logger, priority, PRIORITY, message, ...) do \
{ \
const bool is_clients_log = (CurrentThread::getGroup() != nullptr) && \
(CurrentThread::getGroup()->client_logs_level >= (priority)); \
if ((logger)->is((PRIORITY)) || is_clients_log) \
{ \
std::string formatted_message = fmt::format(message, __VA_ARGS__); \
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)
#define LOG_TRACE_FORMATTED(logger, message, ...) LOG_IMPL_FORMATTED(logger, LogsLevel::trace, Message::PRIO_TRACE, message, __VA_ARGS__)
#define LOG_DEBUG_FORMATTED(logger, message, ...) LOG_IMPL_FORMATTED(logger, LogsLevel::debug, Message::PRIO_DEBUG, message, __VA_ARGS__)
#define LOG_INFO_FORMATTED(logger, message, ...) LOG_IMPL_FORMATTED(logger, LogsLevel::information, Message::PRIO_INFORMATION, message, __VA_ARGS__)
#define LOG_WARNING_FORMATTED(logger, message, ...) LOG_IMPL_FORMATTED(logger, LogsLevel::warning, Message::PRIO_WARNING, message, __VA_ARGS__)
#define LOG_ERROR_FORMATTED(logger, message, ...) LOG_IMPL_FORMATTED(logger, LogsLevel::error, Message::PRIO_ERROR, message, __VA_ARGS__)
#define LOG_FATAL_FORMATTED(logger, message, ...) LOG_IMPL_FORMATTED(logger, LogsLevel::error, Message::PRIO_FATAL, message, __VA_ARGS__)

View File

@ -317,3 +317,5 @@ endif()
if (USE_FASTOPS) if (USE_FASTOPS)
add_subdirectory (fastops-cmake) add_subdirectory (fastops-cmake)
endif() endif()
add_subdirectory (fmtlib-cmake)

1
contrib/fmtlib vendored Submodule

@ -0,0 +1 @@
Subproject commit 297c3b2ed551a4989826fc8c4780bf533e964bd9

View File

@ -0,0 +1,20 @@
set (SRCS
../fmtlib/src/format.cc
../fmtlib/src/os.cc
../fmtlib/include/fmt/chrono.h
../fmtlib/include/fmt/color.h
../fmtlib/include/fmt/compile.h
../fmtlib/include/fmt/core.h
../fmtlib/include/fmt/format.h
../fmtlib/include/fmt/format-inl.h
../fmtlib/include/fmt/locale.h
../fmtlib/include/fmt/os.h
../fmtlib/include/fmt/ostream.h
../fmtlib/include/fmt/posix.h
../fmtlib/include/fmt/printf.h
../fmtlib/include/fmt/ranges.h
)
add_library(fmt ${SRCS})
target_include_directories(fmt SYSTEM PUBLIC ../fmtlib/include)

View File

@ -40,13 +40,13 @@ public:
void Log(Aws::Utils::Logging::LogLevel log_level, const char * tag, const char * format_str, ...) final // NOLINT void Log(Aws::Utils::Logging::LogLevel log_level, const char * tag, const char * format_str, ...) final // NOLINT
{ {
const auto & [level, prio] = convertLogLevel(log_level); const auto & [level, prio] = convertLogLevel(log_level);
LOG_SIMPLE(log, std::string(tag) + ": " + format_str, level, prio); LOG_IMPL(log, level, prio, std::string(tag) + ": " + format_str);
} }
void LogStream(Aws::Utils::Logging::LogLevel log_level, const char * tag, const Aws::OStringStream & message_stream) final void LogStream(Aws::Utils::Logging::LogLevel log_level, const char * tag, const Aws::OStringStream & message_stream) final
{ {
const auto & [level, prio] = convertLogLevel(log_level); const auto & [level, prio] = convertLogLevel(log_level);
LOG_SIMPLE(log, std::string(tag) + ": " + message_stream.str(), level, prio); LOG_IMPL(log, level, prio, std::string(tag) + ": " + message_stream.str());
} }
void Flush() final {} void Flush() final {}

View File

@ -325,7 +325,7 @@ void StorageKafka::updateConfiguration(cppkafka::Configuration & conf)
conf.set_log_callback([this](cppkafka::KafkaHandleBase &, int level, const std::string & /* facility */, const std::string & message) conf.set_log_callback([this](cppkafka::KafkaHandleBase &, int level, const std::string & /* facility */, const std::string & message)
{ {
auto [poco_level, client_logs_level] = parseSyslogLevel(level); auto [poco_level, client_logs_level] = parseSyslogLevel(level);
LOG_SIMPLE(log, message, client_logs_level, poco_level); LOG_IMPL(log, client_logs_level, poco_level, message);
}); });
// Configure interceptor to change thread name // Configure interceptor to change thread name
@ -334,7 +334,7 @@ void StorageKafka::updateConfiguration(cppkafka::Configuration & conf)
// XXX: rdkafka uses pthread_set_name_np(), but glibc-compatibliity overrides it to noop. // XXX: rdkafka uses pthread_set_name_np(), but glibc-compatibliity overrides it to noop.
{ {
// This should be safe, since we wait the rdkafka object anyway. // This should be safe, since we wait the rdkafka object anyway.
void * self = reinterpret_cast<void *>(this); void * self = static_cast<void *>(this);
int status; int status;