diff --git a/example/example.cpp b/example/example.cpp index e11b8369..032e62c5 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -1,4 +1,4 @@ -// +// // Copyright(c) 2015 Gabi Melman. // Distributed under the MIT License (http://opensource.org/licenses/MIT) @@ -173,6 +173,7 @@ void trace_example() { // stopwatch example #include + #include "spdlog/stopwatch.h" void stopwatch_example() { spdlog::stopwatch sw; @@ -227,7 +228,7 @@ void err_handler_example() { // syslog example (linux/osx/freebsd) #ifndef _WIN32 -#include "spdlog/sinks/syslog_sink.h" + #include "spdlog/sinks/syslog_sink.h" void syslog_example() { std::string ident = "spdlog-example"; auto syslog_logger = spdlog::syslog_logger_mt("syslog", ident, LOG_PID); @@ -237,7 +238,7 @@ void syslog_example() { // Android example. #if defined(__ANDROID__) -#include "spdlog/sinks/android_sink.h" + #include "spdlog/sinks/android_sink.h" void android_example() { std::string tag = "spdlog-android"; auto android_logger = spdlog::android_logger_mt("android", tag); diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 7a9aee2e..8b70901f 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -5,12 +5,12 @@ #include #include +#include #include #include #include #include #include -#include #include #include "./source_loc.h" diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index f6c6d445..346e442d 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -5,6 +5,7 @@ #include // std::time_t #include + #include "../common.h" #include "../filename_t.h" @@ -66,7 +67,6 @@ SPDLOG_API void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target); SPDLOG_API void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target); #endif - // non thread safe, cross platform getenv/getenv_s // return empty string if field not found SPDLOG_API std::string getenv(const char *field); @@ -106,7 +106,6 @@ SPDLOG_API bool rename(const filename_t &filename1, const filename_t &filename2) // Return if file exists. SPDLOG_API bool path_exists(const filename_t &filename) noexcept; - // Return file path and its extension: // // "mylog.txt" => ("mylog", ".txt") @@ -124,7 +123,6 @@ SPDLOG_API std::tuple split_by_extension(const filename_ // Try tp convert filename to string. Return "??" if failed SPDLOG_API std::string filename_to_str(const filename_t &filename); - } // namespace os } // namespace details } // namespace spdlog diff --git a/include/spdlog/file_event_handlers.h b/include/spdlog/file_event_handlers.h index 4a53df7f..f244f6d7 100644 --- a/include/spdlog/file_event_handlers.h +++ b/include/spdlog/file_event_handlers.h @@ -1,6 +1,7 @@ #pragma once #include + #include "./filename_t.h" namespace spdlog { diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 68477707..80145dbb 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -11,13 +11,12 @@ #include #include "../common.h" -#include "./base_sink.h" #include "../details/circular_q.h" #include "../details/file_helper.h" #include "../details/null_mutex.h" #include "../details/os.h" #include "../details/synchronous_factory.h" - +#include "./base_sink.h" namespace spdlog { namespace sinks { @@ -25,21 +24,21 @@ namespace sinks { /* * Generator of daily log file names in format basename_YYYY-MM-DD.ext */ -struct daily_filename_calculator { +struct daily_filename_calculator { static filename_t calc_filename(const filename_t &filename, const tm &now_tm) { filename_t basename, ext; std::tie(basename, ext) = details::os::split_by_extension(filename); - std::basic_ostringstream oss; + std::basic_ostringstream oss; oss << basename.native() << '_' << std::setfill(SPDLOG_FILENAME_T('0')) << std::setw(4) << now_tm.tm_year + 1900 << '-' << std::setw(2) << now_tm.tm_mon + 1 << '-' << std::setw(2) << now_tm.tm_mday << ext.native(); - return oss.str(); + return oss.str(); } }; /* * Generator of daily log file names with strftime format. * Usages: - * + * * std::make_shared("myapp-%Y-%m-%d:%H:%M:%S.log", hour, minute); * or * spdlog::daily_logger_format_mt("loggername, "myapp-%Y-%m-%d:%X.log", hour, minute)" diff --git a/include/spdlog/sinks/hourly_file_sink.h b/include/spdlog/sinks/hourly_file_sink.h index 23a6e77b..cff218f0 100644 --- a/include/spdlog/sinks/hourly_file_sink.h +++ b/include/spdlog/sinks/hourly_file_sink.h @@ -48,9 +48,9 @@ class hourly_file_sink final : public base_sink { public: // create hourly file sink which rotates on given time explicit hourly_file_sink(filename_t base_filename, - bool truncate = false, - uint16_t max_files = 0, - const file_event_handlers &event_handlers = {}) + bool truncate = false, + uint16_t max_files = 0, + const file_event_handlers &event_handlers = {}) : base_filename_(std::move(base_filename)), file_helper_{event_handlers}, truncate_(truncate), diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index af1f748c..eb6ba4b2 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -53,7 +53,6 @@ SPDLOG_API bool should_log(level level); // Set flush level of the global logger. SPDLOG_API void flush_on(level level); - // Set error handler for the global logger SPDLOG_API void set_error_handler(void (*handler)(const std::string &msg)); diff --git a/src/details/context.cpp b/src/details/context.cpp index 6b291f38..580a5e21 100644 --- a/src/details/context.cpp +++ b/src/details/context.cpp @@ -2,10 +2,11 @@ // Distributed under the MIT License (http://opensource.org/licenses/MIT) #include "spdlog/details/context.h" + #include "spdlog/logger.h" #ifndef SPDLOG_DISABLE_GLOBAL_LOGGER -#include "spdlog/sinks/stdout_color_sinks.h" + #include "spdlog/sinks/stdout_color_sinks.h" #endif // SPDLOG_DISABLE_GLOBAL_LOGGER #include @@ -13,24 +14,18 @@ namespace spdlog { namespace details { -context::context(std::unique_ptr global_logger) { - global_logger_ = std::move(global_logger); -} +context::context(std::unique_ptr global_logger) { global_logger_ = std::move(global_logger); } -std::shared_ptr context::global_logger() { - return global_logger_; -} +std::shared_ptr context::global_logger() { return global_logger_; } // Return raw ptr to the global logger. // 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_global_logger(). // e.g do not call set_global_logger() from one thread while calling spdlog::info() from another. -logger *context::global_logger_raw() const noexcept{ return global_logger_.get(); } +logger *context::global_logger_raw() const noexcept { return global_logger_.get(); } // set global logger -void context::set_logger(std::shared_ptr new_global_logger) { - global_logger_ = std::move(new_global_logger); -} +void context::set_logger(std::shared_ptr new_global_logger) { global_logger_ = std::move(new_global_logger); } void context::set_tp(std::shared_ptr tp) { std::lock_guard lock(tp_mutex_); diff --git a/src/details/file_helper.cpp b/src/details/file_helper.cpp index 17016be3..dd406075 100644 --- a/src/details/file_helper.cpp +++ b/src/details/file_helper.cpp @@ -1,13 +1,13 @@ // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. // Distributed under the MIT License (http://opensource.org/licenses/MIT) +#include "spdlog/details/file_helper.h" #include #include -#include #include +#include -#include "spdlog/details/file_helper.h" #include "spdlog/common.h" #include "spdlog/details/os.h" @@ -110,6 +110,5 @@ size_t file_helper::size() const { const filename_t &file_helper::filename() const { return filename_; } - } // namespace details } // namespace spdlog diff --git a/src/details/os_filesystem.cpp b/src/details/os_filesystem.cpp index d7d700f5..45c53c55 100644 --- a/src/details/os_filesystem.cpp +++ b/src/details/os_filesystem.cpp @@ -11,9 +11,7 @@ namespace spdlog { namespace details { namespace os { -bool remove(const filename_t &filename) { - return std::filesystem::remove(filename); -} +bool remove(const filename_t &filename) { return std::filesystem::remove(filename); } bool remove_if_exists(const filename_t &filename) { if (path_exists(filename)) { diff --git a/src/details/os_unix.cpp b/src/details/os_unix.cpp index 98550bc3..5149828b 100644 --- a/src/details/os_unix.cpp +++ b/src/details/os_unix.cpp @@ -99,7 +99,6 @@ bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) { return *fp == nullptr; } - // Return file size according to open FILE* object size_t filesize(FILE *f) { if (f == nullptr) { @@ -231,9 +230,9 @@ void sleep_for_millis(unsigned int milliseconds) noexcept { std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); } -std::string filename_to_str(const filename_t &filename) { +std::string filename_to_str(const filename_t &filename) { static_assert(std::is_same_v, "filename_t type must be char"); - return filename; + return filename; } int pid() noexcept { return static_cast(::getpid()); } diff --git a/src/details/os_windows.cpp b/src/details/os_windows.cpp index 1a3d6851..e9ea0b15 100644 --- a/src/details/os_windows.cpp +++ b/src/details/os_windows.cpp @@ -75,8 +75,6 @@ bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) { return *fp == nullptr; } - - #ifdef _MSC_VER // avoid warning about unreachable statement at the end of filesize() #pragma warning(push) @@ -155,11 +153,9 @@ std::string filename_to_str(const filename_t &filename) { memory_buf_t buf; wstr_to_utf8buf(filename.wstring(), buf); return std::string(buf.data(), buf.size()); + } catch (...) { + return "???"; } - catch (...) { - return "???"; - } - } int pid() noexcept { return static_cast(::GetCurrentProcessId()); } @@ -224,7 +220,6 @@ void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) { throw_spdlog_ex(fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError())); } - std::string getenv(const char *field) { #if defined(_MSC_VER) #if defined(__cplusplus_winrt) diff --git a/src/sinks/rotating_file_sink.cpp b/src/sinks/rotating_file_sink.cpp index e55845c6..776dbf0f 100644 --- a/src/sinks/rotating_file_sink.cpp +++ b/src/sinks/rotating_file_sink.cpp @@ -5,9 +5,9 @@ #include #include +#include #include #include -#include #include "spdlog/common.h" #include "spdlog/details/file_helper.h" @@ -51,10 +51,10 @@ filename_t rotating_file_sink::calc_filename(const filename_t &filename, filename_t basename; filename_t ext; - std::tie(basename, ext) = details::os::split_by_extension(filename); + std::tie(basename, ext) = details::os::split_by_extension(filename); std::basic_ostringstream oss; oss << basename.native() << '.' << index << ext.native(); - return oss.str(); + return oss.str(); } template @@ -132,7 +132,7 @@ void rotating_file_sink::rotate_() { // 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) noexcept{ +bool rotating_file_sink::rename_file_(const filename_t &src_filename, const filename_t &target_filename) noexcept { return details::os::rename(src_filename, target_filename); } diff --git a/src/spdlog.cpp b/src/spdlog.cpp index 9eba7fb0..f364fa3c 100644 --- a/src/spdlog.cpp +++ b/src/spdlog.cpp @@ -1,10 +1,11 @@ // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. // Distributed under the MIT License (http://opensource.org/licenses/MIT) -#include -#include - #include "spdlog/spdlog.h" + +#include +#include + #include "spdlog/common.h" #include "spdlog/logger.h" #include "spdlog/pattern_formatter.h" @@ -16,20 +17,18 @@ static std::shared_ptr s_context = #ifndef SPDLOG_DISABLE_GLOBAL_LOGGER std::make_unique(std::make_unique(std::string(), std::make_unique())); #else - std::make_unique(); // empty context + std::make_unique(); // empty context #endif void set_context(std::shared_ptr context) { s_context = std::move(context); } -std::shared_ptr context() {return s_context;} +std::shared_ptr context() { return s_context; } -const std::shared_ptr &context_ref() {return s_context;} +const std::shared_ptr &context_ref() { return s_context; } std::shared_ptr global_logger() { return context_ref()->global_logger(); } -void set_global_logger(std::shared_ptr global_logger) { - context()->set_logger(std::move(global_logger)); -} +void set_global_logger(std::shared_ptr global_logger) { context()->set_logger(std::move(global_logger)); } logger *global_logger_raw() noexcept { auto *rv = context_ref()->global_logger_raw(); @@ -37,9 +36,7 @@ logger *global_logger_raw() noexcept { return rv; } -void set_formatter(std::unique_ptr formatter) { - global_logger()->set_formatter(std::move(formatter)); -} +void set_formatter(std::unique_ptr formatter) { global_logger()->set_formatter(std::move(formatter)); } void set_pattern(std::string pattern, pattern_time_type time_type) { set_formatter(std::make_unique(std::move(pattern), time_type)); @@ -55,7 +52,6 @@ void flush_on(level level) { global_logger()->flush_on(level); } void set_error_handler(void (*handler)(const std::string &msg)) { global_logger()->set_error_handler(handler); } -void shutdown() { s_context.reset();} - +void shutdown() { s_context.reset(); } } // namespace spdlog diff --git a/tests/test_async.cpp b/tests/test_async.cpp index ad5f00c0..8c8a7e66 100644 --- a/tests/test_async.cpp +++ b/tests/test_async.cpp @@ -88,7 +88,6 @@ TEST_CASE("flush", "[async]") { REQUIRE(test_sink->flush_counter() == 1); } - TEST_CASE("tp->wait_empty() ", "[async]") { auto test_sink = std::make_shared(); test_sink->set_delay(std::chrono::milliseconds(5)); diff --git a/tests/test_create_dir.cpp b/tests/test_create_dir.cpp index 55b8e255..0ae46d0b 100644 --- a/tests/test_create_dir.cpp +++ b/tests/test_create_dir.cpp @@ -43,8 +43,8 @@ TEST_CASE("create_invalid_dir", "[create_dir]") { } TEST_CASE("dir_name", "[create_dir]") { - using spdlog::details::os::dir_name; -#ifdef WIN32 + using spdlog::details::os::dir_name; +#ifdef WIN32 REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\)")) == SPDLOG_FILENAME_T("dir")); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\\\)")) == SPDLOG_FILENAME_T(R"(dir)")); REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\file)")) == SPDLOG_FILENAME_T("dir")); @@ -60,19 +60,19 @@ TEST_CASE("dir_name", "[create_dir]") { REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(c://a/b/c/d/file.txt)")) == SPDLOG_FILENAME_T(R"(c://a/b/c/d)")); #endif REQUIRE(dir_name(SPDLOG_FILENAME_T("")).empty()); - REQUIRE(dir_name(SPDLOG_FILENAME_T("dir")).empty()); + REQUIRE(dir_name(SPDLOG_FILENAME_T("dir")).empty()); REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/")) == SPDLOG_FILENAME_T("dir")); REQUIRE(dir_name(SPDLOG_FILENAME_T("dir///")) == SPDLOG_FILENAME_T("dir")); REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/file")) == SPDLOG_FILENAME_T("dir")); REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/file.txt")) == SPDLOG_FILENAME_T("dir")); REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/file.txt/")) == SPDLOG_FILENAME_T("dir/file.txt")); - REQUIRE(dir_name(SPDLOG_FILENAME_T("/dir/file.txt")) == SPDLOG_FILENAME_T("/dir")); + REQUIRE(dir_name(SPDLOG_FILENAME_T("/dir/file.txt")) == SPDLOG_FILENAME_T("/dir")); REQUIRE(dir_name(SPDLOG_FILENAME_T("../file.txt")) == SPDLOG_FILENAME_T("..")); REQUIRE(dir_name(SPDLOG_FILENAME_T("./file.txt")) == SPDLOG_FILENAME_T(".")); -#ifdef _WIN32 +#ifdef _WIN32 REQUIRE(dir_name(SPDLOG_FILENAME_T("//dir/file.txt")) == SPDLOG_FILENAME_T("//dir/")); -#else +#else REQUIRE(dir_name(SPDLOG_FILENAME_T("//dir/file.txt")) == SPDLOG_FILENAME_T("//dir")); #endif } diff --git a/tests/test_daily_and_rotation_loggers.cpp b/tests/test_daily_and_rotation_loggers.cpp index 757404b5..0bc21444 100644 --- a/tests/test_daily_and_rotation_loggers.cpp +++ b/tests/test_daily_and_rotation_loggers.cpp @@ -4,8 +4,8 @@ */ #include "includes.h" #include "spdlog/sinks/daily_file_sink.h" -#include "spdlog/sinks/rotating_file_sink.h" #include "spdlog/sinks/hourly_file_sink.h" +#include "spdlog/sinks/rotating_file_sink.h" using filename_memory_buf_t = spdlog::memory_buf_t; @@ -48,8 +48,7 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]") { std::tm tm = spdlog::details::os::localtime(); auto w = spdlog::fmt_lib::format(SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename.native(), tm.tm_year + 1900, - tm.tm_mon + 1, - tm.tm_mday); + tm.tm_mon + 1, tm.tm_mday); auto logger = spdlog::create("logger", basename, 0, 0); for (int i = 0; i < 10; ++i) { @@ -82,26 +81,24 @@ TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]") { // regex supported only from gcc 4.9 and above #if defined(_MSC_VER) || !(__GNUC__ <= 4 && __GNUC_MINOR__ < 9) -#include + #include TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]") { // daily_YYYY-MM-DD_hh-mm.txt - auto filename = - spdlog::sinks::daily_filename_calculator::calc_filename(SPDLOG_FILENAME_T("daily.txt"), - spdlog::details::os::localtime()); + auto filename = + spdlog::sinks::daily_filename_calculator::calc_filename(SPDLOG_FILENAME_T("daily.txt"), spdlog::details::os::localtime()); // date regex based on https://www.regular-expressions.info/dates.html - std::basic_regex re( - SPDLOG_FILENAME_T(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)")); - - std::match_results match; - REQUIRE(std::regex_match(filename.native(), match, re)); -} + std::basic_regex re( + SPDLOG_FILENAME_T(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)")); + std::match_results match; + REQUIRE(std::regex_match(filename.native(), match, re)); +} TEST_CASE("hourly_file_sink::hourly_filename_calculator", "[hrouly_file_sink]") { // daily_YYYY-MM-DD_hh-mm.txt - auto filename = - spdlog::sinks::hourly_filename_calculator::calc_filename(SPDLOG_FILENAME_T("hourly.txt"), spdlog::details::os::localtime()); + auto filename = spdlog::sinks::hourly_filename_calculator::calc_filename(SPDLOG_FILENAME_T("hourly.txt"), + spdlog::details::os::localtime()); // date regex based on https://www.regular-expressions.info/dates.html std::basic_regex re( SPDLOG_FILENAME_T(R"(^hourly_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d\.txt$)")); diff --git a/tests/test_errors.cpp b/tests/test_errors.cpp index 0a73d68f..e3914e5d 100644 --- a/tests/test_errors.cpp +++ b/tests/test_errors.cpp @@ -44,7 +44,6 @@ TEST_CASE("custom_error_handler", "[errors]") { } TEST_CASE("default_error_handler2", "[errors]") { - auto logger = std::make_shared("failed_logger", std::make_shared()); logger->set_error_handler([=](const std::string &) { throw custom_ex(); }); REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex); diff --git a/tests/utils.h b/tests/utils.h index f935fb83..af66a937 100644 --- a/tests/utils.h +++ b/tests/utils.h @@ -1,8 +1,8 @@ #pragma once #include -#include #include +#include std::size_t count_files(const std::string &folder); @@ -10,7 +10,7 @@ void prepare_logdir(); std::string file_contents(const std::string &filename); -//std::size_t count_lines(const std::string &filename); +// std::size_t count_lines(const std::string &filename); std::size_t count_lines(const std::filesystem::path &filename); void require_message_count(const std::filesystem::path &filename, const std::size_t messages);