mirror of
https://github.com/gabime/spdlog.git
synced 2026-04-10 11:34:29 +08:00
clang format
This commit is contained in:
@@ -52,7 +52,7 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg
|
||||
// before color range
|
||||
print_range_(formatted, 0, msg.color_range_start);
|
||||
// in color range
|
||||
print_ccode_(colors_[static_cast<size_t>(msg.level)]);
|
||||
print_ccode_(colors_.at(static_cast<size_t>(msg.level)));
|
||||
print_range_(formatted, msg.color_range_start, msg.color_range_end);
|
||||
print_ccode_(reset);
|
||||
// after color range
|
||||
|
||||
@@ -50,12 +50,12 @@ struct daily_filename_format_calculator
|
||||
static filename_t calc_filename(const filename_t &file_path, const tm &now_tm)
|
||||
{
|
||||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||
std::wstringstream stream;
|
||||
std::wstringstream stream;
|
||||
#else
|
||||
std::stringstream stream;
|
||||
std::stringstream stream;
|
||||
#endif
|
||||
stream << std::put_time(&now_tm, file_path.c_str());
|
||||
return stream.str();
|
||||
stream << std::put_time(&now_tm, file_path.c_str());
|
||||
return stream.str();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -28,12 +28,12 @@ struct kafka_sink_config
|
||||
{
|
||||
std::string server_addr;
|
||||
std::string produce_topic;
|
||||
int32_t flush_timeout_ms = 1000;
|
||||
int32_t flush_timeout_ms = 1000;
|
||||
|
||||
kafka_sink_config(std::string addr, std::string topic, int flush_timeout_ms = 1000)
|
||||
: server_addr{std::move(addr)}
|
||||
,produce_topic{std::move(topic)}
|
||||
,flush_timeout_ms(flush_timeout_ms)
|
||||
: server_addr{std::move(addr)}
|
||||
, produce_topic{std::move(topic)}
|
||||
, flush_timeout_ms(flush_timeout_ms)
|
||||
{}
|
||||
};
|
||||
|
||||
@@ -42,8 +42,8 @@ class kafka_sink : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
kafka_sink(kafka_sink_config config)
|
||||
: config_{std::move(config)}
|
||||
{
|
||||
: config_{std::move(config)}
|
||||
{
|
||||
try
|
||||
{
|
||||
std::string errstr;
|
||||
@@ -75,7 +75,7 @@ public:
|
||||
{
|
||||
throw_spdlog_ex(fmt_lib::format("error create kafka instance: {}", e.what()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
~kafka_sink()
|
||||
{
|
||||
@@ -85,7 +85,7 @@ public:
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
producer_->produce(topic_.get(), 0, RdKafka::Producer::RK_MSG_COPY, (void *)msg.payload.data(), msg.payload.size(), NULL, NULL);
|
||||
producer_->produce(topic_.get(), 0, RdKafka::Producer::RK_MSG_COPY, (void *)msg.payload.data(), msg.payload.size(), NULL, NULL);
|
||||
}
|
||||
|
||||
void flush_() override
|
||||
@@ -104,7 +104,7 @@ private:
|
||||
using kafka_sink_mt = kafka_sink<std::mutex>;
|
||||
using kafka_sink_st = kafka_sink<spdlog::details::null_mutex>;
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace sinks
|
||||
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> kafka_logger_mt(const std::string &logger_name, spdlog::sinks::kafka_sink_config config)
|
||||
@@ -121,13 +121,13 @@ inline std::shared_ptr<logger> kafka_logger_st(const std::string &logger_name, s
|
||||
template<typename Factory = spdlog::async_factory>
|
||||
inline std::shared_ptr<spdlog::logger> kafka_logger_async_mt(std::string logger_name, spdlog::sinks::kafka_sink_config config)
|
||||
{
|
||||
return Factory::template create<sinks::kafka_sink_mt>(logger_name, config);
|
||||
return Factory::template create<sinks::kafka_sink_mt>(logger_name, config);
|
||||
}
|
||||
|
||||
template<typename Factory = spdlog::async_factory>
|
||||
inline std::shared_ptr<spdlog::logger> kafka_logger_async_st(std::string logger_name, spdlog::sinks::kafka_sink_config config)
|
||||
{
|
||||
return Factory::template create<sinks::kafka_sink_st>(logger_name, config);
|
||||
return Factory::template create<sinks::kafka_sink_st>(logger_name, config);
|
||||
}
|
||||
|
||||
} // namespace spdlog
|
||||
} // namespace spdlog
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
||||
# include <spdlog/details/null_mutex.h>
|
||||
@@ -16,11 +15,11 @@
|
||||
# include <string>
|
||||
|
||||
// Avoid including windows.h (https://stackoverflow.com/a/30741042)
|
||||
#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
|
||||
# if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
|
||||
extern "C" __declspec(dllimport) void __stdcall OutputDebugStringW(const wchar_t *lpOutputString);
|
||||
#else
|
||||
# else
|
||||
extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char *lpOutputString);
|
||||
#endif
|
||||
# endif
|
||||
extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent();
|
||||
|
||||
namespace spdlog {
|
||||
@@ -46,13 +45,13 @@ protected:
|
||||
memory_buf_t formatted;
|
||||
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||
formatted.push_back('\0'); // add a null terminator for OutputDebugString
|
||||
#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
|
||||
# if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
|
||||
wmemory_buf_t wformatted;
|
||||
details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), wformatted);
|
||||
OutputDebugStringW(wformatted.data());
|
||||
#else
|
||||
# else
|
||||
OutputDebugStringA(formatted.data());
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
void flush_() override {}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
// auto edit = new QTextEdit();
|
||||
// setCentralWidget(edit);
|
||||
// auto logger = spdlog::qt_color_logger_mt("my_logger", my_edit_widget);
|
||||
|
||||
//
|
||||
// Warning: those sinks won't be notified if the target widget is destroyed.
|
||||
// If the widget's lifetime can be shorter than the logger's one, you should provide some permanent QObject,
|
||||
// and then use a standard signal/slot.
|
||||
@@ -72,7 +72,6 @@ private:
|
||||
QMetaMethod meta_method_;
|
||||
};
|
||||
|
||||
|
||||
// QT color sink to QTextEdit.
|
||||
// Color location is determined by the sink log pattern like in the rest of spdlog sinks.
|
||||
// Colors can be modified if needed using sink->set_color(level, qtTextCharFormat).
|
||||
@@ -81,104 +80,104 @@ template<typename Mutex>
|
||||
class qt_color_sink : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
qt_color_sink(QTextEdit *qt_text_edit) : qt_text_edit_(qt_text_edit)
|
||||
qt_color_sink(QTextEdit *qt_text_edit)
|
||||
: qt_text_edit_(qt_text_edit)
|
||||
{
|
||||
if (!qt_text_edit_)
|
||||
{
|
||||
if(!qt_text_edit_)
|
||||
{
|
||||
throw_spdlog_ex("qt_color_text_sink: text_edit is null");
|
||||
}
|
||||
default_color_ = qt_text_edit_->currentCharFormat();
|
||||
// set colors
|
||||
QTextCharFormat format;
|
||||
// trace
|
||||
format.setForeground(Qt::gray);
|
||||
colors_.at(level::trace) = format;
|
||||
// debug
|
||||
format.setForeground(Qt::cyan);
|
||||
colors_.at(level::debug) = format;
|
||||
// info
|
||||
format.setForeground(Qt::green);
|
||||
colors_.at(level::info) = format;
|
||||
// warn
|
||||
format.setForeground(Qt::yellow);
|
||||
colors_.at(level::warn) = format;
|
||||
// err
|
||||
format.setForeground(Qt::red);
|
||||
colors_.at(level::err) = format;
|
||||
// critical
|
||||
format.setForeground(Qt::white);
|
||||
format.setBackground(Qt::red);
|
||||
colors_.at(level::critical) = format;
|
||||
throw_spdlog_ex("qt_color_text_sink: text_edit is null");
|
||||
}
|
||||
default_color_ = qt_text_edit_->currentCharFormat();
|
||||
// set colors
|
||||
QTextCharFormat format;
|
||||
// trace
|
||||
format.setForeground(Qt::gray);
|
||||
colors_.at(level::trace) = format;
|
||||
// debug
|
||||
format.setForeground(Qt::cyan);
|
||||
colors_.at(level::debug) = format;
|
||||
// info
|
||||
format.setForeground(Qt::green);
|
||||
colors_.at(level::info) = format;
|
||||
// warn
|
||||
format.setForeground(Qt::yellow);
|
||||
colors_.at(level::warn) = format;
|
||||
// err
|
||||
format.setForeground(Qt::red);
|
||||
colors_.at(level::err) = format;
|
||||
// critical
|
||||
format.setForeground(Qt::white);
|
||||
format.setBackground(Qt::red);
|
||||
colors_.at(level::critical) = format;
|
||||
}
|
||||
|
||||
~qt_color_sink()
|
||||
{
|
||||
flush_();
|
||||
}
|
||||
~qt_color_sink()
|
||||
{
|
||||
flush_();
|
||||
}
|
||||
|
||||
void set_default_color(QTextCharFormat format)
|
||||
{
|
||||
//std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
default_color_ = format;
|
||||
}
|
||||
void set_default_color(QTextCharFormat format)
|
||||
{
|
||||
// std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
default_color_ = format;
|
||||
}
|
||||
|
||||
void set_level_color(level::level_enum color_level, QTextCharFormat format)
|
||||
{
|
||||
//std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
colors_.at(static_cast<size_t>(color_level)) = format;
|
||||
}
|
||||
void set_level_color(level::level_enum color_level, QTextCharFormat format)
|
||||
{
|
||||
// std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
colors_.at(static_cast<size_t>(color_level)) = format;
|
||||
}
|
||||
|
||||
QTextCharFormat& get_level_color(level::level_enum color_level)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
return colors_.at(static_cast<size_t>(color_level));
|
||||
}
|
||||
QTextCharFormat &get_level_color(level::level_enum color_level)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
return colors_.at(static_cast<size_t>(color_level));
|
||||
}
|
||||
|
||||
QTextCharFormat& get_default_color()
|
||||
{
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
return default_color_;
|
||||
}
|
||||
QTextCharFormat &get_default_color()
|
||||
{
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
return default_color_;
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
memory_buf_t formatted;
|
||||
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||
string_view_t str = string_view_t(formatted.data(), formatted.size());
|
||||
|
||||
// apply the color to the color range in the formatted message.
|
||||
auto payload = QString::fromLatin1(str.data(), static_cast<int>(str.size()));
|
||||
if (msg.color_range_end > msg.color_range_start)
|
||||
{
|
||||
memory_buf_t formatted;
|
||||
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||
string_view_t str = string_view_t(formatted.data(), formatted.size());
|
||||
QTextCursor cursor(qt_text_edit_->document());
|
||||
cursor.movePosition(QTextCursor::End);
|
||||
|
||||
// apply the color to the color range in the formatted message.
|
||||
auto payload = QString::fromLatin1(str.data(), static_cast<int>(str.size()));
|
||||
if(msg.color_range_end > msg.color_range_start)
|
||||
{
|
||||
QTextCursor cursor(qt_text_edit_->document());
|
||||
cursor.movePosition(QTextCursor::End);
|
||||
// insert the text before the color range
|
||||
cursor.setCharFormat(default_color_);
|
||||
cursor.insertText(payload.left(msg.color_range_start));
|
||||
|
||||
// insert the text before the color range
|
||||
cursor.setCharFormat(default_color_);
|
||||
cursor.insertText(payload.left(msg.color_range_start));
|
||||
// insert the colorized text
|
||||
auto color = colors_.at(static_cast<size_t>(msg.level));
|
||||
cursor.setCharFormat(color);
|
||||
cursor.insertText(payload.mid(msg.color_range_start, msg.color_range_end - msg.color_range_start));
|
||||
|
||||
// insert the colorized text
|
||||
auto color = colors_.at(static_cast<size_t>(msg.level));
|
||||
cursor.setCharFormat(color);
|
||||
cursor.insertText(payload.mid(msg.color_range_start, msg.color_range_end - msg.color_range_start));
|
||||
|
||||
// insert the text after the color range with default format
|
||||
cursor.setCharFormat(default_color_);
|
||||
cursor.insertText(payload.mid(msg.color_range_end));
|
||||
}
|
||||
else // no color range
|
||||
{
|
||||
qt_text_edit_->append(payload.trimmed());
|
||||
}
|
||||
// insert the text after the color range with default format
|
||||
cursor.setCharFormat(default_color_);
|
||||
cursor.insertText(payload.mid(msg.color_range_end));
|
||||
}
|
||||
else // no color range
|
||||
{
|
||||
qt_text_edit_->append(payload.trimmed());
|
||||
}
|
||||
}
|
||||
|
||||
void flush_() override {}
|
||||
QTextEdit *qt_text_edit_;
|
||||
void flush_() override {}
|
||||
QTextEdit *qt_text_edit_;
|
||||
QTextCharFormat default_color_;
|
||||
std::array<QTextCharFormat, level::n_levels> colors_;
|
||||
|
||||
};
|
||||
std::array<QTextCharFormat, level::n_levels> colors_;
|
||||
};
|
||||
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include <mutex>
|
||||
@@ -194,13 +193,15 @@ using qt_color_sink_st = qt_color_sink<spdlog::details::null_mutex>;
|
||||
|
||||
// create logger using QTextEdit object
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> qt_logger_mt(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append(QString)")
|
||||
inline std::shared_ptr<logger> qt_logger_mt(
|
||||
const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append(QString)")
|
||||
{
|
||||
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
|
||||
}
|
||||
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append(QString)")
|
||||
inline std::shared_ptr<logger> qt_logger_st(
|
||||
const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append(QString)")
|
||||
{
|
||||
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &m
|
||||
memory_buf_t formatted;
|
||||
formatter_->format(msg, formatted);
|
||||
::fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
|
||||
#endif // WIN32
|
||||
#endif // WIN32
|
||||
::fflush(file_); // flush every line to terminal
|
||||
}
|
||||
|
||||
|
||||
@@ -241,12 +241,12 @@ protected:
|
||||
details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), buf);
|
||||
|
||||
LPCWSTR lp_wstr = buf.data();
|
||||
succeeded = static_cast<bool>(::ReportEventW(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
|
||||
current_user_sid_.as_sid(), 1, 0, &lp_wstr, nullptr));
|
||||
succeeded = static_cast<bool>(::ReportEventW(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg),
|
||||
event_id_, current_user_sid_.as_sid(), 1, 0, &lp_wstr, nullptr));
|
||||
#else
|
||||
LPCSTR lp_str = formatted.data();
|
||||
succeeded = static_cast<bool>(::ReportEventA(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
|
||||
current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr));
|
||||
succeeded = static_cast<bool>(::ReportEventA(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg),
|
||||
event_id_, current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr));
|
||||
#endif
|
||||
|
||||
if (!succeeded)
|
||||
|
||||
Reference in New Issue
Block a user