From e62be8b43feeb6e04374ae936be29c8a36624fa0 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 1 Dec 2024 23:59:09 +0200 Subject: [PATCH] clang-format --- example/example.cpp | 4 ++- include/spdlog/common.h | 1 - include/spdlog/details/os.h | 6 ++-- include/spdlog/logger.h | 6 ++-- include/spdlog/sinks/daily_file_sink.h | 6 ++-- include/spdlog/sinks/systemd_sink.h | 15 ++++---- include/spdlog/source_loc.h | 2 +- src/async_logger.cpp | 8 +++-- src/details/log_msg.cpp | 5 ++- src/details/os_unix.cpp | 18 +++++----- src/details/os_windows.cpp | 42 ++++++++--------------- src/details/thread_pool.cpp | 2 +- src/logger.cpp | 4 ++- src/pattern_formatter.cpp | 3 +- src/sinks/sink.cpp | 4 +-- src/sinks/stdout_sinks.cpp | 4 +-- tests/test_daily_and_rotation_loggers.cpp | 1 - tests/test_misc.cpp | 23 ++++++------- 18 files changed, 69 insertions(+), 85 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index f1fdf62b..35e6c48a 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -284,7 +284,9 @@ public: } [[nodiscard]] - std::unique_ptr clone() const override { return std::make_unique(); } + std::unique_ptr clone() const override { + return std::make_unique(); + } }; void custom_flags_example() { diff --git a/include/spdlog/common.h b/include/spdlog/common.h index da8e1a09..ca039aa9 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -16,7 +16,6 @@ #include "./source_loc.h" - #if defined(SPDLOG_SHARED_LIB) #if defined(_WIN32) #ifdef spdlog_EXPORTS diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index d23c57fc..cb669458 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -76,11 +76,10 @@ SPDLOG_API bool is_color_terminal() noexcept; SPDLOG_API bool in_terminal(FILE *file) noexcept; #if (defined _WIN32) - 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); +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 - // Return directory name from given path or empty string // "abc/file" => "abc" // "abc/" => "abc" @@ -100,7 +99,6 @@ SPDLOG_API std::string getenv(const char *field); // Return true on success. SPDLOG_API bool fsync(FILE *fp); - // Do non-locking fwrite if possible by the os or use the regular locking fwrite // Return true on success. SPDLOG_API bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp); diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index fd009fbe..f8a79de2 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -22,7 +22,6 @@ #include "./details/log_msg.h" #include "./sinks/sink.h" - #define SPDLOG_LOGGER_CATCH(location) \ catch (const std::exception &ex) { \ if (!location.empty()) { \ @@ -36,7 +35,6 @@ throw; \ } - namespace spdlog { class SPDLOG_API logger { @@ -197,7 +195,9 @@ protected: assert(should_log(msg.log_level)); for (auto &sink : sinks_) { if (sink->should_log(msg.log_level)) { - try { sink->log(msg); } + try { + sink->log(msg); + } SPDLOG_LOGGER_CATCH(msg.source) } } diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index d8f2a100..d4b3ebac 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -45,9 +45,9 @@ struct daily_filename_calculator { */ struct daily_filename_format_calculator { static filename_t calc_filename(const filename_t &file_path, const tm &now_tm) { - std::stringstream stream; - stream << std::put_time(&now_tm, file_path.c_str()); - return stream.str(); + std::stringstream stream; + stream << std::put_time(&now_tm, file_path.c_str()); + return stream.str(); } }; diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index 6d8b8df7..9390d0de 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -68,17 +68,14 @@ protected: if (msg.source.empty()) { // Note: function call inside '()' to avoid macro expansion err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), payload.data(), "PRIORITY=%d", - syslog_level(msg.log_level), - "TID=%zu", msg.thread_id, - "SYSLOG_IDENTIFIER=%.*s", static_cast(syslog_identifier.size()), - syslog_identifier.data(), nullptr); + syslog_level(msg.log_level), "TID=%zu", msg.thread_id, "SYSLOG_IDENTIFIER=%.*s", + static_cast(syslog_identifier.size()), syslog_identifier.data(), nullptr); } else { err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), payload.data(), "PRIORITY=%d", - syslog_level(msg.log_level), - "TID=%zu", msg.thread_id, - "SYSLOG_IDENTIFIER=%.*s", static_cast(syslog_identifier.size()), - syslog_identifier.data(), "CODE_FILE=%s", msg.source.filename, "CODE_LINE=%d", - msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr); + syslog_level(msg.log_level), "TID=%zu", msg.thread_id, "SYSLOG_IDENTIFIER=%.*s", + static_cast(syslog_identifier.size()), syslog_identifier.data(), "CODE_FILE=%s", + msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, + nullptr); } if (err) { diff --git a/include/spdlog/source_loc.h b/include/spdlog/source_loc.h index cc9116ee..2ea19e54 100644 --- a/include/spdlog/source_loc.h +++ b/include/spdlog/source_loc.h @@ -4,7 +4,7 @@ #pragma once #include -namespace spdlog {// source location +namespace spdlog { // source location struct source_loc { constexpr source_loc() = default; constexpr source_loc(const char *filename_in, std::uint_least32_t line_in, const char *funcname_in) diff --git a/src/async_logger.cpp b/src/async_logger.cpp index 5e4db513..d891bb77 100644 --- a/src/async_logger.cpp +++ b/src/async_logger.cpp @@ -51,7 +51,9 @@ void spdlog::async_logger::flush_() { void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) { for (auto &sink : sinks_) { if (sink->should_log(msg.log_level)) { - try { sink->log(msg); } + try { + sink->log(msg); + } SPDLOG_LOGGER_CATCH(msg.source) } } @@ -63,7 +65,9 @@ void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) { void spdlog::async_logger::backend_flush_() { for (auto &sink : sinks_) { - try { sink->flush(); } + try { + sink->flush(); + } SPDLOG_LOGGER_CATCH(source_loc()) } } diff --git a/src/details/log_msg.cpp b/src/details/log_msg.cpp index fe5f705b..bd04a577 100644 --- a/src/details/log_msg.cpp +++ b/src/details/log_msg.cpp @@ -16,11 +16,10 @@ log_msg::log_msg(spdlog::log_clock::time_point log_time, : logger_name(a_logger_name), log_level(lvl), time(log_time), - #ifdef SPDLOG_NO_THREAD_ID - thread_id(0), + thread_id(0), #else - thread_id(os::thread_id()), + thread_id(os::thread_id()), #endif source(loc), payload(msg) { diff --git a/src/details/os_unix.cpp b/src/details/os_unix.cpp index 089e81e0..8319f633 100644 --- a/src/details/os_unix.cpp +++ b/src/details/os_unix.cpp @@ -109,7 +109,7 @@ int rename(const filename_t &filename1, const filename_t &filename2) noexcept { // Return true if path exists (file or directory) bool path_exists(const filename_t &filename) noexcept { - struct stat buffer{}; + struct stat buffer {}; return (::stat(filename.c_str(), &buffer) == 0); } @@ -127,7 +127,7 @@ size_t filesize(FILE *f) { #endif // 64 bits(but not in osx, linux/musl or cygwin, where fstat64 is deprecated) #if ((defined(__linux__) && defined(__GLIBC__)) || defined(__sun) || defined(_AIX)) && (defined(__LP64__) || defined(_LP64)) - struct stat64 st{}; + struct stat64 st {}; if (::fstat64(fd, &st) == 0) { return static_cast(st.st_size); } @@ -326,15 +326,13 @@ std::string getenv(const char *field) { // Return true on success bool fsync(FILE *fp) { return ::fsync(fileno(fp)) == 0; } - // Non locking ::fwrite if possible (SPDLOG_FWRITE_UNLOCKED defined) or use the regular locking fwrite -bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp) -{ - #if defined(SPDLOG_FWRITE_UNLOCKED) - return ::fwrite_unlocked(ptr, 1, n_bytes, fp) == n_bytes; - #else - return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes; - #endif +bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp) { +#if defined(SPDLOG_FWRITE_UNLOCKED) + return ::fwrite_unlocked(ptr, 1, n_bytes, fp) == n_bytes; +#else + return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes; +#endif } } // namespace os diff --git a/src/details/os_windows.cpp b/src/details/os_windows.cpp index 1e75065b..074a8c09 100644 --- a/src/details/os_windows.cpp +++ b/src/details/os_windows.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -23,7 +24,6 @@ #include #include #include -#include #include "spdlog/common.h" #include "spdlog/details/os.h" @@ -76,9 +76,7 @@ bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) { return *fp == nullptr; } -int remove(const filename_t &filename) noexcept { - return std::remove(filename.c_str()); -} +int remove(const filename_t &filename) noexcept { return std::remove(filename.c_str()); } int remove_if_exists(const filename_t &filename) noexcept { return path_exists(filename) ? remove(filename) : 0; } @@ -169,7 +167,6 @@ bool is_color_terminal() noexcept { return true; } // Determine if the terminal attached bool in_terminal(FILE *file) noexcept { return ::_isatty(_fileno(file)) != 0; } - void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) { if (wstr.size() > static_cast((std::numeric_limits::max)()) / 4 - 1) { throw_spdlog_ex("UTF-16 string is too big to be converted to UTF-8"); @@ -183,14 +180,12 @@ void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) { int result_size = static_cast(target.capacity()); if ((wstr_size + 1) * 4 > result_size) { - result_size = - ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL); + result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL); } if (result_size > 0) { target.resize(result_size); - result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(), - result_size, NULL, NULL); + result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(), result_size, NULL, NULL); if (result_size > 0) { target.resize(result_size); @@ -198,8 +193,7 @@ void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) { } } - throw_spdlog_ex( - fmt_lib::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError())); + throw_spdlog_ex(fmt_lib::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError())); } void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) { @@ -214,27 +208,22 @@ void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) { } // find the size to allocate for the result buffer - int result_size = - ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, NULL, 0); + int result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, NULL, 0); if (result_size > 0) { target.resize(result_size); - result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, target.data(), - result_size); + result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, target.data(), result_size); if (result_size > 0) { assert(result_size == target.size()); return; } } - throw_spdlog_ex( - fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError())); + throw_spdlog_ex(fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError())); } // return true on success -static bool mkdir_(const filename_t &path) { - return ::_mkdir(path.c_str()) == 0; -} +static bool mkdir_(const filename_t &path) { return ::_mkdir(path.c_str()) == 0; } // create the given directory - and all directories leading to it // return true on success or if the directory already exists @@ -305,13 +294,12 @@ std::string getenv(const char *field) { bool fsync(FILE *fp) { return FlushFileBuffers(reinterpret_cast(_get_osfhandle(_fileno(fp)))) != 0; } // Non locking fwrite if possible (SPDLOG_FWRITE_UNLOCKED defined) or use the regular locking fwrite -bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp) -{ - #if defined(SPDLOG_FWRITE_UNLOCKED) - return _fwrite_nolock(ptr, 1, n_bytes, fp) == n_bytes; - #else - return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes; - #endif +bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp) { +#if defined(SPDLOG_FWRITE_UNLOCKED) + return _fwrite_nolock(ptr, 1, n_bytes, fp) == n_bytes; +#else + return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes; +#endif } } // namespace os } // namespace details diff --git a/src/details/thread_pool.cpp b/src/details/thread_pool.cpp index 7bd0b93f..584241f4 100644 --- a/src/details/thread_pool.cpp +++ b/src/details/thread_pool.cpp @@ -45,8 +45,8 @@ thread_pool::~thread_pool() { for (auto &t : threads_) { t.join(); } + } catch (...) { } - catch (...) {} } void thread_pool::post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy) { diff --git a/src/logger.cpp b/src/logger.cpp index 2e6b99ae..600efdc1 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -75,7 +75,9 @@ std::shared_ptr logger::clone(std::string logger_name) { // private/protected methods void logger::flush_() { for (auto &sink : sinks_) { - try { sink->flush(); } + try { + sink->flush(); + } SPDLOG_LOGGER_CATCH(source_loc()) } } diff --git a/src/pattern_formatter.cpp b/src/pattern_formatter.cpp index 855c5193..90dcb73b 100644 --- a/src/pattern_formatter.cpp +++ b/src/pattern_formatter.cpp @@ -148,7 +148,8 @@ public: }; // Full weekday name -static constexpr std::array full_days{{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}}; +static constexpr std::array full_days{ + {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}}; template class A_formatter final : public flag_formatter { diff --git a/src/sinks/sink.cpp b/src/sinks/sink.cpp index 8ecead60..c0d8509e 100644 --- a/src/sinks/sink.cpp +++ b/src/sinks/sink.cpp @@ -11,6 +11,4 @@ bool spdlog::sinks::sink::should_log(spdlog::level msg_level) const { void spdlog::sinks::sink::set_level(level level) { level_.store(level, std::memory_order_relaxed); } -spdlog::level spdlog::sinks::sink::log_level() const { - return level_.load(std::memory_order_relaxed); -} +spdlog::level spdlog::sinks::sink::log_level() const { return level_.load(std::memory_order_relaxed); } diff --git a/src/sinks/stdout_sinks.cpp b/src/sinks/stdout_sinks.cpp index e8720733..ad081d8f 100644 --- a/src/sinks/stdout_sinks.cpp +++ b/src/sinks/stdout_sinks.cpp @@ -1,12 +1,12 @@ // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. // Distributed under the MIT License (http://opensource.org/licenses/MIT) +#include "spdlog/sinks/stdout_sinks.h" #include -#include "spdlog/pattern_formatter.h" -#include "spdlog/sinks/stdout_sinks.h" #include "spdlog/details/os.h" +#include "spdlog/pattern_formatter.h" // clang-format off #ifdef _WIN32 diff --git a/tests/test_daily_and_rotation_loggers.cpp b/tests/test_daily_and_rotation_loggers.cpp index 630271dd..6505cc1b 100644 --- a/tests/test_daily_and_rotation_loggers.cpp +++ b/tests/test_daily_and_rotation_loggers.cpp @@ -8,7 +8,6 @@ using filename_memory_buf_t = spdlog::memory_buf_t; - TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") { using sink_type = spdlog::sinks::daily_file_sink; diff --git a/tests/test_misc.cpp b/tests/test_misc.cpp index 8ec72bb3..a2b335c2 100644 --- a/tests/test_misc.cpp +++ b/tests/test_misc.cpp @@ -1,17 +1,17 @@ -#ifdef _WIN32 // to prevent fopen warning on windows -#define _CRT_SECURE_NO_WARNINGS +#ifdef _WIN32 // to prevent fopen warning on windows + #define _CRT_SECURE_NO_WARNINGS #endif #include #include + #include "includes.h" -#include "spdlog/sinks/ostream_sink.h" #include "spdlog/details/os.h" +#include "spdlog/sinks/ostream_sink.h" #include "test_sink.h" - template -std::string log_info(const T &what, spdlog::level logger_level = spdlog::level::info) { +std::string log_info(const T& what, spdlog::level logger_level = spdlog::level::info) { std::ostringstream oss; auto oss_sink = std::make_shared(oss); @@ -176,17 +176,18 @@ TEST_CASE("utf8 to utf16 conversion using windows api", "[windows utf]") { spdlog::details::os::utf8_to_wstrbuf("abc", buffer); REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"abc")); - spdlog::details::os::utf8_to_wstrbuf("\xc3\x28", buffer); // Invalid UTF-8 sequence. + spdlog::details::os::utf8_to_wstrbuf("\xc3\x28", buffer); // Invalid UTF-8 sequence. REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"\xfffd(")); - spdlog::details::os::utf8_to_wstrbuf("\xe3\x81\xad\xe3\x81\x93", buffer); // "Neko" in hiragana. + spdlog::details::os::utf8_to_wstrbuf("\xe3\x81\xad\xe3\x81\x93", buffer); // "Neko" in hiragana. REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"\x306d\x3053")); } #endif struct auto_closer { FILE* fp = nullptr; - explicit auto_closer(FILE* f) : fp(f) {} + explicit auto_closer(FILE* f) + : fp(f) {} auto_closer(const auto_closer&) = delete; auto_closer& operator=(const auto_closer&) = delete; ~auto_closer() { @@ -194,13 +195,11 @@ struct auto_closer { } }; - TEST_CASE("os::fwrite_bytes", "[os]") { - - using spdlog::details::os::fwrite_bytes; using spdlog::details::os::create_dir; + using spdlog::details::os::fwrite_bytes; const char* filename = "log_tests/test_fwrite_bytes.txt"; - const char *msg = "hello"; + const char* msg = "hello"; prepare_logdir(); REQUIRE(create_dir("log_tests")); {