clang-format

This commit is contained in:
gabime 2024-12-01 23:59:09 +02:00
parent 58dac85596
commit e62be8b43f
18 changed files with 69 additions and 85 deletions

View File

@ -284,7 +284,9 @@ public:
} }
[[nodiscard]] [[nodiscard]]
std::unique_ptr<custom_flag_formatter> clone() const override { return std::make_unique<my_formatter_flag>(); } std::unique_ptr<custom_flag_formatter> clone() const override {
return std::make_unique<my_formatter_flag>();
}
}; };
void custom_flags_example() { void custom_flags_example() {

View File

@ -16,7 +16,6 @@
#include "./source_loc.h" #include "./source_loc.h"
#if defined(SPDLOG_SHARED_LIB) #if defined(SPDLOG_SHARED_LIB)
#if defined(_WIN32) #if defined(_WIN32)
#ifdef spdlog_EXPORTS #ifdef spdlog_EXPORTS

View File

@ -76,11 +76,10 @@ SPDLOG_API bool is_color_terminal() noexcept;
SPDLOG_API bool in_terminal(FILE *file) noexcept; SPDLOG_API bool in_terminal(FILE *file) noexcept;
#if (defined _WIN32) #if (defined _WIN32)
SPDLOG_API void wstr_to_utf8buf(wstring_view_t wstr, memory_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); SPDLOG_API void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target);
#endif #endif
// Return directory name from given path or empty string // Return directory name from given path or empty string
// "abc/file" => "abc" // "abc/file" => "abc"
// "abc/" => "abc" // "abc/" => "abc"
@ -100,7 +99,6 @@ SPDLOG_API std::string getenv(const char *field);
// Return true on success. // Return true on success.
SPDLOG_API bool fsync(FILE *fp); SPDLOG_API bool fsync(FILE *fp);
// Do non-locking fwrite if possible by the os or use the regular locking fwrite // Do non-locking fwrite if possible by the os or use the regular locking fwrite
// Return true on success. // Return true on success.
SPDLOG_API bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp); SPDLOG_API bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp);

View File

@ -22,7 +22,6 @@
#include "./details/log_msg.h" #include "./details/log_msg.h"
#include "./sinks/sink.h" #include "./sinks/sink.h"
#define SPDLOG_LOGGER_CATCH(location) \ #define SPDLOG_LOGGER_CATCH(location) \
catch (const std::exception &ex) { \ catch (const std::exception &ex) { \
if (!location.empty()) { \ if (!location.empty()) { \
@ -36,7 +35,6 @@
throw; \ throw; \
} }
namespace spdlog { namespace spdlog {
class SPDLOG_API logger { class SPDLOG_API logger {
@ -197,7 +195,9 @@ protected:
assert(should_log(msg.log_level)); assert(should_log(msg.log_level));
for (auto &sink : sinks_) { for (auto &sink : sinks_) {
if (sink->should_log(msg.log_level)) { if (sink->should_log(msg.log_level)) {
try { sink->log(msg); } try {
sink->log(msg);
}
SPDLOG_LOGGER_CATCH(msg.source) SPDLOG_LOGGER_CATCH(msg.source)
} }
} }

View File

@ -45,9 +45,9 @@ struct daily_filename_calculator {
*/ */
struct daily_filename_format_calculator { struct daily_filename_format_calculator {
static filename_t calc_filename(const filename_t &file_path, const tm &now_tm) { static filename_t calc_filename(const filename_t &file_path, const tm &now_tm) {
std::stringstream stream; std::stringstream stream;
stream << std::put_time(&now_tm, file_path.c_str()); stream << std::put_time(&now_tm, file_path.c_str());
return stream.str(); return stream.str();
} }
}; };

View File

@ -68,17 +68,14 @@ protected:
if (msg.source.empty()) { if (msg.source.empty()) {
// Note: function call inside '()' to avoid macro expansion // Note: function call inside '()' to avoid macro expansion
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d", err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d",
syslog_level(msg.log_level), syslog_level(msg.log_level), "TID=%zu", msg.thread_id, "SYSLOG_IDENTIFIER=%.*s",
"TID=%zu", msg.thread_id, static_cast<int>(syslog_identifier.size()), syslog_identifier.data(), nullptr);
"SYSLOG_IDENTIFIER=%.*s", static_cast<int>(syslog_identifier.size()),
syslog_identifier.data(), nullptr);
} else { } else {
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d", err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d",
syslog_level(msg.log_level), syslog_level(msg.log_level), "TID=%zu", msg.thread_id, "SYSLOG_IDENTIFIER=%.*s",
"TID=%zu", msg.thread_id, static_cast<int>(syslog_identifier.size()), syslog_identifier.data(), "CODE_FILE=%s",
"SYSLOG_IDENTIFIER=%.*s", static_cast<int>(syslog_identifier.size()), msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname,
syslog_identifier.data(), "CODE_FILE=%s", msg.source.filename, "CODE_LINE=%d", nullptr);
msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr);
} }
if (err) { if (err) {

View File

@ -4,7 +4,7 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
namespace spdlog {// source location namespace spdlog { // source location
struct source_loc { struct source_loc {
constexpr source_loc() = default; constexpr source_loc() = default;
constexpr source_loc(const char *filename_in, std::uint_least32_t line_in, const char *funcname_in) constexpr source_loc(const char *filename_in, std::uint_least32_t line_in, const char *funcname_in)

View File

@ -51,7 +51,9 @@ void spdlog::async_logger::flush_() {
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_) { for (auto &sink : sinks_) {
if (sink->should_log(msg.log_level)) { if (sink->should_log(msg.log_level)) {
try { sink->log(msg); } try {
sink->log(msg);
}
SPDLOG_LOGGER_CATCH(msg.source) 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_() { void spdlog::async_logger::backend_flush_() {
for (auto &sink : sinks_) { for (auto &sink : sinks_) {
try { sink->flush(); } try {
sink->flush();
}
SPDLOG_LOGGER_CATCH(source_loc()) SPDLOG_LOGGER_CATCH(source_loc())
} }
} }

View File

@ -16,11 +16,10 @@ log_msg::log_msg(spdlog::log_clock::time_point log_time,
: logger_name(a_logger_name), : logger_name(a_logger_name),
log_level(lvl), log_level(lvl),
time(log_time), time(log_time),
#ifdef SPDLOG_NO_THREAD_ID #ifdef SPDLOG_NO_THREAD_ID
thread_id(0), thread_id(0),
#else #else
thread_id(os::thread_id()), thread_id(os::thread_id()),
#endif #endif
source(loc), source(loc),
payload(msg) { payload(msg) {

View File

@ -109,7 +109,7 @@ int rename(const filename_t &filename1, const filename_t &filename2) noexcept {
// Return true if path exists (file or directory) // Return true if path exists (file or directory)
bool path_exists(const filename_t &filename) noexcept { bool path_exists(const filename_t &filename) noexcept {
struct stat buffer{}; struct stat buffer {};
return (::stat(filename.c_str(), &buffer) == 0); return (::stat(filename.c_str(), &buffer) == 0);
} }
@ -127,7 +127,7 @@ size_t filesize(FILE *f) {
#endif #endif
// 64 bits(but not in osx, linux/musl or cygwin, where fstat64 is deprecated) // 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)) #if ((defined(__linux__) && defined(__GLIBC__)) || defined(__sun) || defined(_AIX)) && (defined(__LP64__) || defined(_LP64))
struct stat64 st{}; struct stat64 st {};
if (::fstat64(fd, &st) == 0) { if (::fstat64(fd, &st) == 0) {
return static_cast<size_t>(st.st_size); return static_cast<size_t>(st.st_size);
} }
@ -326,15 +326,13 @@ std::string getenv(const char *field) {
// Return true on success // Return true on success
bool fsync(FILE *fp) { return ::fsync(fileno(fp)) == 0; } bool fsync(FILE *fp) { return ::fsync(fileno(fp)) == 0; }
// Non locking ::fwrite if possible (SPDLOG_FWRITE_UNLOCKED defined) or use the regular locking fwrite // 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) bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp) {
{ #if defined(SPDLOG_FWRITE_UNLOCKED)
#if defined(SPDLOG_FWRITE_UNLOCKED) return ::fwrite_unlocked(ptr, 1, n_bytes, fp) == n_bytes;
return ::fwrite_unlocked(ptr, 1, n_bytes, fp) == n_bytes; #else
#else return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes;
return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes; #endif
#endif
} }
} // namespace os } // namespace os

View File

@ -16,6 +16,7 @@
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <cassert>
#include <chrono> #include <chrono>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
@ -23,7 +24,6 @@
#include <ctime> #include <ctime>
#include <string> #include <string>
#include <thread> #include <thread>
#include <cassert>
#include "spdlog/common.h" #include "spdlog/common.h"
#include "spdlog/details/os.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; return *fp == nullptr;
} }
int remove(const filename_t &filename) noexcept { int remove(const filename_t &filename) noexcept { return std::remove(filename.c_str()); }
return std::remove(filename.c_str());
}
int remove_if_exists(const filename_t &filename) noexcept { return path_exists(filename) ? remove(filename) : 0; } 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 // Determine if the terminal attached
bool in_terminal(FILE *file) noexcept { return ::_isatty(_fileno(file)) != 0; } bool in_terminal(FILE *file) noexcept { return ::_isatty(_fileno(file)) != 0; }
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<size_t>((std::numeric_limits<int>::max)()) / 4 - 1) { if (wstr.size() > static_cast<size_t>((std::numeric_limits<int>::max)()) / 4 - 1) {
throw_spdlog_ex("UTF-16 string is too big to be converted to UTF-8"); 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<int>(target.capacity()); int result_size = static_cast<int>(target.capacity());
if ((wstr_size + 1) * 4 > result_size) { if ((wstr_size + 1) * 4 > result_size) {
result_size = result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL);
::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL);
} }
if (result_size > 0) { if (result_size > 0) {
target.resize(result_size); target.resize(result_size);
result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(), result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(), result_size, NULL, NULL);
result_size, NULL, NULL);
if (result_size > 0) { if (result_size > 0) {
target.resize(result_size); target.resize(result_size);
@ -198,8 +193,7 @@ void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) {
} }
} }
throw_spdlog_ex( throw_spdlog_ex(fmt_lib::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
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) {
@ -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 // find the size to allocate for the result buffer
int result_size = int result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, NULL, 0);
::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, NULL, 0);
if (result_size > 0) { if (result_size > 0) {
target.resize(result_size); target.resize(result_size);
result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, target.data(), result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, target.data(), result_size);
result_size);
if (result_size > 0) { if (result_size > 0) {
assert(result_size == target.size()); assert(result_size == target.size());
return; return;
} }
} }
throw_spdlog_ex( throw_spdlog_ex(fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError()));
fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError()));
} }
// return true on success // return true on success
static bool mkdir_(const filename_t &path) { static bool mkdir_(const filename_t &path) { return ::_mkdir(path.c_str()) == 0; }
return ::_mkdir(path.c_str()) == 0;
}
// create the given directory - and all directories leading to it // create the given directory - and all directories leading to it
// return true on success or if the directory already exists // 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<HANDLE>(_get_osfhandle(_fileno(fp)))) != 0; } bool fsync(FILE *fp) { return FlushFileBuffers(reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(fp)))) != 0; }
// Non locking fwrite if possible (SPDLOG_FWRITE_UNLOCKED defined) or use the regular locking fwrite // 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) bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp) {
{ #if defined(SPDLOG_FWRITE_UNLOCKED)
#if defined(SPDLOG_FWRITE_UNLOCKED) return _fwrite_nolock(ptr, 1, n_bytes, fp) == n_bytes;
return _fwrite_nolock(ptr, 1, n_bytes, fp) == n_bytes; #else
#else return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes;
return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes; #endif
#endif
} }
} // namespace os } // namespace os
} // namespace details } // namespace details

View File

@ -45,8 +45,8 @@ thread_pool::~thread_pool() {
for (auto &t : threads_) { for (auto &t : threads_) {
t.join(); t.join();
} }
} catch (...) {
} }
catch (...) {}
} }
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) {

View File

@ -75,7 +75,9 @@ std::shared_ptr<logger> logger::clone(std::string logger_name) {
// private/protected methods // private/protected methods
void logger::flush_() { void logger::flush_() {
for (auto &sink : sinks_) { for (auto &sink : sinks_) {
try { sink->flush(); } try {
sink->flush();
}
SPDLOG_LOGGER_CATCH(source_loc()) SPDLOG_LOGGER_CATCH(source_loc())
} }
} }

View File

@ -148,7 +148,8 @@ public:
}; };
// Full weekday name // Full weekday name
static constexpr std::array<const char *, 7> full_days{{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}}; static constexpr std::array<const char *, 7> full_days{
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}};
template <typename ScopedPadder> template <typename ScopedPadder>
class A_formatter final : public flag_formatter { class A_formatter final : public flag_formatter {

View File

@ -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); } 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 level_.load(std::memory_order_relaxed); }
return level_.load(std::memory_order_relaxed);
}

View File

@ -1,12 +1,12 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT) // Distributed under the MIT License (http://opensource.org/licenses/MIT)
#include "spdlog/sinks/stdout_sinks.h"
#include <memory> #include <memory>
#include "spdlog/pattern_formatter.h"
#include "spdlog/sinks/stdout_sinks.h"
#include "spdlog/details/os.h" #include "spdlog/details/os.h"
#include "spdlog/pattern_formatter.h"
// clang-format off // clang-format off
#ifdef _WIN32 #ifdef _WIN32

View File

@ -8,7 +8,6 @@
using filename_memory_buf_t = spdlog::memory_buf_t; using filename_memory_buf_t = spdlog::memory_buf_t;
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") { TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") {
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::daily_filename_calculator>; using sink_type = spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::daily_filename_calculator>;

View File

@ -1,17 +1,17 @@
#ifdef _WIN32 // to prevent fopen warning on windows #ifdef _WIN32 // to prevent fopen warning on windows
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
#endif #endif
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include "includes.h" #include "includes.h"
#include "spdlog/sinks/ostream_sink.h"
#include "spdlog/details/os.h" #include "spdlog/details/os.h"
#include "spdlog/sinks/ostream_sink.h"
#include "test_sink.h" #include "test_sink.h"
template <class T> template <class T>
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; std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss); auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
@ -176,17 +176,18 @@ TEST_CASE("utf8 to utf16 conversion using windows api", "[windows utf]") {
spdlog::details::os::utf8_to_wstrbuf("abc", buffer); spdlog::details::os::utf8_to_wstrbuf("abc", buffer);
REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"abc")); 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(")); 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")); REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"\x306d\x3053"));
} }
#endif #endif
struct auto_closer { struct auto_closer {
FILE* fp = nullptr; 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(const auto_closer&) = delete;
auto_closer& operator=(const auto_closer&) = delete; auto_closer& operator=(const auto_closer&) = delete;
~auto_closer() { ~auto_closer() {
@ -194,13 +195,11 @@ struct auto_closer {
} }
}; };
TEST_CASE("os::fwrite_bytes", "[os]") { TEST_CASE("os::fwrite_bytes", "[os]") {
using spdlog::details::os::fwrite_bytes;
using spdlog::details::os::create_dir; using spdlog::details::os::create_dir;
using spdlog::details::os::fwrite_bytes;
const char* filename = "log_tests/test_fwrite_bytes.txt"; const char* filename = "log_tests/test_fwrite_bytes.txt";
const char *msg = "hello"; const char* msg = "hello";
prepare_logdir(); prepare_logdir();
REQUIRE(create_dir("log_tests")); REQUIRE(create_dir("log_tests"));
{ {