Updated clang format to google style

This commit is contained in:
gabime
2023-09-25 02:35:55 +03:00
parent 66de161355
commit 6de0ffa15f
86 changed files with 2164 additions and 3466 deletions

View File

@@ -3,17 +3,17 @@
#include <spdlog/sinks/ansicolor_sink.h>
#include <spdlog/pattern_formatter.h>
#include <spdlog/details/os.h>
#include <spdlog/pattern_formatter.h>
namespace spdlog {
namespace sinks {
template<typename ConsoleMutex>
template <typename ConsoleMutex>
ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, color_mode mode)
: target_file_(target_file)
, mutex_(ConsoleMutex::mutex())
, formatter_(std::make_unique<spdlog::pattern_formatter>())
: target_file_(target_file),
mutex_(ConsoleMutex::mutex()),
formatter_(std::make_unique<spdlog::pattern_formatter>())
{
set_color_mode(mode);
@@ -26,16 +26,14 @@ ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, color_mode mode)
colors_.at(level_to_number(level::off)) = to_string_(reset);
}
template<typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::set_color(level color_level, string_view_t color)
{
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::set_color(level color_level, string_view_t color) {
std::lock_guard<mutex_t> lock(mutex_);
colors_.at(level_to_number(color_level)) = to_string_(color);
}
template<typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
{
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg) {
// Wrap the originally formatted message in color codes.
// If color is not supported in the terminal, log as is instead.
std::lock_guard<mutex_t> lock(mutex_);
@@ -43,8 +41,7 @@ void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
msg.color_range_end = 0;
memory_buf_t formatted;
formatter_->format(msg, formatted);
if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
{
if (should_do_colors_ && msg.color_range_end > msg.color_range_start) {
// before color range
print_range_(formatted, 0, msg.color_range_start);
// in color range
@@ -53,46 +50,39 @@ void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
print_ccode_(reset);
// after color range
print_range_(formatted, msg.color_range_end, formatted.size());
}
else // no color
} else // no color
{
print_range_(formatted, 0, formatted.size());
}
fflush(target_file_);
}
template<typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::flush()
{
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::flush() {
std::lock_guard<mutex_t> lock(mutex_);
fflush(target_file_);
}
template<typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern)
{
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern) {
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
}
template<typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
{
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter);
}
template<typename ConsoleMutex>
bool ansicolor_sink<ConsoleMutex>::should_color()
{
template <typename ConsoleMutex>
bool ansicolor_sink<ConsoleMutex>::should_color() {
return should_do_colors_;
}
template<typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
{
switch (mode)
{
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode) {
switch (mode) {
case color_mode::always:
should_do_colors_ = true;
return;
@@ -107,35 +97,30 @@ void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
}
}
template<typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::print_ccode_(const string_view_t &color_code)
{
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::print_ccode_(const string_view_t &color_code) {
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
}
template<typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end)
{
template <typename ConsoleMutex>
void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) {
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
}
template<typename ConsoleMutex>
std::string ansicolor_sink<ConsoleMutex>::to_string_(const string_view_t &sv)
{
template <typename ConsoleMutex>
std::string ansicolor_sink<ConsoleMutex>::to_string_(const string_view_t &sv) {
return {sv.data(), sv.size()};
}
// ansicolor_stdout_sink
template<typename ConsoleMutex>
template <typename ConsoleMutex>
ansicolor_stdout_sink<ConsoleMutex>::ansicolor_stdout_sink(color_mode mode)
: ansicolor_sink<ConsoleMutex>(stdout, mode)
{}
: ansicolor_sink<ConsoleMutex>(stdout, mode) {}
// ansicolor_stderr_sink
template<typename ConsoleMutex>
template <typename ConsoleMutex>
ansicolor_stderr_sink<ConsoleMutex>::ansicolor_stderr_sink(color_mode mode)
: ansicolor_sink<ConsoleMutex>(stderr, mode)
{}
: ansicolor_sink<ConsoleMutex>(stderr, mode) {}
} // namespace sinks
} // namespace spdlog

View File

@@ -1,60 +1,52 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#include <spdlog/sinks/base_sink.h>
#include <spdlog/common.h>
#include <spdlog/pattern_formatter.h>
#include <spdlog/sinks/base_sink.h>
#include <memory>
#include <mutex>
template<typename Mutex>
template <typename Mutex>
spdlog::sinks::base_sink<Mutex>::base_sink()
: formatter_{std::make_unique<spdlog::pattern_formatter>()}
{}
: formatter_{std::make_unique<spdlog::pattern_formatter>()} {}
template<typename Mutex>
template <typename Mutex>
spdlog::sinks::base_sink<Mutex>::base_sink(std::unique_ptr<spdlog::formatter> formatter)
: formatter_{std::move(formatter)}
{}
: formatter_{std::move(formatter)} {}
template<typename Mutex>
void spdlog::sinks::base_sink<Mutex>::log(const details::log_msg &msg)
{
template <typename Mutex>
void spdlog::sinks::base_sink<Mutex>::log(const details::log_msg &msg) {
std::lock_guard<Mutex> lock(mutex_);
sink_it_(msg);
}
template<typename Mutex>
void spdlog::sinks::base_sink<Mutex>::flush()
{
template <typename Mutex>
void spdlog::sinks::base_sink<Mutex>::flush() {
std::lock_guard<Mutex> lock(mutex_);
flush_();
}
template<typename Mutex>
void spdlog::sinks::base_sink<Mutex>::set_pattern(const std::string &pattern)
{
template <typename Mutex>
void spdlog::sinks::base_sink<Mutex>::set_pattern(const std::string &pattern) {
std::lock_guard<Mutex> lock(mutex_);
set_pattern_(pattern);
}
template<typename Mutex>
void spdlog::sinks::base_sink<Mutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
{
template <typename Mutex>
void spdlog::sinks::base_sink<Mutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
std::lock_guard<Mutex> lock(mutex_);
set_formatter_(std::move(sink_formatter));
}
template<typename Mutex>
void spdlog::sinks::base_sink<Mutex>::set_pattern_(const std::string &pattern)
{
template <typename Mutex>
void spdlog::sinks::base_sink<Mutex>::set_pattern_(const std::string &pattern) {
set_formatter_(std::make_unique<spdlog::pattern_formatter>(pattern));
}
template<typename Mutex>
void spdlog::sinks::base_sink<Mutex>::set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter)
{
template <typename Mutex>
void spdlog::sinks::base_sink<Mutex>::set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) {
formatter_ = std::move(sink_formatter);
}

View File

@@ -9,30 +9,28 @@
namespace spdlog {
namespace sinks {
template<typename Mutex>
basic_file_sink<Mutex>::basic_file_sink(const filename_t &filename, bool truncate, const file_event_handlers &event_handlers)
: file_helper_{event_handlers}
{
template <typename Mutex>
basic_file_sink<Mutex>::basic_file_sink(const filename_t &filename,
bool truncate,
const file_event_handlers &event_handlers)
: file_helper_{event_handlers} {
file_helper_.open(filename, truncate);
}
template<typename Mutex>
const filename_t &basic_file_sink<Mutex>::filename() const
{
template <typename Mutex>
const filename_t &basic_file_sink<Mutex>::filename() const {
return file_helper_.filename();
}
template<typename Mutex>
void basic_file_sink<Mutex>::sink_it_(const details::log_msg &msg)
{
template <typename Mutex>
void basic_file_sink<Mutex>::sink_it_(const details::log_msg &msg) {
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted);
file_helper_.write(formatted);
}
template<typename Mutex>
void basic_file_sink<Mutex>::flush_()
{
template <typename Mutex>
void basic_file_sink<Mutex>::flush_() {
file_helper_.flush();
}

View File

@@ -16,27 +16,26 @@
namespace spdlog {
namespace sinks {
template<typename Mutex>
rotating_file_sink<Mutex>::rotating_file_sink(
filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open, const file_event_handlers &event_handlers)
: base_filename_(std::move(base_filename))
, max_size_(max_size)
, max_files_(max_files)
, file_helper_{event_handlers}
{
if (max_size == 0)
{
template <typename Mutex>
rotating_file_sink<Mutex>::rotating_file_sink(filename_t base_filename,
std::size_t max_size,
std::size_t max_files,
bool rotate_on_open,
const file_event_handlers &event_handlers)
: base_filename_(std::move(base_filename)),
max_size_(max_size),
max_files_(max_files),
file_helper_{event_handlers} {
if (max_size == 0) {
throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero");
}
if (max_files > 200000)
{
if (max_files > 200000) {
throw_spdlog_ex("rotating sink constructor: max_files arg cannot exceed 200000");
}
file_helper_.open(calc_filename(base_filename_, 0));
current_size_ = file_helper_.size(); // expensive. called only once
if (rotate_on_open && current_size_ > 0)
{
if (rotate_on_open && current_size_ > 0) {
rotate_();
current_size_ = 0;
}
@@ -44,11 +43,9 @@ rotating_file_sink<Mutex>::rotating_file_sink(
// calc filename according to index and file extension if exists.
// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
template<typename Mutex>
filename_t rotating_file_sink<Mutex>::calc_filename(const filename_t &filename, std::size_t index)
{
if (index == 0u)
{
template <typename Mutex>
filename_t rotating_file_sink<Mutex>::calc_filename(const filename_t &filename, std::size_t index) {
if (index == 0u) {
return filename;
}
@@ -57,16 +54,14 @@ filename_t rotating_file_sink<Mutex>::calc_filename(const filename_t &filename,
return fmt_lib::format(SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext);
}
template<typename Mutex>
filename_t rotating_file_sink<Mutex>::filename()
{
template <typename Mutex>
filename_t rotating_file_sink<Mutex>::filename() {
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
return file_helper_.filename();
}
template<typename Mutex>
void rotating_file_sink<Mutex>::sink_it_(const details::log_msg &msg)
{
template <typename Mutex>
void rotating_file_sink<Mutex>::sink_it_(const details::log_msg &msg) {
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted);
auto new_size = current_size_ + formatted.size();
@@ -74,11 +69,9 @@ void rotating_file_sink<Mutex>::sink_it_(const details::log_msg &msg)
// rotate if the new estimated file size exceeds max size.
// rotate only if the real size > 0 to better deal with full disk (see issue #2261).
// we only check the real size when new_size > max_size_ because it is relatively expensive.
if (new_size > max_size_)
{
if (new_size > max_size_) {
file_helper_.flush();
if (file_helper_.size() > 0)
{
if (file_helper_.size() > 0) {
rotate_();
new_size = formatted.size();
}
@@ -87,9 +80,8 @@ void rotating_file_sink<Mutex>::sink_it_(const details::log_msg &msg)
current_size_ = new_size;
}
template<typename Mutex>
void rotating_file_sink<Mutex>::flush_()
{
template <typename Mutex>
void rotating_file_sink<Mutex>::flush_() {
file_helper_.flush();
}
@@ -98,33 +90,30 @@ void rotating_file_sink<Mutex>::flush_()
// log.1.txt -> log.2.txt
// log.2.txt -> log.3.txt
// log.3.txt -> delete
template<typename Mutex>
void rotating_file_sink<Mutex>::rotate_()
{
template <typename Mutex>
void rotating_file_sink<Mutex>::rotate_() {
using details::os::filename_to_str;
using details::os::path_exists;
file_helper_.close();
for (auto i = max_files_; i > 0; --i)
{
for (auto i = max_files_; i > 0; --i) {
filename_t src = calc_filename(base_filename_, i - 1);
if (!path_exists(src))
{
if (!path_exists(src)) {
continue;
}
filename_t target = calc_filename(base_filename_, i);
if (!rename_file_(src, target))
{
if (!rename_file_(src, target)) {
// if failed try again after a small delay.
// this is a workaround to a windows issue, where very high rotation
// rates can cause the rename to fail with permission denied (because of antivirus?).
details::os::sleep_for_millis(100);
if (!rename_file_(src, target))
{
if (!rename_file_(src, target)) {
file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit!
current_size_ = 0;
throw_spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno);
throw_spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " +
filename_to_str(target),
errno);
}
}
}
@@ -133,9 +122,8 @@ void rotating_file_sink<Mutex>::rotate_()
// delete the target if exists, and rename the src file to target
// return true on success, false otherwise.
template<typename Mutex>
bool rotating_file_sink<Mutex>::rename_file_(const filename_t &src_filename, const filename_t &target_filename)
{
template <typename Mutex>
bool rotating_file_sink<Mutex>::rename_file_(const filename_t &src_filename, const filename_t &target_filename) {
// try to delete the target file in case it already exists.
(void)details::os::remove(target_filename);
return details::os::rename(src_filename, target_filename) == 0;

View File

@@ -5,17 +5,12 @@
#include <spdlog/common.h>
bool spdlog::sinks::sink::should_log(spdlog::level msg_level) const
{
bool spdlog::sinks::sink::should_log(spdlog::level msg_level) const {
return msg_level >= level_.load(std::memory_order_relaxed);
}
void spdlog::sinks::sink::set_level(level level)
{
level_.store(level, std::memory_order_relaxed);
}
void spdlog::sinks::sink::set_level(level level) { level_.store(level, std::memory_order_relaxed); }
spdlog::level spdlog::sinks::sink::log_level() const
{
spdlog::level spdlog::sinks::sink::log_level() const {
return static_cast<spdlog::level>(level_.load(std::memory_order_relaxed));
}

View File

@@ -1,60 +1,56 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/details/synchronous_factory.h>
#include <spdlog/async.h>
#include <spdlog/logger.h>
#include <spdlog/common.h>
#include <spdlog/details/synchronous_factory.h>
#include <spdlog/logger.h>
#include <spdlog/sinks/stdout_color_sinks.h>
namespace spdlog {
template<typename Factory>
std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name, color_mode mode)
{
template <typename Factory>
std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name, color_mode mode) {
return Factory::template create<sinks::stdout_color_sink_mt>(logger_name, mode);
}
template<typename Factory>
std::shared_ptr<logger> stdout_color_st(const std::string &logger_name, color_mode mode)
{
template <typename Factory>
std::shared_ptr<logger> stdout_color_st(const std::string &logger_name, color_mode mode) {
return Factory::template create<sinks::stdout_color_sink_st>(logger_name, mode);
}
template<typename Factory>
std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name, color_mode mode)
{
template <typename Factory>
std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name, color_mode mode) {
return Factory::template create<sinks::stderr_color_sink_mt>(logger_name, mode);
}
template<typename Factory>
std::shared_ptr<logger> stderr_color_st(const std::string &logger_name, color_mode mode)
{
template <typename Factory>
std::shared_ptr<logger> stderr_color_st(const std::string &logger_name, color_mode mode) {
return Factory::template create<sinks::stderr_color_sink_st>(logger_name, mode);
}
} // namespace spdlog
// template instantiations
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt<spdlog::synchronous_factory>(
const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stdout_color_mt<spdlog::synchronous_factory>(const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_color_st<spdlog::synchronous_factory>(
const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stdout_color_st<spdlog::synchronous_factory>(const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt<spdlog::synchronous_factory>(
const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stderr_color_mt<spdlog::synchronous_factory>(const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_color_st<spdlog::synchronous_factory>(
const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stderr_color_st<spdlog::synchronous_factory>(const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt<spdlog::async_factory>(
const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stdout_color_mt<spdlog::async_factory>(const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_color_st<spdlog::async_factory>(
const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stdout_color_st<spdlog::async_factory>(const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt<spdlog::async_factory>(
const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stderr_color_mt<spdlog::async_factory>(const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_color_st<spdlog::async_factory>(
const std::string &logger_name, color_mode mode);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stderr_color_st<spdlog::async_factory>(const std::string &logger_name, color_mode mode);

View File

@@ -1,33 +1,32 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#include <spdlog/sinks/stdout_sinks.h>
#include <spdlog/pattern_formatter.h>
#include <memory>
#include <spdlog/pattern_formatter.h>
#include <spdlog/sinks/stdout_sinks.h>
#ifdef _WIN32
// under windows using fwrite to non-binary stream results in \r\r\n (see issue #1675)
// so instead we use ::FileWrite
# include <spdlog/details/windows_include.h>
// under windows using fwrite to non-binary stream results in \r\r\n (see issue #1675)
// so instead we use ::FileWrite
#include <spdlog/details/windows_include.h>
# ifndef _USING_V110_SDK71_ // fileapi.h doesn't exist in winxp
# include <fileapi.h> // WriteFile (..)
# endif
#ifndef _USING_V110_SDK71_ // fileapi.h doesn't exist in winxp
#include <fileapi.h> // WriteFile (..)
#endif
# include <io.h> // _get_osfhandle(..)
# include <stdio.h> // _fileno(..)
#include <io.h> // _get_osfhandle(..)
#include <stdio.h> // _fileno(..)
#endif // WIN32
namespace spdlog {
namespace sinks {
template<typename ConsoleMutex>
template <typename ConsoleMutex>
stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
: mutex_(ConsoleMutex::mutex())
, file_(file)
, formatter_(std::make_unique<spdlog::pattern_formatter>())
{
: mutex_(ConsoleMutex::mutex()),
file_(file),
formatter_(std::make_unique<spdlog::pattern_formatter>()) {
#ifdef _WIN32
// get windows handle from the FILE* object
@@ -36,19 +35,16 @@ stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
// don't throw to support cases where no console is attached,
// and let the log method to do nothing if (handle_ == INVALID_HANDLE_VALUE).
// throw only if non stdout/stderr target is requested (probably regular file and not console).
if (handle_ == INVALID_HANDLE_VALUE && file != stdout && file != stderr)
{
if (handle_ == INVALID_HANDLE_VALUE && file != stdout && file != stderr) {
throw_spdlog_ex("spdlog::stdout_sink_base: _get_osfhandle() failed", errno);
}
#endif // WIN32
}
template<typename ConsoleMutex>
void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg)
{
template <typename ConsoleMutex>
void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg) {
#ifdef _WIN32
if (handle_ == INVALID_HANDLE_VALUE)
{
if (handle_ == INVALID_HANDLE_VALUE) {
return;
}
std::lock_guard<mutex_t> lock(mutex_);
@@ -57,8 +53,7 @@ void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg)
auto size = static_cast<DWORD>(formatted.size());
DWORD bytes_written = 0;
bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0;
if (!ok)
{
if (!ok) {
throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " + std::to_string(::GetLastError()));
}
#else
@@ -70,63 +65,54 @@ void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg)
::fflush(file_); // flush every line to terminal
}
template<typename ConsoleMutex>
void stdout_sink_base<ConsoleMutex>::flush()
{
template <typename ConsoleMutex>
void stdout_sink_base<ConsoleMutex>::flush() {
std::lock_guard<mutex_t> lock(mutex_);
fflush(file_);
}
template<typename ConsoleMutex>
void stdout_sink_base<ConsoleMutex>::set_pattern(const std::string &pattern)
{
template <typename ConsoleMutex>
void stdout_sink_base<ConsoleMutex>::set_pattern(const std::string &pattern) {
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
}
template<typename ConsoleMutex>
void stdout_sink_base<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
{
template <typename ConsoleMutex>
void stdout_sink_base<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter);
}
// stdout sink
template<typename ConsoleMutex>
template <typename ConsoleMutex>
stdout_sink<ConsoleMutex>::stdout_sink()
: stdout_sink_base<ConsoleMutex>(stdout)
{}
: stdout_sink_base<ConsoleMutex>(stdout) {}
// stderr sink
template<typename ConsoleMutex>
template <typename ConsoleMutex>
stderr_sink<ConsoleMutex>::stderr_sink()
: stdout_sink_base<ConsoleMutex>(stderr)
{}
: stdout_sink_base<ConsoleMutex>(stderr) {}
} // namespace sinks
// factory methods
template<typename Factory>
std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name)
{
template <typename Factory>
std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name) {
return Factory::template create<sinks::stdout_sink_mt>(logger_name);
}
template<typename Factory>
std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name)
{
template <typename Factory>
std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name) {
return Factory::template create<sinks::stdout_sink_st>(logger_name);
}
template<typename Factory>
std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name)
{
template <typename Factory>
std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name) {
return Factory::template create<sinks::stderr_sink_mt>(logger_name);
}
template<typename Factory>
std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name)
{
template <typename Factory>
std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name) {
return Factory::template create<sinks::stderr_sink_st>(logger_name);
}
} // namespace spdlog
@@ -141,15 +127,23 @@ template class SPDLOG_API spdlog::sinks::stderr_sink<spdlog::details::console_mu
template class SPDLOG_API spdlog::sinks::stderr_sink<spdlog::details::console_nullmutex>;
// template instantiations for stdout/stderr factory functions
#include <spdlog/details/synchronous_factory.h>
#include <spdlog/async.h>
#include <spdlog/details/synchronous_factory.h>
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt<spdlog::synchronous_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st<spdlog::synchronous_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt<spdlog::synchronous_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st<spdlog::synchronous_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stdout_logger_mt<spdlog::synchronous_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stdout_logger_st<spdlog::synchronous_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stderr_logger_mt<spdlog::synchronous_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stderr_logger_st<spdlog::synchronous_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt<spdlog::async_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st<spdlog::async_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt<spdlog::async_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st<spdlog::async_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stdout_logger_mt<spdlog::async_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stdout_logger_st<spdlog::async_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stderr_logger_mt<spdlog::async_factory>(const std::string &logger_name);
template SPDLOG_API std::shared_ptr<spdlog::logger>
spdlog::stderr_logger_st<spdlog::async_factory>(const std::string &logger_name);

View File

@@ -2,52 +2,49 @@
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#ifdef _WIN32
#include <spdlog/sinks/wincolor_sink.h>
#include <spdlog/details/windows_include.h>
#include <spdlog/common.h>
#include <spdlog/pattern_formatter.h>
#include <spdlog/common.h>
#include <spdlog/details/windows_include.h>
#include <spdlog/pattern_formatter.h>
#include <spdlog/sinks/wincolor_sink.h>
#include <wincon.h>
#include <wincon.h>
namespace spdlog {
namespace sinks {
template<typename ConsoleMutex>
template <typename ConsoleMutex>
wincolor_sink<ConsoleMutex>::wincolor_sink(void *out_handle, color_mode mode)
: out_handle_(out_handle)
, mutex_(ConsoleMutex::mutex())
, formatter_(std::make_unique<spdlog::pattern_formatter>())
{
: out_handle_(out_handle),
mutex_(ConsoleMutex::mutex()),
formatter_(std::make_unique<spdlog::pattern_formatter>()) {
set_color_mode_impl(mode);
// set level colors
colors_.at(level_to_number(level::trace)) = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white
colors_.at(level_to_number(level::debug)) = FOREGROUND_GREEN | FOREGROUND_BLUE; // cyan
colors_.at(level_to_number(level::info)) = FOREGROUND_GREEN; // green
colors_.at(level_to_number(level::warn)) = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; // intense yellow
colors_.at(level_to_number(level::err)) = FOREGROUND_RED | FOREGROUND_INTENSITY; // intense red
colors_.at(level_to_number(level::critical)) =
BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; // intense white on red background
colors_.at(level_to_number(level::trace)) = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white
colors_.at(level_to_number(level::debug)) = FOREGROUND_GREEN | FOREGROUND_BLUE; // cyan
colors_.at(level_to_number(level::info)) = FOREGROUND_GREEN; // green
colors_.at(level_to_number(level::warn)) =
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; // intense yellow
colors_.at(level_to_number(level::err)) = FOREGROUND_RED | FOREGROUND_INTENSITY; // intense red
colors_.at(level_to_number(level::critical)) = BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN |
FOREGROUND_BLUE |
FOREGROUND_INTENSITY; // intense white on red background
colors_.at(level_to_number(level::off)) = 0;
}
template<typename ConsoleMutex>
wincolor_sink<ConsoleMutex>::~wincolor_sink()
{
template <typename ConsoleMutex>
wincolor_sink<ConsoleMutex>::~wincolor_sink() {
this->flush();
}
// change the color for the given level
template<typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::set_color(level level, std::uint16_t color)
{
template <typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::set_color(level level, std::uint16_t color) {
std::lock_guard<mutex_t> lock(mutex_);
colors_[level_to_number(level)] = color;
}
template<typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
{
if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE)
{
template <typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg) {
if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE) {
return;
}
@@ -56,8 +53,7 @@ void wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
msg.color_range_end = 0;
memory_buf_t formatted;
formatter_->format(msg, formatted);
if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
{
if (should_do_colors_ && msg.color_range_end > msg.color_range_start) {
// before color range
print_range_(formatted, 0, msg.color_range_start);
// in color range
@@ -66,63 +62,52 @@ void wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
// reset to orig colors
::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), orig_attribs);
print_range_(formatted, msg.color_range_end, formatted.size());
}
else // print without colors if color range is invalid (or color is disabled)
} else // print without colors if color range is invalid (or color is disabled)
{
write_to_file_(formatted);
}
}
template<typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::flush()
{
template <typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::flush() {
// windows console always flushed?
}
template<typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern)
{
template <typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern) {
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
}
template<typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
{
template <typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter);
}
template<typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
{
template <typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::set_color_mode(color_mode mode) {
std::lock_guard<mutex_t> lock(mutex_);
set_color_mode_impl(mode);
}
template<typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::set_color_mode_impl(color_mode mode)
{
if (mode == color_mode::automatic)
{
template <typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::set_color_mode_impl(color_mode mode) {
if (mode == color_mode::automatic) {
// should do colors only if out_handle_ points to actual console.
DWORD console_mode;
bool in_console = ::GetConsoleMode(static_cast<HANDLE>(out_handle_), &console_mode) != 0;
should_do_colors_ = in_console;
}
else
{
} else {
should_do_colors_ = mode == color_mode::always ? true : false;
}
}
// set foreground color and return the orig console attributes (for resetting later)
template<typename ConsoleMutex>
std::uint16_t wincolor_sink<ConsoleMutex>::set_foreground_color_(std::uint16_t attribs)
{
template <typename ConsoleMutex>
std::uint16_t wincolor_sink<ConsoleMutex>::set_foreground_color_(std::uint16_t attribs) {
CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
if (!::GetConsoleScreenBufferInfo(static_cast<HANDLE>(out_handle_), &orig_buffer_info))
{
if (!::GetConsoleScreenBufferInfo(static_cast<HANDLE>(out_handle_), &orig_buffer_info)) {
// just return white if failed getting console info
return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
}
@@ -135,20 +120,18 @@ std::uint16_t wincolor_sink<ConsoleMutex>::set_foreground_color_(std::uint16_t a
}
// print a range of formatted message to console
template<typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end)
{
if (end > start)
{
template <typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) {
if (end > start) {
auto size = static_cast<DWORD>(end - start);
auto ignored = ::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start, size, nullptr, nullptr);
auto ignored =
::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start, size, nullptr, nullptr);
(void)(ignored);
}
}
template<typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::write_to_file_(const memory_buf_t &formatted)
{
template <typename ConsoleMutex>
void wincolor_sink<ConsoleMutex>::write_to_file_(const memory_buf_t &formatted) {
auto size = static_cast<DWORD>(formatted.size());
DWORD bytes_written = 0;
auto ignored = ::WriteFile(static_cast<HANDLE>(out_handle_), formatted.data(), size, &bytes_written, nullptr);
@@ -156,16 +139,14 @@ void wincolor_sink<ConsoleMutex>::write_to_file_(const memory_buf_t &formatted)
}
// wincolor_stdout_sink
template<typename ConsoleMutex>
template <typename ConsoleMutex>
wincolor_stdout_sink<ConsoleMutex>::wincolor_stdout_sink(color_mode mode)
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_OUTPUT_HANDLE), mode)
{}
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_OUTPUT_HANDLE), mode) {}
// wincolor_stderr_sink
template<typename ConsoleMutex>
template <typename ConsoleMutex>
wincolor_stderr_sink<ConsoleMutex>::wincolor_stderr_sink(color_mode mode)
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode)
{}
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode) {}
} // namespace sinks
} // namespace spdlog