mirror of
https://github.com/gabime/spdlog.git
synced 2026-04-10 11:34:29 +08:00
Synchronize v2.x With Changes from v1.x (#3581)
* docs(tasks): v1.x inventory, triage template, merge report; update task checklist - Record merge-base and export v1-only commits (v2..v1) - Document failed merge attempt: v2 file tree diverges from v1 - Mark completed checklist items 0.x, 1.x, merge report draft (5.2) Made-with: Cursor * fix(pattern): %z with UTC pattern time shows +00:00 (port v109a674b7) - z_formatter takes pattern_time_type; emit +00:00 when pattern uses UTC - Track port in tasks/commits-ported.txt and merge-report; update checklist Made-with: Cursor * fix(os): Windows utc_minutes_offset via mktime/_mkgmtime (port v1b656d1ce) - Replace GetTimeZoneInformation-based offset with v1.x mktime/_mkgmtime approach - Add tests/test_timezone.cpp and extend pattern_formatter tests; update os.h comment Made-with: Cursor * test(timezone): POSIX TZ with DST rules; include fcntl in tcp_client_unix (v1 ports) -0f7562a0: EST5EDT / IST-2IDT macros for POSIX vs Windows -d2100d5d: fcntl.h for Unix tcp client header (v2 path) Made-with: Cursor * feat(tcp): connect timeouts and socket IO timeouts; ci: checkout@v6 (v1 ports) -9ecdf5c8: connect_socket_with_timeout on Unix/Windows; tcp_sink timeout_ms -3c61b051: actions/checkout@v6 on all workflows - tcp_sink: remove duplicate #pragma once Made-with: Cursor * feat(dup_filter_sink): ctor with sink list (v145b67eee); add full v1 SHA triage table - tasks/v1-triage-complete.md: 245 commits PORTED/PENDING/N/A for PRD 3A tracking - Merge report and task checklist updated Made-with: Cursor * feat(level): case-insensitive level_from_str; cmake: BUILD_TYPE only if top-level (v1 ports) -566b2d14: common.cpp + test_misc (SUPERSEDED note ford5af52d9in triage) -dd3ca04a: guard default CMAKE_BUILD_TYPE for add_subdirectory consumers - Update v1-triage-complete.md counts and statuses Made-with: Cursor * v1 parity: MSVC UTF-8, ansicolor/syslog/os, getenv, triage docs - CMake: SPDLOG_MSVC_UTF8 + /utf-8 for real MSVC only - ansicolor: protected target_file_; fix set_color_mode_ lock nesting - os: drop redundant fileapi.h; spdlog::details::os::getenv via std::getenv - tests: stopwatch waits 500ms; includes/triage/commits-ported/merge-report updates - Reclassify superseded v1 SHAs (Sep, syslog, stopwatch ms, utf8 tests, etc.) Made-with: Cursor * parity: ringbuffer zero capacity, utf8 assert; v1 triage sync - ringbuffer_sink: throw if n_items==0 (#3436); test expects spdlog_ex - utf8_to_wstrbuf: assert compares int to static_cast<int>(target.size()) (#3479) - Triage: PORTEDad725d34getenv,677a2d93stopwatch,3f7e5028, a6215527; SUPERSEDEDa45c9390,eeb22c13,5673e9e5,fe4f9952,287333ee- tasks: 27 PORTED / 26 SUPERSEDED / 78 PENDING; merge-report + commits-ported Made-with: Cursor * docs(tasks): note regular commits and push after parity ports Made-with: Cursor * parity: dup_filter_sink notification level from last duplicate (#3390) - Remove notification_level ctor arg; track skipped_msg_log_level_ on duplicate skips - Test: skipped summary line uses same short level as duplicated messages - Triage:847db337PORTED; 28/26/77 counts; merge-report + commits-ported Made-with: Cursor * parity: UWP getenv (WINAPI_FAMILY); triage fmt/cfg/no-except - os_windows: detect non-desktop/UWP for empty getenv (#3489) - Triage: PORTED 8806ca65; SUPERSEDEDe3f5a4fee655dbb6; N/Aae1de0dc548b2642- Counts 29/28/116/72; merge-report + commits-ported Made-with: Cursor * parity: qt_sinks sign casts (#3487); triage9c582574superseded - qt_color_sink: qsizetype for UTF-8 color range lengths; index colors_ with size_t - SUPERSEDED:9c582574— os_unix utc_minutes_offset already matches #3366 - Counts 30/29/70; merge-report + commits-ported Made-with: Cursor * parity: SPDLOG_NO_TZ_OFFSET (#3483); triage #3360 superseded - CMake: option SPDLOG_NO_TZ_OFFSET; PUBLIC compile definition when ON - z_formatter: +??:?? when macro; else keep UTC +00:00 and local offset - utc_minutes_offset: stub on Unix/Windows when macro - test_pattern_formatter: %z UTC case matches placeholder when NO_TZ - SUPERSEDED10320184(ScopedPadder / %D already in v2) - Triage 31/30/68; merge-report + commits-ported Made-with: Cursor * triage: supersede5931a3d6ba50805747b7e7c7(already on v2 tree) Made-with: Cursor * tasks: sync 5.1 triage counts (33 superseded, 65 pending) Made-with: Cursor * parity: MSVC/clang hygiene #3515–#3519 #3521; triage batch - dup_filter_sink: filter_ const, const filter_duration (#3515) - logger: should_flush uses flush_level() (#3516) - daily_file_sink: new_filename locals (#3516) - spdlog::should_log(level log_level) (#3519) - example my_type value_ / ctor param (#3521) - Triage: PORTED1774e700309204d5f2a9dec0472945ba; N/Ad299603e57505989; SUPERSEDED1ef8d3ce8cfd4a7e; fixba508057row Made-with: Cursor * parity: udp_sink const udp_sink_config& (#3520); triagefc7e9c871685e694- dist_sink already used std::move(sinks) - SUPERSEDED: no common-inl.h on v2; log_with_format_ avoids fmt copy path (#3541) Made-with: Cursor * parity: README fmt::format_to (#3259); triage2670f47dd276069a951c5b99- Document ambiguous format_to fix in README user-defined type example - SUPERSEDED: z_formatter warning, fmt11 const formatter, rotate_now + test Made-with: Cursor * parity: lock rotate_now mutex (#3281); triagea2b42620f355b3d5d276069a- rotating_file_sink::rotate_now matches #3281 (sync with sink_it_) - SUPERSEDED: CMake 3.10..3.21 (v2 uses 3.23); daily test fmt::format; fmt11 const row Made-with: Cursor * parity: basic_file_sink::truncate (#3280); triage fwrite/fmt/test batch - truncate(): lock + file_helper::reopen(true) - test basic_file_sink_truncate - SUPERSEDED:b7e0e2c271925ca3fa6605dc885b547396c9a62b1e6250e1d7155530Made-with: Cursor * parity: test_sink/callback iterator cast (#3315); triage Catch2 #3038 - difference_type cast for formatted.end() - eol_len (ad0f31c0) - SUPERSEDED:c1569a3dCatch2 v3.5.0,73e2e02bwstr_to_utf8buf bounds Made-with: Cursor * parity: SPDLOG_WCHAR_CONSOLE WriteConsoleW path (#3092); triageb6da5944- WIN32 option SPDLOG_WCHAR_CONSOLE; SPDLOG_UTF8_TO_WCHAR_CONSOLE in private defs - wincolor print_range_: utf8_to_wstrbuf + WriteConsoleW when defined - N/A: async_msg flush_callback move-assign (no v1 shape on v2) Made-with: Cursor * triage: v1 async_logger/thread_pool + fmt 11.1 batch (15 SHAs) - N/A:fe79bfcc6725584easync tests16e0d2e763d18842d8e0ad461e7d7e073c23c27d- SUPERSEDED:faa0a7a985bdab0c276ee5f57f8060d596a8f625- Counts: 58 SUPERSEDED, 129 N/A, 17 PENDING; merge-report subsection Made-with: Cursor * Port v1 TSAN CMake (#3237); close 3A triage (MDC N/A, fmt 5A) Made-with: Cursor * docs(triage): note 3A table complete Made-with: Cursor * 5A: bundle fmt 12.1.0 (match v1.x), FMT_INSTALL, MSVC /wd4834, find_dependency(fmt 12) Made-with: Cursor * docs(5.4): migration and release notes for v1 parity integration branch Made-with: Cursor * docs: cross-link migration notes; refresh PRD/tasks/merge-report for completed 5A/3A Made-with: Cursor * revert: remove README and PRD cross-links to migration notes Made-with: Cursor * docs(merge-report): audit — add9fe79692to ports table; fix6004e3d1paths Made-with: Cursor * Remove tasks * PR comment fixes * Fix small random issues * Fix PR comments - un-remove the comment and allow any fmt library version --------- Co-authored-by: Jan Moravec <jan.moravec@hidglobal.com>
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
|
||||
#include <spdlog/common.h>
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
template <typename T>
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
|
||||
// multi producer-multi consumer blocking queue.
|
||||
// enqueue(..) - will block until room found to put the new message.
|
||||
// enqueue_nowait(..) - will return immediately with false if no room left in
|
||||
// the queue.
|
||||
// enqueue_nowait(..) - enqueue immediately; overruns oldest message if no
|
||||
// room left.
|
||||
// dequeue_for(..) - will block until the queue is not empty or timeout have
|
||||
// passed.
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ struct null_mutex {
|
||||
|
||||
template <typename T>
|
||||
struct null_atomic {
|
||||
T value;
|
||||
T value{};
|
||||
|
||||
null_atomic() = default;
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ SPDLOG_API bool fopen_s(FILE **fp, const filename_t &filename, const filename_t
|
||||
// Return file size according to open FILE* object
|
||||
SPDLOG_API size_t filesize(FILE *f);
|
||||
|
||||
// Return utc offset in minutes or throw spdlog_ex on failure
|
||||
// Return utc offset in minutes (0 on failure to compute offset)
|
||||
SPDLOG_API int utc_minutes_offset(const std::tm &tm = details::os::localtime());
|
||||
|
||||
// Return current thread id as size_t
|
||||
|
||||
@@ -13,9 +13,13 @@
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <string.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <string>
|
||||
|
||||
#include "../common.h"
|
||||
@@ -40,8 +44,67 @@ public:
|
||||
|
||||
~tcp_client_unix() { close(); }
|
||||
|
||||
static int connect_socket_with_timeout(int sockfd,
|
||||
const struct sockaddr *addr,
|
||||
socklen_t addrlen,
|
||||
const struct timeval &tv) {
|
||||
if (tv.tv_sec == 0 && tv.tv_usec == 0) {
|
||||
int rv = ::connect(sockfd, addr, addrlen);
|
||||
if (rv < 0 && errno == EISCONN) {
|
||||
return 0;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
int orig_flags = ::fcntl(sockfd, F_GETFL, 0);
|
||||
if (orig_flags < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (::fcntl(sockfd, F_SETFL, orig_flags | O_NONBLOCK) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int rv = ::connect(sockfd, addr, addrlen);
|
||||
if (rv == 0 || (rv < 0 && errno == EISCONN)) {
|
||||
::fcntl(sockfd, F_SETFL, orig_flags);
|
||||
return 0;
|
||||
}
|
||||
if (errno != EINPROGRESS) {
|
||||
::fcntl(sockfd, F_SETFL, orig_flags);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fd_set wfds;
|
||||
FD_ZERO(&wfds);
|
||||
FD_SET(sockfd, &wfds);
|
||||
|
||||
struct timeval tv_copy = tv;
|
||||
rv = ::select(sockfd + 1, nullptr, &wfds, nullptr, &tv_copy);
|
||||
if (rv <= 0) {
|
||||
::fcntl(sockfd, F_SETFL, orig_flags);
|
||||
if (rv == 0) {
|
||||
errno = ETIMEDOUT;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int so_error = 0;
|
||||
socklen_t len = sizeof(so_error);
|
||||
if (::getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &so_error, &len) < 0) {
|
||||
::fcntl(sockfd, F_SETFL, orig_flags);
|
||||
return -1;
|
||||
}
|
||||
::fcntl(sockfd, F_SETFL, orig_flags);
|
||||
if (so_error != 0 && so_error != EISCONN) {
|
||||
errno = so_error;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// try to connect or throw on failure
|
||||
void connect(const std::string &host, int port) {
|
||||
void connect(const std::string &host, int port, int timeout_ms = 0) {
|
||||
close();
|
||||
struct addrinfo hints {};
|
||||
memset(&hints, 0, sizeof(struct addrinfo));
|
||||
@@ -50,6 +113,11 @@ public:
|
||||
hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value
|
||||
hints.ai_protocol = 0;
|
||||
|
||||
const int validated_timeout_ms = timeout_ms > 0 ? timeout_ms : 0;
|
||||
struct timeval tv;
|
||||
tv.tv_sec = validated_timeout_ms / 1000;
|
||||
tv.tv_usec = (validated_timeout_ms % 1000) * 1000;
|
||||
|
||||
auto port_str = std::to_string(port);
|
||||
struct addrinfo *addrinfo_result;
|
||||
auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result);
|
||||
@@ -70,8 +138,11 @@ public:
|
||||
last_errno = errno;
|
||||
continue;
|
||||
}
|
||||
rv = ::connect(socket_, rp->ai_addr, rp->ai_addrlen);
|
||||
if (rv == 0) {
|
||||
#ifndef SOCK_CLOEXEC
|
||||
::fcntl(socket_, F_SETFD, FD_CLOEXEC);
|
||||
#endif
|
||||
if (connect_socket_with_timeout(socket_, rp->ai_addr, rp->ai_addrlen, tv) == 0) {
|
||||
last_errno = 0;
|
||||
break;
|
||||
}
|
||||
last_errno = errno;
|
||||
@@ -83,6 +154,11 @@ public:
|
||||
throw_spdlog_ex("::connect failed", last_errno);
|
||||
}
|
||||
|
||||
if (validated_timeout_ms > 0) {
|
||||
::setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
::setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
||||
}
|
||||
|
||||
// set TCP_NODELAY
|
||||
int enable_flag = 1;
|
||||
::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&enable_flag), sizeof(enable_flag));
|
||||
|
||||
@@ -58,8 +58,76 @@ public:
|
||||
|
||||
SOCKET fd() const { return socket_; }
|
||||
|
||||
int connect_socket_with_timeout(SOCKET sockfd,
|
||||
const struct sockaddr *addr,
|
||||
int addrlen,
|
||||
const timeval &tv) {
|
||||
if (tv.tv_sec == 0 && tv.tv_usec == 0) {
|
||||
int rv = ::connect(sockfd, addr, addrlen);
|
||||
if (rv == SOCKET_ERROR && WSAGetLastError() == WSAEISCONN) {
|
||||
return 0;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
u_long mode = 1UL;
|
||||
if (::ioctlsocket(sockfd, FIONBIO, &mode) == SOCKET_ERROR) {
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
|
||||
int rv = ::connect(sockfd, addr, addrlen);
|
||||
int last_error = WSAGetLastError();
|
||||
if (rv == 0 || last_error == WSAEISCONN) {
|
||||
mode = 0UL;
|
||||
if (::ioctlsocket(sockfd, FIONBIO, &mode) == SOCKET_ERROR) {
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (last_error != WSAEWOULDBLOCK) {
|
||||
mode = 0UL;
|
||||
if (::ioctlsocket(sockfd, FIONBIO, &mode)) {
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
|
||||
fd_set wfds;
|
||||
FD_ZERO(&wfds);
|
||||
FD_SET(sockfd, &wfds);
|
||||
|
||||
timeval tv_copy = tv;
|
||||
rv = ::select(0, nullptr, &wfds, nullptr, &tv_copy);
|
||||
|
||||
mode = 0UL;
|
||||
if (::ioctlsocket(sockfd, FIONBIO, &mode) == SOCKET_ERROR) {
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
|
||||
if (rv == 0) {
|
||||
WSASetLastError(WSAETIMEDOUT);
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
if (rv == SOCKET_ERROR) {
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
|
||||
int so_error = 0;
|
||||
int len = sizeof(so_error);
|
||||
if (::getsockopt(sockfd, SOL_SOCKET, SO_ERROR, reinterpret_cast<char *>(&so_error), &len) ==
|
||||
SOCKET_ERROR) {
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
if (so_error != 0 && so_error != WSAEISCONN) {
|
||||
WSASetLastError(so_error);
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// try to connect or throw on failure
|
||||
void connect(const std::string &host, int port) {
|
||||
void connect(const std::string &host, int port, int timeout_ms = 0) {
|
||||
if (is_connected()) {
|
||||
close();
|
||||
}
|
||||
@@ -71,13 +139,17 @@ public:
|
||||
hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value
|
||||
hints.ai_protocol = 0;
|
||||
|
||||
const int validated_timeout_ms = timeout_ms > 0 ? timeout_ms : 0;
|
||||
timeval tv;
|
||||
tv.tv_sec = validated_timeout_ms / 1000;
|
||||
tv.tv_usec = (validated_timeout_ms % 1000) * 1000;
|
||||
|
||||
auto port_str = std::to_string(port);
|
||||
struct addrinfo *addrinfo_result;
|
||||
auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result);
|
||||
int last_error = 0;
|
||||
if (rv != 0) {
|
||||
last_error = ::WSAGetLastError();
|
||||
WSACleanup();
|
||||
throw_winsock_error_("getaddrinfo failed", last_error);
|
||||
}
|
||||
|
||||
@@ -87,21 +159,25 @@ public:
|
||||
socket_ = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||
if (socket_ == INVALID_SOCKET) {
|
||||
last_error = ::WSAGetLastError();
|
||||
WSACleanup();
|
||||
continue;
|
||||
}
|
||||
if (::connect(socket_, rp->ai_addr, (int)rp->ai_addrlen) == 0) {
|
||||
if (connect_socket_with_timeout(socket_, rp->ai_addr, (int)rp->ai_addrlen, tv) == 0) {
|
||||
last_error = 0;
|
||||
break;
|
||||
} else {
|
||||
last_error = ::WSAGetLastError();
|
||||
close();
|
||||
}
|
||||
last_error = WSAGetLastError();
|
||||
::closesocket(socket_);
|
||||
socket_ = INVALID_SOCKET;
|
||||
}
|
||||
::freeaddrinfo(addrinfo_result);
|
||||
if (socket_ == INVALID_SOCKET) {
|
||||
WSACleanup();
|
||||
throw_winsock_error_("connect failed", last_error);
|
||||
}
|
||||
if (validated_timeout_ms > 0) {
|
||||
DWORD timeout_dword = static_cast<DWORD>(validated_timeout_ms);
|
||||
::setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout_dword, sizeof(timeout_dword));
|
||||
::setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, (const char *)&timeout_dword, sizeof(timeout_dword));
|
||||
}
|
||||
|
||||
// set TCP_NODELAY
|
||||
int enable_flag = 1;
|
||||
|
||||
@@ -70,9 +70,9 @@ public:
|
||||
// Send exactly n_bytes of the given data.
|
||||
// On error close the connection and throw.
|
||||
void send(const char *data, size_t n_bytes) {
|
||||
ssize_t toslen = 0;
|
||||
socklen_t tolen = sizeof(struct sockaddr);
|
||||
if ((toslen = ::sendto(socket_, data, n_bytes, 0, (struct sockaddr *)&sockAddr_, tolen)) == -1) {
|
||||
socklen_t tolen = sizeof(sockAddr_);
|
||||
if (::sendto(socket_, data, n_bytes, 0, reinterpret_cast<const sockaddr *>(&sockAddr_),
|
||||
tolen) == -1) {
|
||||
throw_spdlog_ex("sendto(2) failed", errno);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ public:
|
||||
|
||||
// return true if the given message should be flushed
|
||||
[[nodiscard]] bool should_flush(const details::log_msg &msg) const noexcept {
|
||||
return (msg.log_level >= flush_level_.load(std::memory_order_relaxed)) && (msg.log_level != level::off);
|
||||
return (msg.log_level >= flush_level()) && (msg.log_level != level::off);
|
||||
}
|
||||
|
||||
// set the level of logging
|
||||
|
||||
@@ -69,8 +69,10 @@ public:
|
||||
static constexpr std::string_view red_bold = "\033[31m\033[1m";
|
||||
static constexpr std::string_view bold_on_red = "\033[1m\033[41m";
|
||||
|
||||
private:
|
||||
protected:
|
||||
FILE *target_file_;
|
||||
|
||||
private:
|
||||
bool should_do_colors_;
|
||||
std::array<std::string, levels_count> colors_;
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ class basic_file_sink final : public base_sink<Mutex> {
|
||||
public:
|
||||
explicit basic_file_sink(const filename_t &filename, bool truncate = false, const file_event_handlers &event_handlers = {});
|
||||
const filename_t &filename() const;
|
||||
void truncate();
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override;
|
||||
|
||||
@@ -55,7 +55,6 @@ struct daily_filename_format_calculator {
|
||||
* Rotating file sink based on date.
|
||||
* If truncate != false , the created file will be truncated.
|
||||
* If max_files > 0, retain only the last max_files and delete previous.
|
||||
* If max_files > 0, retain only the last max_files and delete previous.
|
||||
* Note that old log files from previous executions will not be deleted by this class,
|
||||
* rotation and deletion is only applied while the program is running.
|
||||
*/
|
||||
@@ -81,8 +80,8 @@ public:
|
||||
}
|
||||
|
||||
auto now = log_clock::now();
|
||||
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
|
||||
file_helper_.open(filename, truncate_);
|
||||
const auto new_filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
|
||||
file_helper_.open(new_filename, truncate_);
|
||||
rotation_tp_ = next_rotation_tp_();
|
||||
|
||||
if (max_files_ > 0) {
|
||||
@@ -100,8 +99,8 @@ protected:
|
||||
auto time = msg.time;
|
||||
bool should_rotate = time >= rotation_tp_;
|
||||
if (should_rotate) {
|
||||
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time));
|
||||
file_helper_.open(filename, truncate_);
|
||||
const auto new_filename = FileNameCalc::calc_filename(base_filename_, now_tm(time));
|
||||
file_helper_.open(new_filename, truncate_);
|
||||
rotation_tp_ = next_rotation_tp_();
|
||||
}
|
||||
memory_buf_t formatted;
|
||||
@@ -124,11 +123,11 @@ private:
|
||||
std::vector<filename_t> filenames;
|
||||
auto now = log_clock::now();
|
||||
while (filenames.size() < max_files_) {
|
||||
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
|
||||
if (!path_exists(filename)) {
|
||||
const auto new_filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
|
||||
if (!path_exists(new_filename)) {
|
||||
break;
|
||||
}
|
||||
filenames.emplace_back(filename);
|
||||
filenames.emplace_back(new_filename);
|
||||
now -= std::chrono::hours(24);
|
||||
}
|
||||
for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter) {
|
||||
|
||||
@@ -5,8 +5,10 @@
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "../details/log_msg.h"
|
||||
#include "../details/null_mutex.h"
|
||||
@@ -20,8 +22,8 @@
|
||||
// #include "spdlog/sinks/dup_filter_sink.h"
|
||||
//
|
||||
// int main() {
|
||||
// auto dup_filter = std::make_shared<dup_filter_sink_st>(std::chrono::seconds(5),
|
||||
// level::info); dup_filter->add_sink(std::make_shared<stdout_color_sink_mt>());
|
||||
// auto dup_filter = std::make_shared<dup_filter_sink_st>(std::chrono::seconds(5));
|
||||
// dup_filter->add_sink(std::make_shared<stdout_color_sink_mt>());
|
||||
// spdlog::logger l("logger", dup_filter);
|
||||
// l.info("Hello");
|
||||
// l.info("Hello");
|
||||
@@ -40,21 +42,39 @@ template <typename Mutex>
|
||||
class dup_filter_sink final : public dist_sink<Mutex> {
|
||||
public:
|
||||
template <class Rep, class Period>
|
||||
explicit dup_filter_sink(std::chrono::duration<Rep, Period> max_skip_duration, level notification_level = level::info)
|
||||
explicit dup_filter_sink(std::chrono::duration<Rep, Period> max_skip_duration)
|
||||
: max_skip_duration_{max_skip_duration} {}
|
||||
|
||||
// Optional: force the "Skipped N duplicate..." line to a fixed level.
|
||||
template <class Rep, class Period>
|
||||
explicit dup_filter_sink(std::chrono::duration<Rep, Period> max_skip_duration,
|
||||
level notification_level)
|
||||
: max_skip_duration_{max_skip_duration},
|
||||
log_level_{notification_level} {}
|
||||
use_fixed_notification_summary_level_{true},
|
||||
fixed_notification_summary_level_{notification_level} {}
|
||||
|
||||
template <class Rep, class Period>
|
||||
explicit dup_filter_sink(std::chrono::duration<Rep, Period> max_skip_duration,
|
||||
std::vector<std::shared_ptr<sink>> sinks)
|
||||
: dist_sink<Mutex>(std::move(sinks)),
|
||||
max_skip_duration_{max_skip_duration} {}
|
||||
|
||||
protected:
|
||||
std::chrono::microseconds max_skip_duration_;
|
||||
log_clock::time_point last_msg_time_;
|
||||
std::string last_msg_payload_;
|
||||
size_t skip_counter_ = 0;
|
||||
level log_level_;
|
||||
level skipped_msg_log_level_{level::off};
|
||||
bool use_fixed_notification_summary_level_{false};
|
||||
level fixed_notification_summary_level_{level::info};
|
||||
|
||||
void sink_it_(const details::log_msg &msg) override {
|
||||
bool filtered = filter_(msg);
|
||||
if (!filtered) {
|
||||
skip_counter_ += 1;
|
||||
if (!use_fixed_notification_summary_level_) {
|
||||
skipped_msg_log_level_ = msg.log_level;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -63,7 +83,10 @@ protected:
|
||||
char buf[64];
|
||||
auto msg_size = ::snprintf(buf, sizeof(buf), "Skipped %u duplicate messages..", static_cast<unsigned>(skip_counter_));
|
||||
if (msg_size > 0 && static_cast<size_t>(msg_size) < sizeof(buf)) {
|
||||
details::log_msg skipped_msg{msg.source, msg.logger_name, log_level_,
|
||||
const level summary_level = use_fixed_notification_summary_level_
|
||||
? fixed_notification_summary_level_
|
||||
: skipped_msg_log_level_;
|
||||
details::log_msg skipped_msg{msg.source, msg.logger_name, summary_level,
|
||||
string_view_t{buf, static_cast<size_t>(msg_size)}};
|
||||
dist_sink<Mutex>::sink_it_(skipped_msg);
|
||||
}
|
||||
@@ -77,8 +100,8 @@ protected:
|
||||
}
|
||||
|
||||
// return whether the log msg should be displayed (true) or skipped (false)
|
||||
bool filter_(const details::log_msg &msg) {
|
||||
auto filter_duration = msg.time - last_msg_time_;
|
||||
bool filter_(const details::log_msg &msg) const {
|
||||
const auto filter_duration = msg.time - last_msg_time_;
|
||||
return (filter_duration > max_skip_duration_) || (msg.payload != last_msg_payload_);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -154,8 +154,10 @@ protected:
|
||||
payload = QString::fromUtf8(str.data(), static_cast<int>(str.size()));
|
||||
// convert color ranges from byte index to character index.
|
||||
if (msg.color_range_start < msg.color_range_end) {
|
||||
color_range_start = QString::fromUtf8(str.data(), msg.color_range_start).size();
|
||||
color_range_end = QString::fromUtf8(str.data(), msg.color_range_end).size();
|
||||
color_range_start =
|
||||
QString::fromUtf8(str.data(), static_cast<qsizetype>(msg.color_range_start)).size();
|
||||
color_range_end =
|
||||
QString::fromUtf8(str.data(), static_cast<qsizetype>(msg.color_range_end)).size();
|
||||
}
|
||||
} else {
|
||||
payload = QString::fromLatin1(str.data(), static_cast<int>(str.size()));
|
||||
@@ -165,7 +167,7 @@ protected:
|
||||
qt_text_edit_, // text edit to append to
|
||||
std::move(payload), // text to append
|
||||
default_color_, // default color
|
||||
colors_.at(msg.log_level), // color to apply
|
||||
colors_.at(static_cast<size_t>(msg.log_level)), // color to apply
|
||||
color_range_start, // color range start
|
||||
color_range_end}; // color range end
|
||||
|
||||
|
||||
@@ -24,7 +24,11 @@ template <typename Mutex>
|
||||
class ringbuffer_sink final : public base_sink<Mutex> {
|
||||
public:
|
||||
explicit ringbuffer_sink(size_t n_items)
|
||||
: q_{n_items} {}
|
||||
: q_{n_items} {
|
||||
if (n_items == 0) {
|
||||
throw_spdlog_ex("ringbuffer_sink: n_items cannot be zero");
|
||||
}
|
||||
}
|
||||
|
||||
void drain_raw(std::function<void(const details::async_log_msg &)> callback) {
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
|
||||
@@ -17,8 +17,6 @@
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#pragma once
|
||||
|
||||
// Simple tcp client sink
|
||||
// Connects to remote address and send the formatted log.
|
||||
// Will attempt to reconnect if connection drops.
|
||||
@@ -31,6 +29,7 @@ namespace sinks {
|
||||
struct tcp_sink_config {
|
||||
std::string server_host;
|
||||
int server_port;
|
||||
int timeout_ms = 0; // timeout for connect, send, and recv (milliseconds)
|
||||
bool lazy_connect = false; // if true connect on first log call instead of on construction
|
||||
|
||||
tcp_sink_config(std::string host, int port)
|
||||
@@ -44,10 +43,22 @@ public:
|
||||
// connect to tcp host/port or throw if failed
|
||||
// host can be hostname or ip address
|
||||
|
||||
explicit tcp_sink(const std::string &host,
|
||||
int port,
|
||||
int timeout_ms = 0,
|
||||
bool lazy_connect = false)
|
||||
: config_{host, port} {
|
||||
config_.timeout_ms = timeout_ms;
|
||||
config_.lazy_connect = lazy_connect;
|
||||
if (!config_.lazy_connect) {
|
||||
client_.connect(config_.server_host, config_.server_port, config_.timeout_ms);
|
||||
}
|
||||
}
|
||||
|
||||
explicit tcp_sink(tcp_sink_config sink_config)
|
||||
: config_{std::move(sink_config)} {
|
||||
if (!config_.lazy_connect) {
|
||||
this->client_.connect(config_.server_host, config_.server_port);
|
||||
client_.connect(config_.server_host, config_.server_port, config_.timeout_ms);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +69,7 @@ protected:
|
||||
spdlog::memory_buf_t formatted;
|
||||
spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||
if (!client_.is_connected()) {
|
||||
client_.connect(config_.server_host, config_.server_port);
|
||||
client_.connect(config_.server_host, config_.server_port, config_.timeout_ms);
|
||||
}
|
||||
client_.send(formatted.data(), formatted.size());
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ template <typename Mutex>
|
||||
class udp_sink final : public base_sink<Mutex> {
|
||||
public:
|
||||
// host can be hostname or ip address
|
||||
explicit udp_sink(udp_sink_config sink_config)
|
||||
explicit udp_sink(const udp_sink_config &sink_config)
|
||||
: client_{sink_config.server_host, sink_config.server_port} {}
|
||||
|
||||
~udp_sink() override = default;
|
||||
|
||||
@@ -40,7 +40,7 @@ SPDLOG_API level get_level();
|
||||
SPDLOG_API void set_level(level level);
|
||||
|
||||
// Determine whether the global logger should log messages with a certain level
|
||||
SPDLOG_API bool should_log(level level);
|
||||
SPDLOG_API bool should_log(level log_level);
|
||||
|
||||
// Set flush level of the global logger.
|
||||
SPDLOG_API void flush_on(level level);
|
||||
|
||||
Reference in New Issue
Block a user