diff --git a/CMakeLists.txt b/CMakeLists.txt index 29ee1d6b..02498411 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -145,12 +145,7 @@ message(STATUS "Build type: " ${CMAKE_BUILD_TYPE}) # --------------------------------------------------------------------------------------- # Static/Shared library # --------------------------------------------------------------------------------------- -file(GLOB SPDLOG_SRCS - src/*.cpp - src/details/*.cpp - src/sinks/*.cpp - src/cfg/*.cpp -) +file(GLOB SPDLOG_SRCS src/*.cpp src/details/*.cpp src/sinks/*.cpp src/cfg/*.cpp) if(NOT SPDLOG_USE_STD_FORMAT AND NOT SPDLOG_FMT_EXTERNAL AND NOT SPDLOG_FMT_EXTERNAL_HO) list(APPEND SPDLOG_SRCS src/fmt/bundled_fmtlib_format.cpp) diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h index 084c32b0..52866fbb 100644 --- a/include/spdlog/async_logger.h +++ b/include/spdlog/async_logger.h @@ -63,4 +63,3 @@ private: async_overflow_policy overflow_policy_; }; } // namespace spdlog - diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 48ff80e6..df1a80dc 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -37,7 +37,6 @@ # endif #endif - #if defined(SPDLOG_SHARED_LIB) # if defined(_WIN32) # ifdef spdlog_EXPORTS @@ -134,7 +133,6 @@ using wmemory_buf_t = fmt::basic_memory_buffer; # define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x) #endif // SPDLOG_USE_STD_FORMAT - #define SPDLOG_LEVEL_TRACE 0 #define SPDLOG_LEVEL_DEBUG 1 #define SPDLOG_LEVEL_INFO 2 @@ -165,17 +163,23 @@ enum class level }; #if defined(SPDLOG_NO_ATOMIC_LEVELS) - using atomic_level_t = details::null_atomic; +using atomic_level_t = details::null_atomic; #else - using atomic_level_t = std::atomic; +using atomic_level_t = std::atomic; #endif #if !defined(SPDLOG_LEVEL_NAMES) -#define SPDLOG_LEVEL_NAMES { "trace", "debug", "info", "warning", "error", "critical", "off"} +# define SPDLOG_LEVEL_NAMES \ + { \ + "trace", "debug", "info", "warning", "error", "critical", "off" \ + } #endif #if !defined(SPDLOG_SHORT_LEVEL_NAMES) -#define SPDLOG_SHORT_LEVEL_NAMES {"T", "D", "I", "W", "E", "C", "O"} +# define SPDLOG_SHORT_LEVEL_NAMES \ + { \ + "T", "D", "I", "W", "E", "C", "O" \ + } #endif [[nodiscard]] constexpr size_t level_to_number(level lvl) noexcept @@ -198,8 +202,6 @@ constexpr std::array short_level_names SPDLOG_SHORT_ SPDLOG_API [[nodiscard]] spdlog::level level_from_str(const std::string &name) noexcept; - - // // Color mode used by sinks with color support. // diff --git a/include/spdlog/details/log_msg.h b/include/spdlog/details/log_msg.h index 62deb6bc..ea868530 100644 --- a/include/spdlog/details/log_msg.h +++ b/include/spdlog/details/log_msg.h @@ -31,4 +31,3 @@ struct SPDLOG_API log_msg }; } // namespace details } // namespace spdlog - diff --git a/include/spdlog/details/log_msg_buffer.h b/include/spdlog/details/log_msg_buffer.h index 6a111965..762f4ff6 100644 --- a/include/spdlog/details/log_msg_buffer.h +++ b/include/spdlog/details/log_msg_buffer.h @@ -27,4 +27,3 @@ public: } // namespace details } // namespace spdlog - diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 13ffeb41..a9da48a7 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -116,4 +116,3 @@ SPDLOG_API bool fsync(FILE *fp); } // namespace os } // namespace details } // namespace spdlog - diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 9ad7525c..38fd40a3 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -27,7 +27,7 @@ { \ if (!location.empty()) \ { \ - err_handler_(fmt_lib::format("{} [{}({})]", ex.what(), location.filename, location.line)); \ + err_handler_(fmt_lib::format("{} [{}({})]", ex.what(), location.filename, location.line)); \ } \ else \ { \ diff --git a/include/spdlog/pattern_formatter.h b/include/spdlog/pattern_formatter.h index bf2eadeb..1623bfac 100644 --- a/include/spdlog/pattern_formatter.h +++ b/include/spdlog/pattern_formatter.h @@ -122,4 +122,3 @@ private: void compile_pattern_(const std::string &pattern); }; } // namespace spdlog - diff --git a/include/spdlog/sinks/base_sink.h b/include/spdlog/sinks/base_sink.h index 12dc7610..0660f7a3 100644 --- a/include/spdlog/sinks/base_sink.h +++ b/include/spdlog/sinks/base_sink.h @@ -46,4 +46,3 @@ protected: }; } // namespace sinks } // namespace spdlog - diff --git a/include/spdlog/sinks/basic_file_sink.h b/include/spdlog/sinks/basic_file_sink.h index c4113dee..ae7c4e9e 100644 --- a/include/spdlog/sinks/basic_file_sink.h +++ b/include/spdlog/sinks/basic_file_sink.h @@ -54,4 +54,3 @@ inline std::shared_ptr basic_logger_st( } } // namespace spdlog - diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 7650ec44..7dc31501 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -33,8 +33,8 @@ struct daily_filename_calculator { filename_t basename, ext; std::tie(basename, ext) = details::file_helper::split_by_extension(filename); - return fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, now_tm.tm_year + 1900, - now_tm.tm_mon + 1, now_tm.tm_mday, ext); + return fmt_lib::format( + SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, ext); } }; diff --git a/include/spdlog/sinks/mongo_sink.h b/include/spdlog/sinks/mongo_sink.h index 5b52ef09..4a28f00b 100644 --- a/include/spdlog/sinks/mongo_sink.h +++ b/include/spdlog/sinks/mongo_sink.h @@ -66,9 +66,10 @@ protected: if (client_ != nullptr) { - auto doc = document{} << "timestamp" << bsoncxx::types::b_date(msg.time) << "level" << level::to_string_view(msg.log_level).data() - << "level_num" << msg.log_level << "message" << std::string(msg.payload.begin(), msg.payload.end()) - << "logger_name" << std::string(msg.logger_name.begin(), msg.logger_name.end()) << "thread_id" + auto doc = document{} << "timestamp" << bsoncxx::types::b_date(msg.time) << "level" + << level::to_string_view(msg.log_level).data() << "level_num" << msg.log_level << "message" + << std::string(msg.payload.begin(), msg.payload.end()) << "logger_name" + << std::string(msg.logger_name.begin(), msg.logger_name.end()) << "thread_id" << static_cast(msg.thread_id) << finalize; client_->database(db_name_).collection(coll_name_).insert_one(doc.view()); } diff --git a/include/spdlog/sinks/qt_sinks.h b/include/spdlog/sinks/qt_sinks.h index 48e50d8a..34499cd3 100644 --- a/include/spdlog/sinks/qt_sinks.h +++ b/include/spdlog/sinks/qt_sinks.h @@ -185,7 +185,7 @@ protected: qt_text_edit_, // text edit to append to std::move(payload), // text to append default_color_, // default color - colors_.at(msg.log_level), // color to apply + colors_.at(msg.log_level), // color to apply color_range_start, // color range start color_range_end}; // color range end diff --git a/include/spdlog/sinks/sink.h b/include/spdlog/sinks/sink.h index 9fae6dee..fb74477d 100644 --- a/include/spdlog/sinks/sink.h +++ b/include/spdlog/sinks/sink.h @@ -29,4 +29,3 @@ protected: } // namespace sinks } // namespace spdlog - diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h index e519985f..c630683a 100644 --- a/include/spdlog/sinks/stdout_sinks.h +++ b/include/spdlog/sinks/stdout_sinks.h @@ -81,4 +81,3 @@ template std::shared_ptr stderr_logger_st(const std::string &logger_name); } // namespace spdlog - diff --git a/src/async_logger.cpp b/src/async_logger.cpp index 1ead1e1e..0904cf5d 100644 --- a/src/async_logger.cpp +++ b/src/async_logger.cpp @@ -8,18 +8,18 @@ #include #include - spdlog::async_logger::async_logger( +spdlog::async_logger::async_logger( std::string logger_name, sinks_init_list sinks_list, std::weak_ptr tp, async_overflow_policy overflow_policy) : async_logger(std::move(logger_name), sinks_list.begin(), sinks_list.end(), std::move(tp), overflow_policy) {} - spdlog::async_logger::async_logger( +spdlog::async_logger::async_logger( std::string logger_name, sink_ptr single_sink, std::weak_ptr tp, async_overflow_policy overflow_policy) : async_logger(std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy) {} // send the log message to the thread pool - void spdlog::async_logger::sink_it_(const details::log_msg &msg) +void spdlog::async_logger::sink_it_(const details::log_msg &msg) { SPDLOG_TRY { @@ -36,7 +36,7 @@ } // send flush request to the thread pool - void spdlog::async_logger::flush_() +void spdlog::async_logger::flush_() { SPDLOG_TRY { @@ -55,7 +55,7 @@ // // backend functions - called from the thread pool to do the actual job // - void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) +void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) { for (auto &sink : sinks_) { @@ -75,7 +75,7 @@ } } - void spdlog::async_logger::backend_flush_() +void spdlog::async_logger::backend_flush_() { for (auto &sink : sinks_) { @@ -87,7 +87,7 @@ } } - std::shared_ptr spdlog::async_logger::clone(std::string new_name) +std::shared_ptr spdlog::async_logger::clone(std::string new_name) { auto cloned = std::make_shared(*this); cloned->name_ = std::move(new_name); diff --git a/src/cfg/.helpers.cpp b/src/cfg/.helpers.cpp index b0f0a8d4..9ee8c832 100644 --- a/src/cfg/.helpers.cpp +++ b/src/cfg/.helpers.cpp @@ -1,7 +1,6 @@ // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. // Distributed under the MIT License (http://opensource.org/licenses/MIT) - #include #include #include @@ -74,7 +73,7 @@ inline std::unordered_map extract_key_vals_(const std: return rv; } - void load_levels(const std::string &input) +void load_levels(const std::string &input) { if (input.empty() || input.size() > 512) { diff --git a/src/common.cpp b/src/common.cpp index 9b19c2c3..c6034dd9 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -8,7 +8,7 @@ namespace spdlog { - spdlog::level level_from_str(const std::string &name) noexcept +spdlog::level level_from_str(const std::string &name) noexcept { auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name); if (it != std::end(level_string_views)) @@ -26,11 +26,11 @@ namespace spdlog { return level::off; } - spdlog_ex::spdlog_ex(std::string msg) +spdlog_ex::spdlog_ex(std::string msg) : msg_(std::move(msg)) {} - spdlog_ex::spdlog_ex(const std::string &msg, int last_errno) +spdlog_ex::spdlog_ex(const std::string &msg, int last_errno) { #ifdef SPDLOG_USE_STD_FORMAT msg_ = std::system_error(std::error_code(last_errno, std::generic_category()), msg).what(); @@ -41,17 +41,17 @@ namespace spdlog { #endif } - const char *spdlog_ex::what() const noexcept +const char *spdlog_ex::what() const noexcept { return msg_.c_str(); } - void throw_spdlog_ex(const std::string &msg, int last_errno) +void throw_spdlog_ex(const std::string &msg, int last_errno) { SPDLOG_THROW(spdlog_ex(msg, last_errno)); } - void throw_spdlog_ex(std::string msg) +void throw_spdlog_ex(std::string msg) { SPDLOG_THROW(spdlog_ex(std::move(msg))); } diff --git a/src/details/file_helper.cpp b/src/details/file_helper.cpp index 3901e7af..671ed05b 100644 --- a/src/details/file_helper.cpp +++ b/src/details/file_helper.cpp @@ -13,16 +13,16 @@ namespace spdlog { namespace details { - file_helper::file_helper(const file_event_handlers &event_handlers) +file_helper::file_helper(const file_event_handlers &event_handlers) : event_handlers_(event_handlers) {} - file_helper::~file_helper() +file_helper::~file_helper() { close(); } - void file_helper::open(const filename_t &fname, bool truncate) +void file_helper::open(const filename_t &fname, bool truncate) { close(); filename_ = fname; @@ -66,7 +66,7 @@ namespace details { throw_spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing", errno); } - void file_helper::reopen(bool truncate) +void file_helper::reopen(bool truncate) { if (filename_.empty()) { @@ -75,7 +75,7 @@ namespace details { this->open(filename_, truncate); } - void file_helper::flush() +void file_helper::flush() { if (std::fflush(fd_) != 0) { @@ -83,7 +83,7 @@ namespace details { } } - void file_helper::sync() +void file_helper::sync() { if (!os::fsync(fd_)) { @@ -91,7 +91,7 @@ namespace details { } } - void file_helper::close() +void file_helper::close() { if (fd_ != nullptr) { @@ -110,7 +110,7 @@ namespace details { } } - void file_helper::write(const memory_buf_t &buf) +void file_helper::write(const memory_buf_t &buf) { size_t msg_size = buf.size(); auto data = buf.data(); @@ -120,7 +120,7 @@ namespace details { } } - size_t file_helper::size() const +size_t file_helper::size() const { if (fd_ == nullptr) { @@ -129,7 +129,7 @@ namespace details { return os::filesize(fd_); } - const filename_t &file_helper::filename() const +const filename_t &file_helper::filename() const { return filename_; } @@ -147,7 +147,7 @@ namespace details { // ".mylog" => (".mylog". "") // "my_folder/.mylog" => ("my_folder/.mylog", "") // "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt") - std::tuple file_helper::split_by_extension(const filename_t &fname) +std::tuple file_helper::split_by_extension(const filename_t &fname) { auto ext_index = fname.rfind('.'); diff --git a/src/details/log_msg.cpp b/src/details/log_msg.cpp index 58818edc..a94752dd 100644 --- a/src/details/log_msg.cpp +++ b/src/details/log_msg.cpp @@ -7,8 +7,8 @@ namespace spdlog { namespace details { - log_msg::log_msg(spdlog::log_clock::time_point log_time, spdlog::source_loc loc, string_view_t a_logger_name, - spdlog::level lvl, spdlog::string_view_t msg) +log_msg::log_msg(spdlog::log_clock::time_point log_time, spdlog::source_loc loc, string_view_t a_logger_name, spdlog::level lvl, + spdlog::string_view_t msg) : logger_name(a_logger_name) , log_level(lvl) , time(log_time) @@ -19,12 +19,11 @@ namespace details { , payload(msg) {} - log_msg::log_msg( - spdlog::source_loc loc, string_view_t a_logger_name, spdlog::level lvl, spdlog::string_view_t msg) +log_msg::log_msg(spdlog::source_loc loc, string_view_t a_logger_name, spdlog::level lvl, spdlog::string_view_t msg) : log_msg(os::now(), loc, a_logger_name, lvl, msg) {} - log_msg::log_msg(string_view_t a_logger_name, spdlog::level lvl, spdlog::string_view_t msg) +log_msg::log_msg(string_view_t a_logger_name, spdlog::level lvl, spdlog::string_view_t msg) : log_msg(os::now(), source_loc{}, a_logger_name, lvl, msg) {} diff --git a/src/details/log_msg_buffer.cpp b/src/details/log_msg_buffer.cpp index 956689b3..f4276ed8 100644 --- a/src/details/log_msg_buffer.cpp +++ b/src/details/log_msg_buffer.cpp @@ -6,7 +6,7 @@ namespace spdlog { namespace details { - log_msg_buffer::log_msg_buffer(const log_msg &orig_msg) +log_msg_buffer::log_msg_buffer(const log_msg &orig_msg) : log_msg{orig_msg} { buffer.append(logger_name.begin(), logger_name.end()); @@ -14,7 +14,7 @@ namespace details { update_string_views(); } - log_msg_buffer::log_msg_buffer(const log_msg_buffer &other) +log_msg_buffer::log_msg_buffer(const log_msg_buffer &other) : log_msg{other} { buffer.append(logger_name.begin(), logger_name.end()); @@ -22,14 +22,14 @@ namespace details { update_string_views(); } - log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) noexcept +log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) noexcept : log_msg{other} , buffer{std::move(other.buffer)} { update_string_views(); } - log_msg_buffer &log_msg_buffer::operator=(const log_msg_buffer &other) +log_msg_buffer &log_msg_buffer::operator=(const log_msg_buffer &other) { log_msg::operator=(other); buffer.clear(); @@ -38,7 +38,7 @@ namespace details { return *this; } - log_msg_buffer &log_msg_buffer::operator=(log_msg_buffer &&other) noexcept +log_msg_buffer &log_msg_buffer::operator=(log_msg_buffer &&other) noexcept { log_msg::operator=(other); buffer = std::move(other.buffer); @@ -46,7 +46,7 @@ namespace details { return *this; } - void log_msg_buffer::update_string_views() +void log_msg_buffer::update_string_views() { logger_name = string_view_t{buffer.data(), logger_name.size()}; payload = string_view_t{buffer.data() + logger_name.size(), payload.size()}; diff --git a/src/details/os.cpp b/src/details/os.cpp index 7165469f..913e8ed3 100644 --- a/src/details/os.cpp +++ b/src/details/os.cpp @@ -68,7 +68,7 @@ namespace spdlog { namespace details { namespace os { - spdlog::log_clock::time_point now() noexcept +spdlog::log_clock::time_point now() noexcept { #if defined __linux__ && defined SPDLOG_CLOCK_COARSE @@ -81,7 +81,7 @@ namespace os { return log_clock::now(); #endif } - std::tm localtime(const std::time_t &time_tt) noexcept +std::tm localtime(const std::time_t &time_tt) noexcept { #ifdef _WIN32 @@ -94,13 +94,13 @@ namespace os { return tm; } - std::tm localtime() noexcept +std::tm localtime() noexcept { std::time_t now_t = ::time(nullptr); return localtime(now_t); } - std::tm gmtime(const std::time_t &time_tt) noexcept +std::tm gmtime(const std::time_t &time_tt) noexcept { #ifdef _WIN32 @@ -113,14 +113,14 @@ namespace os { return tm; } - std::tm gmtime() noexcept +std::tm gmtime() noexcept { std::time_t now_t = ::time(nullptr); return gmtime(now_t); } // fopen_s on non windows for writing - bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) +bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) { #ifdef _WIN32 # ifdef SPDLOG_WCHAR_FILENAMES @@ -160,7 +160,7 @@ namespace os { return *fp == nullptr; } - int remove(const filename_t &filename) noexcept +int remove(const filename_t &filename) noexcept { #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) return ::_wremove(filename.c_str()); @@ -169,12 +169,12 @@ namespace os { #endif } - int remove_if_exists(const filename_t &filename) noexcept +int remove_if_exists(const filename_t &filename) noexcept { return path_exists(filename) ? remove(filename) : 0; } - int rename(const filename_t &filename1, const filename_t &filename2) noexcept +int rename(const filename_t &filename1, const filename_t &filename2) noexcept { #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) return ::_wrename(filename1.c_str(), filename2.c_str()); @@ -184,7 +184,7 @@ namespace os { } // Return true if path exists (file or directory) - bool path_exists(const filename_t &filename) noexcept +bool path_exists(const filename_t &filename) noexcept { #ifdef _WIN32 # ifdef SPDLOG_WCHAR_FILENAMES @@ -206,7 +206,7 @@ namespace os { #endif // Return file size according to open FILE* object - size_t filesize(FILE *f) +size_t filesize(FILE *f) { if (f == nullptr) { @@ -260,7 +260,7 @@ namespace os { #endif // Return utc offset in minutes or throw spdlog_ex on failure - int utc_minutes_offset(const std::tm &tm) +int utc_minutes_offset(const std::tm &tm) { #ifdef _WIN32 @@ -328,7 +328,7 @@ namespace os { // Return current thread id as size_t // It exists because the std::this_thread::get_id() is much slower(especially // under VS 2013) - size_t _thread_id() noexcept +size_t _thread_id() noexcept { #ifdef _WIN32 return static_cast(::GetCurrentThreadId()); @@ -377,7 +377,7 @@ namespace os { } // Return current thread id as size_t (from thread local storage) - size_t thread_id() noexcept +size_t thread_id() noexcept { // cache thread id in tls static thread_local const size_t tid = _thread_id(); @@ -386,7 +386,7 @@ namespace os { // This is avoid msvc issue in sleep_for that happens if the clock changes. // See https://github.com/gabime/spdlog/issues/609 - void sleep_for_millis(unsigned int milliseconds) noexcept +void sleep_for_millis(unsigned int milliseconds) noexcept { #if defined(_WIN32) ::Sleep(milliseconds); @@ -397,20 +397,20 @@ namespace os { // wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined) #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) - std::string filename_to_str(const filename_t &filename) +std::string filename_to_str(const filename_t &filename) { memory_buf_t buf; wstr_to_utf8buf(filename, buf); return SPDLOG_BUF_TO_STRING(buf); } #else - std::string filename_to_str(const filename_t &filename) +std::string filename_to_str(const filename_t &filename) { return filename; } #endif - int pid() noexcept +int pid() noexcept { #ifdef _WIN32 @@ -422,7 +422,7 @@ namespace os { // Determine if the terminal supports colors // Based on: https://github.com/agauniyal/rang/ - bool is_color_terminal() noexcept +bool is_color_terminal() noexcept { #ifdef _WIN32 return true; @@ -453,7 +453,7 @@ namespace os { // Determine if the terminal attached // Source: https://github.com/agauniyal/rang/ - bool in_terminal(FILE *file) noexcept +bool in_terminal(FILE *file) noexcept { #ifdef _WIN32 @@ -464,7 +464,7 @@ namespace os { } #if defined(SPDLOG_WCHAR_FILENAMES) && defined(_WIN32) - void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) +void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) { if (wstr.size() > static_cast((std::numeric_limits::max)()) / 2 - 1) { @@ -499,7 +499,7 @@ namespace os { throw_spdlog_ex(fmt_lib::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError())); } - void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) +void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) { if (str.size() > static_cast((std::numeric_limits::max)()) - 1) { @@ -532,7 +532,7 @@ namespace os { #endif // defined(SPDLOG_WCHAR_FILENAMES) && defined(_WIN32) // return true on success -static bool mkdir_(const filename_t &path) +static bool mkdir_(const filename_t &path) { #ifdef _WIN32 # ifdef SPDLOG_WCHAR_FILENAMES @@ -547,7 +547,7 @@ static bool mkdir_(const filename_t &path) // create the given directory - and all directories leading to it // return true on success or if the directory already exists - bool create_dir(const filename_t &path) +bool create_dir(const filename_t &path) { if (path_exists(path)) { @@ -586,13 +586,13 @@ static bool mkdir_(const filename_t &path) // "abc/" => "abc" // "abc" => "" // "abc///" => "abc//" - filename_t dir_name(const filename_t &path) +filename_t dir_name(const filename_t &path) { auto pos = path.find_last_of(folder_seps_filename); return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; } -std::string getenv(const char *field) +std::string getenv(const char *field) { #if defined(_MSC_VER) @@ -612,7 +612,7 @@ std::string getenv(const char *field) // Do fsync by FILE handlerpointer // Return true on success - bool fsync(FILE *fp) +bool fsync(FILE *fp) { #ifdef _WIN32 return FlushFileBuffers(reinterpret_cast(_get_osfhandle(_fileno(fp)))) != 0; diff --git a/src/details/periodic_worker.cpp b/src/details/periodic_worker.cpp index c7f3d126..960a540a 100644 --- a/src/details/periodic_worker.cpp +++ b/src/details/periodic_worker.cpp @@ -7,7 +7,7 @@ namespace spdlog { namespace details { // stop the worker thread and join it - periodic_worker::~periodic_worker() +periodic_worker::~periodic_worker() { if (worker_thread_.joinable()) { diff --git a/src/details/registry.cpp b/src/details/registry.cpp index 1e627f1b..42736b50 100644 --- a/src/details/registry.cpp +++ b/src/details/registry.cpp @@ -26,7 +26,7 @@ namespace spdlog { namespace details { - registry::registry() +registry::registry() : formatter_(new pattern_formatter()) { @@ -45,15 +45,15 @@ namespace details { #endif // SPDLOG_DISABLE_DEFAULT_LOGGER } - registry::~registry() = default; +registry::~registry() = default; - void registry::register_logger(std::shared_ptr new_logger) +void registry::register_logger(std::shared_ptr new_logger) { std::lock_guard lock(logger_map_mutex_); register_logger_(std::move(new_logger)); } - void registry::initialize_logger(std::shared_ptr new_logger) +void registry::initialize_logger(std::shared_ptr new_logger) { std::lock_guard lock(logger_map_mutex_); new_logger->set_formatter(formatter_->clone()); @@ -76,14 +76,14 @@ namespace details { } } - std::shared_ptr registry::get(const std::string &logger_name) +std::shared_ptr registry::get(const std::string &logger_name) { std::lock_guard lock(logger_map_mutex_); auto found = loggers_.find(logger_name); return found == loggers_.end() ? nullptr : found->second; } - std::shared_ptr registry::default_logger() +std::shared_ptr registry::default_logger() { std::lock_guard lock(logger_map_mutex_); return default_logger_; @@ -93,14 +93,14 @@ namespace details { // To be used directly by the spdlog default api (e.g. spdlog::info) // This make the default API faster, but cannot be used concurrently with set_default_logger(). // e.g do not call set_default_logger() from one thread while calling spdlog::info() from another. - logger *registry::get_default_raw() +logger *registry::get_default_raw() { return default_logger_.get(); } // set default logger. // default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map. - void registry::set_default_logger(std::shared_ptr new_default_logger) +void registry::set_default_logger(std::shared_ptr new_default_logger) { std::lock_guard lock(logger_map_mutex_); // remove previous default logger from the map @@ -115,20 +115,20 @@ namespace details { default_logger_ = std::move(new_default_logger); } - void registry::set_tp(std::shared_ptr tp) +void registry::set_tp(std::shared_ptr tp) { std::lock_guard lock(tp_mutex_); tp_ = std::move(tp); } - std::shared_ptr registry::get_tp() +std::shared_ptr registry::get_tp() { std::lock_guard lock(tp_mutex_); return tp_; } // Set global formatter. Each sink in each logger will get a clone of this object - void registry::set_formatter(std::unique_ptr formatter) +void registry::set_formatter(std::unique_ptr formatter) { std::lock_guard lock(logger_map_mutex_); formatter_ = std::move(formatter); @@ -138,7 +138,7 @@ namespace details { } } - void registry::set_level(level level) +void registry::set_level(level level) { std::lock_guard lock(logger_map_mutex_); for (auto &l : loggers_) @@ -148,7 +148,7 @@ namespace details { global_log_level_ = level; } - void registry::flush_on(level level) +void registry::flush_on(level level) { std::lock_guard lock(logger_map_mutex_); for (auto &l : loggers_) @@ -158,7 +158,7 @@ namespace details { flush_level_ = level; } - void registry::set_error_handler(err_handler handler) +void registry::set_error_handler(err_handler handler) { std::lock_guard lock(logger_map_mutex_); for (auto &l : loggers_) @@ -168,7 +168,7 @@ namespace details { err_handler_ = std::move(handler); } - void registry::apply_all(const std::function)> &fun) +void registry::apply_all(const std::function)> &fun) { std::lock_guard lock(logger_map_mutex_); for (auto &l : loggers_) @@ -177,7 +177,7 @@ namespace details { } } - void registry::flush_all() +void registry::flush_all() { std::lock_guard lock(logger_map_mutex_); for (auto &l : loggers_) @@ -186,7 +186,7 @@ namespace details { } } - void registry::drop(const std::string &logger_name) +void registry::drop(const std::string &logger_name) { std::lock_guard lock(logger_map_mutex_); auto is_default_logger = default_logger_ && default_logger_->name() == logger_name; @@ -197,7 +197,7 @@ namespace details { } } - void registry::drop_all() +void registry::drop_all() { std::lock_guard lock(logger_map_mutex_); loggers_.clear(); @@ -205,7 +205,7 @@ namespace details { } // clean all resources and threads started by the registry - void registry::shutdown() +void registry::shutdown() { { std::lock_guard lock(flusher_mutex_); @@ -220,18 +220,18 @@ namespace details { } } - std::recursive_mutex ®istry::tp_mutex() +std::recursive_mutex ®istry::tp_mutex() { return tp_mutex_; } - void registry::set_automatic_registration(bool automatic_registration) +void registry::set_automatic_registration(bool automatic_registration) { std::lock_guard lock(logger_map_mutex_); automatic_registration_ = automatic_registration; } - void registry::set_levels(log_levels levels, level *global_level) +void registry::set_levels(log_levels levels, level *global_level) { std::lock_guard lock(logger_map_mutex_); log_levels_ = std::move(levels); @@ -252,13 +252,13 @@ namespace details { } } - registry ®istry::instance() +registry ®istry::instance() { static registry s_instance; return s_instance; } - void registry::apply_logger_env_levels(std::shared_ptr new_logger) +void registry::apply_logger_env_levels(std::shared_ptr new_logger) { std::lock_guard lock(logger_map_mutex_); auto it = log_levels_.find(new_logger->name()); @@ -266,7 +266,7 @@ namespace details { new_logger->set_level(new_level); } - void registry::throw_if_exists_(const std::string &logger_name) +void registry::throw_if_exists_(const std::string &logger_name) { if (loggers_.find(logger_name) != loggers_.end()) { @@ -274,7 +274,7 @@ namespace details { } } - void registry::register_logger_(std::shared_ptr new_logger) +void registry::register_logger_(std::shared_ptr new_logger) { auto logger_name = new_logger->name(); throw_if_exists_(logger_name); diff --git a/src/details/thread_pool.cpp b/src/details/thread_pool.cpp index c4e8e2ac..26f3f5aa 100644 --- a/src/details/thread_pool.cpp +++ b/src/details/thread_pool.cpp @@ -8,8 +8,7 @@ namespace spdlog { namespace details { - thread_pool::thread_pool( - size_t q_max_items, size_t threads_n, std::function on_thread_start, std::function on_thread_stop) +thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function on_thread_start, std::function on_thread_stop) : q_(q_max_items) { if (threads_n == 0 || threads_n > 1000) @@ -27,17 +26,17 @@ namespace details { } } - thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function on_thread_start) +thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function on_thread_start) : thread_pool(q_max_items, threads_n, on_thread_start, [] {}) {} - thread_pool::thread_pool(size_t q_max_items, size_t threads_n) +thread_pool::thread_pool(size_t q_max_items, size_t threads_n) : thread_pool( q_max_items, threads_n, [] {}, [] {}) {} // message all threads to terminate gracefully join them - thread_pool::~thread_pool() +thread_pool::~thread_pool() { SPDLOG_TRY { @@ -54,43 +53,43 @@ namespace details { SPDLOG_CATCH_STD } -void thread_pool::post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy) +void thread_pool::post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy) { async_msg async_m(std::move(worker_ptr), async_msg_type::log, msg); post_async_msg_(std::move(async_m), overflow_policy); } -void thread_pool::post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy) +void thread_pool::post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy) { post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy); } -size_t thread_pool::overrun_counter() +size_t thread_pool::overrun_counter() { return q_.overrun_counter(); } -void thread_pool::reset_overrun_counter() +void thread_pool::reset_overrun_counter() { q_.reset_overrun_counter(); } -size_t thread_pool::discard_counter() +size_t thread_pool::discard_counter() { return q_.discard_counter(); } -void thread_pool::reset_discard_counter() +void thread_pool::reset_discard_counter() { q_.reset_discard_counter(); } -size_t thread_pool::queue_size() +size_t thread_pool::queue_size() { return q_.size(); } -void thread_pool::post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy) +void thread_pool::post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy) { if (overflow_policy == async_overflow_policy::block) { @@ -107,7 +106,7 @@ void thread_pool::post_async_msg_(async_msg &&new_msg, async_overflow_policy ov } } -void thread_pool::worker_loop_() +void thread_pool::worker_loop_() { while (process_next_msg_()) {} } @@ -115,7 +114,7 @@ void thread_pool::worker_loop_() // process next message in the queue // return true if this thread should still be active (while no terminate msg // was received) -bool thread_pool::process_next_msg_() +bool thread_pool::process_next_msg_() { async_msg incoming_async_msg; q_.dequeue(incoming_async_msg); diff --git a/src/logger.cpp b/src/logger.cpp index 596cd66b..d075826f 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -11,7 +11,7 @@ namespace spdlog { // public methods - logger::logger(const logger &other) noexcept +logger::logger(const logger &other) noexcept : name_(other.name_) , sinks_(other.sinks_) , level_(other.level_.load(std::memory_order_relaxed)) @@ -19,7 +19,7 @@ namespace spdlog { , custom_err_handler_(other.custom_err_handler_) {} - logger::logger(logger &&other) noexcept +logger::logger(logger &&other) noexcept : name_(std::move(other.name_)) , sinks_(std::move(other.sinks_)) , level_(other.level_.load(std::memory_order_relaxed)) @@ -27,24 +27,24 @@ namespace spdlog { , custom_err_handler_(std::move(other.custom_err_handler_)) {} - void logger::set_level(level level) +void logger::set_level(level level) { level_.store(level); } - level logger::log_level() const +level logger::log_level() const { return static_cast(level_.load(std::memory_order_relaxed)); } - const std::string &logger::name() const +const std::string &logger::name() const { return name_; } // set formatting for the sinks in this logger. // each sink will get a separate instance of the formatter object. - void logger::set_formatter(std::unique_ptr f) +void logger::set_formatter(std::unique_ptr f) { for (auto it = sinks_.begin(); it != sinks_.end(); ++it) { @@ -61,54 +61,54 @@ namespace spdlog { } } - void logger::set_pattern(std::string pattern, pattern_time_type time_type) +void logger::set_pattern(std::string pattern, pattern_time_type time_type) { auto new_formatter = std::make_unique(std::move(pattern), time_type); set_formatter(std::move(new_formatter)); } // flush functions - void logger::flush() +void logger::flush() { flush_(); } - void logger::flush_on(level level) +void logger::flush_on(level level) { flush_level_.store(level); } - level logger::flush_level() const +level logger::flush_level() const { return static_cast(flush_level_.load(std::memory_order_relaxed)); } // sinks - const std::vector &logger::sinks() const +const std::vector &logger::sinks() const { return sinks_; } - std::vector &logger::sinks() +std::vector &logger::sinks() { return sinks_; } // error handler - void logger::set_error_handler(err_handler handler) +void logger::set_error_handler(err_handler handler) { custom_err_handler_ = std::move(handler); } // create new logger with same sinks and configuration. - std::shared_ptr logger::clone(std::string logger_name) +std::shared_ptr logger::clone(std::string logger_name) { auto cloned = std::make_shared(*this); cloned->name_ = std::move(logger_name); return cloned; } - void logger::flush_() +void logger::flush_() { for (auto &sink : sinks_) { @@ -120,13 +120,13 @@ namespace spdlog { } } - bool logger::should_flush_(const details::log_msg &msg) +bool logger::should_flush_(const details::log_msg &msg) { auto flush_level = flush_level_.load(std::memory_order_relaxed); return (msg.log_level >= flush_level) && (msg.log_level != level::off); } - void logger::err_handler_(const std::string &msg) +void logger::err_handler_(const std::string &msg) { if (custom_err_handler_) { diff --git a/src/pattern_formatter.cpp b/src/pattern_formatter.cpp index 848e9441..9dbcc36f 100644 --- a/src/pattern_formatter.cpp +++ b/src/pattern_formatter.cpp @@ -1009,8 +1009,7 @@ private: } // namespace details - pattern_formatter::pattern_formatter( - std::string pattern, pattern_time_type time_type, std::string eol, custom_flags custom_user_flags) +pattern_formatter::pattern_formatter(std::string pattern, pattern_time_type time_type, std::string eol, custom_flags custom_user_flags) : pattern_(std::move(pattern)) , eol_(std::move(eol)) , pattern_time_type_(time_type) @@ -1023,7 +1022,7 @@ private: } // use by default full formatter for if pattern is not given - pattern_formatter::pattern_formatter(pattern_time_type time_type, std::string eol) +pattern_formatter::pattern_formatter(pattern_time_type time_type, std::string eol) : pattern_("%+") , eol_(std::move(eol)) , pattern_time_type_(time_type) @@ -1034,7 +1033,7 @@ private: formatters_.push_back(std::make_unique(details::padding_info{})); } - std::unique_ptr pattern_formatter::clone() const +std::unique_ptr pattern_formatter::clone() const { custom_flags cloned_custom_formatters; for (auto &it : custom_handlers_) @@ -1050,7 +1049,7 @@ private: #endif } - void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest) +void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest) { if (need_localtime_) { @@ -1070,19 +1069,19 @@ private: details::fmt_helper::append_string_view(eol_, dest); } - void pattern_formatter::set_pattern(std::string pattern) +void pattern_formatter::set_pattern(std::string pattern) { pattern_ = std::move(pattern); need_localtime_ = false; compile_pattern_(pattern_); } - void pattern_formatter::need_localtime(bool need) +void pattern_formatter::need_localtime(bool need) { need_localtime_ = need; } - std::tm pattern_formatter::get_time_(const details::log_msg &msg) +std::tm pattern_formatter::get_time_(const details::log_msg &msg) { if (pattern_time_type_ == pattern_time_type::local) { @@ -1092,7 +1091,7 @@ private: } template - void pattern_formatter::handle_flag_(char flag, details::padding_info padding) +void pattern_formatter::handle_flag_(char flag, details::padding_info padding) { // process custom flags auto it = custom_handlers_.find(flag); @@ -1325,7 +1324,7 @@ template // Extract given pad spec (e.g. %8X, %=8X, %-8!X, %8!X, %=8!X, %-8!X, %+8!X) // Advance the given it pass the end of the padding spec found (if any) // Return padding. - details::padding_info pattern_formatter::handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end) +details::padding_info pattern_formatter::handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end) { using details::padding_info; using details::scoped_padder; @@ -1377,7 +1376,7 @@ template return details::padding_info{std::min(width, max_width), side, truncate}; } - void pattern_formatter::compile_pattern_(const std::string &pattern) +void pattern_formatter::compile_pattern_(const std::string &pattern) { auto end = pattern.end(); std::unique_ptr user_chars; diff --git a/src/sinks/ansicolor_sink.cpp b/src/sinks/ansicolor_sink.cpp index 080c6674..0c00e650 100644 --- a/src/sinks/ansicolor_sink.cpp +++ b/src/sinks/ansicolor_sink.cpp @@ -1,18 +1,16 @@ // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. // Distributed under the MIT License (http://opensource.org/licenses/MIT) - #include #include #include - namespace spdlog { namespace sinks { template - ansicolor_sink::ansicolor_sink(FILE *target_file, color_mode mode) +ansicolor_sink::ansicolor_sink(FILE *target_file, color_mode mode) : target_file_(target_file) , mutex_(ConsoleMutex::mutex()) , formatter_(std::make_unique()) @@ -29,14 +27,14 @@ template } template - void ansicolor_sink::set_color(level color_level, string_view_t color) +void ansicolor_sink::set_color(level color_level, string_view_t color) { std::lock_guard lock(mutex_); colors_.at(level_to_number(color_level)) = to_string_(color); } template - void ansicolor_sink::log(const details::log_msg &msg) +void ansicolor_sink::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. @@ -64,34 +62,34 @@ template } template - void ansicolor_sink::flush() +void ansicolor_sink::flush() { std::lock_guard lock(mutex_); fflush(target_file_); } template - void ansicolor_sink::set_pattern(const std::string &pattern) +void ansicolor_sink::set_pattern(const std::string &pattern) { std::lock_guard lock(mutex_); formatter_ = std::unique_ptr(new pattern_formatter(pattern)); } template - void ansicolor_sink::set_formatter(std::unique_ptr sink_formatter) +void ansicolor_sink::set_formatter(std::unique_ptr sink_formatter) { std::lock_guard lock(mutex_); formatter_ = std::move(sink_formatter); } template - bool ansicolor_sink::should_color() +bool ansicolor_sink::should_color() { return should_do_colors_; } template - void ansicolor_sink::set_color_mode(color_mode mode) +void ansicolor_sink::set_color_mode(color_mode mode) { switch (mode) { @@ -110,32 +108,32 @@ template } template - void ansicolor_sink::print_ccode_(const string_view_t &color_code) +void ansicolor_sink::print_ccode_(const string_view_t &color_code) { fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_); } template - void ansicolor_sink::print_range_(const memory_buf_t &formatted, size_t start, size_t end) +void ansicolor_sink::print_range_(const memory_buf_t &formatted, size_t start, size_t end) { fwrite(formatted.data() + start, sizeof(char), end - start, target_file_); } template - std::string ansicolor_sink::to_string_(const string_view_t &sv) +std::string ansicolor_sink::to_string_(const string_view_t &sv) { return {sv.data(), sv.size()}; } // ansicolor_stdout_sink template - ansicolor_stdout_sink::ansicolor_stdout_sink(color_mode mode) +ansicolor_stdout_sink::ansicolor_stdout_sink(color_mode mode) : ansicolor_sink(stdout, mode) {} // ansicolor_stderr_sink template - ansicolor_stderr_sink::ansicolor_stderr_sink(color_mode mode) +ansicolor_stderr_sink::ansicolor_stderr_sink(color_mode mode) : ansicolor_sink(stderr, mode) {} diff --git a/src/sinks/base_sink.cpp b/src/sinks/base_sink.cpp index 9c059cec..92dba2ba 100644 --- a/src/sinks/base_sink.cpp +++ b/src/sinks/base_sink.cpp @@ -1,8 +1,6 @@ // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. // Distributed under the MIT License (http://opensource.org/licenses/MIT) - - #include #include #include @@ -11,45 +9,45 @@ #include template - spdlog::sinks::base_sink::base_sink() +spdlog::sinks::base_sink::base_sink() : formatter_{std::make_unique()} {} template - spdlog::sinks::base_sink::base_sink(std::unique_ptr formatter) +spdlog::sinks::base_sink::base_sink(std::unique_ptr formatter) : formatter_{std::move(formatter)} {} template -void spdlog::sinks::base_sink::log(const details::log_msg &msg) +void spdlog::sinks::base_sink::log(const details::log_msg &msg) { std::lock_guard lock(mutex_); sink_it_(msg); } template -void spdlog::sinks::base_sink::flush() +void spdlog::sinks::base_sink::flush() { std::lock_guard lock(mutex_); flush_(); } template -void spdlog::sinks::base_sink::set_pattern(const std::string &pattern) +void spdlog::sinks::base_sink::set_pattern(const std::string &pattern) { std::lock_guard lock(mutex_); set_pattern_(pattern); } template -void spdlog::sinks::base_sink::set_formatter(std::unique_ptr sink_formatter) +void spdlog::sinks::base_sink::set_formatter(std::unique_ptr sink_formatter) { std::lock_guard lock(mutex_); set_formatter_(std::move(sink_formatter)); } template -void spdlog::sinks::base_sink::set_pattern_(const std::string &pattern) +void spdlog::sinks::base_sink::set_pattern_(const std::string &pattern) { set_formatter_(std::make_unique(pattern)); } diff --git a/src/sinks/basic_file_sink.cpp b/src/sinks/basic_file_sink.cpp index 36d58ee3..2510d416 100644 --- a/src/sinks/basic_file_sink.cpp +++ b/src/sinks/basic_file_sink.cpp @@ -10,20 +10,20 @@ namespace spdlog { namespace sinks { template - basic_file_sink::basic_file_sink(const filename_t &filename, bool truncate, const file_event_handlers &event_handlers) +basic_file_sink::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 - const filename_t &basic_file_sink::filename() const +const filename_t &basic_file_sink::filename() const { return file_helper_.filename(); } template - void basic_file_sink::sink_it_(const details::log_msg &msg) +void basic_file_sink::sink_it_(const details::log_msg &msg) { memory_buf_t formatted; base_sink::formatter_->format(msg, formatted); @@ -31,7 +31,7 @@ template } template - void basic_file_sink::flush_() +void basic_file_sink::flush_() { file_helper_.flush(); } diff --git a/src/sinks/rotating_file_sink.cpp b/src/sinks/rotating_file_sink.cpp index 077fd5a2..7cd984ef 100644 --- a/src/sinks/rotating_file_sink.cpp +++ b/src/sinks/rotating_file_sink.cpp @@ -17,7 +17,7 @@ namespace spdlog { namespace sinks { template - rotating_file_sink::rotating_file_sink( +rotating_file_sink::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) @@ -45,7 +45,7 @@ template // calc filename according to index and file extension if exists. // e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt". template - filename_t rotating_file_sink::calc_filename(const filename_t &filename, std::size_t index) +filename_t rotating_file_sink::calc_filename(const filename_t &filename, std::size_t index) { if (index == 0u) { @@ -58,14 +58,14 @@ template } template - filename_t rotating_file_sink::filename() +filename_t rotating_file_sink::filename() { std::lock_guard lock(base_sink::mutex_); return file_helper_.filename(); } template - void rotating_file_sink::sink_it_(const details::log_msg &msg) +void rotating_file_sink::sink_it_(const details::log_msg &msg) { memory_buf_t formatted; base_sink::formatter_->format(msg, formatted); @@ -88,7 +88,7 @@ template } template - void rotating_file_sink::flush_() +void rotating_file_sink::flush_() { file_helper_.flush(); } @@ -99,7 +99,7 @@ template // log.2.txt -> log.3.txt // log.3.txt -> delete template - void rotating_file_sink::rotate_() +void rotating_file_sink::rotate_() { using details::os::filename_to_str; using details::os::path_exists; @@ -134,7 +134,7 @@ template // delete the target if exists, and rename the src file to target // return true on success, false otherwise. template - bool rotating_file_sink::rename_file_(const filename_t &src_filename, const filename_t &target_filename) +bool rotating_file_sink::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); diff --git a/src/sinks/sink.cpp b/src/sinks/sink.cpp index 19144bda..0b15f535 100644 --- a/src/sinks/sink.cpp +++ b/src/sinks/sink.cpp @@ -1,21 +1,21 @@ // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. // Distributed under the MIT License (http://opensource.org/licenses/MIT) - #include #include - 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) +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(level_.load(std::memory_order_relaxed)); } diff --git a/src/sinks/stdout_color_sinks.cpp b/src/sinks/stdout_color_sinks.cpp index 296a2a39..2b477c7c 100644 --- a/src/sinks/stdout_color_sinks.cpp +++ b/src/sinks/stdout_color_sinks.cpp @@ -9,50 +9,52 @@ namespace spdlog { - template - std::shared_ptr stdout_color_mt(const std::string &logger_name, color_mode mode) { - return Factory::template create(logger_name, mode); - } +template +std::shared_ptr stdout_color_mt(const std::string &logger_name, color_mode mode) +{ + return Factory::template create(logger_name, mode); +} - template - std::shared_ptr stdout_color_st(const std::string &logger_name, color_mode mode) { - return Factory::template create(logger_name, mode); - } +template +std::shared_ptr stdout_color_st(const std::string &logger_name, color_mode mode) +{ + return Factory::template create(logger_name, mode); +} - template - std::shared_ptr stderr_color_mt(const std::string &logger_name, color_mode mode) { - return Factory::template create(logger_name, mode); - } +template +std::shared_ptr stderr_color_mt(const std::string &logger_name, color_mode mode) +{ + return Factory::template create(logger_name, mode); +} - template - std::shared_ptr stderr_color_st(const std::string &logger_name, color_mode mode) { - return Factory::template create(logger_name, mode); - } +template +std::shared_ptr stderr_color_st(const std::string &logger_name, color_mode mode) +{ + return Factory::template create(logger_name, mode); +} } // namespace spdlog // template instantiations template SPDLOG_API std::shared_ptr spdlog::stdout_color_mt( - const std::string &logger_name, color_mode mode); + const std::string &logger_name, color_mode mode); template SPDLOG_API std::shared_ptr spdlog::stdout_color_st( - const std::string &logger_name, color_mode mode); + const std::string &logger_name, color_mode mode); template SPDLOG_API std::shared_ptr spdlog::stderr_color_mt( - const std::string &logger_name, color_mode mode); + const std::string &logger_name, color_mode mode); template SPDLOG_API std::shared_ptr spdlog::stderr_color_st( - const std::string &logger_name, color_mode mode); + const std::string &logger_name, color_mode mode); template SPDLOG_API std::shared_ptr spdlog::stdout_color_mt( - const std::string &logger_name, color_mode mode); + const std::string &logger_name, color_mode mode); template SPDLOG_API std::shared_ptr spdlog::stdout_color_st( - const std::string &logger_name, color_mode mode); + const std::string &logger_name, color_mode mode); template SPDLOG_API std::shared_ptr spdlog::stderr_color_mt( - const std::string &logger_name, color_mode mode); + const std::string &logger_name, color_mode mode); template SPDLOG_API std::shared_ptr spdlog::stderr_color_st( - const std::string &logger_name, color_mode mode); - - + const std::string &logger_name, color_mode mode); diff --git a/src/sinks/stdout_sinks.cpp b/src/sinks/stdout_sinks.cpp index 31084538..02ee1803 100644 --- a/src/sinks/stdout_sinks.cpp +++ b/src/sinks/stdout_sinks.cpp @@ -20,16 +20,16 @@ namespace spdlog { - namespace sinks { +namespace sinks { - template - stdout_sink_base::stdout_sink_base(FILE *file) - : mutex_(ConsoleMutex::mutex()) - , file_(file) - , formatter_(std::make_unique()) - { +template +stdout_sink_base::stdout_sink_base(FILE *file) + : mutex_(ConsoleMutex::mutex()) + , file_(file) + , formatter_(std::make_unique()) +{ #ifdef _WIN32 - // get windows handle from the FILE* object + // get windows handle from the FILE* object handle_ = reinterpret_cast(::_get_osfhandle(::_fileno(file_))); @@ -41,13 +41,13 @@ namespace spdlog { throw_spdlog_ex("spdlog::stdout_sink_base: _get_osfhandle() failed", errno); } #endif // WIN32 - } +} - template - void stdout_sink_base::log(const details::log_msg &msg) - { +template +void stdout_sink_base::log(const details::log_msg &msg) +{ #ifdef _WIN32 - if (handle_ == INVALID_HANDLE_VALUE) + if (handle_ == INVALID_HANDLE_VALUE) { return; } @@ -62,73 +62,73 @@ namespace spdlog { throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " + std::to_string(::GetLastError())); } #else - std::lock_guard lock(mutex_); - memory_buf_t formatted; - formatter_->format(msg, formatted); - ::fwrite(formatted.data(), sizeof(char), formatted.size(), file_); + std::lock_guard lock(mutex_); + memory_buf_t formatted; + formatter_->format(msg, formatted); + ::fwrite(formatted.data(), sizeof(char), formatted.size(), file_); #endif // WIN32 - ::fflush(file_); // flush every line to terminal - } + ::fflush(file_); // flush every line to terminal +} - template - void stdout_sink_base::flush() - { - std::lock_guard lock(mutex_); - fflush(file_); - } +template +void stdout_sink_base::flush() +{ + std::lock_guard lock(mutex_); + fflush(file_); +} - template - void stdout_sink_base::set_pattern(const std::string &pattern) - { - std::lock_guard lock(mutex_); - formatter_ = std::unique_ptr(new pattern_formatter(pattern)); - } +template +void stdout_sink_base::set_pattern(const std::string &pattern) +{ + std::lock_guard lock(mutex_); + formatter_ = std::unique_ptr(new pattern_formatter(pattern)); +} - template - void stdout_sink_base::set_formatter(std::unique_ptr sink_formatter) - { - std::lock_guard lock(mutex_); - formatter_ = std::move(sink_formatter); - } +template +void stdout_sink_base::set_formatter(std::unique_ptr sink_formatter) +{ + std::lock_guard lock(mutex_); + formatter_ = std::move(sink_formatter); +} // stdout sink - template - stdout_sink::stdout_sink() - : stdout_sink_base(stdout) - {} +template +stdout_sink::stdout_sink() + : stdout_sink_base(stdout) +{} // stderr sink - template - stderr_sink::stderr_sink() - : stdout_sink_base(stderr) - {} +template +stderr_sink::stderr_sink() + : stdout_sink_base(stderr) +{} - } // namespace sinks +} // namespace sinks // factory methods - template - std::shared_ptr stdout_logger_mt(const std::string &logger_name) - { - return Factory::template create(logger_name); - } +template +std::shared_ptr stdout_logger_mt(const std::string &logger_name) +{ + return Factory::template create(logger_name); +} - template - std::shared_ptr stdout_logger_st(const std::string &logger_name) - { - return Factory::template create(logger_name); - } +template +std::shared_ptr stdout_logger_st(const std::string &logger_name) +{ + return Factory::template create(logger_name); +} - template - std::shared_ptr stderr_logger_mt(const std::string &logger_name) - { - return Factory::template create(logger_name); - } +template +std::shared_ptr stderr_logger_mt(const std::string &logger_name) +{ + return Factory::template create(logger_name); +} - template - std::shared_ptr stderr_logger_st(const std::string &logger_name) - { - return Factory::template create(logger_name); - } +template +std::shared_ptr stderr_logger_st(const std::string &logger_name) +{ + return Factory::template create(logger_name); +} } // namespace spdlog // template instantiations for stdout/stderr loggers @@ -153,5 +153,3 @@ template SPDLOG_API std::shared_ptr spdlog::stdout_logger_mt spdlog::stdout_logger_st(const std::string &logger_name); template SPDLOG_API std::shared_ptr spdlog::stderr_logger_mt(const std::string &logger_name); template SPDLOG_API std::shared_ptr spdlog::stderr_logger_st(const std::string &logger_name); - - diff --git a/src/sinks/wincolor_sink.cpp b/src/sinks/wincolor_sink.cpp index 922d2ba1..f4574bcf 100644 --- a/src/sinks/wincolor_sink.cpp +++ b/src/sinks/wincolor_sink.cpp @@ -2,20 +2,20 @@ // Distributed under the MIT License (http://opensource.org/licenses/MIT) #ifdef _WIN32 -#pragma once +# pragma once -#include +# include -#include -#include +# include +# include -#include -#include +# include +# include namespace spdlog { namespace sinks { template - wincolor_sink::wincolor_sink(void *out_handle, color_mode mode) +wincolor_sink::wincolor_sink(void *out_handle, color_mode mode) : out_handle_(out_handle) , mutex_(ConsoleMutex::mutex()) , formatter_(std::make_unique()) @@ -24,7 +24,7 @@ template 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::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 @@ -34,21 +34,21 @@ template } template - wincolor_sink::~wincolor_sink() +wincolor_sink::~wincolor_sink() { this->flush(); } // change the color for the given level template -void wincolor_sink::set_color(level level, std::uint16_t color) +void wincolor_sink::set_color(level level, std::uint16_t color) { std::lock_guard lock(mutex_); colors_[level_to_number(level)] = color; } template -void wincolor_sink::log(const details::log_msg &msg) +void wincolor_sink::log(const details::log_msg &msg) { if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE) { @@ -78,34 +78,34 @@ void wincolor_sink::log(const details::log_msg &msg) } template -void wincolor_sink::flush() +void wincolor_sink::flush() { // windows console always flushed? } template -void wincolor_sink::set_pattern(const std::string &pattern) +void wincolor_sink::set_pattern(const std::string &pattern) { std::lock_guard lock(mutex_); formatter_ = std::unique_ptr(new pattern_formatter(pattern)); } template -void wincolor_sink::set_formatter(std::unique_ptr sink_formatter) +void wincolor_sink::set_formatter(std::unique_ptr sink_formatter) { std::lock_guard lock(mutex_); formatter_ = std::move(sink_formatter); } template -void wincolor_sink::set_color_mode(color_mode mode) +void wincolor_sink::set_color_mode(color_mode mode) { std::lock_guard lock(mutex_); set_color_mode_impl(mode); } template -void wincolor_sink::set_color_mode_impl(color_mode mode) +void wincolor_sink::set_color_mode_impl(color_mode mode) { if (mode == color_mode::automatic) { @@ -122,7 +122,7 @@ void wincolor_sink::set_color_mode_impl(color_mode mode) // set foreground color and return the orig console attributes (for resetting later) template -std::uint16_t wincolor_sink::set_foreground_color_(std::uint16_t attribs) +std::uint16_t wincolor_sink::set_foreground_color_(std::uint16_t attribs) { CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; if (!::GetConsoleScreenBufferInfo(static_cast(out_handle_), &orig_buffer_info)) @@ -140,7 +140,7 @@ std::uint16_t wincolor_sink::set_foreground_color_(std::uint16_t // print a range of formatted message to console template -void wincolor_sink::print_range_(const memory_buf_t &formatted, size_t start, size_t end) +void wincolor_sink::print_range_(const memory_buf_t &formatted, size_t start, size_t end) { if (end > start) { @@ -151,7 +151,7 @@ void wincolor_sink::print_range_(const memory_buf_t &formatted, s } template -void wincolor_sink::write_to_file_(const memory_buf_t &formatted) +void wincolor_sink::write_to_file_(const memory_buf_t &formatted) { auto size = static_cast(formatted.size()); DWORD bytes_written = 0; @@ -161,19 +161,18 @@ void wincolor_sink::write_to_file_(const memory_buf_t &formatted) // wincolor_stdout_sink template - wincolor_stdout_sink::wincolor_stdout_sink(color_mode mode) +wincolor_stdout_sink::wincolor_stdout_sink(color_mode mode) : wincolor_sink(::GetStdHandle(STD_OUTPUT_HANDLE), mode) {} // wincolor_stderr_sink template - wincolor_stderr_sink::wincolor_stderr_sink(color_mode mode) +wincolor_stderr_sink::wincolor_stderr_sink(color_mode mode) : wincolor_sink(::GetStdHandle(STD_ERROR_HANDLE), mode) {} } // namespace sinks } // namespace spdlog - // template instantiations template SPDLOG_API class spdlog::sinks::wincolor_stdout_sink; template SPDLOG_API class spdlog::sinks::wincolor_stdout_sink; diff --git a/src/spdlog.cpp b/src/spdlog.cpp index b30e389e..881c8dee 100644 --- a/src/spdlog.cpp +++ b/src/spdlog.cpp @@ -12,92 +12,92 @@ void initialize_logger(std::shared_ptr logger) details::registry::instance().initialize_logger(std::move(logger)); } - std::shared_ptr get(const std::string &name) +std::shared_ptr get(const std::string &name) { return details::registry::instance().get(name); } - void set_formatter(std::unique_ptr formatter) +void set_formatter(std::unique_ptr formatter) { details::registry::instance().set_formatter(std::move(formatter)); } - void set_pattern(std::string pattern, pattern_time_type time_type) +void set_pattern(std::string pattern, pattern_time_type time_type) { set_formatter(std::unique_ptr(new pattern_formatter(std::move(pattern), time_type))); } - level get_level() +level get_level() { return default_logger_raw()->log_level(); } - bool should_log(level level) +bool should_log(level level) { return default_logger_raw()->should_log(level); } - void set_level(level level) +void set_level(level level) { details::registry::instance().set_level(level); } - void flush_on(level level) +void flush_on(level level) { details::registry::instance().flush_on(level); } - void set_error_handler(void (*handler)(const std::string &msg)) +void set_error_handler(void (*handler)(const std::string &msg)) { details::registry::instance().set_error_handler(handler); } - void register_logger(std::shared_ptr logger) +void register_logger(std::shared_ptr logger) { details::registry::instance().register_logger(std::move(logger)); } - void apply_all(const std::function)> &fun) +void apply_all(const std::function)> &fun) { details::registry::instance().apply_all(fun); } - void drop(const std::string &name) +void drop(const std::string &name) { details::registry::instance().drop(name); } - void drop_all() +void drop_all() { details::registry::instance().drop_all(); } - void shutdown() +void shutdown() { details::registry::instance().shutdown(); } - void set_automatic_registration(bool automatic_registration) +void set_automatic_registration(bool automatic_registration) { details::registry::instance().set_automatic_registration(automatic_registration); } - std::shared_ptr default_logger() +std::shared_ptr default_logger() { return details::registry::instance().default_logger(); } - spdlog::logger *default_logger_raw() +spdlog::logger *default_logger_raw() { return details::registry::instance().get_default_raw(); } - void set_default_logger(std::shared_ptr default_logger) +void set_default_logger(std::shared_ptr default_logger) { details::registry::instance().set_default_logger(std::move(default_logger)); } - void apply_logger_env_levels(std::shared_ptr logger) +void apply_logger_env_levels(std::shared_ptr logger) { details::registry::instance().apply_logger_env_levels(std::move(logger)); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ce9c302f..8bf85e35 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -47,8 +47,7 @@ set(SPDLOG_UTESTS_SOURCES test_circular_q.cpp test_ringbuffer_sink.cpp test_source_location.cpp - test_log_level.cpp -) + test_log_level.cpp) if(NOT SPDLOG_NO_EXCEPTIONS) list(APPEND SPDLOG_UTESTS_SOURCES test_errors.cpp) @@ -82,4 +81,3 @@ endfunction() if(SPDLOG_BUILD_TESTS OR SPDLOG_BUILD_ALL) spdlog_prepare_test(spdlog-utests spdlog::spdlog) endif() - diff --git a/tests/test_log_level.cpp b/tests/test_log_level.cpp index 57d1a9db..69f17b39 100644 --- a/tests/test_log_level.cpp +++ b/tests/test_log_level.cpp @@ -8,11 +8,10 @@ TEST_CASE("test_default_level", "[log_level]") auto test_sink = std::make_shared(); REQUIRE(test_sink->log_level() == spdlog::level::trace); spdlog::logger logger("test-level", test_sink); - REQUIRE(test_sink->log_level() == spdlog::level::trace);\ + REQUIRE(test_sink->log_level() == spdlog::level::trace); REQUIRE(logger.log_level() == spdlog::level::info); } - // test that logger log only messages with level bigger or equal to its level void test_logger_level(spdlog::level level); TEST_CASE("test_logger_levels", "[log_level]") @@ -24,7 +23,6 @@ TEST_CASE("test_logger_levels", "[log_level]") } } - // test that logger log all messages with level bigger or equal to its level void test_sink_level(spdlog::level level); TEST_CASE("test_sink_levels", "[log_level]") @@ -36,7 +34,6 @@ TEST_CASE("test_sink_levels", "[log_level]") } } - // // test helpers to check that logger/sink displays only messages with level bigger or equal to its level // @@ -44,15 +41,11 @@ auto get_expected_messages(spdlog::level level) { // expected messages for each level static const std::map> messages = { - {spdlog::level::trace, {"trace hello", "debug hello", "info hello", "warning hello", "error hello", "critical hello"}}, - {spdlog::level::debug, {"debug hello", "info hello", "warning hello", "error hello", "critical hello"}}, - {spdlog::level::info, {"info hello", "warning hello", "error hello", "critical hello"}}, - {spdlog::level::warn, {"warning hello", "error hello", "critical hello"}}, - {spdlog::level::err, {"error hello", "critical hello"}}, - {spdlog::level::critical, {"critical hello"}}, - {spdlog::level::off, {}}, - {spdlog::level::n_levels, {}} - }; + {spdlog::level::trace, {"trace hello", "debug hello", "info hello", "warning hello", "error hello", "critical hello"}}, + {spdlog::level::debug, {"debug hello", "info hello", "warning hello", "error hello", "critical hello"}}, + {spdlog::level::info, {"info hello", "warning hello", "error hello", "critical hello"}}, + {spdlog::level::warn, {"warning hello", "error hello", "critical hello"}}, {spdlog::level::err, {"error hello", "critical hello"}}, + {spdlog::level::critical, {"critical hello"}}, {spdlog::level::off, {}}, {spdlog::level::n_levels, {}}}; return messages.at(level); } @@ -73,10 +66,9 @@ void test_logger_level(spdlog::level level) logger.critical("hello"); auto lines = test_sink->lines(); - REQUIRE(lines == get_expected_messages(level)); + REQUIRE(lines == get_expected_messages(level)); } - // test that sink displays all messages with level bigger or equal to its level void test_sink_level(spdlog::level level) { @@ -98,5 +90,5 @@ void test_sink_level(spdlog::level level) logger.critical("hello"); auto lines = test_sink->lines(); - REQUIRE(lines == get_expected_messages(level)); + REQUIRE(lines == get_expected_messages(level)); } \ No newline at end of file