mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 16:35:45 +08:00
clang-format
This commit is contained in:
parent
38888ba5b3
commit
78c833a09f
@ -26,13 +26,11 @@
|
||||
#include <windows.h>
|
||||
#endif //_WIN32
|
||||
|
||||
|
||||
#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
|
||||
#include <codecvt>
|
||||
#include <locale>
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef SPDLOG_COMPILED_LIB
|
||||
#undef SPDLOG_HEADER_ONLY
|
||||
#define SPDLOG_INLINE
|
||||
|
@ -6,7 +6,6 @@
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include <mutex>
|
||||
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
|
||||
|
@ -12,58 +12,54 @@
|
||||
|
||||
namespace spdlog {
|
||||
|
||||
// public methods
|
||||
SPDLOG_INLINE logger::logger(const logger &other):
|
||||
name_(other.name_),
|
||||
sinks_(other.sinks_),
|
||||
level_(other.level_.load(std::memory_order_relaxed)),
|
||||
flush_level_(other.flush_level_.load(std::memory_order_relaxed)),
|
||||
custom_err_handler_(other.custom_err_handler_)
|
||||
{
|
||||
}
|
||||
// public methods
|
||||
SPDLOG_INLINE logger::logger(const logger &other)
|
||||
: name_(other.name_)
|
||||
, sinks_(other.sinks_)
|
||||
, level_(other.level_.load(std::memory_order_relaxed))
|
||||
, flush_level_(other.flush_level_.load(std::memory_order_relaxed))
|
||||
, custom_err_handler_(other.custom_err_handler_)
|
||||
{}
|
||||
|
||||
SPDLOG_INLINE logger::logger(logger &&other):
|
||||
name_(std::move(other.name_)),
|
||||
sinks_(std::move(other.sinks_)),
|
||||
level_(other.level_.load(std::memory_order_relaxed)),
|
||||
flush_level_(other.flush_level_.load(std::memory_order_relaxed)),
|
||||
custom_err_handler_(std::move(other.custom_err_handler_))
|
||||
{
|
||||
}
|
||||
SPDLOG_INLINE logger::logger(logger &&other)
|
||||
: name_(std::move(other.name_))
|
||||
, sinks_(std::move(other.sinks_))
|
||||
, level_(other.level_.load(std::memory_order_relaxed))
|
||||
, flush_level_(other.flush_level_.load(std::memory_order_relaxed))
|
||||
, custom_err_handler_(std::move(other.custom_err_handler_))
|
||||
{}
|
||||
|
||||
|
||||
SPDLOG_INLINE logger &logger::operator=(logger other)
|
||||
{
|
||||
SPDLOG_INLINE logger &logger::operator=(logger other)
|
||||
{
|
||||
this->swap(other);
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void logger::swap(spdlog::logger &other)
|
||||
{
|
||||
SPDLOG_INLINE void logger::swap(spdlog::logger &other)
|
||||
{
|
||||
name_.swap(other.name_);
|
||||
sinks_.swap(other.sinks_);
|
||||
|
||||
//swap level_
|
||||
// swap level_
|
||||
auto tmp = other.level_.load();
|
||||
tmp = level_.exchange(tmp);
|
||||
other.level_.store(tmp);
|
||||
|
||||
//swap flush level_
|
||||
// swap flush level_
|
||||
tmp = other.flush_level_.load();
|
||||
tmp = flush_level_.exchange(tmp);
|
||||
other.flush_level_.store(tmp);
|
||||
|
||||
custom_err_handler_.swap(other.custom_err_handler_);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SPDLOG_INLINE void swap(logger &a, logger &b)
|
||||
{
|
||||
SPDLOG_INLINE void swap(logger &a, logger &b)
|
||||
{
|
||||
a.swap(b);
|
||||
}
|
||||
}
|
||||
|
||||
void logger::log(source_loc loc, level::level_enum lvl, const char *msg)
|
||||
{
|
||||
void logger::log(source_loc loc, level::level_enum lvl, const char *msg)
|
||||
{
|
||||
if (!should_log(lvl))
|
||||
{
|
||||
return;
|
||||
@ -82,42 +78,42 @@ namespace spdlog {
|
||||
{
|
||||
err_handler_("Unknown exception in logger");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void logger::log(level::level_enum lvl, const char *msg)
|
||||
{
|
||||
SPDLOG_INLINE void logger::log(level::level_enum lvl, const char *msg)
|
||||
{
|
||||
log(source_loc{}, lvl, msg);
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE bool logger::should_log(level::level_enum msg_level) const
|
||||
{
|
||||
SPDLOG_INLINE bool logger::should_log(level::level_enum msg_level) const
|
||||
{
|
||||
return msg_level >= level_.load(std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void logger::set_level(level::level_enum log_level)
|
||||
{
|
||||
SPDLOG_INLINE void logger::set_level(level::level_enum log_level)
|
||||
{
|
||||
level_.store(log_level);
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE level::level_enum logger::default_level()
|
||||
{
|
||||
SPDLOG_INLINE level::level_enum logger::default_level()
|
||||
{
|
||||
return static_cast<level::level_enum>(SPDLOG_ACTIVE_LEVEL);
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE level::level_enum logger::level() const
|
||||
{
|
||||
SPDLOG_INLINE level::level_enum logger::level() const
|
||||
{
|
||||
return static_cast<level::level_enum>(level_.load(std::memory_order_relaxed));
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE const std::string &logger::name() const
|
||||
{
|
||||
SPDLOG_INLINE const std::string &logger::name() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
}
|
||||
|
||||
// set formatting for the sinks in this logger.
|
||||
// each sink will get a seperate instance of the formatter object.
|
||||
SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
|
||||
{
|
||||
// set formatting for the sinks in this logger.
|
||||
// each sink will get a seperate instance of the formatter object.
|
||||
SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
|
||||
{
|
||||
for (auto it = sinks_.begin(); it != sinks_.end(); ++it)
|
||||
{
|
||||
if (std::next(it) == sinks_.end())
|
||||
@ -130,17 +126,17 @@ namespace spdlog {
|
||||
(*it)->set_formatter(f->clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type)
|
||||
{
|
||||
SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type)
|
||||
{
|
||||
auto new_formatter = details::make_unique<pattern_formatter>(std::move(pattern), time_type);
|
||||
set_formatter(std::move(new_formatter));
|
||||
}
|
||||
}
|
||||
|
||||
// flush functions
|
||||
SPDLOG_INLINE void logger::flush()
|
||||
{
|
||||
// flush functions
|
||||
SPDLOG_INLINE void logger::flush()
|
||||
{
|
||||
try
|
||||
{
|
||||
flush_();
|
||||
@ -153,46 +149,46 @@ namespace spdlog {
|
||||
{
|
||||
err_handler_("Unknown exception in logger");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void logger::flush_on(level::level_enum log_level)
|
||||
{
|
||||
SPDLOG_INLINE void logger::flush_on(level::level_enum log_level)
|
||||
{
|
||||
flush_level_.store(log_level);
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE level::level_enum logger::flush_level() const
|
||||
{
|
||||
SPDLOG_INLINE level::level_enum logger::flush_level() const
|
||||
{
|
||||
return static_cast<level::level_enum>(flush_level_.load(std::memory_order_relaxed));
|
||||
}
|
||||
}
|
||||
|
||||
// sinks
|
||||
SPDLOG_INLINE const std::vector<sink_ptr> &logger::sinks() const
|
||||
{
|
||||
SPDLOG_INLINE const std::vector<sink_ptr> &logger::sinks() const
|
||||
{
|
||||
return sinks_;
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE std::vector<sink_ptr> &logger::sinks()
|
||||
{
|
||||
SPDLOG_INLINE std::vector<sink_ptr> &logger::sinks()
|
||||
{
|
||||
return sinks_;
|
||||
}
|
||||
}
|
||||
|
||||
// error handler
|
||||
SPDLOG_INLINE void logger::set_error_handler(err_handler handler)
|
||||
{
|
||||
// error handler
|
||||
SPDLOG_INLINE void logger::set_error_handler(err_handler handler)
|
||||
{
|
||||
custom_err_handler_ = handler;
|
||||
}
|
||||
}
|
||||
|
||||
// create new logger with same sinks and configuration.
|
||||
SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name)
|
||||
{
|
||||
// create new logger with same sinks and configuration.
|
||||
SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name)
|
||||
{
|
||||
auto cloned = std::make_shared<logger>(*this);
|
||||
cloned->name_ = std::move(logger_name);
|
||||
return cloned;
|
||||
}
|
||||
}
|
||||
|
||||
// protected methods
|
||||
SPDLOG_INLINE void logger::sink_it_(details::log_msg &msg)
|
||||
{
|
||||
// protected methods
|
||||
SPDLOG_INLINE void logger::sink_it_(details::log_msg &msg)
|
||||
{
|
||||
for (auto &sink : sinks_)
|
||||
{
|
||||
if (sink->should_log(msg.level))
|
||||
@ -205,24 +201,24 @@ namespace spdlog {
|
||||
{
|
||||
flush_();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void logger::flush_()
|
||||
{
|
||||
SPDLOG_INLINE void logger::flush_()
|
||||
{
|
||||
for (auto &sink : sinks_)
|
||||
{
|
||||
sink->flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg)
|
||||
{
|
||||
SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg)
|
||||
{
|
||||
auto flush_level = flush_level_.load(std::memory_order_relaxed);
|
||||
return (msg.level >= flush_level) && (msg.level != level::off);
|
||||
}
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
|
||||
{
|
||||
SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
|
||||
{
|
||||
if (custom_err_handler_)
|
||||
{
|
||||
custom_err_handler_(msg);
|
||||
@ -234,5 +230,5 @@ namespace spdlog {
|
||||
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
||||
fmt::print(stderr, "[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, name(), msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace spdlog
|
@ -346,11 +346,10 @@ protected:
|
||||
void err_handler_(const std::string &msg);
|
||||
};
|
||||
|
||||
void swap(logger& a, logger& b);
|
||||
void swap(logger &a, logger &b);
|
||||
|
||||
} // namespace spdlog
|
||||
|
||||
|
||||
#ifdef SPDLOG_HEADER_ONLY
|
||||
#include "logger-inl.h"
|
||||
#endif
|
||||
|
@ -25,7 +25,7 @@ class ansicolor_sink : public sink
|
||||
{
|
||||
public:
|
||||
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||
ansicolor_sink(FILE* target_file, color_mode mode);
|
||||
ansicolor_sink(FILE *target_file, color_mode mode);
|
||||
~ansicolor_sink() override = default;
|
||||
|
||||
ansicolor_sink(const ansicolor_sink &other) = delete;
|
||||
@ -77,7 +77,6 @@ private:
|
||||
void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end);
|
||||
};
|
||||
|
||||
|
||||
template<typename ConsoleMutex>
|
||||
class ansicolor_stdout_sink : public ansicolor_sink<ConsoleMutex>
|
||||
{
|
||||
@ -92,7 +91,6 @@ public:
|
||||
explicit ansicolor_stderr_sink(color_mode mode = color_mode::automatic);
|
||||
};
|
||||
|
||||
|
||||
using ansicolor_stdout_sink_mt = ansicolor_stdout_sink<details::console_mutex>;
|
||||
using ansicolor_stdout_sink_st = ansicolor_stdout_sink<details::console_nullmutex>;
|
||||
|
||||
|
@ -125,7 +125,6 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const fmt::memory_b
|
||||
::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr);
|
||||
}
|
||||
|
||||
|
||||
// wincolor_stdout_sink
|
||||
template<typename ConsoleMutex>
|
||||
SPDLOG_INLINE wincolor_stdout_sink<ConsoleMutex>::wincolor_stdout_sink(color_mode mode)
|
||||
@ -138,6 +137,5 @@ SPDLOG_INLINE wincolor_stderr_sink<ConsoleMutex>::wincolor_stderr_sink(color_mod
|
||||
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode)
|
||||
{}
|
||||
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
@ -264,14 +264,12 @@ static const char *test_path = "\\a\\b\\myfile.cpp";
|
||||
static const char *test_path = "/a/b//myfile.cpp";
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
TEST_CASE("short filename formatter-1", "[pattern_formatter]")
|
||||
{
|
||||
spdlog::pattern_formatter formatter("%s", spdlog::pattern_time_type::local, "");
|
||||
fmt::memory_buffer formatted;
|
||||
std::string logger_name = "logger-name";
|
||||
spdlog::source_loc source_loc{test_path , 123, "some_func()"};
|
||||
spdlog::source_loc source_loc{test_path, 123, "some_func()"};
|
||||
spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello");
|
||||
formatter.format(msg, formatted);
|
||||
REQUIRE(fmt::to_string(formatted) == "myfile.cpp");
|
||||
@ -304,9 +302,8 @@ TEST_CASE("full filename formatter", "[pattern_formatter]")
|
||||
spdlog::pattern_formatter formatter("%g", spdlog::pattern_time_type::local, "");
|
||||
fmt::memory_buffer formatted;
|
||||
std::string logger_name = "logger-name";
|
||||
spdlog::source_loc source_loc{test_path , 123, "some_func()"};
|
||||
spdlog::source_loc source_loc{test_path, 123, "some_func()"};
|
||||
spdlog::details::log_msg msg(source_loc, "logger-name", spdlog::level::info, "Hello");
|
||||
formatter.format(msg, formatted);
|
||||
REQUIRE(fmt::to_string(formatted) == test_path);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user