mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-26 07:29:03 +08:00
clang-format
This commit is contained in:
parent
58dac85596
commit
e62be8b43f
@ -284,7 +284,9 @@ public:
|
||||
}
|
||||
|
||||
[[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() {
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
#include "./source_loc.h"
|
||||
|
||||
|
||||
#if defined(SPDLOG_SHARED_LIB)
|
||||
#if defined(_WIN32)
|
||||
#ifdef spdlog_EXPORTS
|
||||
|
@ -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);
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -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<int>(length), payload.data(), "PRIORITY=%d",
|
||||
syslog_level(msg.log_level),
|
||||
"TID=%zu", msg.thread_id,
|
||||
"SYSLOG_IDENTIFIER=%.*s", static_cast<int>(syslog_identifier.size()),
|
||||
syslog_identifier.data(), nullptr);
|
||||
syslog_level(msg.log_level), "TID=%zu", msg.thread_id, "SYSLOG_IDENTIFIER=%.*s",
|
||||
static_cast<int>(syslog_identifier.size()), syslog_identifier.data(), nullptr);
|
||||
} else {
|
||||
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d",
|
||||
syslog_level(msg.log_level),
|
||||
"TID=%zu", msg.thread_id,
|
||||
"SYSLOG_IDENTIFIER=%.*s", static_cast<int>(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<int>(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) {
|
||||
|
@ -4,7 +4,7 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
||||
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)
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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<size_t>(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
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
@ -23,7 +24,6 @@
|
||||
#include <ctime>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <cassert>
|
||||
|
||||
#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<size_t>((std::numeric_limits<int>::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<int>(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<HANDLE>(_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
|
||||
|
@ -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) {
|
||||
|
@ -75,7 +75,9 @@ std::shared_ptr<logger> 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())
|
||||
}
|
||||
}
|
||||
|
@ -148,7 +148,8 @@ public:
|
||||
};
|
||||
|
||||
// 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>
|
||||
class A_formatter final : public flag_formatter {
|
||||
|
@ -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); }
|
||||
|
@ -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 <memory>
|
||||
|
||||
#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
|
||||
|
@ -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<std::mutex, spdlog::sinks::daily_filename_calculator>;
|
||||
|
||||
|
@ -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 <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#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 <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;
|
||||
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);
|
||||
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"));
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user