mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-15 17:27:57 +00:00
Compare commits
35 Commits
d4ce938679
...
574563d711
Author | SHA1 | Date | |
---|---|---|---|
|
574563d711 | ||
|
e9d0b424d5 | ||
|
eef981e05f | ||
|
9f24f4bc69 | ||
|
5da9818676 | ||
|
ff59b07986 | ||
|
1b6d4fd277 | ||
|
7b19890deb | ||
|
5370443ece | ||
|
ad4fb1cf84 | ||
|
7f8169f0da | ||
|
66e8652862 | ||
|
05cbdbc1ef | ||
|
38584a1fca | ||
|
d96d8c49ac | ||
|
4bb623a0a3 | ||
|
3aa94a0997 | ||
|
ccad4ae04f | ||
|
346b9ae5a1 | ||
|
12f36debae | ||
|
87acec6a91 | ||
|
58a5e654f9 | ||
|
e278953191 | ||
|
573ddf8aec | ||
|
4f32243214 | ||
|
601bdfb1b4 | ||
|
90454a93b2 | ||
|
640921cd3f | ||
|
fccee959b1 | ||
|
67a8ecf2bf | ||
|
d8701890b2 | ||
|
2435f46d06 | ||
|
4bece787c8 | ||
|
25b10dc264 | ||
|
db1a221427 |
@ -150,7 +150,7 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename
|
||||
#if defined(SPDLOG_PREVENT_CHILD_FD)
|
||||
const int mode_flag = mode == SPDLOG_FILENAME_T("ab") ? O_APPEND : O_TRUNC;
|
||||
const int fd = ::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644));
|
||||
if(fd == -1)
|
||||
if (fd == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -143,6 +143,11 @@ public:
|
||||
// T can be statically converted to string_view
|
||||
template<class T, typename std::enable_if<std::is_convertible<const T &, spdlog::string_view_t>::value, T>::type * = nullptr>
|
||||
void log(source_loc loc, level::level_enum lvl, const T &msg)
|
||||
{
|
||||
log(loc, lvl, string_view_t{msg});
|
||||
}
|
||||
|
||||
void log(source_loc loc, level::level_enum lvl, string_view_t msg)
|
||||
{
|
||||
bool log_enabled = should_log(lvl);
|
||||
bool traceback_enabled = tracer_.enabled();
|
||||
|
@ -120,7 +120,8 @@ private:
|
||||
filenames.emplace_back(filename);
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
130
include/spdlog/sinks/tcp_sink.h
Normal file
130
include/spdlog/sinks/tcp_sink.h
Normal file
@ -0,0 +1,130 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <spdlog/common.h>
|
||||
#include <spdlog/sinks/base_sink.h>
|
||||
#include <spdlog/details/null_mutex.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
template<typename Mutex>
|
||||
class tcp_sink : public spdlog::sinks::base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
// connect to tcp host/port or throw if failed
|
||||
// host can be hostname or ip address
|
||||
tcp_sink(std::string host, int port)
|
||||
{
|
||||
sock_ = connect_to(host, port);
|
||||
}
|
||||
|
||||
~tcp_sink() override
|
||||
{
|
||||
if (sock_ != -1)
|
||||
{
|
||||
::close(sock_);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const spdlog::details::log_msg &msg) override
|
||||
{
|
||||
spdlog::memory_buf_t formatted;
|
||||
spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||
size_t bytes_sent = 0;
|
||||
while (bytes_sent < formatted.size())
|
||||
{
|
||||
auto write_result = ::write(sock_, formatted.data() + bytes_sent, formatted.size() - bytes_sent);
|
||||
if (write_result < 0)
|
||||
{
|
||||
SPDLOG_THROW(spdlog::spdlog_ex("write(2) failed", errno));
|
||||
}
|
||||
|
||||
if (write_result == 0) // (probably should not happen but in any case..)
|
||||
{
|
||||
break;
|
||||
}
|
||||
bytes_sent += static_cast<size_t>(write_result);
|
||||
}
|
||||
}
|
||||
|
||||
void flush_() override {}
|
||||
|
||||
private:
|
||||
// try to connect and return socket fd or throw on failure
|
||||
int connect_to(const std::string &host, int port)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
memset(&hints, 0, sizeof(struct addrinfo));
|
||||
hints.ai_family = AF_INET; // IPv4
|
||||
hints.ai_socktype = SOCK_STREAM; // TCP
|
||||
hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value
|
||||
hints.ai_protocol = 0;
|
||||
|
||||
auto port_str = std::to_string(port);
|
||||
struct addrinfo *addrinfo_result;
|
||||
auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result);
|
||||
if (rv != 0)
|
||||
{
|
||||
auto msg = fmt::format("::getaddrinfo failed: {}", gai_strerror(rv));
|
||||
SPDLOG_THROW(spdlog::spdlog_ex(msg));
|
||||
}
|
||||
|
||||
// Try each address until we successfully connect(2).
|
||||
int socket_rv = -1;
|
||||
int last_errno = 0;
|
||||
for (auto *rp = addrinfo_result; rp != nullptr; rp = rp->ai_next)
|
||||
{
|
||||
#ifdef SPDLOG_PREVENT_CHILD_FD
|
||||
int const flags = SOCK_CLOEXEC;
|
||||
#else
|
||||
int const flags = 0;
|
||||
#endif
|
||||
socket_rv = ::socket(rp->ai_family, rp->ai_socktype | flags, rp->ai_protocol);
|
||||
if (socket_rv == -1)
|
||||
{
|
||||
last_errno = errno;
|
||||
continue;
|
||||
}
|
||||
rv = ::connect(socket_rv, rp->ai_addr, rp->ai_addrlen);
|
||||
if (rv == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
last_errno = errno;
|
||||
::close(socket_rv);
|
||||
socket_rv = -1;
|
||||
}
|
||||
}
|
||||
::freeaddrinfo(addrinfo_result);
|
||||
if (socket_rv == -1)
|
||||
{
|
||||
SPDLOG_THROW(spdlog::spdlog_ex("::connect failed", last_errno));
|
||||
}
|
||||
return socket_rv;
|
||||
}
|
||||
|
||||
private:
|
||||
int sock_ = -1;
|
||||
};
|
||||
|
||||
using tcp_sink_mt = tcp_sink<std::mutex>;
|
||||
using tcp_sink_st = tcp_sink<spdlog::details::null_mutex>;
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
266
include/spdlog/sinks/win_eventlog_sink.h
Normal file
266
include/spdlog/sinks/win_eventlog_sink.h
Normal file
@ -0,0 +1,266 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
// Writing to Windows Event Log requires the registry entries below to be present, with the following modifications:
|
||||
// 1. <log_name> should be replaced with your log name (e.g. your application name)
|
||||
// 2. <source_name> should be replaced with the specific source name and the key should be duplicated for
|
||||
// each source used in the application
|
||||
//
|
||||
// Since typically modifications of this kind require elevation, it's better to do it as a part of setup procedure.
|
||||
// The snippet below uses mscoree.dll as the message file as it exists on most of the Windows systems anyway and
|
||||
// happens to contain the needed resource.
|
||||
//
|
||||
// You can also specify a custom message file if needed.
|
||||
// Please refer to Event Log functions descriptions in MSDN for more details on custom message files.
|
||||
|
||||
/*---------------------------------------------------------------------------------------
|
||||
|
||||
Windows Registry Editor Version 5.00
|
||||
|
||||
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\<log_name>]
|
||||
|
||||
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\<log_name>\<source_name>]
|
||||
"TypesSupported"=dword:00000007
|
||||
"EventMessageFile"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,\
|
||||
00,6f,00,74,00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,\
|
||||
5c,00,6d,00,73,00,63,00,6f,00,72,00,65,00,65,00,2e,00,64,00,6c,00,6c,00,00,\
|
||||
00
|
||||
|
||||
-----------------------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <winbase.h>
|
||||
|
||||
#include <spdlog/details/null_mutex.h>
|
||||
#include <spdlog/sinks/base_sink.h>
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
namespace win_eventlog {
|
||||
|
||||
namespace internal {
|
||||
|
||||
/** Windows error */
|
||||
struct win32_error : public spdlog_ex
|
||||
{
|
||||
/** Formats an error report line: "user-message: error-code (system message)" */
|
||||
static std::string format(std::string const &user_message, DWORD error_code = GetLastError())
|
||||
{
|
||||
std::string system_message;
|
||||
|
||||
LPSTR format_message_result{};
|
||||
auto format_message_succeeded =
|
||||
::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);
|
||||
|
||||
if (format_message_succeeded && format_message_result)
|
||||
{
|
||||
system_message = fmt::format(" ({})", format_message_result);
|
||||
}
|
||||
|
||||
if (format_message_result)
|
||||
{
|
||||
LocalFree((HLOCAL)format_message_result);
|
||||
}
|
||||
|
||||
return fmt::format("{}: {}{}", user_message, error_code, system_message);
|
||||
}
|
||||
|
||||
win32_error(std::string const &func_name, DWORD error = GetLastError())
|
||||
: spdlog_ex(format(func_name, error))
|
||||
{}
|
||||
};
|
||||
|
||||
/** Wrapper for security identifiers (SID) on Windows */
|
||||
struct sid_t
|
||||
{
|
||||
std::vector<char> buffer_;
|
||||
|
||||
public:
|
||||
sid_t() {}
|
||||
|
||||
/** creates a wrapped SID copy */
|
||||
static sid_t duplicate_sid(PSID psid)
|
||||
{
|
||||
if (!::IsValidSid(psid))
|
||||
{
|
||||
SPDLOG_THROW(spdlog_ex("sid_t::sid_t(): invalid SID received"));
|
||||
}
|
||||
|
||||
auto const sid_length{::GetLengthSid(psid)};
|
||||
|
||||
sid_t result;
|
||||
result.buffer_.resize(sid_length);
|
||||
if (!::CopySid(sid_length, (PSID)result.as_sid(), psid))
|
||||
{
|
||||
SPDLOG_THROW(win32_error("CopySid"));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Retrieves pointer to the internal buffer contents as SID* */
|
||||
SID *as_sid() const
|
||||
{
|
||||
return buffer_.empty() ? nullptr : (SID *)buffer_.data();
|
||||
}
|
||||
|
||||
/** Get SID for the current user */
|
||||
static sid_t get_current_user_sid()
|
||||
{
|
||||
/* create and init RAII holder for process token */
|
||||
struct process_token_t
|
||||
{
|
||||
HANDLE token_handle_ = INVALID_HANDLE_VALUE;
|
||||
explicit process_token_t(HANDLE process)
|
||||
{
|
||||
if (!::OpenProcessToken(process, TOKEN_QUERY, &token_handle_))
|
||||
{
|
||||
SPDLOG_THROW(win32_error("OpenProcessToken"));
|
||||
}
|
||||
}
|
||||
|
||||
~process_token_t()
|
||||
{
|
||||
::CloseHandle(token_handle_);
|
||||
}
|
||||
|
||||
} current_process_token(::GetCurrentProcess()); // GetCurrentProcess returns pseudohandle, no leak here!
|
||||
|
||||
// Get the required size, this is expected to fail with ERROR_INSUFFICIENT_BUFFER and return the token size
|
||||
DWORD tusize = 0;
|
||||
if (::GetTokenInformation(current_process_token.token_handle_, TokenUser, NULL, 0, &tusize))
|
||||
{
|
||||
SPDLOG_THROW(win32_error("GetTokenInformation should fail"));
|
||||
}
|
||||
|
||||
// get user token
|
||||
std::vector<unsigned char> buffer(static_cast<size_t>(tusize));
|
||||
if (!::GetTokenInformation(current_process_token.token_handle_, TokenUser, (LPVOID)buffer.data(), tusize, &tusize))
|
||||
{
|
||||
SPDLOG_THROW(win32_error("GetTokenInformation"));
|
||||
}
|
||||
|
||||
// create a wrapper of the SID data as stored in the user token
|
||||
return sid_t::duplicate_sid(((TOKEN_USER *)buffer.data())->User.Sid);
|
||||
}
|
||||
};
|
||||
|
||||
struct eventlog
|
||||
{
|
||||
static WORD get_event_type(details::log_msg const &msg)
|
||||
{
|
||||
switch (msg.level)
|
||||
{
|
||||
case level::trace:
|
||||
case level::debug:
|
||||
return EVENTLOG_SUCCESS;
|
||||
|
||||
case level::info:
|
||||
return EVENTLOG_INFORMATION_TYPE;
|
||||
|
||||
case level::warn:
|
||||
return EVENTLOG_WARNING_TYPE;
|
||||
|
||||
case level::err:
|
||||
case level::critical:
|
||||
case level::off:
|
||||
return EVENTLOG_ERROR_TYPE;
|
||||
|
||||
default:
|
||||
// should be unreachable
|
||||
SPDLOG_THROW(std::logic_error(fmt::format("Unsupported log level {}", msg.level)));
|
||||
}
|
||||
}
|
||||
|
||||
static WORD get_event_category(details::log_msg const &msg)
|
||||
{
|
||||
return (WORD)msg.level;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
/*
|
||||
* Windows Event Log sink
|
||||
*/
|
||||
template<typename Mutex>
|
||||
class win_eventlog_sink : public base_sink<Mutex>
|
||||
{
|
||||
private:
|
||||
HANDLE hEventLog_{NULL};
|
||||
internal::sid_t current_user_sid_;
|
||||
std::string source_;
|
||||
WORD event_id_;
|
||||
|
||||
HANDLE event_log_handle()
|
||||
{
|
||||
if (!hEventLog_)
|
||||
{
|
||||
hEventLog_ = ::RegisterEventSource(nullptr, source_.c_str());
|
||||
if (!hEventLog_ || hEventLog_ == (HANDLE)ERROR_ACCESS_DENIED)
|
||||
{
|
||||
SPDLOG_THROW(internal::win32_error("RegisterEventSource"));
|
||||
}
|
||||
}
|
||||
|
||||
return hEventLog_;
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
using namespace internal;
|
||||
|
||||
memory_buf_t formatted;
|
||||
formatter_->format(msg, formatted);
|
||||
formatted.push_back('\0');
|
||||
LPCSTR lp_str = static_cast<LPCSTR>(formatted.data());
|
||||
|
||||
auto succeeded = ::ReportEvent(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
|
||||
current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr);
|
||||
|
||||
if (!succeeded)
|
||||
{
|
||||
SPDLOG_THROW(win32_error("ReportEvent"));
|
||||
}
|
||||
}
|
||||
|
||||
void flush_() override {}
|
||||
|
||||
public:
|
||||
win_eventlog_sink(std::string const &source, WORD event_id = 1000 /* according to mscoree.dll */)
|
||||
: source_(source)
|
||||
, event_id_(event_id)
|
||||
{
|
||||
try
|
||||
{
|
||||
current_user_sid_ = internal::sid_t::get_current_user_sid();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// get_current_user_sid() is unlikely to fail and if it does, we can still proceed without
|
||||
// current_user_sid but in the event log the record will have no user name
|
||||
}
|
||||
}
|
||||
|
||||
~win_eventlog_sink()
|
||||
{
|
||||
if (hEventLog_)
|
||||
DeregisterEventSource(hEventLog_);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace win_eventlog
|
||||
|
||||
using win_eventlog_sink_mt = win_eventlog::win_eventlog_sink<std::mutex>;
|
||||
using win_eventlog_sink_st = win_eventlog::win_eventlog_sink<details::null_mutex>;
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
265
src/fmt.cpp
265
src/fmt.cpp
@ -9,174 +9,181 @@
|
||||
#if !defined(SPDLOG_FMT_EXTERNAL)
|
||||
#include "spdlog/fmt/bundled/format-inl.h"
|
||||
|
||||
|
||||
FMT_BEGIN_NAMESPACE
|
||||
namespace internal {
|
||||
namespace internal {
|
||||
|
||||
template <typename T>
|
||||
int format_float(char* buf, std::size_t size, const char* format, int precision,
|
||||
T value) {
|
||||
template<typename T>
|
||||
int format_float(char *buf, std::size_t size, const char *format, int precision, T value)
|
||||
{
|
||||
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
||||
if (precision > 100000)
|
||||
throw std::runtime_error(
|
||||
"fuzz mode - avoid large allocation inside snprintf");
|
||||
if (precision > 100000)
|
||||
throw std::runtime_error("fuzz mode - avoid large allocation inside snprintf");
|
||||
#endif
|
||||
// Suppress the warning about nonliteral format string.
|
||||
auto snprintf_ptr = FMT_SNPRINTF;
|
||||
return precision < 0 ? snprintf_ptr(buf, size, format, value)
|
||||
: snprintf_ptr(buf, size, format, precision, value);
|
||||
}
|
||||
struct sprintf_specs {
|
||||
int precision;
|
||||
char type;
|
||||
bool alt : 1;
|
||||
// Suppress the warning about nonliteral format string.
|
||||
auto snprintf_ptr = FMT_SNPRINTF;
|
||||
return precision < 0 ? snprintf_ptr(buf, size, format, value) : snprintf_ptr(buf, size, format, precision, value);
|
||||
}
|
||||
struct sprintf_specs
|
||||
{
|
||||
int precision;
|
||||
char type;
|
||||
bool alt : 1;
|
||||
|
||||
template <typename Char>
|
||||
constexpr sprintf_specs(basic_format_specs<Char> specs)
|
||||
: precision(specs.precision), type(specs.type), alt(specs.alt) {}
|
||||
template<typename Char>
|
||||
constexpr sprintf_specs(basic_format_specs<Char> specs)
|
||||
: 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.
|
||||
template <typename Double>
|
||||
char* sprintf_format(Double value, internal::buffer<char>& buf,
|
||||
sprintf_specs specs) {
|
||||
// Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail.
|
||||
FMT_ASSERT(buf.capacity() != 0, "empty buffer");
|
||||
template<typename Double>
|
||||
char *sprintf_format(Double value, internal::buffer<char> &buf, sprintf_specs specs)
|
||||
{
|
||||
// Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail.
|
||||
FMT_ASSERT(buf.capacity() != 0, "empty buffer");
|
||||
|
||||
// Build format string.
|
||||
enum { max_format_size = 10 }; // longest format: %#-*.*Lg
|
||||
char format[max_format_size];
|
||||
char* format_ptr = format;
|
||||
*format_ptr++ = '%';
|
||||
if (specs.alt || !specs.type) *format_ptr++ = '#';
|
||||
if (specs.precision >= 0) {
|
||||
*format_ptr++ = '.';
|
||||
*format_ptr++ = '*';
|
||||
}
|
||||
if (std::is_same<Double, long double>::value) *format_ptr++ = 'L';
|
||||
// Build format string.
|
||||
enum
|
||||
{
|
||||
max_format_size = 10
|
||||
}; // longest format: %#-*.*Lg
|
||||
char format[max_format_size];
|
||||
char *format_ptr = format;
|
||||
*format_ptr++ = '%';
|
||||
if (specs.alt || !specs.type)
|
||||
*format_ptr++ = '#';
|
||||
if (specs.precision >= 0)
|
||||
{
|
||||
*format_ptr++ = '.';
|
||||
*format_ptr++ = '*';
|
||||
}
|
||||
if (std::is_same<Double, long double>::value)
|
||||
*format_ptr++ = 'L';
|
||||
|
||||
char type = specs.type;
|
||||
char type = specs.type;
|
||||
|
||||
if (type == '%')
|
||||
type = 'f';
|
||||
else if (type == 0 || type == 'n')
|
||||
type = 'g';
|
||||
if (type == '%')
|
||||
type = 'f';
|
||||
else if (type == 0 || type == 'n')
|
||||
type = 'g';
|
||||
#if FMT_MSC_VER
|
||||
if (type == 'F') {
|
||||
// MSVC's printf doesn't support 'F'.
|
||||
type = 'f';
|
||||
}
|
||||
if (type == 'F')
|
||||
{
|
||||
// MSVC's printf doesn't support 'F'.
|
||||
type = 'f';
|
||||
}
|
||||
#endif
|
||||
*format_ptr++ = type;
|
||||
*format_ptr = '\0';
|
||||
*format_ptr++ = type;
|
||||
*format_ptr = '\0';
|
||||
|
||||
// Format using snprintf.
|
||||
char* start = nullptr;
|
||||
char* decimal_point_pos = nullptr;
|
||||
for (;;) {
|
||||
std::size_t buffer_size = buf.capacity();
|
||||
start = &buf[0];
|
||||
int result =
|
||||
format_float(start, buffer_size, format, specs.precision, value);
|
||||
if (result >= 0) {
|
||||
unsigned n = internal::to_unsigned(result);
|
||||
if (n < buf.capacity()) {
|
||||
// Find the decimal point.
|
||||
auto p = buf.data(), end = p + n;
|
||||
if (*p == '+' || *p == '-') ++p;
|
||||
if (specs.type != 'a' && specs.type != 'A') {
|
||||
while (p < end && *p >= '0' && *p <= '9') ++p;
|
||||
if (p < end && *p != 'e' && *p != 'E') {
|
||||
decimal_point_pos = p;
|
||||
if (!specs.type) {
|
||||
// Keep only one trailing zero after the decimal point.
|
||||
++p;
|
||||
if (*p == '0') ++p;
|
||||
while (p != end && *p >= '1' && *p <= '9') ++p;
|
||||
char* where = p;
|
||||
while (p != end && *p == '0') ++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);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Format using snprintf.
|
||||
char *start = nullptr;
|
||||
char *decimal_point_pos = nullptr;
|
||||
for (;;)
|
||||
{
|
||||
std::size_t buffer_size = buf.capacity();
|
||||
start = &buf[0];
|
||||
int result = format_float(start, buffer_size, format, specs.precision, value);
|
||||
if (result >= 0)
|
||||
{
|
||||
unsigned n = internal::to_unsigned(result);
|
||||
if (n < buf.capacity())
|
||||
{
|
||||
// Find the decimal point.
|
||||
auto p = buf.data(), end = p + n;
|
||||
if (*p == '+' || *p == '-')
|
||||
++p;
|
||||
if (specs.type != 'a' && specs.type != 'A')
|
||||
{
|
||||
while (p < end && *p >= '0' && *p <= '9')
|
||||
++p;
|
||||
if (p < end && *p != 'e' && *p != 'E')
|
||||
{
|
||||
decimal_point_pos = p;
|
||||
if (!specs.type)
|
||||
{
|
||||
// Keep only one trailing zero after the decimal point.
|
||||
++p;
|
||||
if (*p == '0')
|
||||
++p;
|
||||
while (p != end && *p >= '1' && *p <= '9')
|
||||
++p;
|
||||
char *where = p;
|
||||
while (p != end && *p == '0')
|
||||
++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);
|
||||
}
|
||||
buf.resize(n);
|
||||
break; // The buffer is large enough - continue with formatting.
|
||||
}
|
||||
buf.reserve(n + 1);
|
||||
} else {
|
||||
// If result is negative we ask to increase the capacity by at least 1,
|
||||
// but as std::vector, the buffer grows exponentially.
|
||||
buf.reserve(buf.capacity() + 1);
|
||||
}
|
||||
}
|
||||
return decimal_point_pos;
|
||||
buf.resize(n);
|
||||
break; // The buffer is large enough - continue with formatting.
|
||||
}
|
||||
} // namespace internal
|
||||
buf.reserve(n + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If result is negative we ask to increase the capacity by at least 1,
|
||||
// but as std::vector, the buffer grows exponentially.
|
||||
buf.reserve(buf.capacity() + 1);
|
||||
}
|
||||
}
|
||||
return decimal_point_pos;
|
||||
}
|
||||
} // namespace internal
|
||||
|
||||
template FMT_API char* internal::sprintf_format(double, internal::buffer<char>&,
|
||||
sprintf_specs);
|
||||
template FMT_API char* internal::sprintf_format(long double,
|
||||
internal::buffer<char>&,
|
||||
sprintf_specs);
|
||||
template FMT_API char *internal::sprintf_format(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.
|
||||
int (*instantiate_format_float)(double, int, internal::float_specs,
|
||||
internal::buffer<char>&) =
|
||||
internal::format_float;
|
||||
int (*instantiate_format_float)(double, int, internal::float_specs, internal::buffer<char> &) = internal::format_float;
|
||||
|
||||
#ifndef FMT_STATIC_THOUSANDS_SEPARATOR
|
||||
template FMT_API internal::locale_ref::locale_ref(const std::locale& loc);
|
||||
template FMT_API std::locale internal::locale_ref::get<std::locale>() const;
|
||||
template FMT_API internal::locale_ref::locale_ref(const std::locale &loc);
|
||||
template FMT_API std::locale internal::locale_ref::get<std::locale>() const;
|
||||
#endif
|
||||
|
||||
// Explicit instantiations for char.
|
||||
|
||||
template FMT_API std::string internal::grouping_impl<char>(locale_ref);
|
||||
template FMT_API char internal::thousands_sep_impl(locale_ref);
|
||||
template FMT_API char internal::decimal_point_impl(locale_ref);
|
||||
template FMT_API std::string internal::grouping_impl<char>(locale_ref);
|
||||
template FMT_API char internal::thousands_sep_impl(locale_ref);
|
||||
template FMT_API char internal::decimal_point_impl(locale_ref);
|
||||
|
||||
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(
|
||||
const basic_format_args<format_context>& args);
|
||||
template FMT_API void internal::arg_map<format_context>::init(const basic_format_args<format_context> &args);
|
||||
|
||||
template FMT_API std::string internal::vformat<char>(
|
||||
string_view, basic_format_args<format_context>);
|
||||
template FMT_API std::string internal::vformat<char>(string_view, basic_format_args<format_context>);
|
||||
|
||||
template FMT_API format_context::iterator internal::vformat_to(
|
||||
internal::buffer<char>&, string_view, basic_format_args<format_context>);
|
||||
template FMT_API format_context::iterator internal::vformat_to(internal::buffer<char> &, string_view, basic_format_args<format_context>);
|
||||
|
||||
template FMT_API int internal::snprintf_float(double, int,
|
||||
internal::float_specs,
|
||||
internal::buffer<char>&);
|
||||
template FMT_API int internal::snprintf_float(long double, int,
|
||||
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>&);
|
||||
template FMT_API int internal::snprintf_float(double, int, internal::float_specs, internal::buffer<char> &);
|
||||
template FMT_API int internal::snprintf_float(long double, int, 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.
|
||||
|
||||
template FMT_API std::string internal::grouping_impl<wchar_t>(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 std::string internal::grouping_impl<wchar_t>(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 void internal::buffer<wchar_t>::append(const wchar_t*,
|
||||
const wchar_t*);
|
||||
template FMT_API void internal::buffer<wchar_t>::append(const wchar_t *, const wchar_t *);
|
||||
|
||||
template FMT_API std::wstring internal::vformat<wchar_t>(
|
||||
wstring_view, basic_format_args<wformat_context>);
|
||||
template FMT_API std::wstring internal::vformat<wchar_t>(wstring_view, basic_format_args<wformat_context>);
|
||||
FMT_END_NAMESPACE
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -19,6 +19,7 @@ set(SPDLOG_UTESTS_SOURCES
|
||||
test_file_logging.cpp
|
||||
test_daily_logger.cpp
|
||||
test_misc.cpp
|
||||
test_eventlog.cpp
|
||||
test_pattern_formatter.cpp
|
||||
test_async.cpp
|
||||
test_registry.cpp
|
||||
|
@ -169,9 +169,10 @@ TEST_CASE("to_file", "[async]")
|
||||
}
|
||||
}
|
||||
|
||||
REQUIRE(count_lines(filename) == messages);
|
||||
require_message_count(filename, messages);
|
||||
auto contents = file_contents(filename);
|
||||
REQUIRE(ends_with(contents, std::string("Hello message #1023\n")));
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(ends_with(contents, fmt::format("Hello message #1023{}", default_eol)));
|
||||
}
|
||||
|
||||
TEST_CASE("to_file multi-workers", "[async]")
|
||||
@ -191,5 +192,5 @@ TEST_CASE("to_file multi-workers", "[async]")
|
||||
}
|
||||
}
|
||||
|
||||
REQUIRE(count_lines(filename) == messages);
|
||||
require_message_count(filename, messages);
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
|
||||
logger->flush();
|
||||
|
||||
auto filename = fmt::to_string(w);
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
require_message_count(filename, 10);
|
||||
}
|
||||
|
||||
struct custom_daily_file_name_calculator
|
||||
@ -55,12 +55,10 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]")
|
||||
logger->info("Test message {}", i);
|
||||
}
|
||||
|
||||
logger->
|
||||
|
||||
flush();
|
||||
logger->flush();
|
||||
|
||||
auto filename = fmt::to_string(w);
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
require_message_count(filename, 10);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -34,7 +34,8 @@ TEST_CASE("default_error_handler", "[errors]]")
|
||||
logger->info("Test message {}", 2);
|
||||
logger->flush();
|
||||
|
||||
REQUIRE(file_contents(filename) == std::string("Test message 2\n"));
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(file_contents(filename) == fmt::format("Test message 2{}", default_eol));
|
||||
REQUIRE(count_lines(filename) == 1);
|
||||
}
|
||||
|
||||
@ -51,7 +52,7 @@ TEST_CASE("custom_error_handler", "[errors]]")
|
||||
|
||||
REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
|
||||
logger->info("Good message #2");
|
||||
REQUIRE(count_lines(filename) == 2);
|
||||
require_message_count(filename, 2);
|
||||
}
|
||||
|
||||
TEST_CASE("default_error_handler2", "[errors]]")
|
||||
@ -93,7 +94,7 @@ TEST_CASE("async_error_handler", "[errors]]")
|
||||
spdlog::drop("logger"); // force logger to drain the queue and shutdown
|
||||
}
|
||||
spdlog::init_thread_pool(128, 1);
|
||||
REQUIRE(count_lines(filename) == 2);
|
||||
require_message_count(filename, 2);
|
||||
REQUIRE(file_contents("test_logs/custom_err.txt") == err_msg);
|
||||
}
|
||||
|
||||
|
71
tests/test_eventlog.cpp
Normal file
71
tests/test_eventlog.cpp
Normal file
@ -0,0 +1,71 @@
|
||||
#if _WIN32
|
||||
|
||||
#include "includes.h"
|
||||
#include "test_sink.h"
|
||||
|
||||
#include "spdlog/sinks/win_eventlog_sink.h"
|
||||
|
||||
static const LPCSTR TEST_SOURCE = "spdlog_test";
|
||||
|
||||
static void test_single_print(std::function<void(std::string const &)> do_log, std::string const &expected_contents, WORD expected_ev_type)
|
||||
{
|
||||
using namespace std::chrono;
|
||||
do_log(expected_contents);
|
||||
const auto expected_time_generated = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
|
||||
|
||||
struct handle_t
|
||||
{
|
||||
HANDLE handle_;
|
||||
|
||||
~handle_t()
|
||||
{
|
||||
if (handle_)
|
||||
{
|
||||
REQUIRE(CloseEventLog(handle_));
|
||||
}
|
||||
}
|
||||
} event_log{::OpenEventLog(nullptr, TEST_SOURCE)};
|
||||
|
||||
REQUIRE(event_log.handle_);
|
||||
|
||||
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);
|
||||
REQUIRE(!ok);
|
||||
REQUIRE(::GetLastError() == ERROR_INSUFFICIENT_BUFFER);
|
||||
|
||||
std::vector<char> record_buffer(size_needed);
|
||||
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);
|
||||
REQUIRE(ok);
|
||||
|
||||
REQUIRE(record->NumStrings == 1);
|
||||
REQUIRE(record->EventType == expected_ev_type);
|
||||
REQUIRE(record->TimeGenerated == expected_time_generated);
|
||||
|
||||
std::string message_in_log(((char *)record + record->StringOffset));
|
||||
REQUIRE(message_in_log == expected_contents + spdlog::details::os::default_eol);
|
||||
}
|
||||
|
||||
TEST_CASE("eventlog", "[eventlog]")
|
||||
{
|
||||
using namespace spdlog;
|
||||
|
||||
auto test_sink = std::make_shared<sinks::win_eventlog_sink_mt>(TEST_SOURCE);
|
||||
|
||||
spdlog::logger test_logger("eventlog", test_sink);
|
||||
test_logger.set_level(level::trace);
|
||||
|
||||
test_sink->set_pattern("%v");
|
||||
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.trace(msg); }, "my trace message", EVENTLOG_SUCCESS);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.debug(msg); }, "my debug message", EVENTLOG_SUCCESS);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.info(msg); }, "my info message", EVENTLOG_INFORMATION_TYPE);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.warn(msg); }, "my warn message", EVENTLOG_WARNING_TYPE);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.error(msg); }, "my error message", EVENTLOG_ERROR_TYPE);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.critical(msg); }, "my critical message", EVENTLOG_ERROR_TYPE);
|
||||
}
|
||||
|
||||
#endif //_WIN32
|
@ -15,8 +15,9 @@ TEST_CASE("simple_file_logger", "[simple_logger]]")
|
||||
logger->info("Test message {}", 2);
|
||||
|
||||
logger->flush();
|
||||
REQUIRE(file_contents(filename) == std::string("Test message 1\nTest message 2\n"));
|
||||
REQUIRE(count_lines(filename) == 2);
|
||||
require_message_count(filename, 2);
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(file_contents(filename) == fmt::format("Test message 1{}Test message 2{}", default_eol, default_eol));
|
||||
}
|
||||
|
||||
TEST_CASE("flush_on", "[flush_on]]")
|
||||
@ -34,8 +35,10 @@ TEST_CASE("flush_on", "[flush_on]]")
|
||||
logger->info("Test message {}", 1);
|
||||
logger->info("Test message {}", 2);
|
||||
|
||||
REQUIRE(file_contents(filename) == std::string("Should not be flushed\nTest message 1\nTest message 2\n"));
|
||||
REQUIRE(count_lines(filename) == 3);
|
||||
require_message_count(filename, 3);
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(file_contents(filename) ==
|
||||
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]]")
|
||||
@ -52,7 +55,7 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
||||
|
||||
logger->flush();
|
||||
auto filename = basename;
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
require_message_count(filename, 10);
|
||||
}
|
||||
|
||||
TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
||||
@ -81,7 +84,8 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
||||
|
||||
logger->flush();
|
||||
auto filename = basename;
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
require_message_count(filename, 10);
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
|
||||
|
@ -22,7 +22,8 @@ TEST_CASE("debug and trace w/o format string", "[macros]]")
|
||||
SPDLOG_LOGGER_DEBUG(logger, "Test message 2");
|
||||
logger->flush();
|
||||
|
||||
REQUIRE(ends_with(file_contents(filename), "Test message 2\n"));
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(ends_with(file_contents(filename), fmt::format("Test message 2{}", default_eol)));
|
||||
REQUIRE(count_lines(filename) == 1);
|
||||
|
||||
spdlog::set_default_logger(logger);
|
||||
@ -31,8 +32,8 @@ TEST_CASE("debug and trace w/o format string", "[macros]]")
|
||||
SPDLOG_DEBUG("Test message {}", 4);
|
||||
logger->flush();
|
||||
|
||||
REQUIRE(ends_with(file_contents(filename), "Test message 4\n"));
|
||||
REQUIRE(count_lines(filename) == 2);
|
||||
require_message_count(filename, 2);
|
||||
REQUIRE(ends_with(file_contents(filename), fmt::format("Test message 4{}", default_eol)));
|
||||
}
|
||||
|
||||
TEST_CASE("disable param evaluation", "[macros]")
|
||||
|
@ -21,7 +21,7 @@ void prepare_logdir()
|
||||
|
||||
std::string file_contents(const std::string &filename)
|
||||
{
|
||||
std::ifstream ifs(filename);
|
||||
std::ifstream ifs(filename, std::ios_base::binary);
|
||||
if (!ifs)
|
||||
{
|
||||
throw std::runtime_error("Failed open file ");
|
||||
@ -44,6 +44,18 @@ std::size_t count_lines(const std::string &filename)
|
||||
return counter;
|
||||
}
|
||||
|
||||
void require_message_count(const std::string &filename, const std::size_t messages)
|
||||
{
|
||||
if (strlen(spdlog::details::os::default_eol) == 0)
|
||||
{
|
||||
REQUIRE(count_lines(filename) == 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
REQUIRE(count_lines(filename) == messages);
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t get_filesize(const std::string &filename)
|
||||
{
|
||||
std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
|
||||
|
@ -11,6 +11,8 @@ std::string file_contents(const std::string &filename);
|
||||
|
||||
std::size_t count_lines(const std::string &filename);
|
||||
|
||||
void require_message_count(const std::string &filename, const std::size_t messages);
|
||||
|
||||
std::size_t get_filesize(const std::string &filename);
|
||||
|
||||
bool ends_with(std::string const &value, std::string const &ending);
|
Loading…
Reference in New Issue
Block a user