2016-04-20 11:57:49 +03:00
|
|
|
/*
|
2023-09-25 16:05:07 +03:00
|
|
|
* This content is released under the MIT License as specified in
|
2023-10-01 12:20:48 +03:00
|
|
|
* https://raw.githubusercontent.com/gabime/spdlog/v2.x/LICENSE
|
2016-04-20 11:57:49 +03:00
|
|
|
*/
|
|
|
|
|
#include "includes.h"
|
2023-09-29 00:45:09 +03:00
|
|
|
#include "spdlog/sinks/basic_file_sink.h"
|
|
|
|
|
#include "spdlog/sinks/rotating_file_sink.h"
|
2016-04-20 11:57:49 +03:00
|
|
|
|
2021-01-06 03:55:46 -05:00
|
|
|
#define SIMPLE_LOG "test_logs/simple_log"
|
|
|
|
|
#define ROTATING_LOG "test_logs/rotating_log"
|
|
|
|
|
|
2025-01-05 02:17:31 +02:00
|
|
|
using namespace spdlog::sinks;
|
|
|
|
|
|
2023-09-25 16:05:07 +03:00
|
|
|
TEST_CASE("simple_file_logger", "[simple_logger]") {
|
2016-04-20 11:57:49 +03:00
|
|
|
prepare_logdir();
|
2021-01-06 03:55:46 -05:00
|
|
|
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
|
2025-01-05 02:17:31 +02:00
|
|
|
auto logger = spdlog::create<basic_file_sink_mt>("test-error", filename);
|
2016-04-20 11:57:49 +03:00
|
|
|
logger->set_pattern("%v");
|
|
|
|
|
logger->info("Test message {}", 1);
|
|
|
|
|
logger->info("Test message {}", 2);
|
|
|
|
|
logger->flush();
|
2021-01-06 03:55:46 -05:00
|
|
|
require_message_count(SIMPLE_LOG, 2);
|
2020-02-04 22:23:36 +01:00
|
|
|
using spdlog::details::os::default_eol;
|
2024-01-13 18:20:08 +02:00
|
|
|
REQUIRE(file_contents(SIMPLE_LOG) == spdlog::fmt_lib::format("Test message 1{}Test message 2{}", default_eol, default_eol));
|
2016-09-18 00:43:42 +03:00
|
|
|
}
|
|
|
|
|
|
2023-09-25 16:05:07 +03:00
|
|
|
TEST_CASE("flush_on", "[flush_on]") {
|
2016-10-12 23:08:44 +03:00
|
|
|
prepare_logdir();
|
2021-01-06 03:55:46 -05:00
|
|
|
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
|
2025-01-05 02:17:31 +02:00
|
|
|
auto logger = spdlog::create<basic_file_sink_mt>("test-error", filename);
|
2016-10-12 23:08:44 +03:00
|
|
|
logger->set_pattern("%v");
|
2023-09-23 17:59:51 +03:00
|
|
|
logger->set_level(spdlog::level::trace);
|
|
|
|
|
logger->flush_on(spdlog::level::info);
|
2016-10-12 23:08:44 +03:00
|
|
|
logger->trace("Should not be flushed");
|
2021-01-06 03:55:46 -05:00
|
|
|
REQUIRE(count_lines(SIMPLE_LOG) == 0);
|
2016-10-12 23:08:44 +03:00
|
|
|
logger->info("Test message {}", 1);
|
|
|
|
|
logger->info("Test message {}", 2);
|
2021-01-06 03:55:46 -05:00
|
|
|
require_message_count(SIMPLE_LOG, 3);
|
2020-02-04 22:23:36 +01:00
|
|
|
using spdlog::details::os::default_eol;
|
2024-01-13 18:20:08 +02:00
|
|
|
REQUIRE(file_contents(SIMPLE_LOG) == spdlog::fmt_lib::format("Should not be flushed{}Test message 1{}Test message 2{}",
|
|
|
|
|
default_eol, default_eol, default_eol));
|
2016-04-20 11:57:49 +03: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 v1 09a674b7)
- 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 v1 b656d1ce)
- 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 (v1 45b67eee); 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 for d5af52d9 in 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: PORTED ad725d34 getenv, 677a2d93 stopwatch, 3f7e5028, a6215527;
SUPERSEDED a45c9390, 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: 847db337 PORTED; 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; SUPERSEDED e3f5a4fe e655dbb6; N/A ae1de0dc 548b2642
- Counts 29/28/116/72; merge-report + commits-ported
Made-with: Cursor
* parity: qt_sinks sign casts (#3487); triage 9c582574 superseded
- 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
- SUPERSEDED 10320184 (ScopedPadder / %D already in v2)
- Triage 31/30/68; merge-report + commits-ported
Made-with: Cursor
* triage: supersede 5931a3d6 ba508057 47b7e7c7 (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: PORTED 1774e700 309204d5 f2a9dec0 472945ba; N/A d299603e 57505989;
SUPERSEDED 1ef8d3ce 8cfd4a7e; fix ba508057 row
Made-with: Cursor
* parity: udp_sink const udp_sink_config& (#3520); triage fc7e9c87 1685e694
- 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); triage 2670f47d d276069a 951c5b99
- 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); triage a2b42620 f355b3d5 d276069a
- 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: b7e0e2c2 71925ca3 fa6605dc 885b5473 96c9a62b 1e6250e1 d7155530
Made-with: Cursor
* parity: test_sink/callback iterator cast (#3315); triage Catch2 #3038
- difference_type cast for formatted.end() - eol_len (ad0f31c0)
- SUPERSEDED: c1569a3d Catch2 v3.5.0, 73e2e02b wstr_to_utf8buf bounds
Made-with: Cursor
* parity: SPDLOG_WCHAR_CONSOLE WriteConsoleW path (#3092); triage b6da5944
- 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: fe79bfcc 6725584e async tests 16e0d2e7 63d18842 d8e0ad46 1e7d7e07 3c23c27d
- SUPERSEDED: faa0a7a9 85bdab0c 276ee5f5 7f8060d5 96a8f625
- 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 — add 9fe79692 to ports table; fix 6004e3d1 paths
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>
2026-04-08 16:17:37 +02:00
|
|
|
TEST_CASE("basic_file_sink_truncate", "[truncate]") {
|
|
|
|
|
prepare_logdir();
|
|
|
|
|
const spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
|
|
|
|
|
const bool truncate = true;
|
|
|
|
|
const auto sink = std::make_shared<basic_file_sink_mt>(filename, truncate);
|
|
|
|
|
const auto logger = std::make_shared<spdlog::logger>("simple_file_logger", sink);
|
|
|
|
|
|
|
|
|
|
logger->info("Test message {}", 3.14);
|
|
|
|
|
logger->info("Test message {}", 2.71);
|
|
|
|
|
logger->flush();
|
|
|
|
|
REQUIRE(count_lines(SIMPLE_LOG) == 2);
|
|
|
|
|
|
|
|
|
|
sink->truncate();
|
|
|
|
|
REQUIRE(count_lines(SIMPLE_LOG) == 0);
|
|
|
|
|
|
|
|
|
|
logger->info("Test message {}", 6.28);
|
|
|
|
|
logger->flush();
|
|
|
|
|
REQUIRE(count_lines(SIMPLE_LOG) == 1);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-25 16:05:07 +03:00
|
|
|
TEST_CASE("rotating_file_logger1", "[rotating_logger]") {
|
2016-04-20 11:57:49 +03:00
|
|
|
prepare_logdir();
|
2018-05-26 18:48:39 +03:00
|
|
|
size_t max_size = 1024 * 10;
|
2021-01-06 03:55:46 -05:00
|
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
2025-01-05 02:17:31 +02:00
|
|
|
auto logger = spdlog::create<rotating_file_sink_mt>("logger", basename, max_size, 0);
|
2023-09-25 16:05:07 +03:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2016-04-20 11:57:49 +03:00
|
|
|
logger->info("Test message {}", i);
|
2017-10-05 08:20:21 -03:00
|
|
|
}
|
2016-04-20 11:57:49 +03:00
|
|
|
|
2016-10-12 23:08:44 +03:00
|
|
|
logger->flush();
|
2021-01-06 03:55:46 -05:00
|
|
|
require_message_count(ROTATING_LOG, 10);
|
2016-04-20 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
|
2023-09-25 16:05:07 +03:00
|
|
|
TEST_CASE("rotating_file_logger2", "[rotating_logger]") {
|
2016-04-20 11:57:49 +03:00
|
|
|
prepare_logdir();
|
2018-05-26 18:48:39 +03:00
|
|
|
size_t max_size = 1024 * 10;
|
2021-01-06 03:55:46 -05:00
|
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
2019-01-24 00:06:15 -05:00
|
|
|
{
|
|
|
|
|
// make an initial logger to create the first output file
|
2025-01-05 02:17:31 +02:00
|
|
|
auto logger = spdlog::create<rotating_file_sink_mt>("logger", basename, max_size, 2, true);
|
2023-09-25 16:05:07 +03:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2019-01-24 00:06:15 -05:00
|
|
|
logger->info("Test message {}", i);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-05 02:17:31 +02:00
|
|
|
auto logger = spdlog::create<rotating_file_sink_mt>("logger", basename, max_size, 2, true);
|
2023-09-25 16:05:07 +03:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2016-04-20 11:57:49 +03:00
|
|
|
logger->info("Test message {}", i);
|
2018-11-22 21:35:11 +02:00
|
|
|
}
|
2016-04-20 11:57:49 +03:00
|
|
|
|
|
|
|
|
logger->flush();
|
2020-04-08 18:17:21 +03:00
|
|
|
|
2021-01-06 03:55:46 -05:00
|
|
|
require_message_count(ROTATING_LOG, 10);
|
2020-02-04 22:23:36 +01:00
|
|
|
|
2023-09-25 16:05:07 +03:00
|
|
|
for (int i = 0; i < 1000; i++) {
|
2016-04-20 11:57:49 +03:00
|
|
|
logger->info("Test message {}", i);
|
2017-10-05 08:20:21 -03:00
|
|
|
}
|
2016-04-20 11:57:49 +03:00
|
|
|
|
|
|
|
|
logger->flush();
|
2021-01-06 03:55:46 -05:00
|
|
|
REQUIRE(get_filesize(ROTATING_LOG) <= max_size);
|
|
|
|
|
REQUIRE(get_filesize(ROTATING_LOG ".1") <= max_size);
|
2016-04-20 11:57:49 +03:00
|
|
|
}
|
2022-02-05 17:37:55 +02:00
|
|
|
|
|
|
|
|
// test that passing max_size=0 throws
|
2023-09-25 16:05:07 +03:00
|
|
|
TEST_CASE("rotating_file_logger3", "[rotating_logger]") {
|
2022-02-05 17:37:55 +02:00
|
|
|
prepare_logdir();
|
|
|
|
|
size_t max_size = 0;
|
|
|
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
2025-01-05 02:17:31 +02:00
|
|
|
REQUIRE_THROWS_AS(spdlog::create<rotating_file_sink_mt>("logger", basename, max_size, 0), spdlog::spdlog_ex);
|
2022-02-05 17:37:55 +02:00
|
|
|
}
|
2024-11-28 17:37:29 +02:00
|
|
|
|
|
|
|
|
// test on-demand rotation of logs
|
|
|
|
|
TEST_CASE("rotating_file_logger4", "[rotating_logger]") {
|
|
|
|
|
prepare_logdir();
|
|
|
|
|
size_t max_size = 1024 * 10;
|
|
|
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
|
|
|
|
auto sink = std::make_shared<spdlog::sinks::rotating_file_sink_st>(basename, max_size, 2);
|
|
|
|
|
auto logger = std::make_shared<spdlog::logger>("rotating_sink_logger", sink);
|
|
|
|
|
logger->info("Test message - pre-rotation");
|
|
|
|
|
logger->flush();
|
|
|
|
|
sink->rotate_now();
|
|
|
|
|
logger->info("Test message - post-rotation");
|
|
|
|
|
logger->flush();
|
|
|
|
|
REQUIRE(get_filesize(ROTATING_LOG) > 0);
|
|
|
|
|
REQUIRE(get_filesize(ROTATING_LOG ".1") > 0);
|
|
|
|
|
}
|