mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-12 17:00:25 +08:00
clang-format
This commit is contained in:
parent
ad4fb1cf84
commit
5370443ece
@ -120,7 +120,8 @@ private:
|
|||||||
filenames.emplace_back(filename);
|
filenames.emplace_back(filename);
|
||||||
now -= std::chrono::hours(24);
|
now -= std::chrono::hours(24);
|
||||||
}
|
}
|
||||||
for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter) {
|
for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter)
|
||||||
|
{
|
||||||
filenames_q_.push_back(std::move(*iter));
|
filenames_q_.push_back(std::move(*iter));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,8 +44,7 @@ namespace sinks {
|
|||||||
|
|
||||||
namespace win_eventlog {
|
namespace win_eventlog {
|
||||||
|
|
||||||
namespace internal
|
namespace internal {
|
||||||
{
|
|
||||||
|
|
||||||
/** Windows error */
|
/** Windows error */
|
||||||
struct win32_error : public spdlog_ex
|
struct win32_error : public spdlog_ex
|
||||||
@ -56,8 +55,8 @@ struct win32_error : public spdlog_ex
|
|||||||
std::string system_message;
|
std::string system_message;
|
||||||
|
|
||||||
LPSTR format_message_result{};
|
LPSTR format_message_result{};
|
||||||
auto format_message_succeeded = ::FormatMessage(
|
auto format_message_succeeded =
|
||||||
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
|
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
|
||||||
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&format_message_result, 0, nullptr);
|
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&format_message_result, 0, nullptr);
|
||||||
|
|
||||||
if (format_message_succeeded && format_message_result)
|
if (format_message_succeeded && format_message_result)
|
||||||
@ -84,8 +83,7 @@ struct sid_t
|
|||||||
std::vector<char> buffer_;
|
std::vector<char> buffer_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
sid_t()
|
sid_t() {}
|
||||||
{}
|
|
||||||
|
|
||||||
/** creates a wrapped SID copy */
|
/** creates a wrapped SID copy */
|
||||||
static sid_t duplicate_sid(PSID psid)
|
static sid_t duplicate_sid(PSID psid)
|
||||||
@ -189,7 +187,6 @@ struct eventlog
|
|||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Windows Event Log sink
|
* Windows Event Log sink
|
||||||
*/
|
*/
|
||||||
@ -226,16 +223,8 @@ protected:
|
|||||||
formatted.push_back('\0');
|
formatted.push_back('\0');
|
||||||
LPCSTR lp_str = static_cast<LPCSTR>(formatted.data());
|
LPCSTR lp_str = static_cast<LPCSTR>(formatted.data());
|
||||||
|
|
||||||
auto succeeded = ::ReportEvent(
|
auto succeeded = ::ReportEvent(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
|
||||||
event_log_handle(),
|
current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr);
|
||||||
eventlog::get_event_type(msg),
|
|
||||||
eventlog::get_event_category(msg),
|
|
||||||
event_id_,
|
|
||||||
current_user_sid_.as_sid(),
|
|
||||||
1,
|
|
||||||
0,
|
|
||||||
&lp_str,
|
|
||||||
nullptr);
|
|
||||||
|
|
||||||
if (!succeeded)
|
if (!succeeded)
|
||||||
{
|
{
|
||||||
@ -275,4 +264,3 @@ using win_eventlog_sink_st = win_eventlog::win_eventlog_sink<details::null_mutex
|
|||||||
|
|
||||||
} // namespace sinks
|
} // namespace sinks
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
|
133
src/fmt.cpp
133
src/fmt.cpp
@ -9,53 +9,63 @@
|
|||||||
#if !defined(SPDLOG_FMT_EXTERNAL)
|
#if !defined(SPDLOG_FMT_EXTERNAL)
|
||||||
#include "spdlog/fmt/bundled/format-inl.h"
|
#include "spdlog/fmt/bundled/format-inl.h"
|
||||||
|
|
||||||
|
|
||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
int format_float(char* buf, std::size_t size, const char* format, int precision,
|
int format_float(char *buf, std::size_t size, const char *format, int precision, T value)
|
||||||
T value) {
|
{
|
||||||
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
||||||
if (precision > 100000)
|
if (precision > 100000)
|
||||||
throw std::runtime_error(
|
throw std::runtime_error("fuzz mode - avoid large allocation inside snprintf");
|
||||||
"fuzz mode - avoid large allocation inside snprintf");
|
|
||||||
#endif
|
#endif
|
||||||
// Suppress the warning about nonliteral format string.
|
// Suppress the warning about nonliteral format string.
|
||||||
auto snprintf_ptr = FMT_SNPRINTF;
|
auto snprintf_ptr = FMT_SNPRINTF;
|
||||||
return precision < 0 ? snprintf_ptr(buf, size, format, value)
|
return precision < 0 ? snprintf_ptr(buf, size, format, value) : snprintf_ptr(buf, size, format, precision, value);
|
||||||
: snprintf_ptr(buf, size, format, precision, value);
|
|
||||||
}
|
}
|
||||||
struct sprintf_specs {
|
struct sprintf_specs
|
||||||
|
{
|
||||||
int precision;
|
int precision;
|
||||||
char type;
|
char type;
|
||||||
bool alt : 1;
|
bool alt : 1;
|
||||||
|
|
||||||
template<typename Char>
|
template<typename Char>
|
||||||
constexpr sprintf_specs(basic_format_specs<Char> specs)
|
constexpr sprintf_specs(basic_format_specs<Char> specs)
|
||||||
: precision(specs.precision), type(specs.type), alt(specs.alt) {}
|
: precision(specs.precision)
|
||||||
|
, type(specs.type)
|
||||||
|
, alt(specs.alt)
|
||||||
|
{}
|
||||||
|
|
||||||
constexpr bool has_precision() const { return precision >= 0; }
|
constexpr bool has_precision() const
|
||||||
|
{
|
||||||
|
return precision >= 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// This is deprecated and is kept only to preserve ABI compatibility.
|
// This is deprecated and is kept only to preserve ABI compatibility.
|
||||||
template<typename Double>
|
template<typename Double>
|
||||||
char* sprintf_format(Double value, internal::buffer<char>& buf,
|
char *sprintf_format(Double value, internal::buffer<char> &buf, sprintf_specs specs)
|
||||||
sprintf_specs specs) {
|
{
|
||||||
// Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail.
|
// Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail.
|
||||||
FMT_ASSERT(buf.capacity() != 0, "empty buffer");
|
FMT_ASSERT(buf.capacity() != 0, "empty buffer");
|
||||||
|
|
||||||
// Build format string.
|
// Build format string.
|
||||||
enum { max_format_size = 10 }; // longest format: %#-*.*Lg
|
enum
|
||||||
|
{
|
||||||
|
max_format_size = 10
|
||||||
|
}; // longest format: %#-*.*Lg
|
||||||
char format[max_format_size];
|
char format[max_format_size];
|
||||||
char *format_ptr = format;
|
char *format_ptr = format;
|
||||||
*format_ptr++ = '%';
|
*format_ptr++ = '%';
|
||||||
if (specs.alt || !specs.type) *format_ptr++ = '#';
|
if (specs.alt || !specs.type)
|
||||||
if (specs.precision >= 0) {
|
*format_ptr++ = '#';
|
||||||
|
if (specs.precision >= 0)
|
||||||
|
{
|
||||||
*format_ptr++ = '.';
|
*format_ptr++ = '.';
|
||||||
*format_ptr++ = '*';
|
*format_ptr++ = '*';
|
||||||
}
|
}
|
||||||
if (std::is_same<Double, long double>::value) *format_ptr++ = 'L';
|
if (std::is_same<Double, long double>::value)
|
||||||
|
*format_ptr++ = 'L';
|
||||||
|
|
||||||
char type = specs.type;
|
char type = specs.type;
|
||||||
|
|
||||||
@ -64,7 +74,8 @@ FMT_BEGIN_NAMESPACE
|
|||||||
else if (type == 0 || type == 'n')
|
else if (type == 0 || type == 'n')
|
||||||
type = 'g';
|
type = 'g';
|
||||||
#if FMT_MSC_VER
|
#if FMT_MSC_VER
|
||||||
if (type == 'F') {
|
if (type == 'F')
|
||||||
|
{
|
||||||
// MSVC's printf doesn't support 'F'.
|
// MSVC's printf doesn't support 'F'.
|
||||||
type = 'f';
|
type = 'f';
|
||||||
}
|
}
|
||||||
@ -75,30 +86,42 @@ FMT_BEGIN_NAMESPACE
|
|||||||
// Format using snprintf.
|
// Format using snprintf.
|
||||||
char *start = nullptr;
|
char *start = nullptr;
|
||||||
char *decimal_point_pos = nullptr;
|
char *decimal_point_pos = nullptr;
|
||||||
for (;;) {
|
for (;;)
|
||||||
|
{
|
||||||
std::size_t buffer_size = buf.capacity();
|
std::size_t buffer_size = buf.capacity();
|
||||||
start = &buf[0];
|
start = &buf[0];
|
||||||
int result =
|
int result = format_float(start, buffer_size, format, specs.precision, value);
|
||||||
format_float(start, buffer_size, format, specs.precision, value);
|
if (result >= 0)
|
||||||
if (result >= 0) {
|
{
|
||||||
unsigned n = internal::to_unsigned(result);
|
unsigned n = internal::to_unsigned(result);
|
||||||
if (n < buf.capacity()) {
|
if (n < buf.capacity())
|
||||||
|
{
|
||||||
// Find the decimal point.
|
// Find the decimal point.
|
||||||
auto p = buf.data(), end = p + n;
|
auto p = buf.data(), end = p + n;
|
||||||
if (*p == '+' || *p == '-') ++p;
|
if (*p == '+' || *p == '-')
|
||||||
if (specs.type != 'a' && specs.type != 'A') {
|
++p;
|
||||||
while (p < end && *p >= '0' && *p <= '9') ++p;
|
if (specs.type != 'a' && specs.type != 'A')
|
||||||
if (p < end && *p != 'e' && *p != 'E') {
|
{
|
||||||
|
while (p < end && *p >= '0' && *p <= '9')
|
||||||
|
++p;
|
||||||
|
if (p < end && *p != 'e' && *p != 'E')
|
||||||
|
{
|
||||||
decimal_point_pos = p;
|
decimal_point_pos = p;
|
||||||
if (!specs.type) {
|
if (!specs.type)
|
||||||
|
{
|
||||||
// Keep only one trailing zero after the decimal point.
|
// Keep only one trailing zero after the decimal point.
|
||||||
++p;
|
++p;
|
||||||
if (*p == '0') ++p;
|
if (*p == '0')
|
||||||
while (p != end && *p >= '1' && *p <= '9') ++p;
|
++p;
|
||||||
|
while (p != end && *p >= '1' && *p <= '9')
|
||||||
|
++p;
|
||||||
char *where = p;
|
char *where = p;
|
||||||
while (p != end && *p == '0') ++p;
|
while (p != end && *p == '0')
|
||||||
if (p == end || *p < '0' || *p > '9') {
|
++p;
|
||||||
if (p != end) std::memmove(where, p, to_unsigned(end - p));
|
if (p == end || *p < '0' || *p > '9')
|
||||||
|
{
|
||||||
|
if (p != end)
|
||||||
|
std::memmove(where, p, to_unsigned(end - p));
|
||||||
n -= static_cast<unsigned>(p - where);
|
n -= static_cast<unsigned>(p - where);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,7 +131,9 @@ FMT_BEGIN_NAMESPACE
|
|||||||
break; // The buffer is large enough - continue with formatting.
|
break; // The buffer is large enough - continue with formatting.
|
||||||
}
|
}
|
||||||
buf.reserve(n + 1);
|
buf.reserve(n + 1);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
// If result is negative we ask to increase the capacity by at least 1,
|
// If result is negative we ask to increase the capacity by at least 1,
|
||||||
// but as std::vector, the buffer grows exponentially.
|
// but as std::vector, the buffer grows exponentially.
|
||||||
buf.reserve(buf.capacity() + 1);
|
buf.reserve(buf.capacity() + 1);
|
||||||
@ -118,18 +143,13 @@ FMT_BEGIN_NAMESPACE
|
|||||||
}
|
}
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
||||||
template FMT_API char* internal::sprintf_format(double, internal::buffer<char>&,
|
template FMT_API char *internal::sprintf_format(double, internal::buffer<char> &, sprintf_specs);
|
||||||
sprintf_specs);
|
template FMT_API char *internal::sprintf_format(long double, internal::buffer<char> &, sprintf_specs);
|
||||||
template FMT_API char* internal::sprintf_format(long double,
|
|
||||||
internal::buffer<char>&,
|
|
||||||
sprintf_specs);
|
|
||||||
|
|
||||||
template struct FMT_API internal::basic_data<void>;
|
template struct FMT_API internal::basic_data<void>;
|
||||||
|
|
||||||
// Workaround a bug in MSVC2013 that prevents instantiation of format_float.
|
// Workaround a bug in MSVC2013 that prevents instantiation of format_float.
|
||||||
int (*instantiate_format_float)(double, int, internal::float_specs,
|
int (*instantiate_format_float)(double, int, internal::float_specs, internal::buffer<char> &) = internal::format_float;
|
||||||
internal::buffer<char>&) =
|
|
||||||
internal::format_float;
|
|
||||||
|
|
||||||
#ifndef FMT_STATIC_THOUSANDS_SEPARATOR
|
#ifndef FMT_STATIC_THOUSANDS_SEPARATOR
|
||||||
template FMT_API internal::locale_ref::locale_ref(const std::locale &loc);
|
template FMT_API internal::locale_ref::locale_ref(const std::locale &loc);
|
||||||
@ -144,26 +164,16 @@ FMT_BEGIN_NAMESPACE
|
|||||||
|
|
||||||
template FMT_API void internal::buffer<char>::append(const char *, const char *);
|
template FMT_API void internal::buffer<char>::append(const char *, const char *);
|
||||||
|
|
||||||
template FMT_API void internal::arg_map<format_context>::init(
|
template FMT_API void internal::arg_map<format_context>::init(const basic_format_args<format_context> &args);
|
||||||
const basic_format_args<format_context>& args);
|
|
||||||
|
|
||||||
template FMT_API std::string internal::vformat<char>(
|
template FMT_API std::string internal::vformat<char>(string_view, basic_format_args<format_context>);
|
||||||
string_view, basic_format_args<format_context>);
|
|
||||||
|
|
||||||
template FMT_API format_context::iterator internal::vformat_to(
|
template FMT_API format_context::iterator internal::vformat_to(internal::buffer<char> &, string_view, basic_format_args<format_context>);
|
||||||
internal::buffer<char>&, string_view, basic_format_args<format_context>);
|
|
||||||
|
|
||||||
template FMT_API int internal::snprintf_float(double, int,
|
template FMT_API int internal::snprintf_float(double, int, internal::float_specs, internal::buffer<char> &);
|
||||||
internal::float_specs,
|
template FMT_API int internal::snprintf_float(long double, int, internal::float_specs, internal::buffer<char> &);
|
||||||
internal::buffer<char>&);
|
template FMT_API int internal::format_float(double, int, internal::float_specs, internal::buffer<char> &);
|
||||||
template FMT_API int internal::snprintf_float(long double, int,
|
template FMT_API int internal::format_float(long double, int, internal::float_specs, internal::buffer<char> &);
|
||||||
internal::float_specs,
|
|
||||||
internal::buffer<char>&);
|
|
||||||
template FMT_API int internal::format_float(double, int, internal::float_specs,
|
|
||||||
internal::buffer<char>&);
|
|
||||||
template FMT_API int internal::format_float(long double, int,
|
|
||||||
internal::float_specs,
|
|
||||||
internal::buffer<char>&);
|
|
||||||
|
|
||||||
// Explicit instantiations for wchar_t.
|
// Explicit instantiations for wchar_t.
|
||||||
|
|
||||||
@ -171,12 +181,9 @@ FMT_BEGIN_NAMESPACE
|
|||||||
template FMT_API wchar_t internal::thousands_sep_impl(locale_ref);
|
template FMT_API wchar_t internal::thousands_sep_impl(locale_ref);
|
||||||
template FMT_API wchar_t internal::decimal_point_impl(locale_ref);
|
template FMT_API wchar_t internal::decimal_point_impl(locale_ref);
|
||||||
|
|
||||||
template FMT_API void internal::buffer<wchar_t>::append(const wchar_t*,
|
template FMT_API void internal::buffer<wchar_t>::append(const wchar_t *, const wchar_t *);
|
||||||
const wchar_t*);
|
|
||||||
|
|
||||||
template FMT_API std::wstring internal::vformat<wchar_t>(
|
template FMT_API std::wstring internal::vformat<wchar_t>(wstring_view, basic_format_args<wformat_context>);
|
||||||
wstring_view, basic_format_args<wformat_context>);
|
|
||||||
FMT_END_NAMESPACE
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -29,14 +29,16 @@ static void test_single_print(std::function<void(std::string const&)> do_log, st
|
|||||||
REQUIRE(event_log.handle_);
|
REQUIRE(event_log.handle_);
|
||||||
|
|
||||||
DWORD read_bytes{}, size_needed{};
|
DWORD read_bytes{}, size_needed{};
|
||||||
auto ok = ::ReadEventLog(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, &read_bytes, 0, &read_bytes, &size_needed);
|
auto ok =
|
||||||
|
::ReadEventLog(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, &read_bytes, 0, &read_bytes, &size_needed);
|
||||||
REQUIRE(!ok);
|
REQUIRE(!ok);
|
||||||
REQUIRE(::GetLastError() == ERROR_INSUFFICIENT_BUFFER);
|
REQUIRE(::GetLastError() == ERROR_INSUFFICIENT_BUFFER);
|
||||||
|
|
||||||
std::vector<char> record_buffer(size_needed);
|
std::vector<char> record_buffer(size_needed);
|
||||||
PEVENTLOGRECORD record = (PEVENTLOGRECORD)record_buffer.data();
|
PEVENTLOGRECORD record = (PEVENTLOGRECORD)record_buffer.data();
|
||||||
|
|
||||||
ok = ::ReadEventLog(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, record, size_needed, &read_bytes, &size_needed);
|
ok = ::ReadEventLog(
|
||||||
|
event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, record, size_needed, &read_bytes, &size_needed);
|
||||||
REQUIRE(ok);
|
REQUIRE(ok);
|
||||||
|
|
||||||
REQUIRE(record->NumStrings == 1);
|
REQUIRE(record->NumStrings == 1);
|
||||||
|
@ -17,8 +17,7 @@ TEST_CASE("simple_file_logger", "[simple_logger]]")
|
|||||||
logger->flush();
|
logger->flush();
|
||||||
require_message_count(filename, 2);
|
require_message_count(filename, 2);
|
||||||
using spdlog::details::os::default_eol;
|
using spdlog::details::os::default_eol;
|
||||||
REQUIRE(file_contents(filename) == fmt::format("Test message 1{}Test message 2{}",
|
REQUIRE(file_contents(filename) == fmt::format("Test message 1{}Test message 2{}", default_eol, default_eol));
|
||||||
default_eol, default_eol));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("flush_on", "[flush_on]]")
|
TEST_CASE("flush_on", "[flush_on]]")
|
||||||
@ -38,8 +37,8 @@ TEST_CASE("flush_on", "[flush_on]]")
|
|||||||
|
|
||||||
require_message_count(filename, 3);
|
require_message_count(filename, 3);
|
||||||
using spdlog::details::os::default_eol;
|
using spdlog::details::os::default_eol;
|
||||||
REQUIRE(file_contents(filename) == fmt::format("Should not be flushed{}Test message 1{}Test message 2{}",
|
REQUIRE(file_contents(filename) ==
|
||||||
default_eol, default_eol, default_eol));
|
fmt::format("Should not be flushed{}Test message 1{}Test message 2{}", default_eol, default_eol, default_eol));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
||||||
|
@ -46,9 +46,12 @@ std::size_t count_lines(const std::string &filename)
|
|||||||
|
|
||||||
void require_message_count(const std::string &filename, const std::size_t messages)
|
void require_message_count(const std::string &filename, const std::size_t messages)
|
||||||
{
|
{
|
||||||
if (strlen(spdlog::details::os::default_eol) == 0) {
|
if (strlen(spdlog::details::os::default_eol) == 0)
|
||||||
|
{
|
||||||
REQUIRE(count_lines(filename) == 1);
|
REQUIRE(count_lines(filename) == 1);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
REQUIRE(count_lines(filename) == messages);
|
REQUIRE(count_lines(filename) == messages);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user