Compare commits

..

20 Commits

Author SHA1 Message Date
gabime
ce0424bb37 Bump fmt to 11.1.4 2025-04-13 12:46:01 +03:00
Gabi Melman
1f9272eb7d Update logger.h 2025-04-11 14:45:11 +03:00
gabime
22122f3901 Merge PR #3366 from 1.x (Fix zformatter on Apple and POSIX.1-2024 conforming platform) 2025-03-29 13:58:40 +03:00
Jan Moravec
1b0a1dda33 Update CMake minimum version required (#3359)
FILE_SET functionality was introduced in CMake version 3.23.

https://cmake.org/cmake/help/latest/command/target_sources.html
2025-03-17 16:15:12 +02:00
gabime
88715d29e9 cherrypick pr #3661 2025-03-17 16:07:31 +02:00
gabime
22405cf9ae Fixed async bench 2025-02-01 19:23:23 +02:00
gabime
27d8580131 Bump fmt to version 11.1.3 2025-02-01 19:06:19 +02:00
gabime
fbffd38030 CMakeLists.txt format 2025-02-01 19:03:41 +02:00
gabime
3a54caee36 CMakeLists.txt format 2025-02-01 19:02:52 +02:00
gabime
9db0ba648a Refactor SPDLOG_API define 2025-01-18 20:32:34 +02:00
gabime
463e41f049 Refactor exporing in common.h 2025-01-18 19:45:13 +02:00
gabime
ace82f7da6 Try fix windows link error with visiblity 2025-01-18 19:17:45 +02:00
gabime
b93c0f8e8d Update comment 2025-01-18 16:37:18 +02:00
gabime
aec733b7a9 change async queue limits to constexpr 2025-01-18 16:29:14 +02:00
gabime
eb660caa6c Limit max async queue size to 250,000 entries 2025-01-18 16:26:43 +02:00
gabime
af8440b248 Small clean of enqueue_if_have_room in mpmc_blocking_q.h 2025-01-18 16:04:31 +02:00
gabime
214e26e8b2 Make err_helper time printing more robust 2025-01-18 14:09:19 +02:00
gabime
35060923d9 improved err_helper message while catching exceptions 2025-01-18 13:42:55 +02:00
gabime
a8e7527d2d Merge branch 'v2.x' of github.com:gabime/spdlog into v2.x 2025-01-18 13:41:00 +02:00
Gabi Melman
2abfa1628b Gabime/visibilty-hidden 2.x (#3324)
Set CMAKE_CXX_VISIBILITY_PRESET and VISIBILITY_INLINES_HIDDEN when build shared lib
2025-01-18 13:36:01 +02:00
13 changed files with 138 additions and 143 deletions

View File

@@ -1,45 +1,34 @@
# Copyright(c) 2019 spdlog authors Distributed under the MIT License (http://opensource.org/licenses/MIT)
cmake_minimum_required(VERSION 3.14)
# ---------------------------------------------------------------------------------------
# Start spdlog project
# Copyright(c) 2019-present by spdlog authors.
# Distributed under the MIT License (http://opensource.org/licenses/MIT)
# ---------------------------------------------------------------------------------------
cmake_minimum_required(VERSION 3.23)
include(cmake/utils.cmake)
include(cmake/ide.cmake)
spdlog_extract_version()
project(spdlog VERSION ${SPDLOG_VERSION} LANGUAGES CXX)
include(GNUInstallDirs)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# ---------------------------------------------------------------------------------------
# Set default build to release
# ---------------------------------------------------------------------------------------
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
endif ()
# ---------------------------------------------------------------------------------------
# Compiler config
# ---------------------------------------------------------------------------------------
# c++ standard >=17 is required
# C++ standard >=17 is required
if (NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
elseif (CMAKE_CXX_STANDARD LESS 17)
message(FATAL_ERROR "Minimum supported CMAKE_CXX_STANDARD is 17, but it is set to ${CMAKE_CXX_STANDARD}")
endif ()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if (CMAKE_SYSTEM_NAME MATCHES "CYGWIN" OR CMAKE_SYSTEM_NAME MATCHES "MSYS" OR CMAKE_SYSTEM_NAME MATCHES "MINGW")
set(CMAKE_CXX_EXTENSIONS ON)
endif ()
# ---------------------------------------------------------------------------------------
# Set SPDLOG_MASTER_PROJECT to ON if we are building spdlog
# ---------------------------------------------------------------------------------------
@@ -51,75 +40,53 @@ if (NOT DEFINED SPDLOG_MASTER_PROJECT)
set(SPDLOG_MASTER_PROJECT OFF)
endif ()
endif ()
# ---------------------------------------------------------------------------------------
# Options
# ---------------------------------------------------------------------------------------
option(SPDLOG_BUILD_ALL "Build all artifacts" OFF)
# build shared option
option(SPDLOG_BUILD_SHARED "Build shared library" OFF)
# example options
option(SPDLOG_BUILD_EXAMPLE "Build example" ${SPDLOG_MASTER_PROJECT})
# testing options
option(SPDLOG_BUILD_TESTS "Build tests" OFF)
# bench options
option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/benchmark.git to be installed)" OFF)
# sanitizer options
option(SPDLOG_SANITIZE_ADDRESS "Enable address sanitizer in tests" OFF)
# warning options
option(SPDLOG_BUILD_WARNINGS "Enable compiler warnings" OFF)
# install options
option(SPDLOG_SYSTEM_INCLUDES "Include as system headers (skip for clang-tidy)." OFF)
option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT})
option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of of fetching from gitub." OFF)
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
option(SPDLOG_CLOCK_COARSE "Use CLOCK_REALTIME_COARSE instead of the regular clock," OFF)
else ()
set(SPDLOG_CLOCK_COARSE OFF CACHE BOOL "non supported option" FORCE)
endif ()
option(SPDLOG_PREVENT_CHILD_FD "Prevent from child processes to inherit log file descriptors" OFF)
option(SPDLOG_NO_THREAD_ID "prevent spdlog from querying the thread id on each log call if thread id is not needed" OFF)
option(SPDLOG_DISABLE_GLOBAL_LOGGER "Disable global logger creation" OFF)
option(SPDLOG_NO_TLS "Disable thread local storage" OFF)
# clang-tidy
option(SPDLOG_TIDY "run clang-tidy" OFF)
if (SPDLOG_TIDY)
set(CMAKE_CXX_CLANG_TIDY "clang-tidy")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
message(STATUS "Enabled clang-tidy")
endif ()
if (SPDLOG_BUILD_SHARED)
set(BUILD_SHARED_LIBS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif ()
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# place dlls and libs and executables in the same directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
# make sure __cplusplus is defined
add_compile_options(/Zc:__cplusplus)
# enable parallel build for the solution
add_compile_options(/MP)
endif ()
message(STATUS "spdlog version: ${SPDLOG_VERSION}")
message(STATUS "spdlog build type: " ${CMAKE_BUILD_TYPE})
message(STATUS "spdlog build shared: " ${BUILD_SHARED_LIBS})
message(STATUS "spdlog fmt external: " ${SPDLOG_FMT_EXTERNAL})
# ---------------------------------------------------------------------------------------
# Find {fmt} library
# ---------------------------------------------------------------------------------------
@@ -129,12 +96,10 @@ if (SPDLOG_FMT_EXTERNAL)
else ()
include(cmake/fmtlib.cmake)
endif ()
# ---------------------------------------------------------------------------------------
# Threads library is required
# ---------------------------------------------------------------------------------------
find_package(Threads REQUIRED)
# ---------------------------------------------------------------------------------------
# Library sources
# ---------------------------------------------------------------------------------------
@@ -182,7 +147,6 @@ set(SPDLOG_HEADERS
"include/spdlog/sinks/tcp_sink.h"
"include/spdlog/sinks/udp_sink.h"
"include/spdlog/sinks/async_sink.h")
set(SPDLOG_SRCS
"src/common.cpp"
"src/logger.cpp"
@@ -198,7 +162,6 @@ set(SPDLOG_SRCS
"src/sinks/rotating_file_sink.cpp"
"src/sinks/stdout_sinks.cpp"
"src/sinks/async_sink.cpp")
if (WIN32)
list(APPEND SPDLOG_SRCS
"src/details/os_windows.cpp"
@@ -218,7 +181,6 @@ else ()
"include/spdlog/details/udp_client_unix.h"
"include/spdlog/sinks/ansicolor_sink.h")
endif ()
# ---------------------------------------------------------------------------------------
# Check if fwrite_unlocked/_fwrite_nolock is available
# ---------------------------------------------------------------------------------------
@@ -231,7 +193,6 @@ endif ()
if (HAVE_FWRITE_UNLOCKED)
set(SPDLOG_FWRITE_UNLOCKED 1)
endif ()
# ---------------------------------------------------------------------------------------
# spdlog library
# ---------------------------------------------------------------------------------------
@@ -242,18 +203,14 @@ if (BUILD_SHARED_LIBS)
endif ()
add_library(spdlog SHARED ${VERSION_RC})
target_compile_definitions(spdlog PUBLIC SPDLOG_SHARED_LIB)
set_target_properties(spdlog PROPERTIES
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON)
if (MSVC)
# disable dlls related warnings on msvc
target_compile_options(spdlog PUBLIC $<$<AND:$<CXX_COMPILER_ID:MSVC>,$<NOT:$<COMPILE_LANGUAGE:CUDA>>>:/wd4251
/wd4275>)
target_compile_options(spdlog PUBLIC $<$<AND:$<CXX_COMPILER_ID:MSVC>,$<NOT:$<COMPILE_LANGUAGE:CUDA>>>:/wd4251 /wd4275>)
endif ()
else ()
add_library(spdlog STATIC)
endif ()
set_target_properties(spdlog PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN ON)
add_library(spdlog::spdlog ALIAS spdlog)
target_sources(spdlog PRIVATE ${SPDLOG_SRCS})
target_sources(
@@ -262,42 +219,35 @@ target_sources(
TYPE HEADERS
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include
FILES ${SPDLOG_HEADERS})
set(SPDLOG_INCLUDES_LEVEL "")
if (SPDLOG_SYSTEM_INCLUDES)
set(SPDLOG_INCLUDES_LEVEL "SYSTEM")
endif ()
target_include_directories(spdlog ${SPDLOG_INCLUDES_LEVEL} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
target_link_libraries(spdlog PUBLIC Threads::Threads)
target_link_libraries(spdlog PUBLIC fmt::fmt)
spdlog_enable_warnings(spdlog)
set_target_properties(spdlog PROPERTIES VERSION ${SPDLOG_VERSION} SOVERSION
${SPDLOG_VERSION_MAJOR}.${SPDLOG_VERSION_MINOR})
set(SPDLOG_NAME spdlog-${SPDLOG_VERSION_MAJOR})
set_target_properties(spdlog PROPERTIES DEBUG_POSTFIX "-${SPDLOG_VERSION_MAJOR}.${SPDLOG_VERSION_MINOR}d")
# ---------------------------------------------------------------------------------------
# set source groups for visual studio
# Set prefix and source group for visual studio
# ---------------------------------------------------------------------------------------
if (CMAKE_GENERATOR MATCHES "Visual Studio")
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/include PREFIX include FILES ${SPDLOG_HEADERS})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/src PREFIX sources FILES ${SPDLOG_SRCS})
source_group(sources FILES ${VERSION_RC})
endif ()
# ---------------------------------------------------------------------------------------
# Add required libraries for Android CMake build
# Android support
# ---------------------------------------------------------------------------------------
if (ANDROID)
target_link_libraries(spdlog PUBLIC log)
endif ()
# ---------------------------------------------------------------------------------------
# spdlog private defines according to the options
# Private defines according to the options
# ---------------------------------------------------------------------------------------
foreach (SPDLOG_OPTION
SPDLOG_CLOCK_COARSE
@@ -310,42 +260,31 @@ foreach (SPDLOG_OPTION
target_compile_definitions(spdlog PRIVATE ${SPDLOG_OPTION})
endif ()
endforeach ()
# ---------------------------------------------------------------------------------------
# Build binaries
# ---------------------------------------------------------------------------------------
# examples
if (SPDLOG_BUILD_EXAMPLE OR SPDLOG_BUILD_ALL)
message(STATUS "Generating example(s)")
add_subdirectory(example)
spdlog_enable_warnings(example)
endif ()
# tests
if (SPDLOG_BUILD_TESTS OR SPDLOG_BUILD_ALL)
message(STATUS "Generating tests")
enable_testing()
add_subdirectory(tests)
endif ()
# benchmarks
if (SPDLOG_BUILD_BENCH OR SPDLOG_BUILD_ALL)
message(STATUS "Generating benchmarks")
add_subdirectory(bench)
endif ()
# ---------------------------------------------------------------------------------------
# Install
# ---------------------------------------------------------------------------------------
if (SPDLOG_INSTALL)
message(STATUS "Generating install")
set(project_config_in "${CMAKE_CURRENT_LIST_DIR}/cmake/spdlogConfig.cmake.in")
set(project_config_out "${CMAKE_CURRENT_BINARY_DIR}/spdlogConfig.cmake")
set(config_targets_file "spdlogConfigTargets.cmake")
set(version_config_file "${CMAKE_CURRENT_BINARY_DIR}/spdlogConfigVersion.cmake")
set(export_dest_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${SPDLOG_NAME}")
# ---------------------------------------------------------------------------------------
# Include files
# ---------------------------------------------------------------------------------------
set(installed_include_dir "${CMAKE_INSTALL_INCLUDEDIR}/${SPDLOG_NAME}")
install(
TARGETS spdlog
EXPORT spdlogTargets
@@ -355,20 +294,17 @@ if (SPDLOG_INSTALL)
FILE_SET pub_headers
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${SPDLOG_NAME}")
message(STATUS "Installing spdlog in ${CMAKE_INSTALL_LIBDIR}/${SPDLOG_NAME}")
# ---------------------------------------------------------------------------------------
# Install CMake config files
# ---------------------------------------------------------------------------------------
# Install CMake spdlogConfig.cmake, spdlogConfigVersion.cmake and spdlogTargets.cmake
set(project_config_in "${CMAKE_CURRENT_LIST_DIR}/cmake/spdlogConfig.cmake.in")
set(project_config_out "${CMAKE_CURRENT_BINARY_DIR}/spdlogConfig.cmake")
set(config_targets_file "spdlogConfigTargets.cmake")
set(version_config_file "${CMAKE_CURRENT_BINARY_DIR}/spdlogConfigVersion.cmake")
set(export_dest_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${SPDLOG_NAME}")
install(EXPORT spdlogTargets DESTINATION ${export_dest_dir} NAMESPACE spdlog:: FILE ${config_targets_file})
include(CMakePackageConfigHelpers)
configure_package_config_file("${project_config_in}" "${project_config_out}" INSTALL_DESTINATION ${export_dest_dir})
write_basic_package_version_file("${version_config_file}" COMPATIBILITY SameMajorVersion)
install(FILES "${project_config_out}" "${version_config_file}" DESTINATION "${export_dest_dir}")
# ---------------------------------------------------------------------------------------
# Support creation of installable packages
# ---------------------------------------------------------------------------------------
# CPack
include(cmake/spdlogCPack.cmake)
endif ()

View File

@@ -21,8 +21,7 @@
using namespace std;
using namespace std::chrono;
using namespace spdlog;
using namespace spdlog::sinks;
using spdlog::sinks::async_sink;
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);
@@ -50,8 +49,8 @@ using namespace spdlog::sinks;
int main(int argc, char *argv[]) {
// setlocale to show thousands separators
std::locale::global(std::locale("en_US.UTF-8"));
int howmany = 1000000;
int queue_size = std::min(howmany + 2, 8192);
int howmany = 1'000'000;
int queue_size = async_sink::default_queue_size;
int threads = 10;
int iters = 3;
@@ -66,10 +65,6 @@ int main(int argc, char *argv[]) {
if (argc > 2) threads = atoi(argv[2]);
if (argc > 3) {
queue_size = atoi(argv[3]);
if (queue_size > 500000) {
spdlog::error("Max queue size allowed: 500,000");
exit(1);
}
}
if (argc > 4) iters = atoi(argv[4]);
@@ -79,7 +74,14 @@ int main(int argc, char *argv[]) {
exit(1);
}
auto slot_size = sizeof(details::async_log_msg);
constexpr int max_q_size = async_sink::max_queue_size;
if(queue_size > max_q_size)
{
spdlog::error("Queue size too large. Max queue size is {:L}", max_q_size);
exit(1);
}
auto slot_size = sizeof(spdlog::details::async_log_msg);
spdlog::info("-------------------------------------------------");
spdlog::info("Messages : {:L}", howmany);
spdlog::info("Threads : {:L}", threads);
@@ -99,8 +101,8 @@ int main(int argc, char *argv[]) {
auto cfg = async_sink::config();
cfg.queue_size = queue_size;
cfg.sinks.push_back(std::move(file_sink));
auto async_sink = std::make_shared<sinks::async_sink>(cfg);
auto logger = std::make_shared<spdlog::logger>("async_logger", std::move(async_sink));
auto sink = std::make_shared<async_sink>(cfg);
auto logger = std::make_shared<spdlog::logger>("async_logger", std::move(sink));
bench_mt(howmany, std::move(logger), threads);
}
// verify_file(filename, howmany); // in separate scope to ensure logger is destroyed and all logs were written
@@ -117,8 +119,8 @@ int main(int argc, char *argv[]) {
cfg.queue_size = queue_size;
auto file_sink = std::make_shared<basic_file_sink_mt>(filename, true);
cfg.sinks.push_back(std::move(file_sink));
auto async_sink = std::make_shared<sinks::async_sink>(cfg);
auto logger = std::make_shared<spdlog::logger>("async_logger", std::move(async_sink));
auto sink = std::make_shared<async_sink>(cfg);
auto logger = std::make_shared<spdlog::logger>("async_logger", std::move(sink));
bench_mt(howmany, std::move(logger), threads);
}
spdlog::shutdown();

View File

@@ -153,8 +153,8 @@ int main(int argc, char *argv[]) {
}
using spdlog::sinks::async_sink;
async_sink::config config;
config.queue_size = 3 * 1024 * 1024;
;
config.queue_size = async_sink::default_queue_size;;
config.sinks.push_back(std::make_shared<null_sink_st>());
config.policy = async_sink::overflow_policy::overrun_oldest;
auto async_logger = std::make_shared<spdlog::logger>("async_logger", std::make_shared<async_sink>(config));

View File

@@ -3,8 +3,8 @@ include(FetchContent)
FetchContent_Declare(
fmt
DOWNLOAD_EXTRACT_TIMESTAMP FALSE
URL https://github.com/fmtlib/fmt/archive/refs/tags/11.1.2.tar.gz
URL_HASH SHA256=d8773cf062cc806d4dd4df658111f15ba7a2c9c65db5084d2491696828b1eb97)
URL https://github.com/fmtlib/fmt/archive/refs/tags/11.1.4.tar.gz
URL_HASH SHA256=ac366b7b4c2e9f0dde63a59b3feb5ee59b67974b14ee5dc9ea8ad78aa2c1ee1e)
FetchContent_GetProperties(fmt)
if(NOT fmt_POPULATED)

View File

@@ -18,19 +18,21 @@
#include "fmt/base.h"
#include "fmt/xchar.h"
#if defined(SPDLOG_SHARED_LIB)
#if defined(_WIN32)
// Define SPDLOG_API according to current build settings
#ifndef SPDLOG_SHARED_LIB
#define SPDLOG_API
#elif defined(_WIN32)
#ifdef spdlog_EXPORTS
#define SPDLOG_API __declspec(dllexport)
#else // !spdlog_EXPORTS
#define SPDLOG_API __declspec(dllimport)
#define SPDLOG_API __declspec(dllexport) // Export symbols when building the library
#else
#define SPDLOG_API __declspec(dllimport) // Import symbols when using the library
#endif
#else // !defined(_WIN32)
#define SPDLOG_API __attribute__((visibility("default")))
#endif
#else // !defined(SPDLOG_SHARED_LIB)
#elif (defined(__GNUC__) || defined(__clang__))
#define SPDLOG_API __attribute__((visibility("default"))) // Export symbols for shared libraries
#else
#define SPDLOG_API
#endif
// End of SPDLOG_API definition
#define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string)
#define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string)

View File

@@ -47,20 +47,15 @@ public:
}
void enqueue_if_have_room(T &&item) {
bool pushed = false;
{
std::unique_lock<std::mutex> lock(queue_mutex_);
if (!q_.full()) {
q_.push_back(std::move(item));
pushed = true;
}
}
if (pushed) {
push_cv_.notify_one();
} else {
std::unique_lock lock(queue_mutex_);
if (q_.full()) {
++discard_counter_;
return;
}
q_.push_back(std::move(item));
}
push_cv_.notify_one();
}
// dequeue with a timeout.

View File

@@ -200,7 +200,6 @@ private:
}
}
}
if (should_flush(msg)) {
flush_();
}

View File

@@ -31,7 +31,8 @@ public:
discard_new // Discard the log message if the queue is full
};
enum { default_queue_size = 8192, max_queue_size = 10 * 1024 * 1024 };
static constexpr size_t default_queue_size = 8192;
static constexpr size_t max_queue_size = 250'000;
struct config {
size_t queue_size = default_queue_size;
@@ -54,13 +55,12 @@ public:
void set_pattern(const std::string &pattern) override;
void set_formatter(std::unique_ptr<formatter> sink_formatter) override;
// enqueue flush request to the worker thread and return immediately(default)
// if you need to wait for the actual flush to finish, call wait_for_all() after flush()
// if you need to wait for the actual flush to finish, call wait_all() after flush() or destruct the sink
void flush() override;
// async_sink specific methods
// non sink interface methods
// wait until all logs were processed up to timeout milliseconds.
// returns true if all messages were processed, false if timeout was reached
// wait until all logs were processed up to timeout millis and return false if timeout was reached
[[nodiscard]] bool wait_all(std::chrono::milliseconds timeout) const;
// wait until all logs were processed

View File

@@ -35,8 +35,10 @@ void err_helper::handle_ex(const std::string &origin, const source_loc &loc, con
}
last_report_time_ = now;
const auto tm_time = os::localtime();
char date_buf[32];
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
char date_buf[64];
if (std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time) == 0) {
std::snprintf(date_buf, sizeof(date_buf), "unknown time");
}
std::string msg;
if (loc.empty()) {
msg = fmt_lib::format("[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, origin, ex.what());
@@ -46,10 +48,10 @@ void err_helper::handle_ex(const std::string &origin, const source_loc &loc, con
}
std::fputs(msg.c_str(), stderr);
} catch (const std::exception &handler_ex) {
std::fprintf(stderr, "[*** LOG ERROR ***] [%s] caught exception during error handler: %s\n", origin.c_str(),
std::fprintf(stderr, "[*** LOG ERROR ***] [%s] exception during %s handler: %s\n", origin.c_str(), custom_err_handler_ ? "custom" : "default",
handler_ex.what());
} catch (...) { // catch all exceptions
std::fprintf(stderr, "[*** LOG ERROR ***] [%s] caught unknown exception during error handler\n", origin.c_str());
std::fprintf(stderr, "[*** LOG ERROR ***] [%s] unknown exception during %s handler\n", origin.c_str(), custom_err_handler_ ? "custom" : "default");
}
}

View File

@@ -129,8 +129,10 @@ size_t filesize(FILE *f) {
// Return utc offset in minutes or throw spdlog_ex on failure
int utc_minutes_offset(const std::tm &tm) {
#if defined(sun) || defined(__sun) || defined(_AIX) || (defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
(!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE))
#if defined(sun) || defined(__sun) || defined(_AIX) || \
(defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
(!defined(__APPLE__) && !defined(_BSD_SOURCE) && !defined(_GNU_SOURCE) && \
(!defined(_POSIX_VERSION) || (_POSIX_VERSION < 202405L)))
// 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
struct helper {
static long int calculate_gmt_offset(const std::tm &localtm = details::os::localtime(),

View File

@@ -60,6 +60,9 @@ public:
pad_it(remaining_pad_);
} else if (padinfo_.truncate_) {
long new_size = static_cast<long>(dest_.size()) + remaining_pad_;
if (new_size < 0) {
new_size = 0;
}
dest_.resize(static_cast<size_t>(new_size));
}
}
@@ -247,7 +250,7 @@ public:
: flag_formatter(padinfo) {}
void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override {
constexpr size_t field_size = 10;
constexpr size_t field_size = 8;
ScopedPadder p(field_size, padinfo_, dest);
fmt_helper::pad2(tm_time.tm_mon + 1, dest);

View File

@@ -21,7 +21,7 @@ struct custom_ex {};
using namespace spdlog::sinks;
TEST_CASE("default_error_handler", "[errors]") {
prepare_logdir();
auto logger = spdlog::create<basic_file_sink_mt>("test-error", log_filename);
auto logger = spdlog::create<basic_file_sink_mt>("test-bad-format", log_filename);
logger->set_pattern("%v");
logger->info(SPDLOG_FMT_RUNTIME("Test message {} {}"), 1);
logger->info("Test message {}", 2);
@@ -42,8 +42,36 @@ TEST_CASE("custom_error_handler", "[errors]") {
require_message_count(log_filename, 2);
}
TEST_CASE("default_error_handler2", "[errors]") {
auto logger = std::make_shared<spdlog::logger>("test-failing-sink", std::make_shared<failing_sink>());
TEST_CASE("throwing_sink", "[errors]") {
auto logger = std::make_shared<spdlog::logger>("test-throwing-sink", std::make_shared<failing_sink>());
REQUIRE_NOTHROW(logger->info("Some message"));
}
TEST_CASE("throwing_flush", "[errors]") {
auto logger = spdlog::create<failing_sink>("test-throwing-sink");
REQUIRE_NOTHROW(logger->flush());
}
TEST_CASE("throwing_error_handler", "[errors]") {
auto logger = std::make_shared<spdlog::logger>("test-throwing-error-handler", std::make_shared<failing_sink>());
logger->set_error_handler([=](const std::string &msg) {
REQUIRE(msg == log_err_msg);
throw std::runtime_error("test throw");
});
REQUIRE_NOTHROW(logger->info("Some message"));
}
TEST_CASE("throwing_flush_error_handler", "[errors]") {
auto logger = spdlog::create<failing_sink>("test-throwing-error-handler");
logger->set_error_handler([=](const std::string &msg) {
REQUIRE(msg == flush_err_msg);
throw std::runtime_error("test throw");
});
REQUIRE_NOTHROW(logger->flush());
}
TEST_CASE("unknown_ex_from_err_handler", "[errors]") {
auto logger = std::make_shared<spdlog::logger>("test-throwing-error-handler", std::make_shared<failing_sink>());
logger->set_error_handler([=](const std::string &msg) {
REQUIRE(msg == log_err_msg);
throw custom_ex();
@@ -51,8 +79,8 @@ TEST_CASE("default_error_handler2", "[errors]") {
REQUIRE_NOTHROW(logger->info("Some message"));
}
TEST_CASE("flush_error_handler", "[errors]") {
auto logger = spdlog::create<failing_sink>("test-failing-sink");
TEST_CASE("unknown_ex_from_flush_err_handler", "[errors]") {
auto logger = spdlog::create<failing_sink>("test-throwing-error-handler");
logger->set_error_handler([=](const std::string &msg) {
REQUIRE(msg == flush_err_msg);
throw custom_ex();

View File

@@ -2,6 +2,8 @@
#include "spdlog/sinks/ostream_sink.h"
#include "test_sink.h"
#include <chrono>
using spdlog::memory_buf_t;
// log to str and return it
@@ -18,6 +20,21 @@ static std::string log_to_str(const std::string &msg, const Args &...args) {
return oss.str();
}
// log to str and return it with time
template <typename... Args>
static std::string log_to_str_with_time(spdlog::log_clock::time_point log_time, const std::string &msg, const Args &...args) {
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("pattern_tester", oss_sink);
oss_logger.set_level(spdlog::level::info);
oss_logger.set_formatter(
std::unique_ptr<spdlog::formatter>(new spdlog::pattern_formatter(args...)));
oss_logger.log(log_time, {}, spdlog::level::info, msg);
return oss.str();
}
TEST_CASE("custom eol", "[pattern_formatter]") {
std::string msg = "Hello custom eol test";
std::string eol = ";)";
@@ -52,6 +69,15 @@ TEST_CASE("date MM/DD/YY ", "[pattern_formatter]") {
REQUIRE(log_to_str("Some message", "%D %v", spdlog::pattern_time_type::local, "\n") == oss.str());
}
TEST_CASE("GMT offset ", "[pattern_formatter]") {
using namespace std::chrono_literals;
const auto now = std::chrono::system_clock::now();
const auto yesterday = now - 24h;
REQUIRE(log_to_str_with_time(yesterday, "Some message", "%z", spdlog::pattern_time_type::utc, "\n") ==
"+00:00\n");
}
TEST_CASE("color range test1", "[pattern_formatter]") {
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%v%$", spdlog::pattern_time_type::local, "\n");