mirror of
https://github.com/gabime/spdlog.git
synced 2026-04-10 11:34:29 +08:00
Compare commits
51 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12d65eebe6 | ||
|
|
ce0424bb37 | ||
|
|
1f9272eb7d | ||
|
|
22122f3901 | ||
|
|
1b0a1dda33 | ||
|
|
88715d29e9 | ||
|
|
22405cf9ae | ||
|
|
27d8580131 | ||
|
|
fbffd38030 | ||
|
|
3a54caee36 | ||
|
|
9db0ba648a | ||
|
|
463e41f049 | ||
|
|
ace82f7da6 | ||
|
|
b93c0f8e8d | ||
|
|
aec733b7a9 | ||
|
|
eb660caa6c | ||
|
|
af8440b248 | ||
|
|
214e26e8b2 | ||
|
|
35060923d9 | ||
|
|
a8e7527d2d | ||
|
|
2abfa1628b | ||
|
|
d6389d696e | ||
|
|
c563b62aea | ||
|
|
e3f8349d0c | ||
|
|
128a9fcc49 | ||
|
|
43812ddaf1 | ||
|
|
23b1c4c079 | ||
|
|
b01f15cb26 | ||
|
|
370dad3225 | ||
|
|
418a39f6ce | ||
|
|
177f2618fb | ||
|
|
6a794b1dff | ||
|
|
fbe626d828 | ||
|
|
47fe6ef92a | ||
|
|
b9f0243405 | ||
|
|
873026a254 | ||
|
|
e99e09eba7 | ||
|
|
07be1b4767 | ||
|
|
f00a6550fa | ||
|
|
391eb198bf | ||
|
|
3f4cfa72d1 | ||
|
|
80f00797e3 | ||
|
|
62bbd87bdb | ||
|
|
8736ee28e0 | ||
|
|
82358e8ebe | ||
|
|
9673c1ba09 | ||
|
|
5dce654473 | ||
|
|
be942e0a2d | ||
|
|
af7b061773 | ||
|
|
bc2eed7913 | ||
|
|
b46b6dcb00 |
2
.github/workflows/linux.yml
vendored
2
.github/workflows/linux.yml
vendored
@@ -28,7 +28,7 @@ jobs:
|
|||||||
image: ${{ matrix.config.compiler == 'clang' && 'teeks99/clang-ubuntu' || matrix.config.compiler }}:${{ matrix.config.version }}
|
image: ${{ matrix.config.compiler == 'clang' && 'teeks99/clang-ubuntu' || matrix.config.compiler }}:${{ matrix.config.version }}
|
||||||
name: "${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }})"
|
name: "${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }})"
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v6
|
||||||
- name: Setup
|
- name: Setup
|
||||||
run: |
|
run: |
|
||||||
apt-get update
|
apt-get update
|
||||||
|
|||||||
2
.github/workflows/macos.yml
vendored
2
.github/workflows/macos.yml
vendored
@@ -10,7 +10,7 @@ jobs:
|
|||||||
runs-on: macOS-latest
|
runs-on: macOS-latest
|
||||||
name: "macOS Clang (C++17, Release)"
|
name: "macOS Clang (C++17, Release)"
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v6
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
mkdir -p build && cd build
|
mkdir -p build && cd build
|
||||||
|
|||||||
4
.github/workflows/windows.yml
vendored
4
.github/workflows/windows.yml
vendored
@@ -33,7 +33,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v6
|
||||||
|
|
||||||
- name: CMake ${{ matrix.config.GENERATOR }} CXX=${{matrix.config.CXX_STANDARD}}
|
- name: CMake ${{ matrix.config.GENERATOR }} CXX=${{matrix.config.CXX_STANDARD}}
|
||||||
shell: pwsh
|
shell: pwsh
|
||||||
@@ -85,7 +85,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v6
|
||||||
|
|
||||||
- name: CMake ${{ matrix.config.GENERATOR }} CXX=${{matrix.config.CXX_STANDARD}}
|
- name: CMake ${{ matrix.config.GENERATOR }} CXX=${{matrix.config.CXX_STANDARD}}
|
||||||
shell: pwsh
|
shell: pwsh
|
||||||
|
|||||||
149
CMakeLists.txt
149
CMakeLists.txt
@@ -1,45 +1,38 @@
|
|||||||
# 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/utils.cmake)
|
||||||
include(cmake/ide.cmake)
|
include(cmake/ide.cmake)
|
||||||
|
|
||||||
spdlog_extract_version()
|
spdlog_extract_version()
|
||||||
|
|
||||||
project(spdlog VERSION ${SPDLOG_VERSION} LANGUAGES CXX)
|
project(spdlog VERSION ${SPDLOG_VERSION} LANGUAGES CXX)
|
||||||
|
|
||||||
include(GNUInstallDirs)
|
include(GNUInstallDirs)
|
||||||
|
|
||||||
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Set default build to release
|
# Set default build to release
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
|
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
|
||||||
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
|
# Set CMAKE_BUILD_TYPE only if this project is top-level (avoid overriding parent)
|
||||||
|
if ((DEFINED PROJECT_IS_TOP_LEVEL AND PROJECT_IS_TOP_LEVEL) OR
|
||||||
|
(NOT DEFINED PROJECT_IS_TOP_LEVEL AND CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR))
|
||||||
|
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
|
||||||
|
endif ()
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Compiler config
|
# Compiler config
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# c++ standard >=17 is required
|
# C++ standard >=17 is required
|
||||||
if (NOT DEFINED CMAKE_CXX_STANDARD)
|
if (NOT DEFINED CMAKE_CXX_STANDARD)
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
elseif (CMAKE_CXX_STANDARD LESS 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}")
|
message(FATAL_ERROR "Minimum supported CMAKE_CXX_STANDARD is 17, but it is set to ${CMAKE_CXX_STANDARD}")
|
||||||
endif ()
|
endif ()
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
if (CMAKE_SYSTEM_NAME MATCHES "CYGWIN" OR CMAKE_SYSTEM_NAME MATCHES "MSYS" OR CMAKE_SYSTEM_NAME MATCHES "MINGW")
|
if (CMAKE_SYSTEM_NAME MATCHES "CYGWIN" OR CMAKE_SYSTEM_NAME MATCHES "MSYS" OR CMAKE_SYSTEM_NAME MATCHES "MINGW")
|
||||||
set(CMAKE_CXX_EXTENSIONS ON)
|
set(CMAKE_CXX_EXTENSIONS ON)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Set SPDLOG_MASTER_PROJECT to ON if we are building spdlog
|
# Set SPDLOG_MASTER_PROJECT to ON if we are building spdlog
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
@@ -51,95 +44,86 @@ if (NOT DEFINED SPDLOG_MASTER_PROJECT)
|
|||||||
set(SPDLOG_MASTER_PROJECT OFF)
|
set(SPDLOG_MASTER_PROJECT OFF)
|
||||||
endif ()
|
endif ()
|
||||||
endif ()
|
endif ()
|
||||||
|
# ---------------------------------------------------------------------------------------
|
||||||
|
# Options
|
||||||
|
# ---------------------------------------------------------------------------------------
|
||||||
option(SPDLOG_BUILD_ALL "Build all artifacts" OFF)
|
option(SPDLOG_BUILD_ALL "Build all artifacts" OFF)
|
||||||
|
|
||||||
# build shared option
|
|
||||||
option(SPDLOG_BUILD_SHARED "Build shared library" OFF)
|
option(SPDLOG_BUILD_SHARED "Build shared library" OFF)
|
||||||
|
|
||||||
# example options
|
|
||||||
option(SPDLOG_BUILD_EXAMPLE "Build example" ${SPDLOG_MASTER_PROJECT})
|
option(SPDLOG_BUILD_EXAMPLE "Build example" ${SPDLOG_MASTER_PROJECT})
|
||||||
|
|
||||||
# testing options
|
|
||||||
option(SPDLOG_BUILD_TESTS "Build tests" OFF)
|
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)
|
option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/benchmark.git to be installed)" OFF)
|
||||||
|
option(SPDLOG_SANITIZE_ADDRESS "Enable address sanitizer for the spdlog library and anything that links it" OFF)
|
||||||
# sanitizer options
|
option(SPDLOG_SANITIZE_THREAD "Enable thread sanitizer for the spdlog library and anything that links it" OFF)
|
||||||
option(SPDLOG_SANITIZE_ADDRESS "Enable address sanitizer in tests" OFF)
|
if (SPDLOG_SANITIZE_ADDRESS AND SPDLOG_SANITIZE_THREAD)
|
||||||
|
message(FATAL_ERROR "SPDLOG_SANITIZE_ADDRESS and SPDLOG_SANITIZE_THREAD are mutually exclusive")
|
||||||
# warning options
|
endif ()
|
||||||
option(SPDLOG_BUILD_WARNINGS "Enable compiler warnings" OFF)
|
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_SYSTEM_INCLUDES "Include as system headers (skip for clang-tidy)." OFF)
|
||||||
option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT})
|
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)
|
option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of of fetching from gitub." OFF)
|
||||||
|
|
||||||
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
|
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
|
||||||
option(SPDLOG_CLOCK_COARSE "Use CLOCK_REALTIME_COARSE instead of the regular clock," OFF)
|
option(SPDLOG_CLOCK_COARSE "Use CLOCK_REALTIME_COARSE instead of the regular clock," OFF)
|
||||||
else ()
|
else ()
|
||||||
set(SPDLOG_CLOCK_COARSE OFF CACHE BOOL "non supported option" FORCE)
|
set(SPDLOG_CLOCK_COARSE OFF CACHE BOOL "non supported option" FORCE)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
option(SPDLOG_PREVENT_CHILD_FD "Prevent from child processes to inherit log file descriptors" OFF)
|
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_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_DISABLE_GLOBAL_LOGGER "Disable global logger creation" OFF)
|
||||||
option(SPDLOG_NO_TLS "Disable thread local storage" OFF)
|
option(SPDLOG_NO_TLS "Disable thread local storage" OFF)
|
||||||
|
option(SPDLOG_NO_TZ_OFFSET "Omit %z timezone offset (use on platforms without tm_gmtoff / full offset)" OFF)
|
||||||
# clang-tidy
|
|
||||||
option(SPDLOG_TIDY "run clang-tidy" OFF)
|
option(SPDLOG_TIDY "run clang-tidy" OFF)
|
||||||
|
set(SPDLOG_DEBUG_POSTFIX "-${SPDLOG_VERSION_MAJOR}.${SPDLOG_VERSION_MINOR}d" CACHE STRING
|
||||||
|
"Filename postfix for libraries in debug builds (empty string allowed)")
|
||||||
|
option(SPDLOG_MSVC_UTF8 "Enable/disable MSVC /utf-8 flag (recommended for fmt)" ON)
|
||||||
|
if (WIN32)
|
||||||
|
option(SPDLOG_WCHAR_CONSOLE "Decode UTF-8 and write with WriteConsoleW (Unicode console output)" OFF)
|
||||||
|
else ()
|
||||||
|
set(SPDLOG_WCHAR_CONSOLE OFF CACHE BOOL "non supported option" FORCE)
|
||||||
|
endif ()
|
||||||
if (SPDLOG_TIDY)
|
if (SPDLOG_TIDY)
|
||||||
set(CMAKE_CXX_CLANG_TIDY "clang-tidy")
|
set(CMAKE_CXX_CLANG_TIDY "clang-tidy")
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
message(STATUS "Enabled clang-tidy")
|
message(STATUS "Enabled clang-tidy")
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
if (SPDLOG_BUILD_SHARED)
|
if (SPDLOG_BUILD_SHARED)
|
||||||
set(BUILD_SHARED_LIBS ON)
|
set(BUILD_SHARED_LIBS ON)
|
||||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
||||||
# place dlls and libs and executables in the same directory
|
# place dlls and libs and executables in the same directory
|
||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
|
||||||
set(CMAKE_LIBRARY_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_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
|
||||||
set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
|
set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
|
||||||
|
|
||||||
# make sure __cplusplus is defined
|
# make sure __cplusplus is defined
|
||||||
add_compile_options(/Zc:__cplusplus)
|
add_compile_options(/Zc:__cplusplus)
|
||||||
# enable parallel build for the solution
|
# enable parallel build for the solution
|
||||||
add_compile_options(/MP)
|
add_compile_options(/MP)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
message(STATUS "spdlog version: ${SPDLOG_VERSION}")
|
message(STATUS "spdlog version: ${SPDLOG_VERSION}")
|
||||||
message(STATUS "spdlog build type: " ${CMAKE_BUILD_TYPE})
|
message(STATUS "spdlog build type: " ${CMAKE_BUILD_TYPE})
|
||||||
message(STATUS "spdlog build shared: " ${BUILD_SHARED_LIBS})
|
message(STATUS "spdlog build shared: " ${BUILD_SHARED_LIBS})
|
||||||
message(STATUS "spdlog fmt external: " ${SPDLOG_FMT_EXTERNAL})
|
message(STATUS "spdlog fmt external: " ${SPDLOG_FMT_EXTERNAL})
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Find {fmt} library
|
# Find {fmt} library
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
if (SPDLOG_FMT_EXTERNAL)
|
if (SPDLOG_FMT_EXTERNAL)
|
||||||
find_package(fmt REQUIRED)
|
find_package(fmt REQUIRED CONFIG)
|
||||||
message(STATUS "Using external fmt lib version: ${fmt_VERSION}")
|
message(STATUS "Using external fmt lib version: ${fmt_VERSION}")
|
||||||
else ()
|
else ()
|
||||||
include(cmake/fmtlib.cmake)
|
include(cmake/fmtlib.cmake)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Threads library is required
|
# Threads library is required
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
find_package(Threads REQUIRED)
|
find_package(Threads REQUIRED)
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Library sources
|
# Library sources
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
set(SPDLOG_HEADERS
|
set(SPDLOG_HEADERS
|
||||||
"include/spdlog/common.h"
|
"include/spdlog/common.h"
|
||||||
|
"include/spdlog/file_event_handlers.h"
|
||||||
|
"include/spdlog/filename_t.h"
|
||||||
"include/spdlog/formatter.h"
|
"include/spdlog/formatter.h"
|
||||||
"include/spdlog/fwd.h"
|
"include/spdlog/fwd.h"
|
||||||
"include/spdlog/logger.h"
|
"include/spdlog/logger.h"
|
||||||
@@ -182,7 +166,6 @@ set(SPDLOG_HEADERS
|
|||||||
"include/spdlog/sinks/tcp_sink.h"
|
"include/spdlog/sinks/tcp_sink.h"
|
||||||
"include/spdlog/sinks/udp_sink.h"
|
"include/spdlog/sinks/udp_sink.h"
|
||||||
"include/spdlog/sinks/async_sink.h")
|
"include/spdlog/sinks/async_sink.h")
|
||||||
|
|
||||||
set(SPDLOG_SRCS
|
set(SPDLOG_SRCS
|
||||||
"src/common.cpp"
|
"src/common.cpp"
|
||||||
"src/logger.cpp"
|
"src/logger.cpp"
|
||||||
@@ -198,7 +181,6 @@ set(SPDLOG_SRCS
|
|||||||
"src/sinks/rotating_file_sink.cpp"
|
"src/sinks/rotating_file_sink.cpp"
|
||||||
"src/sinks/stdout_sinks.cpp"
|
"src/sinks/stdout_sinks.cpp"
|
||||||
"src/sinks/async_sink.cpp")
|
"src/sinks/async_sink.cpp")
|
||||||
|
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
list(APPEND SPDLOG_SRCS
|
list(APPEND SPDLOG_SRCS
|
||||||
"src/details/os_windows.cpp"
|
"src/details/os_windows.cpp"
|
||||||
@@ -218,7 +200,6 @@ else ()
|
|||||||
"include/spdlog/details/udp_client_unix.h"
|
"include/spdlog/details/udp_client_unix.h"
|
||||||
"include/spdlog/sinks/ansicolor_sink.h")
|
"include/spdlog/sinks/ansicolor_sink.h")
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Check if fwrite_unlocked/_fwrite_nolock is available
|
# Check if fwrite_unlocked/_fwrite_nolock is available
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
@@ -231,7 +212,6 @@ endif ()
|
|||||||
if (HAVE_FWRITE_UNLOCKED)
|
if (HAVE_FWRITE_UNLOCKED)
|
||||||
set(SPDLOG_FWRITE_UNLOCKED 1)
|
set(SPDLOG_FWRITE_UNLOCKED 1)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# spdlog library
|
# spdlog library
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
@@ -244,12 +224,12 @@ if (BUILD_SHARED_LIBS)
|
|||||||
target_compile_definitions(spdlog PUBLIC SPDLOG_SHARED_LIB)
|
target_compile_definitions(spdlog PUBLIC SPDLOG_SHARED_LIB)
|
||||||
if (MSVC)
|
if (MSVC)
|
||||||
# disable dlls related warnings on msvc
|
# disable dlls related warnings on msvc
|
||||||
target_compile_options(spdlog PUBLIC $<$<AND:$<CXX_COMPILER_ID:MSVC>,$<NOT:$<COMPILE_LANGUAGE:CUDA>>>:/wd4251
|
target_compile_options(spdlog PUBLIC $<$<AND:$<CXX_COMPILER_ID:MSVC>,$<NOT:$<COMPILE_LANGUAGE:CUDA>>>:/wd4251 /wd4275>)
|
||||||
/wd4275>)
|
|
||||||
endif ()
|
endif ()
|
||||||
else ()
|
else ()
|
||||||
add_library(spdlog STATIC)
|
add_library(spdlog STATIC)
|
||||||
endif ()
|
endif ()
|
||||||
|
set_target_properties(spdlog PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN ON)
|
||||||
add_library(spdlog::spdlog ALIAS spdlog)
|
add_library(spdlog::spdlog ALIAS spdlog)
|
||||||
target_sources(spdlog PRIVATE ${SPDLOG_SRCS})
|
target_sources(spdlog PRIVATE ${SPDLOG_SRCS})
|
||||||
target_sources(
|
target_sources(
|
||||||
@@ -258,90 +238,88 @@ target_sources(
|
|||||||
TYPE HEADERS
|
TYPE HEADERS
|
||||||
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include
|
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||||
FILES ${SPDLOG_HEADERS})
|
FILES ${SPDLOG_HEADERS})
|
||||||
|
|
||||||
set(SPDLOG_INCLUDES_LEVEL "")
|
set(SPDLOG_INCLUDES_LEVEL "")
|
||||||
if (SPDLOG_SYSTEM_INCLUDES)
|
if (SPDLOG_SYSTEM_INCLUDES)
|
||||||
set(SPDLOG_INCLUDES_LEVEL "SYSTEM")
|
set(SPDLOG_INCLUDES_LEVEL "SYSTEM")
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
target_include_directories(spdlog ${SPDLOG_INCLUDES_LEVEL} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
|
target_include_directories(spdlog ${SPDLOG_INCLUDES_LEVEL} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
|
||||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
|
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
|
||||||
|
|
||||||
target_link_libraries(spdlog PUBLIC Threads::Threads)
|
target_link_libraries(spdlog PUBLIC Threads::Threads)
|
||||||
target_link_libraries(spdlog PUBLIC fmt::fmt)
|
target_link_libraries(spdlog PUBLIC fmt::fmt)
|
||||||
|
if (MSVC)
|
||||||
|
if (SPDLOG_MSVC_UTF8)
|
||||||
|
# Pass /utf-8 only for real MSVC (not clang-cl); see fmtlib and #3260.
|
||||||
|
target_compile_options(spdlog PUBLIC $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/utf-8>)
|
||||||
|
endif ()
|
||||||
|
endif ()
|
||||||
spdlog_enable_warnings(spdlog)
|
spdlog_enable_warnings(spdlog)
|
||||||
|
if (SPDLOG_SANITIZE_ADDRESS)
|
||||||
|
spdlog_enable_addr_sanitizer(spdlog)
|
||||||
|
elseif (SPDLOG_SANITIZE_THREAD)
|
||||||
|
spdlog_enable_thread_sanitizer(spdlog)
|
||||||
|
endif ()
|
||||||
set_target_properties(spdlog PROPERTIES VERSION ${SPDLOG_VERSION} SOVERSION
|
set_target_properties(spdlog PROPERTIES VERSION ${SPDLOG_VERSION} SOVERSION
|
||||||
${SPDLOG_VERSION_MAJOR}.${SPDLOG_VERSION_MINOR})
|
${SPDLOG_VERSION_MAJOR}.${SPDLOG_VERSION_MINOR})
|
||||||
|
|
||||||
set(SPDLOG_NAME spdlog-${SPDLOG_VERSION_MAJOR})
|
set(SPDLOG_NAME spdlog-${SPDLOG_VERSION_MAJOR})
|
||||||
set_target_properties(spdlog PROPERTIES DEBUG_POSTFIX "-${SPDLOG_VERSION_MAJOR}.${SPDLOG_VERSION_MINOR}d")
|
set_target_properties(spdlog PROPERTIES DEBUG_POSTFIX "${SPDLOG_DEBUG_POSTFIX}")
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# set source groups for visual studio
|
# Set prefix and source group for visual studio
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
if (CMAKE_GENERATOR MATCHES "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}/include PREFIX include FILES ${SPDLOG_HEADERS})
|
||||||
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/src PREFIX sources FILES ${SPDLOG_SRCS})
|
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/src PREFIX sources FILES ${SPDLOG_SRCS})
|
||||||
source_group(sources FILES ${VERSION_RC})
|
source_group(sources FILES ${VERSION_RC})
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Add required libraries for Android CMake build
|
# Android support
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
if (ANDROID)
|
if (ANDROID)
|
||||||
target_link_libraries(spdlog PUBLIC log)
|
target_link_libraries(spdlog PUBLIC log)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# spdlog private defines according to the options
|
# Private defines according to the options
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
|
set(SPDLOG_UTF8_TO_WCHAR_CONSOLE ${SPDLOG_WCHAR_CONSOLE})
|
||||||
foreach (SPDLOG_OPTION
|
foreach (SPDLOG_OPTION
|
||||||
SPDLOG_CLOCK_COARSE
|
SPDLOG_CLOCK_COARSE
|
||||||
SPDLOG_PREVENT_CHILD_FD
|
SPDLOG_PREVENT_CHILD_FD
|
||||||
SPDLOG_NO_THREAD_ID
|
SPDLOG_NO_THREAD_ID
|
||||||
SPDLOG_DISABLE_GLOBAL_LOGGER
|
SPDLOG_DISABLE_GLOBAL_LOGGER
|
||||||
SPDLOG_NO_TLS
|
SPDLOG_NO_TLS
|
||||||
SPDLOG_FWRITE_UNLOCKED)
|
SPDLOG_FWRITE_UNLOCKED
|
||||||
|
SPDLOG_UTF8_TO_WCHAR_CONSOLE)
|
||||||
if (${SPDLOG_OPTION})
|
if (${SPDLOG_OPTION})
|
||||||
target_compile_definitions(spdlog PRIVATE ${SPDLOG_OPTION})
|
target_compile_definitions(spdlog PRIVATE ${SPDLOG_OPTION})
|
||||||
endif ()
|
endif ()
|
||||||
endforeach ()
|
endforeach ()
|
||||||
|
if (SPDLOG_NO_TZ_OFFSET)
|
||||||
|
target_compile_definitions(spdlog PUBLIC SPDLOG_NO_TZ_OFFSET)
|
||||||
|
endif ()
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Build binaries
|
# Build binaries
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
|
# examples
|
||||||
if (SPDLOG_BUILD_EXAMPLE OR SPDLOG_BUILD_ALL)
|
if (SPDLOG_BUILD_EXAMPLE OR SPDLOG_BUILD_ALL)
|
||||||
message(STATUS "Generating example(s)")
|
message(STATUS "Generating example(s)")
|
||||||
add_subdirectory(example)
|
add_subdirectory(example)
|
||||||
spdlog_enable_warnings(example)
|
spdlog_enable_warnings(example)
|
||||||
endif ()
|
endif ()
|
||||||
|
# tests
|
||||||
if (SPDLOG_BUILD_TESTS OR SPDLOG_BUILD_ALL)
|
if (SPDLOG_BUILD_TESTS OR SPDLOG_BUILD_ALL)
|
||||||
message(STATUS "Generating tests")
|
message(STATUS "Generating tests")
|
||||||
enable_testing()
|
enable_testing()
|
||||||
add_subdirectory(tests)
|
add_subdirectory(tests)
|
||||||
endif ()
|
endif ()
|
||||||
|
# benchmarks
|
||||||
if (SPDLOG_BUILD_BENCH OR SPDLOG_BUILD_ALL)
|
if (SPDLOG_BUILD_BENCH OR SPDLOG_BUILD_ALL)
|
||||||
message(STATUS "Generating benchmarks")
|
message(STATUS "Generating benchmarks")
|
||||||
add_subdirectory(bench)
|
add_subdirectory(bench)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Install
|
# Install
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
if (SPDLOG_INSTALL)
|
if (SPDLOG_INSTALL)
|
||||||
message(STATUS "Generating 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(
|
install(
|
||||||
TARGETS spdlog
|
TARGETS spdlog
|
||||||
EXPORT spdlogTargets
|
EXPORT spdlogTargets
|
||||||
@@ -351,20 +329,17 @@ if (SPDLOG_INSTALL)
|
|||||||
FILE_SET pub_headers
|
FILE_SET pub_headers
|
||||||
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${SPDLOG_NAME}")
|
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${SPDLOG_NAME}")
|
||||||
message(STATUS "Installing spdlog in ${CMAKE_INSTALL_LIBDIR}/${SPDLOG_NAME}")
|
message(STATUS "Installing spdlog in ${CMAKE_INSTALL_LIBDIR}/${SPDLOG_NAME}")
|
||||||
|
# Install CMake spdlogConfig.cmake, spdlogConfigVersion.cmake and spdlogTargets.cmake
|
||||||
# ---------------------------------------------------------------------------------------
|
set(project_config_in "${CMAKE_CURRENT_LIST_DIR}/cmake/spdlogConfig.cmake.in")
|
||||||
# Install CMake config files
|
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})
|
install(EXPORT spdlogTargets DESTINATION ${export_dest_dir} NAMESPACE spdlog:: FILE ${config_targets_file})
|
||||||
|
|
||||||
include(CMakePackageConfigHelpers)
|
include(CMakePackageConfigHelpers)
|
||||||
configure_package_config_file("${project_config_in}" "${project_config_out}" INSTALL_DESTINATION ${export_dest_dir})
|
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)
|
write_basic_package_version_file("${version_config_file}" COMPATIBILITY SameMajorVersion)
|
||||||
install(FILES "${project_config_out}" "${version_config_file}" DESTINATION "${export_dest_dir}")
|
install(FILES "${project_config_out}" "${version_config_file}" DESTINATION "${export_dest_dir}")
|
||||||
|
# CPack
|
||||||
# ---------------------------------------------------------------------------------------
|
|
||||||
# Support creation of installable packages
|
|
||||||
# ---------------------------------------------------------------------------------------
|
|
||||||
include(cmake/spdlogCPack.cmake)
|
include(cmake/spdlogCPack.cmake)
|
||||||
endif ()
|
endif ()
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ see example [CMakeLists.txt](https://github.com/gabime/spdlog/blob/v2.x/example/
|
|||||||
## Features
|
## Features
|
||||||
* Very fast (see [benchmarks](#benchmarks) below).
|
* Very fast (see [benchmarks](#benchmarks) below).
|
||||||
* Headers only or compiled
|
* Headers only or compiled
|
||||||
* Feature-rich formatting, using the excellent [fmt](https://github.com/fmtlib/fmt) library.
|
* Feature-rich formatting, using the excellent [fmt](https://github.com/fmtlib/fmt) library (bundled **12.1.0** by default; use `SPDLOG_FMT_EXTERNAL=ON` for a system **fmt**, **12.x** recommended).
|
||||||
* Asynchronous mode (optional)
|
* Asynchronous mode (optional)
|
||||||
* [Custom](https://github.com/gabime/spdlog/wiki/3.-Custom-formatting) formatting.
|
* [Custom](https://github.com/gabime/spdlog/wiki/3.-Custom-formatting) formatting.
|
||||||
* Multi/Single threaded loggers.
|
* Multi/Single threaded loggers.
|
||||||
@@ -258,7 +258,7 @@ struct fmt::formatter<my_type> : fmt::formatter<std::string>
|
|||||||
{
|
{
|
||||||
auto format(my_type my, format_context &ctx) const -> decltype(ctx.out())
|
auto format(my_type my, format_context &ctx) const -> decltype(ctx.out())
|
||||||
{
|
{
|
||||||
return format_to(ctx.out(), "[my_type i={}]", my.i);
|
return fmt::format_to(ctx.out(), "[my_type i={}]", my.i);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -6,14 +6,14 @@
|
|||||||
//
|
//
|
||||||
// bench.cpp : spdlog benchmarks
|
// bench.cpp : spdlog benchmarks
|
||||||
//
|
//
|
||||||
|
#include <algorithm>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <iostream>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <locale>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <locale>
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
#include "spdlog/sinks/async_sink.h"
|
#include "spdlog/sinks/async_sink.h"
|
||||||
#include "spdlog/sinks/basic_file_sink.h"
|
#include "spdlog/sinks/basic_file_sink.h"
|
||||||
@@ -21,8 +21,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
using namespace spdlog;
|
using spdlog::sinks::async_sink;
|
||||||
using namespace spdlog::sinks;
|
|
||||||
|
|
||||||
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);
|
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[]) {
|
int main(int argc, char *argv[]) {
|
||||||
// setlocale to show thousands separators
|
// setlocale to show thousands separators
|
||||||
std::locale::global(std::locale("en_US.UTF-8"));
|
std::locale::global(std::locale("en_US.UTF-8"));
|
||||||
int howmany = 1000000;
|
int howmany = 1'000'000;
|
||||||
int queue_size = std::min(howmany + 2, 8192);
|
int queue_size = async_sink::default_queue_size;
|
||||||
int threads = 10;
|
int threads = 10;
|
||||||
int iters = 3;
|
int iters = 3;
|
||||||
|
|
||||||
@@ -66,20 +65,23 @@ int main(int argc, char *argv[]) {
|
|||||||
if (argc > 2) threads = atoi(argv[2]);
|
if (argc > 2) threads = atoi(argv[2]);
|
||||||
if (argc > 3) {
|
if (argc > 3) {
|
||||||
queue_size = atoi(argv[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]);
|
if (argc > 4) iters = atoi(argv[4]);
|
||||||
// validate all argc values
|
// validate all argc values
|
||||||
if (howmany < 1 || threads < 1 || queue_size < 1 || iters < 1) {
|
if (howmany < 1 || threads < 1 || queue_size < 1 || iters < 1) {
|
||||||
spdlog::error("Invalid input values");
|
spdlog::error("Invalid input values");
|
||||||
exit(1);
|
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("-------------------------------------------------");
|
||||||
spdlog::info("Messages : {:L}", howmany);
|
spdlog::info("Messages : {:L}", howmany);
|
||||||
spdlog::info("Threads : {:L}", threads);
|
spdlog::info("Threads : {:L}", threads);
|
||||||
@@ -99,11 +101,11 @@ int main(int argc, char *argv[]) {
|
|||||||
auto cfg = async_sink::config();
|
auto cfg = async_sink::config();
|
||||||
cfg.queue_size = queue_size;
|
cfg.queue_size = queue_size;
|
||||||
cfg.sinks.push_back(std::move(file_sink));
|
cfg.sinks.push_back(std::move(file_sink));
|
||||||
auto async_sink = std::make_shared<sinks::async_sink>(cfg);
|
auto sink = std::make_shared<async_sink>(cfg);
|
||||||
auto logger = std::make_shared<spdlog::logger>("async_logger", std::move(async_sink));
|
auto logger = std::make_shared<spdlog::logger>("async_logger", std::move(sink));
|
||||||
bench_mt(howmany, std::move(logger), threads);
|
bench_mt(howmany, std::move(logger), threads);
|
||||||
}
|
}
|
||||||
//verify_file(filename, howmany); // in separate scope to ensure logger is destroyed and all logs were written
|
// verify_file(filename, howmany); // in separate scope to ensure logger is destroyed and all logs were written
|
||||||
}
|
}
|
||||||
spdlog::info("");
|
spdlog::info("");
|
||||||
spdlog::info("*********************************");
|
spdlog::info("*********************************");
|
||||||
@@ -117,8 +119,8 @@ int main(int argc, char *argv[]) {
|
|||||||
cfg.queue_size = queue_size;
|
cfg.queue_size = queue_size;
|
||||||
auto file_sink = std::make_shared<basic_file_sink_mt>(filename, true);
|
auto file_sink = std::make_shared<basic_file_sink_mt>(filename, true);
|
||||||
cfg.sinks.push_back(std::move(file_sink));
|
cfg.sinks.push_back(std::move(file_sink));
|
||||||
auto async_sink = std::make_shared<sinks::async_sink>(cfg);
|
auto sink = std::make_shared<async_sink>(cfg);
|
||||||
auto logger = std::make_shared<spdlog::logger>("async_logger", std::move(async_sink));
|
auto logger = std::make_shared<spdlog::logger>("async_logger", std::move(sink));
|
||||||
bench_mt(howmany, std::move(logger), threads);
|
bench_mt(howmany, std::move(logger), threads);
|
||||||
}
|
}
|
||||||
spdlog::shutdown();
|
spdlog::shutdown();
|
||||||
|
|||||||
@@ -124,7 +124,8 @@ int main(int argc, char *argv[]) {
|
|||||||
benchmark::RegisterBenchmark("basic_st", bench_logger, std::move(basic_st))->UseRealTime();
|
benchmark::RegisterBenchmark("basic_st", bench_logger, std::move(basic_st))->UseRealTime();
|
||||||
|
|
||||||
// rotating st
|
// rotating st
|
||||||
auto rotating_st = spdlog::create<rotating_file_sink_st>("rotating_st", "latency_logs/rotating_st.log", file_size, rotating_files);
|
auto rotating_st =
|
||||||
|
spdlog::create<rotating_file_sink_st>("rotating_st", "latency_logs/rotating_st.log", file_size, rotating_files);
|
||||||
benchmark::RegisterBenchmark("rotating_st", bench_logger, std::move(rotating_st))->UseRealTime();
|
benchmark::RegisterBenchmark("rotating_st", bench_logger, std::move(rotating_st))->UseRealTime();
|
||||||
|
|
||||||
// daily st
|
// daily st
|
||||||
@@ -142,7 +143,8 @@ int main(int argc, char *argv[]) {
|
|||||||
benchmark::RegisterBenchmark("basic_mt", bench_logger, std::move(basic_mt))->Threads(n_threads)->UseRealTime();
|
benchmark::RegisterBenchmark("basic_mt", bench_logger, std::move(basic_mt))->Threads(n_threads)->UseRealTime();
|
||||||
|
|
||||||
// rotating mt
|
// rotating mt
|
||||||
auto rotating_mt = spdlog::create<rotating_file_sink_mt>("rotating_mt", "latency_logs/rotating_mt.log", file_size, rotating_files);
|
auto rotating_mt =
|
||||||
|
spdlog::create<rotating_file_sink_mt>("rotating_mt", "latency_logs/rotating_mt.log", file_size, rotating_files);
|
||||||
benchmark::RegisterBenchmark("rotating_mt", bench_logger, std::move(rotating_mt))->Threads(n_threads)->UseRealTime();
|
benchmark::RegisterBenchmark("rotating_mt", bench_logger, std::move(rotating_mt))->Threads(n_threads)->UseRealTime();
|
||||||
|
|
||||||
// daily mt
|
// daily mt
|
||||||
@@ -151,7 +153,8 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
using spdlog::sinks::async_sink;
|
using spdlog::sinks::async_sink;
|
||||||
async_sink::config config;
|
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.sinks.push_back(std::make_shared<null_sink_st>());
|
||||||
config.policy = async_sink::overflow_policy::overrun_oldest;
|
config.policy = async_sink::overflow_policy::overrun_oldest;
|
||||||
auto async_logger = std::make_shared<spdlog::logger>("async_logger", std::make_shared<async_sink>(config));
|
auto async_logger = std::make_shared<spdlog::logger>("async_logger", std::make_shared<async_sink>(config));
|
||||||
|
|||||||
@@ -3,13 +3,25 @@ include(FetchContent)
|
|||||||
FetchContent_Declare(
|
FetchContent_Declare(
|
||||||
fmt
|
fmt
|
||||||
DOWNLOAD_EXTRACT_TIMESTAMP FALSE
|
DOWNLOAD_EXTRACT_TIMESTAMP FALSE
|
||||||
URL https://github.com/fmtlib/fmt/archive/refs/tags/11.0.2.tar.gz
|
URL https://github.com/fmtlib/fmt/archive/refs/tags/12.1.0.tar.gz
|
||||||
URL_HASH SHA256=6cb1e6d37bdcb756dbbe59be438790db409cdb4868c66e888d5df9f13f7c027f)
|
URL_HASH SHA256=ea7de4299689e12b6dddd392f9896f08fb0777ac7168897a244a6d6085043fea)
|
||||||
|
|
||||||
FetchContent_GetProperties(fmt)
|
FetchContent_GetProperties(fmt)
|
||||||
if(NOT fmt_POPULATED)
|
if(NOT fmt_POPULATED)
|
||||||
# We do not require os features of fmt
|
# We do not require os features of fmt
|
||||||
set(FMT_OS OFF CACHE BOOL "Disable FMT_OS" FORCE)
|
set(FMT_OS OFF CACHE BOOL "Disable FMT_OS" FORCE)
|
||||||
|
# fmt 12+ defaults FMT_INSTALL to OFF when built as a subproject; spdlog's
|
||||||
|
# install(EXPORT) requires fmt to participate in an export set (CMake 3.23+).
|
||||||
|
# Only enable fmt's install rules when spdlog installs
|
||||||
|
if(SPDLOG_INSTALL)
|
||||||
|
set(FMT_INSTALL ON CACHE BOOL "Generate the install target." FORCE)
|
||||||
|
else()
|
||||||
|
set(FMT_INSTALL OFF CACHE BOOL "Generate the install target." FORCE)
|
||||||
|
endif()
|
||||||
FetchContent_MakeAvailable(fmt)
|
FetchContent_MakeAvailable(fmt)
|
||||||
set_target_properties(fmt PROPERTIES FOLDER "third-party")
|
set_target_properties(fmt PROPERTIES FOLDER "third-party")
|
||||||
|
# fmt 12.1.0: MSVC C4834 on locale_ref ctor (discarded [[nodiscard]] from isalpha); fixed on fmt master after 12.1.0.
|
||||||
|
if (MSVC)
|
||||||
|
target_compile_options(fmt PRIVATE /wd4834)
|
||||||
|
endif ()
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ set(CPACK_DEBIAN_PACKAGE_SECTION "libs")
|
|||||||
set(CPACK_RPM_PACKAGE_URL ${CPACK_PROJECT_URL})
|
set(CPACK_RPM_PACKAGE_URL ${CPACK_PROJECT_URL})
|
||||||
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE ${CPACK_PROJECT_URL})
|
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE ${CPACK_PROJECT_URL})
|
||||||
set(CPACK_RPM_PACKAGE_DESCRIPTION "Fast C++ logging library.")
|
set(CPACK_RPM_PACKAGE_DESCRIPTION "Fast C++ logging library.")
|
||||||
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "FastC++ logging library.")
|
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "Fast C++ logging library.")
|
||||||
|
|
||||||
if(CPACK_PACKAGE_NAME)
|
if(CPACK_PACKAGE_NAME)
|
||||||
set(CPACK_RPM_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
|
set(CPACK_RPM_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ include(CMakeFindDependencyMacro)
|
|||||||
|
|
||||||
find_package(Threads REQUIRED)
|
find_package(Threads REQUIRED)
|
||||||
|
|
||||||
find_dependency(fmt 11 CONFIG)
|
find_dependency(fmt CONFIG)
|
||||||
|
|
||||||
set(config_targets_file @config_targets_file@)
|
set(config_targets_file @config_targets_file@)
|
||||||
include("${CMAKE_CURRENT_LIST_DIR}/${config_targets_file}")
|
include("${CMAKE_CURRENT_LIST_DIR}/${config_targets_file}")
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ void stdout_logger_example() {
|
|||||||
// Create color multithreading logger.
|
// Create color multithreading logger.
|
||||||
auto console = spdlog::create<stdout_color_sink_mt>("console");
|
auto console = spdlog::create<stdout_color_sink_mt>("console");
|
||||||
// or for stderr:
|
// or for stderr:
|
||||||
//auto console = spdlog::create<stderr_color_sink_mt>("console");
|
// auto console = spdlog::create<stderr_color_sink_mt>("console");
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "spdlog/sinks/basic_file_sink.h"
|
#include "spdlog/sinks/basic_file_sink.h"
|
||||||
@@ -203,15 +203,15 @@ void multi_sink_example() {
|
|||||||
|
|
||||||
// User defined types logging
|
// User defined types logging
|
||||||
struct my_type {
|
struct my_type {
|
||||||
int i = 0;
|
int value_ = 0;
|
||||||
explicit my_type(int i)
|
explicit my_type(int value)
|
||||||
: i(i) {}
|
: value_(value) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct fmt::formatter<my_type> : fmt::formatter<std::string> {
|
struct fmt::formatter<my_type> : fmt::formatter<std::string> {
|
||||||
auto format(my_type my, format_context &ctx) const -> decltype(ctx.out()) {
|
auto format(my_type my, format_context &ctx) const -> decltype(ctx.out()) {
|
||||||
return fmt::format_to(ctx.out(), "[my_type i={}]", my.i);
|
return fmt::format_to(ctx.out(), "[my_type value={}]", my.value_);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ public:
|
|||||||
// do not use begin() and end() to avoid collision with fmt/ranges
|
// do not use begin() and end() to avoid collision with fmt/ranges
|
||||||
It get_begin() const { return begin_; }
|
It get_begin() const { return begin_; }
|
||||||
It get_end() const { return end_; }
|
It get_end() const { return end_; }
|
||||||
size_t size_per_line() const { return size_per_line_; }
|
[[nodiscard]] size_t size_per_line() const { return size_per_line_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
It begin_, end_;
|
It begin_, end_;
|
||||||
@@ -90,7 +90,7 @@ inline details::dump_info<It> to_hex(const It range_begin, const It range_end, s
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct fmt::formatter<spdlog::details::dump_info<T>, char> {
|
struct fmt::formatter<spdlog::details::dump_info<T>, char> {
|
||||||
const char delimiter = ' ';
|
char delimiter = ' ';
|
||||||
bool put_newlines = true;
|
bool put_newlines = true;
|
||||||
bool put_delimiters = true;
|
bool put_delimiters = true;
|
||||||
bool use_uppercase = false;
|
bool use_uppercase = false;
|
||||||
@@ -138,14 +138,14 @@ struct fmt::formatter<spdlog::details::dump_info<T>, char> {
|
|||||||
auto inserter = ctx.out();
|
auto inserter = ctx.out();
|
||||||
int size_per_line = static_cast<int>(the_range.size_per_line());
|
int size_per_line = static_cast<int>(the_range.size_per_line());
|
||||||
auto start_of_line = the_range.get_begin();
|
auto start_of_line = the_range.get_begin();
|
||||||
for (auto i = the_range.get_begin(); i != the_range.get_end(); i++) {
|
for (auto i = the_range.get_begin(); i != the_range.get_end(); ++i) {
|
||||||
auto ch = static_cast<unsigned char>(*i);
|
auto ch = static_cast<unsigned char>(*i);
|
||||||
|
|
||||||
if (put_newlines && (i == the_range.get_begin() || i - start_of_line >= size_per_line)) {
|
if (put_newlines && (i == the_range.get_begin() || i - start_of_line >= size_per_line)) {
|
||||||
if (show_ascii && i != the_range.get_begin()) {
|
if (show_ascii && i != the_range.get_begin()) {
|
||||||
*inserter++ = delimiter;
|
*inserter++ = delimiter;
|
||||||
*inserter++ = delimiter;
|
*inserter++ = delimiter;
|
||||||
for (auto j = start_of_line; j < i; j++) {
|
for (auto j = start_of_line; j < i; ++j) {
|
||||||
auto pc = static_cast<unsigned char>(*j);
|
auto pc = static_cast<unsigned char>(*j);
|
||||||
*inserter++ = std::isprint(pc) ? static_cast<char>(*j) : '.';
|
*inserter++ = std::isprint(pc) ? static_cast<char>(*j) : '.';
|
||||||
}
|
}
|
||||||
@@ -181,7 +181,7 @@ struct fmt::formatter<spdlog::details::dump_info<T>, char> {
|
|||||||
}
|
}
|
||||||
*inserter++ = delimiter;
|
*inserter++ = delimiter;
|
||||||
*inserter++ = delimiter;
|
*inserter++ = delimiter;
|
||||||
for (auto j = start_of_line; j != the_range.get_end(); j++) {
|
for (auto j = start_of_line; j != the_range.get_end(); ++j) {
|
||||||
auto pc = static_cast<unsigned char>(*j);
|
auto pc = static_cast<unsigned char>(*j);
|
||||||
*inserter++ = std::isprint(pc) ? static_cast<char>(*j) : '.';
|
*inserter++ = std::isprint(pc) ? static_cast<char>(*j) : '.';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,31 +6,33 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <cstdint>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
#include "./source_loc.h"
|
#include "./source_loc.h"
|
||||||
#include "fmt/base.h"
|
#include "fmt/base.h"
|
||||||
#include "fmt/xchar.h"
|
#include "fmt/xchar.h"
|
||||||
|
|
||||||
#if defined(SPDLOG_SHARED_LIB)
|
// Define SPDLOG_API according to current build settings
|
||||||
#if defined(_WIN32)
|
#ifndef SPDLOG_SHARED_LIB
|
||||||
#ifdef spdlog_EXPORTS
|
#define SPDLOG_API
|
||||||
#define SPDLOG_API __declspec(dllexport)
|
#elif defined(_WIN32)
|
||||||
#else // !spdlog_EXPORTS
|
#ifdef spdlog_EXPORTS
|
||||||
#define SPDLOG_API __declspec(dllimport)
|
#define SPDLOG_API __declspec(dllexport) // Export symbols when building the library
|
||||||
#endif
|
#else
|
||||||
#else // !defined(_WIN32)
|
#define SPDLOG_API __declspec(dllimport) // Import symbols when using the library
|
||||||
#define SPDLOG_API __attribute__((visibility("default")))
|
|
||||||
#endif
|
#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
|
#define SPDLOG_API
|
||||||
#endif
|
#endif
|
||||||
|
// End of SPDLOG_API definition
|
||||||
|
|
||||||
#define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string)
|
#define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string)
|
||||||
#define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string)
|
#define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string)
|
||||||
|
|||||||
@@ -4,29 +4,27 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
#include "./log_msg.h"
|
#include "./log_msg.h"
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
|
|
||||||
|
|
||||||
// Extend log_msg with internal buffer to store its payload.
|
// Extend log_msg with internal buffer to store its payload.
|
||||||
// This is needed since log_msg holds string_views that points to stack data.
|
// This is needed since log_msg holds string_views that points to stack data.
|
||||||
|
|
||||||
class SPDLOG_API async_log_msg : public log_msg {
|
class SPDLOG_API async_log_msg : public log_msg {
|
||||||
public:
|
public:
|
||||||
enum class type:std::uint8_t { log, flush, terminate };
|
enum class type : std::uint8_t { log, flush, terminate };
|
||||||
async_log_msg() = default;
|
async_log_msg() = default;
|
||||||
explicit async_log_msg(type type);
|
explicit async_log_msg(type type);
|
||||||
async_log_msg(type type, const log_msg &orig_msg);
|
async_log_msg(type type, const log_msg &orig_msg);
|
||||||
|
|
||||||
~async_log_msg() = default;
|
~async_log_msg() = default;
|
||||||
async_log_msg(const async_log_msg &other);
|
async_log_msg(const async_log_msg &other);
|
||||||
async_log_msg(async_log_msg &&other) noexcept;
|
async_log_msg(async_log_msg &&other) noexcept;
|
||||||
async_log_msg &operator=(const async_log_msg &other);
|
async_log_msg &operator=(const async_log_msg &other);
|
||||||
async_log_msg &operator=(async_log_msg &&other) noexcept;
|
async_log_msg &operator=(async_log_msg &&other) noexcept;
|
||||||
|
[[nodiscard]] type message_type() const { return msg_type_; }
|
||||||
|
|
||||||
type message_type() const {return msg_type_;}
|
|
||||||
private:
|
private:
|
||||||
type msg_type_{type::log};
|
type msg_type_{type::log};
|
||||||
memory_buf_t buffer_;
|
memory_buf_t buffer_;
|
||||||
|
|||||||
@@ -7,6 +7,8 @@
|
|||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <spdlog/common.h>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
|||||||
@@ -3,21 +3,29 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <chrono>
|
||||||
#include <mutex>
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <mutex>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include "spdlog/common.h"
|
||||||
|
|
||||||
// by default, prints the error to stderr, thread safe
|
// by default, prints the error to stderr, at max rate of 1/sec thread safe
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
class SPDLOG_API err_helper {
|
class SPDLOG_API err_helper {
|
||||||
err_handler custom_err_handler_;
|
err_handler custom_err_handler_;
|
||||||
|
std::chrono::steady_clock::time_point last_report_time_;
|
||||||
|
mutable std::mutex mutex_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void handle_ex(const std::string& origin, const source_loc& loc, const std::exception& ex) const noexcept;
|
err_helper() = default;
|
||||||
void handle_unknown_ex(const std::string& origin, const source_loc& loc) const noexcept;
|
~err_helper() = default;
|
||||||
|
err_helper(const err_helper& other);
|
||||||
|
err_helper(err_helper&& other) noexcept;
|
||||||
|
void handle_ex(const std::string& origin, const source_loc& loc, const std::exception& ex) noexcept;
|
||||||
|
void handle_unknown_ex(const std::string& origin, const source_loc& loc) noexcept;
|
||||||
void set_err_handler(err_handler handler);
|
void set_err_handler(err_handler handler);
|
||||||
};
|
};
|
||||||
|
} // namespace details
|
||||||
|
} // namespace spdlog
|
||||||
}} // namespace spdlog::details
|
|
||||||
|
|||||||
@@ -14,12 +14,12 @@ namespace details {
|
|||||||
namespace fmt_helper {
|
namespace fmt_helper {
|
||||||
|
|
||||||
inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest) {
|
inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest) {
|
||||||
auto *buf_ptr = view.data();
|
const auto *buf_ptr = view.data();
|
||||||
dest.append(buf_ptr, buf_ptr + view.size());
|
dest.append(buf_ptr, buf_ptr + view.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline void append_int(T n, memory_buf_t &dest) {
|
void append_int(T n, memory_buf_t &dest) {
|
||||||
fmt::format_int i(n);
|
fmt::format_int i(n);
|
||||||
dest.append(i.data(), i.data() + i.size());
|
dest.append(i.data(), i.data() + i.size());
|
||||||
}
|
}
|
||||||
@@ -36,14 +36,14 @@ constexpr unsigned int count_digits_fallback(T n) {
|
|||||||
if (n < 100) return count + 1;
|
if (n < 100) return count + 1;
|
||||||
if (n < 1000) return count + 2;
|
if (n < 1000) return count + 2;
|
||||||
if (n < 10000) return count + 3;
|
if (n < 10000) return count + 3;
|
||||||
n /= 10000u;
|
n /= 10000U;
|
||||||
count += 4;
|
count += 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline unsigned int count_digits(T n) {
|
inline unsigned int count_digits(T n) {
|
||||||
using count_type = typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type;
|
using count_type = std::conditional_t<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>;
|
||||||
|
|
||||||
return static_cast<unsigned int>(fmt::
|
return static_cast<unsigned int>(fmt::
|
||||||
// fmt 7.0.0 renamed the internal namespace to detail.
|
// fmt 7.0.0 renamed the internal namespace to detail.
|
||||||
@@ -59,8 +59,8 @@ inline unsigned int count_digits(T n) {
|
|||||||
inline void pad2(int n, memory_buf_t &dest) {
|
inline void pad2(int n, memory_buf_t &dest) {
|
||||||
if (n >= 0 && n < 100) // 0-99
|
if (n >= 0 && n < 100) // 0-99
|
||||||
{
|
{
|
||||||
dest.push_back(static_cast<char>('0' + n / 10));
|
dest.push_back(static_cast<char>('0' + (n / 10)));
|
||||||
dest.push_back(static_cast<char>('0' + n % 10));
|
dest.push_back(static_cast<char>('0' + (n % 10)));
|
||||||
} else // unlikely, but just in case, let fmt deal with it
|
} else // unlikely, but just in case, let fmt deal with it
|
||||||
{
|
{
|
||||||
fmt_lib::format_to(std::back_inserter(dest), SPDLOG_FMT_STRING("{:02}"), n);
|
fmt_lib::format_to(std::back_inserter(dest), SPDLOG_FMT_STRING("{:02}"), n);
|
||||||
@@ -69,18 +69,18 @@ inline void pad2(int n, memory_buf_t &dest) {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline void pad_uint(T n, unsigned int width, memory_buf_t &dest) {
|
inline void pad_uint(T n, unsigned int width, memory_buf_t &dest) {
|
||||||
static_assert(std::is_unsigned<T>::value, "pad_uint must get unsigned T");
|
static_assert(std::is_unsigned_v<T>, "pad_uint must get unsigned T");
|
||||||
for (auto digits = count_digits(n); digits < width; digits++) {
|
for (auto digits = count_digits(n); digits < width; ++digits) {
|
||||||
dest.push_back('0');
|
dest.push_back('0');
|
||||||
}
|
}
|
||||||
append_int(n, dest);
|
append_int(n, dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline void pad3(T n, memory_buf_t &dest) {
|
void pad3(T n, memory_buf_t &dest) {
|
||||||
static_assert(std::is_unsigned<T>::value, "pad3 must get unsigned T");
|
static_assert(std::is_unsigned_v<T>, "pad3 must get unsigned T");
|
||||||
if (n < 1000) {
|
if (n < 1000) {
|
||||||
dest.push_back(static_cast<char>(n / 100 + '0'));
|
dest.push_back(static_cast<char>((n / 100) + '0'));
|
||||||
n = n % 100;
|
n = n % 100;
|
||||||
dest.push_back(static_cast<char>((n / 10) + '0'));
|
dest.push_back(static_cast<char>((n / 10) + '0'));
|
||||||
dest.push_back(static_cast<char>((n % 10) + '0'));
|
dest.push_back(static_cast<char>((n % 10) + '0'));
|
||||||
@@ -90,12 +90,12 @@ inline void pad3(T n, memory_buf_t &dest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline void pad6(T n, memory_buf_t &dest) {
|
void pad6(T n, memory_buf_t &dest) {
|
||||||
pad_uint(n, 6, dest);
|
pad_uint(n, 6, dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline void pad9(T n, memory_buf_t &dest) {
|
void pad9(T n, memory_buf_t &dest) {
|
||||||
pad_uint(n, 9, dest);
|
pad_uint(n, 9, dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,7 +103,7 @@ inline void pad9(T n, memory_buf_t &dest) {
|
|||||||
// e.g.
|
// e.g.
|
||||||
// fraction<std::milliseconds>(tp) -> will return the millis part of the second
|
// fraction<std::milliseconds>(tp) -> will return the millis part of the second
|
||||||
template <typename ToDuration>
|
template <typename ToDuration>
|
||||||
inline ToDuration time_fraction(log_clock::time_point tp) {
|
ToDuration time_fraction(log_clock::time_point tp) {
|
||||||
using std::chrono::duration_cast;
|
using std::chrono::duration_cast;
|
||||||
using std::chrono::seconds;
|
using std::chrono::seconds;
|
||||||
auto duration = tp.time_since_epoch();
|
auto duration = tp.time_since_epoch();
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
// multi producer-multi consumer blocking queue.
|
// multi producer-multi consumer blocking queue.
|
||||||
// enqueue(..) - will block until room found to put the new message.
|
// enqueue(..) - will block until room found to put the new message.
|
||||||
// enqueue_nowait(..) - will return immediately with false if no room left in
|
// enqueue_nowait(..) - enqueue immediately; overruns oldest message if no
|
||||||
// the queue.
|
// room left.
|
||||||
// dequeue_for(..) - will block until the queue is not empty or timeout have
|
// dequeue_for(..) - will block until the queue is not empty or timeout have
|
||||||
// passed.
|
// passed.
|
||||||
|
|
||||||
@@ -47,20 +47,15 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void enqueue_if_have_room(T &&item) {
|
void enqueue_if_have_room(T &&item) {
|
||||||
bool pushed = false;
|
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(queue_mutex_);
|
std::unique_lock lock(queue_mutex_);
|
||||||
if (!q_.full()) {
|
if (q_.full()) {
|
||||||
q_.push_back(std::move(item));
|
++discard_counter_;
|
||||||
pushed = true;
|
return;
|
||||||
}
|
}
|
||||||
|
q_.push_back(std::move(item));
|
||||||
}
|
}
|
||||||
|
push_cv_.notify_one();
|
||||||
if (pushed) {
|
|
||||||
push_cv_.notify_one();
|
|
||||||
} else {
|
|
||||||
++discard_counter_;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// dequeue with a timeout.
|
// dequeue with a timeout.
|
||||||
|
|||||||
@@ -7,7 +7,6 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
// null, no cost dummy "mutex" and dummy "atomic" log level
|
// null, no cost dummy "mutex" and dummy "atomic" log level
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
struct null_mutex {
|
struct null_mutex {
|
||||||
@@ -17,7 +16,7 @@ struct null_mutex {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct null_atomic {
|
struct null_atomic {
|
||||||
T value;
|
T value{};
|
||||||
|
|
||||||
null_atomic() = default;
|
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
|
// Return file size according to open FILE* object
|
||||||
SPDLOG_API size_t filesize(FILE *f);
|
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());
|
SPDLOG_API int utc_minutes_offset(const std::tm &tm = details::os::localtime());
|
||||||
|
|
||||||
// Return current thread id as size_t
|
// Return current thread id as size_t
|
||||||
|
|||||||
@@ -13,9 +13,13 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/select.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <sys/time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#include <cerrno>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "../common.h"
|
#include "../common.h"
|
||||||
@@ -40,8 +44,67 @@ public:
|
|||||||
|
|
||||||
~tcp_client_unix() { close(); }
|
~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
|
// 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();
|
close();
|
||||||
struct addrinfo hints {};
|
struct addrinfo hints {};
|
||||||
memset(&hints, 0, sizeof(struct addrinfo));
|
memset(&hints, 0, sizeof(struct addrinfo));
|
||||||
@@ -50,6 +113,11 @@ public:
|
|||||||
hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value
|
hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value
|
||||||
hints.ai_protocol = 0;
|
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);
|
auto port_str = std::to_string(port);
|
||||||
struct addrinfo *addrinfo_result;
|
struct addrinfo *addrinfo_result;
|
||||||
auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result);
|
auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result);
|
||||||
@@ -70,8 +138,11 @@ public:
|
|||||||
last_errno = errno;
|
last_errno = errno;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
rv = ::connect(socket_, rp->ai_addr, rp->ai_addrlen);
|
#ifndef SOCK_CLOEXEC
|
||||||
if (rv == 0) {
|
::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;
|
break;
|
||||||
}
|
}
|
||||||
last_errno = errno;
|
last_errno = errno;
|
||||||
@@ -83,6 +154,11 @@ public:
|
|||||||
throw_spdlog_ex("::connect failed", last_errno);
|
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
|
// set TCP_NODELAY
|
||||||
int enable_flag = 1;
|
int enable_flag = 1;
|
||||||
::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&enable_flag), sizeof(enable_flag));
|
::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&enable_flag), sizeof(enable_flag));
|
||||||
|
|||||||
@@ -58,8 +58,76 @@ public:
|
|||||||
|
|
||||||
SOCKET fd() const { return socket_; }
|
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
|
// 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()) {
|
if (is_connected()) {
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
@@ -71,13 +139,17 @@ public:
|
|||||||
hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value
|
hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value
|
||||||
hints.ai_protocol = 0;
|
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);
|
auto port_str = std::to_string(port);
|
||||||
struct addrinfo *addrinfo_result;
|
struct addrinfo *addrinfo_result;
|
||||||
auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result);
|
auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result);
|
||||||
int last_error = 0;
|
int last_error = 0;
|
||||||
if (rv != 0) {
|
if (rv != 0) {
|
||||||
last_error = ::WSAGetLastError();
|
last_error = ::WSAGetLastError();
|
||||||
WSACleanup();
|
|
||||||
throw_winsock_error_("getaddrinfo failed", last_error);
|
throw_winsock_error_("getaddrinfo failed", last_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,21 +159,25 @@ public:
|
|||||||
socket_ = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
socket_ = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||||
if (socket_ == INVALID_SOCKET) {
|
if (socket_ == INVALID_SOCKET) {
|
||||||
last_error = ::WSAGetLastError();
|
last_error = ::WSAGetLastError();
|
||||||
WSACleanup();
|
|
||||||
continue;
|
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;
|
break;
|
||||||
} else {
|
|
||||||
last_error = ::WSAGetLastError();
|
|
||||||
close();
|
|
||||||
}
|
}
|
||||||
|
last_error = WSAGetLastError();
|
||||||
|
::closesocket(socket_);
|
||||||
|
socket_ = INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
::freeaddrinfo(addrinfo_result);
|
::freeaddrinfo(addrinfo_result);
|
||||||
if (socket_ == INVALID_SOCKET) {
|
if (socket_ == INVALID_SOCKET) {
|
||||||
WSACleanup();
|
|
||||||
throw_winsock_error_("connect failed", last_error);
|
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
|
// set TCP_NODELAY
|
||||||
int enable_flag = 1;
|
int enable_flag = 1;
|
||||||
|
|||||||
@@ -70,9 +70,9 @@ public:
|
|||||||
// Send exactly n_bytes of the given data.
|
// Send exactly n_bytes of the given data.
|
||||||
// On error close the connection and throw.
|
// On error close the connection and throw.
|
||||||
void send(const char *data, size_t n_bytes) {
|
void send(const char *data, size_t n_bytes) {
|
||||||
ssize_t toslen = 0;
|
socklen_t tolen = sizeof(sockAddr_);
|
||||||
socklen_t tolen = sizeof(struct sockaddr);
|
if (::sendto(socket_, data, n_bytes, 0, reinterpret_cast<const sockaddr *>(&sockAddr_),
|
||||||
if ((toslen = ::sendto(socket_, data, n_bytes, 0, (struct sockaddr *)&sockAddr_, tolen)) == -1) {
|
tolen) == -1) {
|
||||||
throw_spdlog_ex("sendto(2) failed", errno);
|
throw_spdlog_ex("sendto(2) failed", errno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ public:
|
|||||||
logger(std::string name, sinks_init_list sinks)
|
logger(std::string name, sinks_init_list sinks)
|
||||||
: logger(std::move(name), sinks.begin(), sinks.end()) {}
|
: logger(std::move(name), sinks.begin(), sinks.end()) {}
|
||||||
|
|
||||||
logger(const logger &other) ;
|
logger(const logger &other);
|
||||||
logger(logger &&other) noexcept;
|
logger(logger &&other) noexcept;
|
||||||
|
|
||||||
~logger() = default;
|
~logger() = default;
|
||||||
@@ -123,7 +123,7 @@ public:
|
|||||||
|
|
||||||
// return true if the given message should be flushed
|
// return true if the given message should be flushed
|
||||||
[[nodiscard]] bool should_flush(const details::log_msg &msg) const noexcept {
|
[[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
|
// set the level of logging
|
||||||
@@ -200,7 +200,6 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (should_flush(msg)) {
|
if (should_flush(msg)) {
|
||||||
flush_();
|
flush_();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,15 +69,19 @@ public:
|
|||||||
static constexpr std::string_view red_bold = "\033[31m\033[1m";
|
static constexpr std::string_view red_bold = "\033[31m\033[1m";
|
||||||
static constexpr std::string_view bold_on_red = "\033[1m\033[41m";
|
static constexpr std::string_view bold_on_red = "\033[1m\033[41m";
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
void sink_it_(const details::log_msg &msg) override;
|
|
||||||
void flush_() override;
|
|
||||||
FILE *target_file_;
|
FILE *target_file_;
|
||||||
|
|
||||||
|
private:
|
||||||
bool should_do_colors_;
|
bool should_do_colors_;
|
||||||
std::array<std::string, levels_count> colors_;
|
std::array<std::string, levels_count> colors_;
|
||||||
void print_ccode_(const string_view_t color_code);
|
|
||||||
void print_range_(const memory_buf_t &formatted, size_t start, size_t end);
|
void sink_it_(const details::log_msg &msg) override;
|
||||||
static std::string to_string_(const string_view_t sv);
|
void flush_() override;
|
||||||
|
void set_color_mode_(color_mode mode);
|
||||||
|
void print_ccode_(string_view_t color_code) const;
|
||||||
|
void print_range_(const memory_buf_t &formatted, size_t start, size_t end) const;
|
||||||
|
static std::string to_string_(string_view_t sv);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
|
|||||||
@@ -31,7 +31,8 @@ public:
|
|||||||
discard_new // Discard the log message if the queue is full
|
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 {
|
struct config {
|
||||||
size_t queue_size = default_queue_size;
|
size_t queue_size = default_queue_size;
|
||||||
@@ -43,8 +44,46 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
explicit async_sink(config async_config);
|
explicit async_sink(config async_config);
|
||||||
|
async_sink(const async_sink &) = delete;
|
||||||
|
async_sink &operator=(const async_sink &) = delete;
|
||||||
|
async_sink(async_sink &&) = delete;
|
||||||
|
async_sink &operator=(async_sink &&) = delete;
|
||||||
|
~async_sink() override;
|
||||||
|
|
||||||
// create an async_sink with one backend sink
|
// sink interface implementation
|
||||||
|
void log(const details::log_msg &msg) override;
|
||||||
|
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_all() after flush() or destruct the sink
|
||||||
|
void flush() override;
|
||||||
|
|
||||||
|
// non sink interface methods
|
||||||
|
|
||||||
|
// 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
|
||||||
|
void wait_all() const;
|
||||||
|
|
||||||
|
// return the number of overrun messages (effective only if policy is overrun_oldest)
|
||||||
|
[[nodiscard]] size_t get_overrun_counter() const;
|
||||||
|
|
||||||
|
// reset the overrun counter
|
||||||
|
void reset_overrun_counter() const;
|
||||||
|
|
||||||
|
// return the number of discarded messages (effective only if policy is discard_new)
|
||||||
|
[[nodiscard]] size_t get_discard_counter() const;
|
||||||
|
|
||||||
|
// reset the discard counter
|
||||||
|
void reset_discard_counter() const;
|
||||||
|
|
||||||
|
// return the current async_sink configuration
|
||||||
|
[[nodiscard]] const config &get_config() const;
|
||||||
|
|
||||||
|
// create an async_sink with one backend sink constructed with the given args.
|
||||||
|
// example:
|
||||||
|
// auto async_file = async_sink::with<spdlog::sinks::basic_file_sink_st>("mylog.txt");
|
||||||
template <typename Sink, typename... SinkArgs>
|
template <typename Sink, typename... SinkArgs>
|
||||||
static std::shared_ptr<async_sink> with(SinkArgs &&...sink_args) {
|
static std::shared_ptr<async_sink> with(SinkArgs &&...sink_args) {
|
||||||
config cfg{};
|
config cfg{};
|
||||||
@@ -52,34 +91,20 @@ public:
|
|||||||
return std::make_shared<async_sink>(cfg);
|
return std::make_shared<async_sink>(cfg);
|
||||||
}
|
}
|
||||||
|
|
||||||
~async_sink() override;
|
|
||||||
|
|
||||||
// sink interface implementation
|
|
||||||
void log(const details::log_msg &msg) override;
|
|
||||||
void flush() override;
|
|
||||||
void set_pattern(const std::string &pattern) override;
|
|
||||||
void set_formatter(std::unique_ptr<formatter> sink_formatter) override;
|
|
||||||
|
|
||||||
// async sink specific methods
|
|
||||||
[[nodiscard]] size_t get_overrun_counter() const;
|
|
||||||
void reset_overrun_counter() const;
|
|
||||||
[[nodiscard]] size_t get_discard_counter() const;
|
|
||||||
void reset_discard_counter() const;
|
|
||||||
[[nodiscard]] const config &get_config() const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using async_log_msg = details::async_log_msg;
|
using async_log_msg = details::async_log_msg;
|
||||||
using queue_t = details::mpmc_blocking_queue<async_log_msg>;
|
using queue_t = details::mpmc_blocking_queue<async_log_msg>;
|
||||||
|
|
||||||
void send_message_(async_log_msg::type msg_type, const details::log_msg &msg) const;
|
void enqueue_message_(details::async_log_msg &&msg) const;
|
||||||
void backend_loop_();
|
void backend_loop_();
|
||||||
void backend_log_(const details::log_msg &msg) ;
|
void backend_log_(const details::log_msg &msg);
|
||||||
void backend_flush_();
|
void backend_flush_();
|
||||||
|
|
||||||
config config_;
|
config config_;
|
||||||
std::unique_ptr<queue_t> q_;
|
std::unique_ptr<queue_t> q_;
|
||||||
std::thread worker_thread_;
|
std::thread worker_thread_;
|
||||||
details::err_helper err_helper_;
|
details::err_helper err_helper_;
|
||||||
|
std::atomic_bool terminate_worker_ = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace sinks
|
} // namespace sinks
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
// sink formatter
|
// sink formatter
|
||||||
std::unique_ptr<spdlog::formatter> formatter_;
|
std::unique_ptr<spdlog::formatter> formatter_;
|
||||||
Mutex mutex_;
|
mutable Mutex mutex_;
|
||||||
|
|
||||||
virtual void sink_it_(const details::log_msg &msg) = 0;
|
virtual void sink_it_(const details::log_msg &msg) = 0;
|
||||||
virtual void flush_() = 0;
|
virtual void flush_() = 0;
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ class basic_file_sink final : public base_sink<Mutex> {
|
|||||||
public:
|
public:
|
||||||
explicit basic_file_sink(const filename_t &filename, bool truncate = false, const file_event_handlers &event_handlers = {});
|
explicit basic_file_sink(const filename_t &filename, bool truncate = false, const file_event_handlers &event_handlers = {});
|
||||||
const filename_t &filename() const;
|
const filename_t &filename() const;
|
||||||
|
void truncate();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void sink_it_(const details::log_msg &msg) override;
|
void sink_it_(const details::log_msg &msg) override;
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ struct daily_filename_format_calculator {
|
|||||||
* Rotating file sink based on date.
|
* Rotating file sink based on date.
|
||||||
* If truncate != false , the created file will be truncated.
|
* 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.
|
||||||
* 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,
|
* 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.
|
* rotation and deletion is only applied while the program is running.
|
||||||
*/
|
*/
|
||||||
@@ -81,8 +80,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto now = log_clock::now();
|
auto now = log_clock::now();
|
||||||
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
|
const auto new_filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
|
||||||
file_helper_.open(filename, truncate_);
|
file_helper_.open(new_filename, truncate_);
|
||||||
rotation_tp_ = next_rotation_tp_();
|
rotation_tp_ = next_rotation_tp_();
|
||||||
|
|
||||||
if (max_files_ > 0) {
|
if (max_files_ > 0) {
|
||||||
@@ -100,8 +99,8 @@ protected:
|
|||||||
auto time = msg.time;
|
auto time = msg.time;
|
||||||
bool should_rotate = time >= rotation_tp_;
|
bool should_rotate = time >= rotation_tp_;
|
||||||
if (should_rotate) {
|
if (should_rotate) {
|
||||||
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time));
|
const auto new_filename = FileNameCalc::calc_filename(base_filename_, now_tm(time));
|
||||||
file_helper_.open(filename, truncate_);
|
file_helper_.open(new_filename, truncate_);
|
||||||
rotation_tp_ = next_rotation_tp_();
|
rotation_tp_ = next_rotation_tp_();
|
||||||
}
|
}
|
||||||
memory_buf_t formatted;
|
memory_buf_t formatted;
|
||||||
@@ -124,11 +123,11 @@ private:
|
|||||||
std::vector<filename_t> filenames;
|
std::vector<filename_t> filenames;
|
||||||
auto now = log_clock::now();
|
auto now = log_clock::now();
|
||||||
while (filenames.size() < max_files_) {
|
while (filenames.size() < max_files_) {
|
||||||
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
|
const auto new_filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
|
||||||
if (!path_exists(filename)) {
|
if (!path_exists(new_filename)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
filenames.emplace_back(filename);
|
filenames.emplace_back(new_filename);
|
||||||
now -= std::chrono::hours(24);
|
now -= std::chrono::hours(24);
|
||||||
}
|
}
|
||||||
for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter) {
|
for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter) {
|
||||||
|
|||||||
@@ -5,8 +5,10 @@
|
|||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "../details/log_msg.h"
|
#include "../details/log_msg.h"
|
||||||
#include "../details/null_mutex.h"
|
#include "../details/null_mutex.h"
|
||||||
@@ -20,8 +22,8 @@
|
|||||||
// #include "spdlog/sinks/dup_filter_sink.h"
|
// #include "spdlog/sinks/dup_filter_sink.h"
|
||||||
//
|
//
|
||||||
// int main() {
|
// int main() {
|
||||||
// auto dup_filter = std::make_shared<dup_filter_sink_st>(std::chrono::seconds(5),
|
// 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>());
|
// dup_filter->add_sink(std::make_shared<stdout_color_sink_mt>());
|
||||||
// spdlog::logger l("logger", dup_filter);
|
// spdlog::logger l("logger", dup_filter);
|
||||||
// l.info("Hello");
|
// l.info("Hello");
|
||||||
// l.info("Hello");
|
// l.info("Hello");
|
||||||
@@ -40,21 +42,39 @@ template <typename Mutex>
|
|||||||
class dup_filter_sink final : public dist_sink<Mutex> {
|
class dup_filter_sink final : public dist_sink<Mutex> {
|
||||||
public:
|
public:
|
||||||
template <class Rep, class Period>
|
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},
|
: 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:
|
protected:
|
||||||
std::chrono::microseconds max_skip_duration_;
|
std::chrono::microseconds max_skip_duration_;
|
||||||
log_clock::time_point last_msg_time_;
|
log_clock::time_point last_msg_time_;
|
||||||
std::string last_msg_payload_;
|
std::string last_msg_payload_;
|
||||||
size_t skip_counter_ = 0;
|
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 {
|
void sink_it_(const details::log_msg &msg) override {
|
||||||
bool filtered = filter_(msg);
|
bool filtered = filter_(msg);
|
||||||
if (!filtered) {
|
if (!filtered) {
|
||||||
skip_counter_ += 1;
|
skip_counter_ += 1;
|
||||||
|
if (!use_fixed_notification_summary_level_) {
|
||||||
|
skipped_msg_log_level_ = msg.log_level;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +83,10 @@ protected:
|
|||||||
char buf[64];
|
char buf[64];
|
||||||
auto msg_size = ::snprintf(buf, sizeof(buf), "Skipped %u duplicate messages..", static_cast<unsigned>(skip_counter_));
|
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)) {
|
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)}};
|
string_view_t{buf, static_cast<size_t>(msg_size)}};
|
||||||
dist_sink<Mutex>::sink_it_(skipped_msg);
|
dist_sink<Mutex>::sink_it_(skipped_msg);
|
||||||
}
|
}
|
||||||
@@ -77,8 +100,8 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// return whether the log msg should be displayed (true) or skipped (false)
|
// return whether the log msg should be displayed (true) or skipped (false)
|
||||||
bool filter_(const details::log_msg &msg) {
|
bool filter_(const details::log_msg &msg) const {
|
||||||
auto filter_duration = msg.time - last_msg_time_;
|
const auto filter_duration = msg.time - last_msg_time_;
|
||||||
return (filter_duration > max_skip_duration_) || (msg.payload != last_msg_payload_);
|
return (filter_duration > max_skip_duration_) || (msg.payload != last_msg_payload_);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -16,11 +16,11 @@
|
|||||||
#include <mongocxx/client.hpp>
|
#include <mongocxx/client.hpp>
|
||||||
#include <mongocxx/instance.hpp>
|
#include <mongocxx/instance.hpp>
|
||||||
#include <mongocxx/uri.hpp>
|
#include <mongocxx/uri.hpp>
|
||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include "../details/null_mutex.h"
|
|
||||||
#include "../common.h"
|
#include "../common.h"
|
||||||
#include "../details/log_msg.h"
|
#include "../details/log_msg.h"
|
||||||
|
#include "../details/null_mutex.h"
|
||||||
#include "./base_sink.h"
|
#include "./base_sink.h"
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
#include "../details/null_mutex.h"
|
#include "../details/null_mutex.h"
|
||||||
#include "./base_sink.h"
|
#include "./base_sink.h"
|
||||||
|
|
||||||
|
|||||||
@@ -154,8 +154,10 @@ protected:
|
|||||||
payload = QString::fromUtf8(str.data(), static_cast<int>(str.size()));
|
payload = QString::fromUtf8(str.data(), static_cast<int>(str.size()));
|
||||||
// convert color ranges from byte index to character index.
|
// convert color ranges from byte index to character index.
|
||||||
if (msg.color_range_start < msg.color_range_end) {
|
if (msg.color_range_start < msg.color_range_end) {
|
||||||
color_range_start = QString::fromUtf8(str.data(), msg.color_range_start).size();
|
color_range_start =
|
||||||
color_range_end = QString::fromUtf8(str.data(), msg.color_range_end).size();
|
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 {
|
} else {
|
||||||
payload = QString::fromLatin1(str.data(), static_cast<int>(str.size()));
|
payload = QString::fromLatin1(str.data(), static_cast<int>(str.size()));
|
||||||
@@ -165,7 +167,7 @@ protected:
|
|||||||
qt_text_edit_, // text edit to append to
|
qt_text_edit_, // text edit to append to
|
||||||
std::move(payload), // text to append
|
std::move(payload), // text to append
|
||||||
default_color_, // default color
|
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_start, // color range start
|
||||||
color_range_end}; // color range end
|
color_range_end}; // color range end
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,11 @@ template <typename Mutex>
|
|||||||
class ringbuffer_sink final : public base_sink<Mutex> {
|
class ringbuffer_sink final : public base_sink<Mutex> {
|
||||||
public:
|
public:
|
||||||
explicit ringbuffer_sink(size_t n_items)
|
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) {
|
void drain_raw(std::function<void(const details::async_log_msg &)> callback) {
|
||||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
@@ -46,7 +50,9 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void sink_it_(const details::log_msg &msg) override { q_.push_back(details::async_log_msg{details::async_log_msg::type::log, msg}); }
|
void sink_it_(const details::log_msg &msg) override {
|
||||||
|
q_.push_back(details::async_log_msg{details::async_log_msg::type::log, msg});
|
||||||
|
}
|
||||||
void flush_() override {}
|
void flush_() override {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -17,8 +17,8 @@ public:
|
|||||||
virtual void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) = 0;
|
virtual void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) = 0;
|
||||||
|
|
||||||
void set_level(level level) { level_.store(level, std::memory_order_relaxed); }
|
void set_level(level level) { level_.store(level, std::memory_order_relaxed); }
|
||||||
level log_level() const { return level_.load(std::memory_order_relaxed);}
|
level log_level() const { return level_.load(std::memory_order_relaxed); }
|
||||||
bool should_log(level msg_level) const {return msg_level >= level_.load(std::memory_order_relaxed);}
|
bool should_log(level msg_level) const { return msg_level >= level_.load(std::memory_order_relaxed); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// sink log level - default is all
|
// sink log level - default is all
|
||||||
|
|||||||
@@ -8,8 +8,8 @@
|
|||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <string>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
@@ -19,7 +19,7 @@ namespace sinks {
|
|||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
class syslog_sink final : public base_sink<Mutex> {
|
class syslog_sink final : public base_sink<Mutex> {
|
||||||
public:
|
public:
|
||||||
syslog_sink(std::string ident = "", int syslog_option = 0, int syslog_facility = LOG_USER, bool enable_formatting=false)
|
syslog_sink(std::string ident = "", int syslog_option = 0, int syslog_facility = LOG_USER, bool enable_formatting = false)
|
||||||
: enable_formatting_{enable_formatting},
|
: enable_formatting_{enable_formatting},
|
||||||
syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
|
syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
|
||||||
/* spdlog::level::debug */ LOG_DEBUG,
|
/* spdlog::level::debug */ LOG_DEBUG,
|
||||||
|
|||||||
@@ -17,8 +17,6 @@
|
|||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
// Simple tcp client sink
|
// Simple tcp client sink
|
||||||
// Connects to remote address and send the formatted log.
|
// Connects to remote address and send the formatted log.
|
||||||
// Will attempt to reconnect if connection drops.
|
// Will attempt to reconnect if connection drops.
|
||||||
@@ -31,6 +29,7 @@ namespace sinks {
|
|||||||
struct tcp_sink_config {
|
struct tcp_sink_config {
|
||||||
std::string server_host;
|
std::string server_host;
|
||||||
int server_port;
|
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
|
bool lazy_connect = false; // if true connect on first log call instead of on construction
|
||||||
|
|
||||||
tcp_sink_config(std::string host, int port)
|
tcp_sink_config(std::string host, int port)
|
||||||
@@ -44,10 +43,22 @@ public:
|
|||||||
// connect to tcp host/port or throw if failed
|
// connect to tcp host/port or throw if failed
|
||||||
// host can be hostname or ip address
|
// 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)
|
explicit tcp_sink(tcp_sink_config sink_config)
|
||||||
: config_{std::move(sink_config)} {
|
: config_{std::move(sink_config)} {
|
||||||
if (!config_.lazy_connect) {
|
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::memory_buf_t formatted;
|
||||||
spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
|
spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||||
if (!client_.is_connected()) {
|
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());
|
client_.send(formatted.data(), formatted.size());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ template <typename Mutex>
|
|||||||
class udp_sink final : public base_sink<Mutex> {
|
class udp_sink final : public base_sink<Mutex> {
|
||||||
public:
|
public:
|
||||||
// host can be hostname or ip address
|
// 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} {}
|
: client_{sink_config.server_host, sink_config.server_port} {}
|
||||||
|
|
||||||
~udp_sink() override = default;
|
~udp_sink() override = default;
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ SPDLOG_API level get_level();
|
|||||||
SPDLOG_API void set_level(level level);
|
SPDLOG_API void set_level(level level);
|
||||||
|
|
||||||
// Determine whether the global logger should log messages with a certain 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.
|
// Set flush level of the global logger.
|
||||||
SPDLOG_API void flush_on(level level);
|
SPDLOG_API void flush_on(level level);
|
||||||
|
|||||||
@@ -4,19 +4,41 @@
|
|||||||
#include "spdlog/common.h"
|
#include "spdlog/common.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
bool iequals(const std::string &a, const std::string &b) {
|
||||||
|
return a.size() == b.size() &&
|
||||||
|
std::equal(a.begin(), a.end(), b.begin(), [](char ac, char bc) {
|
||||||
|
return std::tolower(static_cast<unsigned char>(ac)) ==
|
||||||
|
std::tolower(static_cast<unsigned char>(bc));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
spdlog::level level_from_str(const std::string &name) noexcept {
|
spdlog::level level_from_str(const std::string &name) noexcept {
|
||||||
const auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name);
|
const auto it =
|
||||||
if (it != std::end(level_string_views)) return static_cast<level>(std::distance(std::begin(level_string_views), it));
|
std::find_if(std::begin(level_string_views), std::end(level_string_views),
|
||||||
|
[&name](const string_view_t &level_name) {
|
||||||
|
return level_name.size() == name.size() &&
|
||||||
|
std::equal(name.begin(), name.end(), level_name.begin(),
|
||||||
|
[](char a, char b) {
|
||||||
|
return std::tolower(static_cast<unsigned char>(a)) ==
|
||||||
|
std::tolower(static_cast<unsigned char>(b));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
if (it != std::end(level_string_views)) {
|
||||||
|
return static_cast<level>(std::distance(std::begin(level_string_views), it));
|
||||||
|
}
|
||||||
|
|
||||||
// check also for "warn" and "err" before giving up
|
// check also for "warn" and "err" before giving up
|
||||||
if (name == "warn") {
|
if (iequals(name, "warn")) {
|
||||||
return spdlog::level::warn;
|
return spdlog::level::warn;
|
||||||
}
|
}
|
||||||
if (name == "err") {
|
if (iequals(name, "err")) {
|
||||||
return level::err;
|
return level::err;
|
||||||
}
|
}
|
||||||
return level::off;
|
return level::off;
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
|
|
||||||
|
|
||||||
async_log_msg::async_log_msg(const type type)
|
async_log_msg::async_log_msg(const type type)
|
||||||
: msg_type_{type} {}
|
: msg_type_{type} {}
|
||||||
|
|
||||||
@@ -15,21 +14,25 @@ async_log_msg::async_log_msg(const type type)
|
|||||||
// are compiler generated const chars* (__FILE__, __LINE__, __FUNCTION__)
|
// are compiler generated const chars* (__FILE__, __LINE__, __FUNCTION__)
|
||||||
// if you pass custom strings to source location, make sure they outlive the async_log_msg
|
// if you pass custom strings to source location, make sure they outlive the async_log_msg
|
||||||
async_log_msg::async_log_msg(const type type, const log_msg &orig_msg)
|
async_log_msg::async_log_msg(const type type, const log_msg &orig_msg)
|
||||||
: log_msg{orig_msg}, msg_type_(type) {
|
: log_msg{orig_msg},
|
||||||
|
msg_type_(type) {
|
||||||
buffer_.append(logger_name);
|
buffer_.append(logger_name);
|
||||||
buffer_.append(payload);
|
buffer_.append(payload);
|
||||||
update_string_views();
|
update_string_views();
|
||||||
}
|
}
|
||||||
|
|
||||||
async_log_msg::async_log_msg(const async_log_msg &other)
|
async_log_msg::async_log_msg(const async_log_msg &other)
|
||||||
: log_msg{other}, msg_type_{other.msg_type_} {
|
: log_msg{other},
|
||||||
|
msg_type_{other.msg_type_} {
|
||||||
buffer_.append(logger_name);
|
buffer_.append(logger_name);
|
||||||
buffer_.append(payload);
|
buffer_.append(payload);
|
||||||
update_string_views();
|
update_string_views();
|
||||||
}
|
}
|
||||||
|
|
||||||
async_log_msg::async_log_msg(async_log_msg &&other) noexcept
|
async_log_msg::async_log_msg(async_log_msg &&other) noexcept
|
||||||
: log_msg{other}, msg_type_{other.msg_type_}, buffer_{std::move(other.buffer_)} {
|
: log_msg{other},
|
||||||
|
msg_type_{other.msg_type_},
|
||||||
|
buffer_{std::move(other.buffer_)} {
|
||||||
update_string_views();
|
update_string_views();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,23 +3,42 @@
|
|||||||
|
|
||||||
#include "spdlog/details/err_helper.h"
|
#include "spdlog/details/err_helper.h"
|
||||||
|
|
||||||
#include "iostream"
|
|
||||||
#include "spdlog/details/os.h"
|
#include "spdlog/details/os.h"
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
|
|
||||||
|
err_helper::err_helper(const err_helper &other) {
|
||||||
|
std::lock_guard lock(other.mutex_);
|
||||||
|
custom_err_handler_ = other.custom_err_handler_;
|
||||||
|
last_report_time_ = other.last_report_time_;
|
||||||
|
}
|
||||||
|
|
||||||
|
err_helper::err_helper(err_helper &&other) noexcept {
|
||||||
|
custom_err_handler_ = std::move(other.custom_err_handler_);
|
||||||
|
last_report_time_ = std::move(other.last_report_time_);
|
||||||
|
}
|
||||||
|
|
||||||
// Prints error to stderr with source location (if available). A stderr sink is not used because reaching
|
// Prints error to stderr with source location (if available). A stderr sink is not used because reaching
|
||||||
// this point might indicate a problem with the logging system itself so we use fputs() directly.
|
// this point might indicate a problem with the logging system itself so we use fputs() directly.
|
||||||
void err_helper::handle_ex(const std::string &origin, const source_loc &loc, const std::exception &ex) const noexcept {
|
void err_helper::handle_ex(const std::string &origin, const source_loc &loc, const std::exception &ex) noexcept {
|
||||||
|
std::lock_guard lock(mutex_);
|
||||||
try {
|
try {
|
||||||
if (custom_err_handler_) {
|
if (custom_err_handler_) {
|
||||||
custom_err_handler_(ex.what());
|
custom_err_handler_(ex.what());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto now = std::chrono::steady_clock::now();
|
||||||
|
if (now - last_report_time_ < std::chrono::seconds(1)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
last_report_time_ = now;
|
||||||
const auto tm_time = os::localtime();
|
const auto tm_time = os::localtime();
|
||||||
char date_buf[32];
|
char date_buf[64];
|
||||||
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
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;
|
std::string msg;
|
||||||
if (loc.empty()) {
|
if (loc.empty()) {
|
||||||
msg = fmt_lib::format("[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, origin, ex.what());
|
msg = fmt_lib::format("[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, origin, ex.what());
|
||||||
@@ -29,17 +48,21 @@ void err_helper::handle_ex(const std::string &origin, const source_loc &loc, con
|
|||||||
}
|
}
|
||||||
std::fputs(msg.c_str(), stderr);
|
std::fputs(msg.c_str(), stderr);
|
||||||
} catch (const std::exception &handler_ex) {
|
} catch (const std::exception &handler_ex) {
|
||||||
std::fprintf(stderr, "[*** LOG ERROR ***] [%s] caught exception during error handler: %s\n", origin.c_str(), handler_ex.what());
|
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
|
} 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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void err_helper::handle_unknown_ex(const std::string &origin, const source_loc &loc) const noexcept {
|
void err_helper::handle_unknown_ex(const std::string &origin, const source_loc &loc) noexcept {
|
||||||
handle_ex(origin, loc, std::runtime_error("unknown exception"));
|
handle_ex(origin, loc, std::runtime_error("unknown exception"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void err_helper::set_err_handler(err_handler handler) { custom_err_handler_ = std::move(handler); }
|
void err_helper::set_err_handler(err_handler handler) {
|
||||||
|
std::lock_guard lock(mutex_);
|
||||||
|
custom_err_handler_ = std::move(handler);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace details
|
} // namespace details
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|||||||
@@ -127,10 +127,16 @@ size_t filesize(FILE *f) {
|
|||||||
return 0; // will not be reached.
|
return 0; // will not be reached.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return utc offset in minutes or throw spdlog_ex on failure
|
// Return utc offset in minutes (0 on failure to compute offset)
|
||||||
int utc_minutes_offset(const std::tm &tm) {
|
int utc_minutes_offset(const std::tm &tm) {
|
||||||
#if defined(sun) || defined(__sun) || defined(_AIX) || (defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
|
#if defined(SPDLOG_NO_TZ_OFFSET)
|
||||||
(!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE))
|
(void)tm;
|
||||||
|
return 0;
|
||||||
|
#else
|
||||||
|
#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
|
// 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
|
||||||
struct helper {
|
struct helper {
|
||||||
static long int calculate_gmt_offset(const std::tm &localtm = details::os::localtime(),
|
static long int calculate_gmt_offset(const std::tm &localtm = details::os::localtime(),
|
||||||
@@ -163,6 +169,7 @@ int utc_minutes_offset(const std::tm &tm) {
|
|||||||
auto offset_seconds = tm.tm_gmtoff;
|
auto offset_seconds = tm.tm_gmtoff;
|
||||||
#endif
|
#endif
|
||||||
return static_cast<int>(offset_seconds / 60);
|
return static_cast<int>(offset_seconds / 60);
|
||||||
|
#endif // SPDLOG_NO_TZ_OFFSET
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return current thread id as size_t
|
// Return current thread id as size_t
|
||||||
@@ -267,8 +274,8 @@ bool is_color_terminal() noexcept {
|
|||||||
bool in_terminal(FILE *file) noexcept { return ::isatty(fileno(file)) != 0; }
|
bool in_terminal(FILE *file) noexcept { return ::isatty(fileno(file)) != 0; }
|
||||||
|
|
||||||
std::string getenv(const char *field) {
|
std::string getenv(const char *field) {
|
||||||
char *buf = ::getenv(field);
|
char *buf = std::getenv(field);
|
||||||
return buf != nullptr ? buf : std::string{};
|
return buf != nullptr ? std::string(buf) : std::string{};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do fsync by FILE pointer
|
// Do fsync by FILE pointer
|
||||||
|
|||||||
@@ -6,10 +6,9 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
#include "spdlog/details/windows_include.h" // must be included before fileapi.h etc.
|
#include "spdlog/details/windows_include.h" // must be first; provides FlushFileBuffers via Windows headers
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
#include <fileapi.h> // for FlushFileBuffers
|
|
||||||
#include <io.h> // for _get_osfhandle, _isatty, _fileno
|
#include <io.h> // for _get_osfhandle, _isatty, _fileno
|
||||||
#include <process.h> // for _get_pid
|
#include <process.h> // for _get_pid
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
@@ -107,24 +106,27 @@ size_t filesize(FILE *f) {
|
|||||||
#pragma warning(pop)
|
#pragma warning(pop)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Return utc offset in minutes or throw spdlog_ex on failure
|
// Compare the timestamp as local (mktime) vs UTC (_mkgmtime) to get the offset.
|
||||||
|
// Matches v1.x behavior: better historical DST handling than GetTimeZoneInformation alone.
|
||||||
int utc_minutes_offset(const std::tm &tm) {
|
int utc_minutes_offset(const std::tm &tm) {
|
||||||
#if _WIN32_WINNT < _WIN32_WINNT_WS08
|
#if defined(SPDLOG_NO_TZ_OFFSET)
|
||||||
TIME_ZONE_INFORMATION tzinfo;
|
(void)tm;
|
||||||
auto rv = ::GetTimeZoneInformation(&tzinfo);
|
return 0;
|
||||||
#else
|
#else
|
||||||
DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
|
std::tm local_tm = tm; // copy since mktime might adjust it (normalize dates, set tm_isdst)
|
||||||
auto rv = ::GetDynamicTimeZoneInformation(&tzinfo);
|
std::time_t local_time_t = std::mktime(&local_tm);
|
||||||
#endif
|
if (local_time_t == static_cast<std::time_t>(-1)) {
|
||||||
if (rv == TIME_ZONE_ID_INVALID) throw_spdlog_ex("Failed getting timezone info. ", errno);
|
return 0; // fallback
|
||||||
|
|
||||||
int offset = -tzinfo.Bias;
|
|
||||||
if (tm.tm_isdst) {
|
|
||||||
offset -= tzinfo.DaylightBias;
|
|
||||||
} else {
|
|
||||||
offset -= tzinfo.StandardBias;
|
|
||||||
}
|
}
|
||||||
return offset;
|
|
||||||
|
std::time_t utc_time_t = _mkgmtime(&local_tm);
|
||||||
|
if (utc_time_t == static_cast<std::time_t>(-1)) {
|
||||||
|
return 0; // fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto offset_seconds = utc_time_t - local_time_t;
|
||||||
|
return static_cast<int>(offset_seconds / 60);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return current thread id as size_t
|
// Return current thread id as size_t
|
||||||
@@ -212,7 +214,7 @@ void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) {
|
|||||||
target.resize(result_size);
|
target.resize(result_size);
|
||||||
result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, target.data(), result_size);
|
result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, target.data(), result_size);
|
||||||
if (result_size > 0) {
|
if (result_size > 0) {
|
||||||
assert(result_size == target.size());
|
assert(result_size == static_cast<int>(target.size()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -220,21 +222,22 @@ void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) {
|
|||||||
throw_spdlog_ex(fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError()));
|
throw_spdlog_ex(fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(push)
|
||||||
|
#pragma warning(disable : 4996)
|
||||||
|
#endif
|
||||||
std::string getenv(const char *field) {
|
std::string getenv(const char *field) {
|
||||||
#if defined(_MSC_VER)
|
#if defined(_MSC_VER) && defined(WINAPI_FAMILY) && defined(WINAPI_FAMILY_DESKTOP_APP) && \
|
||||||
#if defined(__cplusplus_winrt)
|
(WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP)
|
||||||
return std::string{}; // not supported under uwp
|
return std::string{}; // not supported on UWP / non-desktop WinRT targets (#3489)
|
||||||
#else
|
#else
|
||||||
size_t len = 0;
|
char *buf = std::getenv(field);
|
||||||
char buf[128];
|
return buf != nullptr ? std::string(buf) : std::string{};
|
||||||
bool ok = ::getenv_s(&len, buf, sizeof(buf), field) == 0;
|
|
||||||
return ok ? buf : std::string{};
|
|
||||||
#endif
|
|
||||||
#else // revert to getenv
|
|
||||||
char *buf = ::getenv(field);
|
|
||||||
return buf != nullptr ? buf : std::string{};
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
|
||||||
// Do fsync by FILE handlerpointer
|
// Do fsync by FILE handlerpointer
|
||||||
// Return true on success
|
// Return true on success
|
||||||
|
|||||||
@@ -60,6 +60,9 @@ public:
|
|||||||
pad_it(remaining_pad_);
|
pad_it(remaining_pad_);
|
||||||
} else if (padinfo_.truncate_) {
|
} else if (padinfo_.truncate_) {
|
||||||
long new_size = static_cast<long>(dest_.size()) + remaining_pad_;
|
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));
|
dest_.resize(static_cast<size_t>(new_size));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -123,9 +126,8 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
// Date time pattern appenders
|
// Date time pattern formatters
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
static const char *ampm(const tm &t) { return t.tm_hour >= 12 ? "PM" : "AM"; }
|
static const char *ampm(const tm &t) { return t.tm_hour >= 12 ? "PM" : "AM"; }
|
||||||
|
|
||||||
static int to12h(const tm &t) { return t.tm_hour > 12 ? t.tm_hour - 12 : t.tm_hour; }
|
static int to12h(const tm &t) { return t.tm_hour > 12 ? t.tm_hour - 12 : t.tm_hour; }
|
||||||
@@ -248,7 +250,7 @@ public:
|
|||||||
: flag_formatter(padinfo) {}
|
: flag_formatter(padinfo) {}
|
||||||
|
|
||||||
void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override {
|
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);
|
ScopedPadder p(field_size, padinfo_, dest);
|
||||||
|
|
||||||
fmt_helper::pad2(tm_time.tm_mon + 1, dest);
|
fmt_helper::pad2(tm_time.tm_mon + 1, dest);
|
||||||
@@ -494,10 +496,10 @@ public:
|
|||||||
template <typename ScopedPadder>
|
template <typename ScopedPadder>
|
||||||
class z_formatter final : public flag_formatter {
|
class z_formatter final : public flag_formatter {
|
||||||
public:
|
public:
|
||||||
explicit z_formatter(padding_info padinfo)
|
explicit z_formatter(padding_info padinfo, pattern_time_type time_type)
|
||||||
: flag_formatter(padinfo) {}
|
: flag_formatter(padinfo),
|
||||||
|
time_type_(time_type) {}
|
||||||
|
|
||||||
z_formatter() = default;
|
|
||||||
~z_formatter() override = default;
|
~z_formatter() override = default;
|
||||||
z_formatter(const z_formatter &) = delete;
|
z_formatter(const z_formatter &) = delete;
|
||||||
z_formatter &operator=(const z_formatter &) = delete;
|
z_formatter &operator=(const z_formatter &) = delete;
|
||||||
@@ -507,6 +509,17 @@ public:
|
|||||||
void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) override {
|
void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) override {
|
||||||
constexpr size_t field_size = 6;
|
constexpr size_t field_size = 6;
|
||||||
ScopedPadder p(field_size, padinfo_, dest);
|
ScopedPadder p(field_size, padinfo_, dest);
|
||||||
|
|
||||||
|
#ifdef SPDLOG_NO_TZ_OFFSET
|
||||||
|
const char *const placeholder = "+??:??";
|
||||||
|
dest.append(placeholder, placeholder + 6);
|
||||||
|
#else
|
||||||
|
if (time_type_ == pattern_time_type::utc) {
|
||||||
|
const char *zeroes = "+00:00";
|
||||||
|
dest.append(zeroes, zeroes + 6);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
auto total_minutes = get_cached_offset(msg, tm_time);
|
auto total_minutes = get_cached_offset(msg, tm_time);
|
||||||
if (total_minutes < 0) {
|
if (total_minutes < 0) {
|
||||||
total_minutes = -total_minutes;
|
total_minutes = -total_minutes;
|
||||||
@@ -517,9 +530,11 @@ public:
|
|||||||
fmt_helper::pad2(total_minutes / 60, dest); // hours
|
fmt_helper::pad2(total_minutes / 60, dest); // hours
|
||||||
dest.push_back(':');
|
dest.push_back(':');
|
||||||
fmt_helper::pad2(total_minutes % 60, dest); // minutes
|
fmt_helper::pad2(total_minutes % 60, dest); // minutes
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
pattern_time_type time_type_;
|
||||||
log_clock::time_point last_update_{std::chrono::seconds(0)};
|
log_clock::time_point last_update_{std::chrono::seconds(0)};
|
||||||
int offset_minutes_{0};
|
int offset_minutes_{0};
|
||||||
|
|
||||||
@@ -742,12 +757,25 @@ private:
|
|||||||
|
|
||||||
// Full info formatter
|
// Full info formatter
|
||||||
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v
|
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v
|
||||||
class full_formatter final : public flag_formatter {
|
class default_format_formatter final : public flag_formatter {
|
||||||
public:
|
public:
|
||||||
explicit full_formatter(padding_info padinfo)
|
// clang-format off
|
||||||
|
// aligned level names
|
||||||
|
static constexpr std::string_view trace_str = "[trace] ";
|
||||||
|
static constexpr std::string_view debug_str = "[debug] ";
|
||||||
|
static constexpr std::string_view info_str = "[info ] ";
|
||||||
|
static constexpr std::string_view warn_str = "[warn ] ";
|
||||||
|
static constexpr std::string_view error_str = "[error] ";
|
||||||
|
static constexpr std::string_view critical_str = "[crit ] ";
|
||||||
|
static constexpr std::string_view off_str = "[off ] ";
|
||||||
|
// clang-format on
|
||||||
|
static constexpr std::array<std::string_view, levels_count> padded_levels{
|
||||||
|
{trace_str, debug_str, info_str, warn_str, error_str, critical_str, off_str}};
|
||||||
|
|
||||||
|
explicit default_format_formatter(padding_info padinfo)
|
||||||
: flag_formatter(padinfo) {}
|
: flag_formatter(padinfo) {}
|
||||||
|
|
||||||
void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) override {
|
void format(const log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) override {
|
||||||
using std::chrono::duration_cast;
|
using std::chrono::duration_cast;
|
||||||
using std::chrono::milliseconds;
|
using std::chrono::milliseconds;
|
||||||
using std::chrono::seconds;
|
using std::chrono::seconds;
|
||||||
@@ -780,13 +808,13 @@ public:
|
|||||||
cache_timestamp_ = secs;
|
cache_timestamp_ = secs;
|
||||||
}
|
}
|
||||||
dest.append(cached_datetime_.begin(), cached_datetime_.end());
|
dest.append(cached_datetime_.begin(), cached_datetime_.end());
|
||||||
|
// append milliseconds
|
||||||
auto millis = fmt_helper::time_fraction<milliseconds>(msg.time);
|
auto millis = fmt_helper::time_fraction<milliseconds>(msg.time);
|
||||||
fmt_helper::pad3(static_cast<uint32_t>(millis.count()), dest);
|
fmt_helper::pad3(static_cast<uint32_t>(millis.count()), dest);
|
||||||
dest.push_back(']');
|
dest.push_back(']');
|
||||||
dest.push_back(' ');
|
dest.push_back(' ');
|
||||||
|
|
||||||
// append logger name if exists
|
// append aligned logger name if exists
|
||||||
if (!msg.logger_name.empty()) {
|
if (!msg.logger_name.empty()) {
|
||||||
dest.push_back('[');
|
dest.push_back('[');
|
||||||
fmt_helper::append_string_view(msg.logger_name, dest);
|
fmt_helper::append_string_view(msg.logger_name, dest);
|
||||||
@@ -794,14 +822,10 @@ public:
|
|||||||
dest.push_back(' ');
|
dest.push_back(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
dest.push_back('[');
|
// wrap the level name inside the [..] with color
|
||||||
// wrap the level name with color
|
msg.color_range_start = dest.size() + 1; // +1 to start coloring after the '[';
|
||||||
msg.color_range_start = dest.size();
|
fmt_helper::append_string_view(padded_levels.at(static_cast<size_t>(msg.log_level)), dest);
|
||||||
// fmt_helper::append_string_view(level::to_c_str(msg.log_level), dest);
|
msg.color_range_end = dest.size() - 2; // -2 to end coloring before the "] "
|
||||||
fmt_helper::append_string_view(to_string_view(msg.log_level), dest);
|
|
||||||
msg.color_range_end = dest.size();
|
|
||||||
dest.push_back(']');
|
|
||||||
dest.push_back(' ');
|
|
||||||
|
|
||||||
// add source location if present
|
// add source location if present
|
||||||
if (!msg.source.empty()) {
|
if (!msg.source.empty()) {
|
||||||
@@ -844,7 +868,7 @@ pattern_formatter::pattern_formatter(pattern_time_type time_type, std::string eo
|
|||||||
need_localtime_{true},
|
need_localtime_{true},
|
||||||
cached_tm_{},
|
cached_tm_{},
|
||||||
last_log_secs_{} {
|
last_log_secs_{} {
|
||||||
formatters_.push_back(std::make_unique<details::full_formatter>(details::padding_info{}));
|
formatters_.push_back(std::make_unique<details::default_format_formatter>(details::padding_info{}));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<formatter> pattern_formatter::clone() const {
|
std::unique_ptr<formatter> pattern_formatter::clone() const {
|
||||||
@@ -906,7 +930,7 @@ void pattern_formatter::handle_flag_(char flag, details::padding_info padding) {
|
|||||||
// process built-in flags
|
// process built-in flags
|
||||||
switch (flag) {
|
switch (flag) {
|
||||||
case ('+'): // default formatter
|
case ('+'): // default formatter
|
||||||
formatters_.push_back(std::make_unique<details::full_formatter>(padding));
|
formatters_.push_back(std::make_unique<details::default_format_formatter>(padding));
|
||||||
need_localtime_ = true;
|
need_localtime_ = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -1040,7 +1064,8 @@ void pattern_formatter::handle_flag_(char flag, details::padding_info padding) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case ('z'): // timezone
|
case ('z'): // timezone
|
||||||
formatters_.push_back(std::make_unique<details::z_formatter<Padder>>(padding));
|
formatters_.push_back(
|
||||||
|
std::make_unique<details::z_formatter<Padder>>(padding, pattern_time_type_));
|
||||||
need_localtime_ = true;
|
need_localtime_ = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
@@ -12,11 +12,8 @@ namespace spdlog {
|
|||||||
namespace sinks {
|
namespace sinks {
|
||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
ansicolor_sink<Mutex>::ansicolor_sink(FILE *target_file, color_mode mode)
|
ansicolor_sink<Mutex>::ansicolor_sink(FILE *target_file, color_mode mode) : target_file_(target_file) {
|
||||||
: target_file_(target_file)
|
set_color_mode_(mode);
|
||||||
|
|
||||||
{
|
|
||||||
set_color_mode(mode);
|
|
||||||
colors_.at(level_to_number(level::trace)) = to_string_(white);
|
colors_.at(level_to_number(level::trace)) = to_string_(white);
|
||||||
colors_.at(level_to_number(level::debug)) = to_string_(cyan);
|
colors_.at(level_to_number(level::debug)) = to_string_(cyan);
|
||||||
colors_.at(level_to_number(level::info)) = to_string_(green);
|
colors_.at(level_to_number(level::info)) = to_string_(green);
|
||||||
@@ -34,31 +31,36 @@ void ansicolor_sink<Mutex>::set_color(level color_level, string_view_t color) {
|
|||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
bool ansicolor_sink<Mutex>::should_color() const {
|
bool ansicolor_sink<Mutex>::should_color() const {
|
||||||
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
return should_do_colors_;
|
return should_do_colors_;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
void ansicolor_sink<Mutex>::set_color_mode(color_mode mode) {
|
void ansicolor_sink<Mutex>::set_color_mode(color_mode mode) {
|
||||||
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
|
set_color_mode_(mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Mutex>
|
||||||
|
void ansicolor_sink<Mutex>::set_color_mode_(color_mode mode) {
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case color_mode::always:
|
case color_mode::always:
|
||||||
should_do_colors_ = true;
|
should_do_colors_ = true;
|
||||||
return;
|
return;
|
||||||
case color_mode::automatic:
|
case color_mode::automatic:
|
||||||
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
||||||
return;
|
return;
|
||||||
case color_mode::never:
|
case color_mode::never:
|
||||||
should_do_colors_ = false;
|
should_do_colors_ = false;
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
should_do_colors_ = false;
|
should_do_colors_ = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
void ansicolor_sink<Mutex>::sink_it_(const details::log_msg &msg) {
|
void ansicolor_sink<Mutex>::sink_it_(const details::log_msg &msg) {
|
||||||
// Wrap the originally formatted message in color codes.
|
// Wrap the originally formatted message in color codes.
|
||||||
// If color is not supported in the terminal, log as is instead.
|
// If color is not supported in the terminal, log as is instead.
|
||||||
|
|
||||||
msg.color_range_start = 0;
|
msg.color_range_start = 0;
|
||||||
msg.color_range_end = 0;
|
msg.color_range_end = 0;
|
||||||
memory_buf_t formatted;
|
memory_buf_t formatted;
|
||||||
@@ -85,12 +87,12 @@ void ansicolor_sink<Mutex>::flush_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
void ansicolor_sink<Mutex>::print_ccode_(const string_view_t color_code) {
|
void ansicolor_sink<Mutex>::print_ccode_(const string_view_t color_code) const {
|
||||||
details::os::fwrite_bytes(color_code.data(), color_code.size(), target_file_);
|
details::os::fwrite_bytes(color_code.data(), color_code.size(), target_file_);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
void ansicolor_sink<Mutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) {
|
void ansicolor_sink<Mutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) const {
|
||||||
details::os::fwrite_bytes(formatted.data() + start, end - start, target_file_);
|
details::os::fwrite_bytes(formatted.data() + start, end - start, target_file_);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,12 +111,13 @@ template <typename Mutex>
|
|||||||
ansicolor_stderr_sink<Mutex>::ansicolor_stderr_sink(color_mode mode)
|
ansicolor_stderr_sink<Mutex>::ansicolor_stderr_sink(color_mode mode)
|
||||||
: ansicolor_sink<Mutex>(stderr, mode) {}
|
: ansicolor_sink<Mutex>(stderr, mode) {}
|
||||||
|
|
||||||
|
|
||||||
} // namespace sinks
|
} // namespace sinks
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
// template instantiations
|
// template instantiations
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include "spdlog/details/null_mutex.h"
|
||||||
|
template class SPDLOG_API spdlog::sinks::ansicolor_sink<std::mutex>;
|
||||||
|
template class SPDLOG_API spdlog::sinks::ansicolor_sink<spdlog::details::null_mutex>;
|
||||||
template class SPDLOG_API spdlog::sinks::ansicolor_stdout_sink<std::mutex>;
|
template class SPDLOG_API spdlog::sinks::ansicolor_stdout_sink<std::mutex>;
|
||||||
template class SPDLOG_API spdlog::sinks::ansicolor_stdout_sink<spdlog::details::null_mutex>;
|
template class SPDLOG_API spdlog::sinks::ansicolor_stdout_sink<spdlog::details::null_mutex>;
|
||||||
template class SPDLOG_API spdlog::sinks::ansicolor_stderr_sink<std::mutex>;
|
template class SPDLOG_API spdlog::sinks::ansicolor_stderr_sink<std::mutex>;
|
||||||
|
|||||||
@@ -3,8 +3,10 @@
|
|||||||
|
|
||||||
#include "spdlog/sinks/async_sink.h"
|
#include "spdlog/sinks/async_sink.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include "spdlog/common.h"
|
||||||
#include "spdlog/details/mpmc_blocking_q.h"
|
#include "spdlog/details/mpmc_blocking_q.h"
|
||||||
@@ -19,6 +21,10 @@ async_sink::async_sink(config async_config)
|
|||||||
if (config_.queue_size == 0 || config_.queue_size > max_queue_size) {
|
if (config_.queue_size == 0 || config_.queue_size > max_queue_size) {
|
||||||
throw spdlog_ex("async_sink: invalid queue size");
|
throw spdlog_ex("async_sink: invalid queue size");
|
||||||
}
|
}
|
||||||
|
if (config_.custom_err_handler) {
|
||||||
|
err_helper_.set_err_handler(config_.custom_err_handler);
|
||||||
|
}
|
||||||
|
|
||||||
q_ = std::make_unique<queue_t>(config_.queue_size);
|
q_ = std::make_unique<queue_t>(config_.queue_size);
|
||||||
worker_thread_ = std::thread([this] {
|
worker_thread_ = std::thread([this] {
|
||||||
if (config_.on_thread_start) config_.on_thread_start();
|
if (config_.on_thread_start) config_.on_thread_start();
|
||||||
@@ -32,13 +38,16 @@ async_sink::~async_sink() {
|
|||||||
q_->enqueue(async_log_msg(async_log_msg::type::terminate));
|
q_->enqueue(async_log_msg(async_log_msg::type::terminate));
|
||||||
worker_thread_.join();
|
worker_thread_.join();
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
|
terminate_worker_ = true; // as last resort, stop the worker thread using terminate_worker_ flag.
|
||||||
|
#ifndef NDEBUG
|
||||||
printf("Exception in ~async_sink()\n");
|
printf("Exception in ~async_sink()\n");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void async_sink::log(const details::log_msg &msg) { send_message_(async_log_msg::type::log, msg); }
|
void async_sink::log(const details::log_msg &msg) { enqueue_message_(async_log_msg(async_log_msg::type::log, msg)); }
|
||||||
|
|
||||||
void async_sink::flush() { send_message_(async_log_msg::type::flush, details::log_msg()); }
|
void async_sink::flush() { enqueue_message_(details::async_log_msg(async_log_msg::type::flush)); }
|
||||||
|
|
||||||
void async_sink::set_pattern(const std::string &pattern) { set_formatter(std::make_unique<pattern_formatter>(pattern)); }
|
void async_sink::set_pattern(const std::string &pattern) { set_formatter(std::make_unique<pattern_formatter>(pattern)); }
|
||||||
|
|
||||||
@@ -54,6 +63,25 @@ void async_sink::set_formatter(std::unique_ptr<formatter> formatter) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool async_sink::wait_all(const std::chrono::milliseconds timeout) const {
|
||||||
|
using std::chrono::steady_clock;
|
||||||
|
constexpr std::chrono::milliseconds sleep_duration(5);
|
||||||
|
const auto start_time = steady_clock::now();
|
||||||
|
while (q_->size() > 0) {
|
||||||
|
auto elapsed = steady_clock::now() - start_time;
|
||||||
|
if (elapsed > timeout) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::this_thread::sleep_for(std::min(sleep_duration, timeout));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void async_sink::wait_all() const {
|
||||||
|
while (!wait_all(std::chrono::milliseconds(10))) { /* empty */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
size_t async_sink::get_overrun_counter() const { return q_->overrun_counter(); }
|
size_t async_sink::get_overrun_counter() const { return q_->overrun_counter(); }
|
||||||
|
|
||||||
void async_sink::reset_overrun_counter() const { q_->reset_overrun_counter(); }
|
void async_sink::reset_overrun_counter() const { q_->reset_overrun_counter(); }
|
||||||
@@ -65,16 +93,16 @@ void async_sink::reset_discard_counter() const { q_->reset_discard_counter(); }
|
|||||||
const async_sink::config &async_sink::get_config() const { return config_; }
|
const async_sink::config &async_sink::get_config() const { return config_; }
|
||||||
|
|
||||||
// private methods
|
// private methods
|
||||||
void async_sink::send_message_(async_log_msg::type msg_type, const details::log_msg &msg) const {
|
void async_sink::enqueue_message_(details::async_log_msg &&msg) const {
|
||||||
switch (config_.policy) {
|
switch (config_.policy) {
|
||||||
case overflow_policy::block:
|
case overflow_policy::block:
|
||||||
q_->enqueue(async_log_msg(msg_type, msg));
|
q_->enqueue(std::move(msg));
|
||||||
break;
|
break;
|
||||||
case overflow_policy::overrun_oldest:
|
case overflow_policy::overrun_oldest:
|
||||||
q_->enqueue_nowait(async_log_msg(msg_type, msg));
|
q_->enqueue_nowait(std::move(msg));
|
||||||
break;
|
break;
|
||||||
case overflow_policy::discard_new:
|
case overflow_policy::discard_new:
|
||||||
q_->enqueue_if_have_room(async_log_msg(msg_type, msg));
|
q_->enqueue_if_have_room(std::move(msg));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
assert(false);
|
assert(false);
|
||||||
@@ -84,7 +112,7 @@ void async_sink::send_message_(async_log_msg::type msg_type, const details::log_
|
|||||||
|
|
||||||
void async_sink::backend_loop_() {
|
void async_sink::backend_loop_() {
|
||||||
details::async_log_msg incoming_msg;
|
details::async_log_msg incoming_msg;
|
||||||
for (;;) {
|
while (!terminate_worker_) {
|
||||||
q_->dequeue(incoming_msg);
|
q_->dequeue(incoming_msg);
|
||||||
switch (incoming_msg.message_type()) {
|
switch (incoming_msg.message_type()) {
|
||||||
case async_log_msg::type::log:
|
case async_log_msg::type::log:
|
||||||
@@ -101,7 +129,7 @@ void async_sink::backend_loop_() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void async_sink::backend_log_(const details::log_msg &msg) {
|
void async_sink::backend_log_(const details::log_msg &msg) {
|
||||||
for (const auto &sink : config_.sinks) {
|
for (const auto &sink : config_.sinks) {
|
||||||
if (sink->should_log(msg.log_level)) {
|
if (sink->should_log(msg.log_level)) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include "spdlog/common.h"
|
||||||
#include "spdlog/pattern_formatter.h"
|
#include "spdlog/pattern_formatter.h"
|
||||||
|
|
||||||
@@ -53,8 +54,8 @@ void base_sink<Mutex>::set_formatter_(std::unique_ptr<formatter> sink_formatter)
|
|||||||
formatter_ = std::move(sink_formatter);
|
formatter_ = std::move(sink_formatter);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace sinks
|
} // namespace sinks
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
// template instantiations
|
// template instantiations
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include "spdlog/details/null_mutex.h"
|
||||||
|
|||||||
@@ -2,9 +2,11 @@
|
|||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
#include "spdlog/sinks/basic_file_sink.h"
|
#include "spdlog/sinks/basic_file_sink.h"
|
||||||
#include "spdlog/common.h"
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
|
#include "spdlog/common.h"
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
|
|
||||||
@@ -19,6 +21,12 @@ const filename_t &basic_file_sink<Mutex>::filename() const {
|
|||||||
return file_helper_.filename();
|
return file_helper_.filename();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename Mutex>
|
||||||
|
void basic_file_sink<Mutex>::truncate() {
|
||||||
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
|
file_helper_.reopen(true);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
void basic_file_sink<Mutex>::sink_it_(const details::log_msg &msg) {
|
void basic_file_sink<Mutex>::sink_it_(const details::log_msg &msg) {
|
||||||
memory_buf_t formatted;
|
memory_buf_t formatted;
|
||||||
@@ -34,7 +42,6 @@ void basic_file_sink<Mutex>::flush_() {
|
|||||||
} // namespace sinks
|
} // namespace sinks
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
|
|
||||||
// template instantiations
|
// template instantiations
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include "spdlog/details/null_mutex.h"
|
||||||
template class SPDLOG_API spdlog::sinks::basic_file_sink<std::mutex>;
|
template class SPDLOG_API spdlog::sinks::basic_file_sink<std::mutex>;
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ filename_t rotating_file_sink<Mutex>::filename() {
|
|||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
void rotating_file_sink<Mutex>::rotate_now() {
|
void rotating_file_sink<Mutex>::rotate_now() {
|
||||||
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
rotate_();
|
rotate_();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,4 +143,4 @@ bool rotating_file_sink<Mutex>::rename_file_(const filename_t &src_filename, con
|
|||||||
// template instantiations
|
// template instantiations
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include "spdlog/details/null_mutex.h"
|
||||||
template class SPDLOG_API spdlog::sinks::rotating_file_sink<std::mutex>;
|
template class SPDLOG_API spdlog::sinks::rotating_file_sink<std::mutex>;
|
||||||
template class SPDLOG_API spdlog::sinks::rotating_file_sink<spdlog::details::null_mutex>;
|
template class SPDLOG_API spdlog::sinks::rotating_file_sink<spdlog::details::null_mutex>;
|
||||||
|
|||||||
@@ -8,7 +8,9 @@
|
|||||||
|
|
||||||
// clang-format on
|
// clang-format on
|
||||||
#include "spdlog/sinks/wincolor_sink.h"
|
#include "spdlog/sinks/wincolor_sink.h"
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include "spdlog/common.h"
|
||||||
|
#include "spdlog/details/os.h"
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
@@ -107,8 +109,15 @@ std::uint16_t wincolor_sink<Mutex>::set_foreground_color_(std::uint16_t attribs)
|
|||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
void wincolor_sink<Mutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) {
|
void wincolor_sink<Mutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) {
|
||||||
if (end > start) {
|
if (end > start) {
|
||||||
|
#if defined(SPDLOG_UTF8_TO_WCHAR_CONSOLE)
|
||||||
|
wmemory_buf_t wformatted;
|
||||||
|
details::os::utf8_to_wstrbuf(string_view_t(formatted.data() + start, end - start), wformatted);
|
||||||
|
auto size = static_cast<DWORD>(wformatted.size());
|
||||||
|
auto ignored = ::WriteConsoleW(static_cast<HANDLE>(out_handle_), wformatted.data(), size, nullptr, nullptr);
|
||||||
|
#else
|
||||||
auto size = static_cast<DWORD>(end - start);
|
auto size = static_cast<DWORD>(end - start);
|
||||||
auto ignored = ::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start, size, nullptr, nullptr);
|
auto ignored = ::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start, size, nullptr, nullptr);
|
||||||
|
#endif
|
||||||
(void)(ignored);
|
(void)(ignored);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -131,7 +140,6 @@ template <typename Mutex>
|
|||||||
wincolor_stderr_sink<Mutex>::wincolor_stderr_sink(color_mode mode)
|
wincolor_stderr_sink<Mutex>::wincolor_stderr_sink(color_mode mode)
|
||||||
: wincolor_sink<Mutex>(::GetStdHandle(STD_ERROR_HANDLE), mode) {}
|
: wincolor_sink<Mutex>(::GetStdHandle(STD_ERROR_HANDLE), mode) {}
|
||||||
|
|
||||||
|
|
||||||
} // namespace sinks
|
} // namespace sinks
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
|
|||||||
@@ -13,21 +13,17 @@
|
|||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|
||||||
|
|
||||||
#ifndef SPDLOG_DISABLE_GLOBAL_LOGGER
|
#ifndef SPDLOG_DISABLE_GLOBAL_LOGGER
|
||||||
static std::shared_ptr<logger> s_logger = std::make_shared<logger>("global", std::make_shared<sinks::stdout_color_sink_mt>());
|
static std::shared_ptr<logger> s_logger = std::make_shared<logger>("", std::make_shared<sinks::stdout_color_sink_mt>());
|
||||||
#else
|
#else
|
||||||
static std::short_ptr<logger> s_logger = nullptr;
|
static std::short_ptr<logger> s_logger = nullptr;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
std::shared_ptr<logger> global_logger() { return s_logger; }
|
std::shared_ptr<logger> global_logger() { return s_logger; }
|
||||||
|
|
||||||
void set_global_logger(std::shared_ptr<logger> global_logger) { s_logger = std::move(global_logger); }
|
void set_global_logger(std::shared_ptr<logger> global_logger) { s_logger = std::move(global_logger); }
|
||||||
|
|
||||||
logger *global_logger_raw() noexcept {
|
logger *global_logger_raw() noexcept { return s_logger.get(); }
|
||||||
return s_logger.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_formatter(std::unique_ptr<formatter> formatter) { global_logger()->set_formatter(std::move(formatter)); }
|
void set_formatter(std::unique_ptr<formatter> formatter) { global_logger()->set_formatter(std::move(formatter)); }
|
||||||
|
|
||||||
@@ -37,7 +33,7 @@ void set_pattern(std::string pattern, pattern_time_type time_type) {
|
|||||||
|
|
||||||
level get_level() { return global_logger()->log_level(); }
|
level get_level() { return global_logger()->log_level(); }
|
||||||
|
|
||||||
bool should_log(level level) { return global_logger()->should_log(level); }
|
bool should_log(level log_level) { return global_logger()->should_log(log_level); }
|
||||||
|
|
||||||
void set_level(level level) { global_logger()->set_level(level); }
|
void set_level(level level) { global_logger()->set_level(level); }
|
||||||
|
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ set(SPDLOG_UTESTS_SOURCES
|
|||||||
test_log_level.cpp
|
test_log_level.cpp
|
||||||
test_include_sinks.cpp
|
test_include_sinks.cpp
|
||||||
test_bin_to_hex.cpp
|
test_bin_to_hex.cpp
|
||||||
|
test_timezone.cpp
|
||||||
test_errors.cpp)
|
test_errors.cpp)
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
|
|||||||
@@ -25,6 +25,9 @@
|
|||||||
|
|
||||||
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
|
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
|
||||||
|
|
||||||
|
#undef SPDLOG_LEVEL_NAMES
|
||||||
|
#undef SPDLOG_SHORT_LEVEL_NAMES
|
||||||
|
|
||||||
#include "spdlog/details/fmt_helper.h"
|
#include "spdlog/details/fmt_helper.h"
|
||||||
#include "spdlog/pattern_formatter.h"
|
#include "spdlog/pattern_formatter.h"
|
||||||
#include "spdlog/sinks/null_sink.h"
|
#include "spdlog/sinks/null_sink.h"
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
using spdlog::sinks::async_sink;
|
using spdlog::sinks::async_sink;
|
||||||
using spdlog::sinks::sink;
|
using spdlog::sinks::sink;
|
||||||
using spdlog::sinks::test_sink_mt;
|
using spdlog::sinks::test_sink_mt;
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
auto creat_async_logger(size_t queue_size, std::shared_ptr<sink> backend_sink) {
|
auto creat_async_logger(size_t queue_size, std::shared_ptr<sink> backend_sink) {
|
||||||
async_sink::config cfg;
|
async_sink::config cfg;
|
||||||
@@ -41,7 +42,7 @@ TEST_CASE("basic async test ", "[async]") {
|
|||||||
|
|
||||||
TEST_CASE("discard policy ", "[async]") {
|
TEST_CASE("discard policy ", "[async]") {
|
||||||
auto test_sink = std::make_shared<test_sink_mt>();
|
auto test_sink = std::make_shared<test_sink_mt>();
|
||||||
test_sink->set_delay(std::chrono::milliseconds(1));
|
test_sink->set_delay(1ms);
|
||||||
async_sink::config config;
|
async_sink::config config;
|
||||||
config.queue_size = 4;
|
config.queue_size = 4;
|
||||||
config.policy = async_sink::overflow_policy::overrun_oldest;
|
config.policy = async_sink::overflow_policy::overrun_oldest;
|
||||||
@@ -62,7 +63,7 @@ TEST_CASE("discard policy ", "[async]") {
|
|||||||
|
|
||||||
TEST_CASE("discard policy discard_new ", "[async]") {
|
TEST_CASE("discard policy discard_new ", "[async]") {
|
||||||
auto test_sink = std::make_shared<test_sink_mt>();
|
auto test_sink = std::make_shared<test_sink_mt>();
|
||||||
test_sink->set_delay(std::chrono::milliseconds(1));
|
test_sink->set_delay(1ms);
|
||||||
async_sink::config config;
|
async_sink::config config;
|
||||||
config.queue_size = 4;
|
config.queue_size = 4;
|
||||||
config.policy = async_sink::overflow_policy::discard_new;
|
config.policy = async_sink::overflow_policy::discard_new;
|
||||||
@@ -71,7 +72,6 @@ TEST_CASE("discard policy discard_new ", "[async]") {
|
|||||||
auto as = std::make_shared<async_sink>(config);
|
auto as = std::make_shared<async_sink>(config);
|
||||||
auto logger = std::make_shared<spdlog::logger>("async_logger", as);
|
auto logger = std::make_shared<spdlog::logger>("async_logger", as);
|
||||||
|
|
||||||
|
|
||||||
REQUIRE(as->get_config().policy == async_sink::overflow_policy::discard_new);
|
REQUIRE(as->get_config().policy == async_sink::overflow_policy::discard_new);
|
||||||
REQUIRE(as->get_discard_counter() == 0);
|
REQUIRE(as->get_discard_counter() == 0);
|
||||||
REQUIRE(as->get_overrun_counter() == 0);
|
REQUIRE(as->get_overrun_counter() == 0);
|
||||||
@@ -95,14 +95,13 @@ TEST_CASE("flush", "[async]") {
|
|||||||
}
|
}
|
||||||
logger->flush();
|
logger->flush();
|
||||||
}
|
}
|
||||||
// std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
|
||||||
REQUIRE(test_sink->msg_counter() == messages);
|
REQUIRE(test_sink->msg_counter() == messages);
|
||||||
REQUIRE(test_sink->flush_counter() == 1);
|
REQUIRE(test_sink->flush_counter() == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("wait_dtor ", "[async]") {
|
TEST_CASE("wait_dtor ", "[async]") {
|
||||||
auto test_sink = std::make_shared<test_sink_mt>();
|
auto test_sink = std::make_shared<test_sink_mt>();
|
||||||
test_sink->set_delay(std::chrono::milliseconds(5));
|
test_sink->set_delay(5ms);
|
||||||
async_sink::config config;
|
async_sink::config config;
|
||||||
config.sinks.push_back(test_sink);
|
config.sinks.push_back(test_sink);
|
||||||
config.queue_size = 4;
|
config.queue_size = 4;
|
||||||
@@ -167,7 +166,6 @@ TEST_CASE("to_file", "[async]") {
|
|||||||
REQUIRE(ends_with(contents, spdlog::fmt_lib::format("Hello message #1023{}", default_eol)));
|
REQUIRE(ends_with(contents, spdlog::fmt_lib::format("Hello message #1023{}", default_eol)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE("bad_ctor", "[async]") {
|
TEST_CASE("bad_ctor", "[async]") {
|
||||||
async_sink::config cfg;
|
async_sink::config cfg;
|
||||||
cfg.queue_size = 0;
|
cfg.queue_size = 0;
|
||||||
@@ -295,3 +293,63 @@ TEST_CASE("backend_ex", "[async]") {
|
|||||||
REQUIRE_NOTHROW(logger->info("Hello message"));
|
REQUIRE_NOTHROW(logger->info("Hello message"));
|
||||||
REQUIRE_NOTHROW(logger->flush());
|
REQUIRE_NOTHROW(logger->flush());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test async custom error handler. trigger it using a backend exception and make sure it's called
|
||||||
|
TEST_CASE("custom_err_handler", "[async]") {
|
||||||
|
bool error_called = false;
|
||||||
|
auto test_sink = std::make_shared<test_sink_mt>();
|
||||||
|
test_sink->set_exception(std::runtime_error("test backend exception"));
|
||||||
|
async_sink::config config;
|
||||||
|
config.sinks.push_back(std::move(test_sink));
|
||||||
|
config.custom_err_handler = [&error_called](const std::string &) { error_called = true; };
|
||||||
|
auto asink = std::make_shared<async_sink>(config);
|
||||||
|
spdlog::logger("async_logger", std::move(asink)).info("Test");
|
||||||
|
// lvalue logger so will be destructed here already so all messages were processed
|
||||||
|
REQUIRE(error_called);
|
||||||
|
}
|
||||||
|
|
||||||
|
// test wait_all
|
||||||
|
TEST_CASE("wait_all", "[async]") {
|
||||||
|
auto test_sink = std::make_shared<test_sink_mt>();
|
||||||
|
auto delay = 10ms;
|
||||||
|
test_sink->set_delay(delay);
|
||||||
|
async_sink::config config;
|
||||||
|
config.sinks.push_back(test_sink);
|
||||||
|
size_t messages = 10;
|
||||||
|
auto as = std::make_shared<async_sink>(config);
|
||||||
|
auto logger = std::make_shared<spdlog::logger>("async_logger", as);
|
||||||
|
for (size_t i = 0; i < messages; i++) {
|
||||||
|
logger->info("Hello message");
|
||||||
|
}
|
||||||
|
REQUIRE_FALSE(as->wait_all(-10ms));
|
||||||
|
REQUIRE_FALSE(as->wait_all(0ms));
|
||||||
|
auto start = std::chrono::steady_clock::now();
|
||||||
|
REQUIRE_FALSE(as->wait_all(delay));
|
||||||
|
|
||||||
|
// should have waited approx 10ms before giving up
|
||||||
|
auto elapsed = std::chrono::steady_clock::now() - start;
|
||||||
|
REQUIRE(elapsed >= delay);
|
||||||
|
REQUIRE(elapsed < delay * 6); // big tolerance, to pass tests in slow virtual machines
|
||||||
|
// wait enough time for all messages to be processed
|
||||||
|
REQUIRE(as->wait_all(messages * delay + 500ms));
|
||||||
|
REQUIRE(as->wait_all(-10ms)); // no more messages
|
||||||
|
REQUIRE(as->wait_all(0ms)); // no more messages
|
||||||
|
REQUIRE(as->wait_all(10ms)); // no more messages
|
||||||
|
}
|
||||||
|
|
||||||
|
// test wait_all without timeout
|
||||||
|
TEST_CASE("wait_all2", "[async]") {
|
||||||
|
auto test_sink = std::make_shared<test_sink_mt>();
|
||||||
|
auto delay = 10ms;
|
||||||
|
test_sink->set_delay(delay);
|
||||||
|
async_sink::config config;
|
||||||
|
config.sinks.push_back(test_sink);
|
||||||
|
size_t messages = 10;
|
||||||
|
auto as = std::make_shared<async_sink>(config);
|
||||||
|
auto logger = std::make_shared<spdlog::logger>("async_logger", as);
|
||||||
|
for (size_t i = 0; i < messages; i++) {
|
||||||
|
logger->info("Hello message");
|
||||||
|
}
|
||||||
|
as->wait_all();
|
||||||
|
REQUIRE(test_sink->msg_counter() == messages);
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,9 @@
|
|||||||
* https://raw.githubusercontent.com/gabime/spdlog/v2.x/LICENSE
|
* https://raw.githubusercontent.com/gabime/spdlog/v2.x/LICENSE
|
||||||
*/
|
*/
|
||||||
#include "includes.h"
|
#include "includes.h"
|
||||||
|
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include "spdlog/common.h"
|
||||||
#include "spdlog/sinks/callback_sink.h"
|
#include "spdlog/sinks/callback_sink.h"
|
||||||
#include "test_sink.h"
|
#include "test_sink.h"
|
||||||
@@ -14,7 +17,8 @@ TEST_CASE("custom_callback_logger", "[custom_callback_logger]") {
|
|||||||
spdlog::memory_buf_t formatted;
|
spdlog::memory_buf_t formatted;
|
||||||
formatter.format(msg, formatted);
|
formatter.format(msg, formatted);
|
||||||
auto eol_len = strlen(spdlog::details::os::default_eol);
|
auto eol_len = strlen(spdlog::details::os::default_eol);
|
||||||
lines.emplace_back(formatted.begin(), formatted.end() - eol_len);
|
using diff_t = typename std::iterator_traits<decltype(formatted.end())>::difference_type;
|
||||||
|
lines.emplace_back(formatted.begin(), formatted.end() - static_cast<diff_t>(eol_len));
|
||||||
});
|
});
|
||||||
std::shared_ptr<spdlog::sinks::test_sink_st> test_sink(new spdlog::sinks::test_sink_st);
|
std::shared_ptr<spdlog::sinks::test_sink_st> test_sink(new spdlog::sinks::test_sink_st);
|
||||||
|
|
||||||
|
|||||||
@@ -80,3 +80,41 @@ TEST_CASE("dup_filter_test5", "[dup_filter_sink]") {
|
|||||||
REQUIRE(test_sink->msg_counter() == 3); // skip 2 messages but log the "skipped.." message before message2
|
REQUIRE(test_sink->msg_counter() == 3); // skip 2 messages but log the "skipped.." message before message2
|
||||||
REQUIRE(test_sink->lines()[1] == "Skipped 2 duplicate messages..");
|
REQUIRE(test_sink->lines()[1] == "Skipped 2 duplicate messages..");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("dup_filter_skipped_notification_uses_last_duplicate_level", "[dup_filter_sink]") {
|
||||||
|
using spdlog::sinks::dup_filter_sink_st;
|
||||||
|
using spdlog::sinks::test_sink_mt;
|
||||||
|
|
||||||
|
dup_filter_sink_st dup_sink{std::chrono::seconds{5}};
|
||||||
|
auto test_sink = std::make_shared<test_sink_mt>();
|
||||||
|
test_sink->set_pattern("%L");
|
||||||
|
dup_sink.add_sink(test_sink);
|
||||||
|
|
||||||
|
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::warn, "same"});
|
||||||
|
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::warn, "same"});
|
||||||
|
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::info, "diff"});
|
||||||
|
|
||||||
|
REQUIRE(test_sink->lines().size() == 3);
|
||||||
|
REQUIRE(test_sink->lines()[0] == "W");
|
||||||
|
REQUIRE(test_sink->lines()[1] == "W");
|
||||||
|
REQUIRE(test_sink->lines()[2] == "I");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("dup_filter_skipped_notification_fixed_level", "[dup_filter_sink]") {
|
||||||
|
using spdlog::sinks::dup_filter_sink_st;
|
||||||
|
using spdlog::sinks::test_sink_mt;
|
||||||
|
|
||||||
|
dup_filter_sink_st dup_sink{std::chrono::seconds{5}, spdlog::level::info};
|
||||||
|
auto test_sink = std::make_shared<test_sink_mt>();
|
||||||
|
test_sink->set_pattern("%L");
|
||||||
|
dup_sink.add_sink(test_sink);
|
||||||
|
|
||||||
|
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::warn, "same"});
|
||||||
|
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::warn, "same"});
|
||||||
|
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::info, "diff"});
|
||||||
|
|
||||||
|
REQUIRE(test_sink->lines().size() == 3);
|
||||||
|
REQUIRE(test_sink->lines()[0] == "W");
|
||||||
|
REQUIRE(test_sink->lines()[1] == "I");
|
||||||
|
REQUIRE(test_sink->lines()[2] == "I");
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,11 +18,10 @@ protected:
|
|||||||
};
|
};
|
||||||
struct custom_ex {};
|
struct custom_ex {};
|
||||||
|
|
||||||
|
|
||||||
using namespace spdlog::sinks;
|
using namespace spdlog::sinks;
|
||||||
TEST_CASE("default_error_handler", "[errors]") {
|
TEST_CASE("default_error_handler", "[errors]") {
|
||||||
prepare_logdir();
|
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->set_pattern("%v");
|
||||||
logger->info(SPDLOG_FMT_RUNTIME("Test message {} {}"), 1);
|
logger->info(SPDLOG_FMT_RUNTIME("Test message {} {}"), 1);
|
||||||
logger->info("Test message {}", 2);
|
logger->info("Test message {}", 2);
|
||||||
@@ -36,17 +35,43 @@ TEST_CASE("custom_error_handler", "[errors]") {
|
|||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
auto logger = spdlog::create<basic_file_sink_mt>("test-format-error", log_filename);
|
auto logger = spdlog::create<basic_file_sink_mt>("test-format-error", log_filename);
|
||||||
logger->flush_on(spdlog::level::info);
|
logger->flush_on(spdlog::level::info);
|
||||||
logger->set_error_handler([=](const std::string & msg) {
|
logger->set_error_handler([=](const std::string &msg) { REQUIRE(msg == "argument not found"); });
|
||||||
REQUIRE(msg == "argument not found");
|
|
||||||
});
|
|
||||||
logger->info("Good message #1");
|
logger->info("Good message #1");
|
||||||
REQUIRE_NOTHROW(logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx"));
|
REQUIRE_NOTHROW(logger->info(SPDLOG_FMT_RUNTIME("Bad format msg {} {}"), "xxx"));
|
||||||
logger->info("Good message #2");
|
logger->info("Good message #2");
|
||||||
require_message_count(log_filename, 2);
|
require_message_count(log_filename, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("default_error_handler2", "[errors]") {
|
TEST_CASE("throwing_sink", "[errors]") {
|
||||||
auto logger = std::make_shared<spdlog::logger>("test-failing-sink", std::make_shared<failing_sink>());
|
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) {
|
logger->set_error_handler([=](const std::string &msg) {
|
||||||
REQUIRE(msg == log_err_msg);
|
REQUIRE(msg == log_err_msg);
|
||||||
throw custom_ex();
|
throw custom_ex();
|
||||||
@@ -54,8 +79,8 @@ TEST_CASE("default_error_handler2", "[errors]") {
|
|||||||
REQUIRE_NOTHROW(logger->info("Some message"));
|
REQUIRE_NOTHROW(logger->info("Some message"));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("flush_error_handler", "[errors]") {
|
TEST_CASE("unknown_ex_from_flush_err_handler", "[errors]") {
|
||||||
auto logger = spdlog::create<failing_sink>("test-failing-sink");
|
auto logger = spdlog::create<failing_sink>("test-throwing-error-handler");
|
||||||
logger->set_error_handler([=](const std::string &msg) {
|
logger->set_error_handler([=](const std::string &msg) {
|
||||||
REQUIRE(msg == flush_err_msg);
|
REQUIRE(msg == flush_err_msg);
|
||||||
throw custom_ex();
|
throw custom_ex();
|
||||||
|
|||||||
@@ -41,6 +41,26 @@ TEST_CASE("flush_on", "[flush_on]") {
|
|||||||
default_eol, default_eol, default_eol));
|
default_eol, default_eol, default_eol));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("rotating_file_logger1", "[rotating_logger]") {
|
TEST_CASE("rotating_file_logger1", "[rotating_logger]") {
|
||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
size_t max_size = 1024 * 10;
|
size_t max_size = 1024 * 10;
|
||||||
|
|||||||
@@ -7,8 +7,8 @@
|
|||||||
|
|
||||||
#include "includes.h"
|
#include "includes.h"
|
||||||
#include "spdlog/details/os.h"
|
#include "spdlog/details/os.h"
|
||||||
#include "spdlog/sinks/ostream_sink.h"
|
|
||||||
#include "spdlog/sinks/async_sink.h"
|
#include "spdlog/sinks/async_sink.h"
|
||||||
|
#include "spdlog/sinks/ostream_sink.h"
|
||||||
#include "test_sink.h"
|
#include "test_sink.h"
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
@@ -72,6 +72,78 @@ TEST_CASE("to_level_enum", "[convert_to_level_enum]") {
|
|||||||
REQUIRE(spdlog::level_from_str("critical") == spdlog::level::critical);
|
REQUIRE(spdlog::level_from_str("critical") == spdlog::level::critical);
|
||||||
REQUIRE(spdlog::level_from_str("off") == spdlog::level::off);
|
REQUIRE(spdlog::level_from_str("off") == spdlog::level::off);
|
||||||
REQUIRE(spdlog::level_from_str("null") == spdlog::level::off);
|
REQUIRE(spdlog::level_from_str("null") == spdlog::level::off);
|
||||||
|
REQUIRE(spdlog::level_from_str("TRACE") == spdlog::level::trace);
|
||||||
|
REQUIRE(spdlog::level_from_str("DEBUG") == spdlog::level::debug);
|
||||||
|
REQUIRE(spdlog::level_from_str("INFO") == spdlog::level::info);
|
||||||
|
REQUIRE(spdlog::level_from_str("WARNING") == spdlog::level::warn);
|
||||||
|
REQUIRE(spdlog::level_from_str("WARN") == spdlog::level::warn);
|
||||||
|
REQUIRE(spdlog::level_from_str("ERROR") == spdlog::level::err);
|
||||||
|
REQUIRE(spdlog::level_from_str("ERR") == spdlog::level::err);
|
||||||
|
REQUIRE(spdlog::level_from_str("CRITICAL") == spdlog::level::critical);
|
||||||
|
REQUIRE(spdlog::level_from_str("OFF") == spdlog::level::off);
|
||||||
|
REQUIRE(spdlog::level_from_str("TrAcE") == spdlog::level::trace);
|
||||||
|
REQUIRE(spdlog::level_from_str("DeBuG") == spdlog::level::debug);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("copy_ctor", "[copy_ctor]") {
|
||||||
|
using spdlog::sinks::test_sink_mt;
|
||||||
|
auto test_sink = std::make_shared<test_sink_mt>();
|
||||||
|
auto logger = std::make_shared<spdlog::logger>("orig", test_sink);
|
||||||
|
logger->set_pattern("%v");
|
||||||
|
bool error_handled = false;
|
||||||
|
logger->set_error_handler([&error_handled](const std::string&) { error_handled = true; });
|
||||||
|
spdlog::logger copied = *logger;
|
||||||
|
|
||||||
|
REQUIRE(copied.name() == logger->name());
|
||||||
|
REQUIRE(logger->sinks() == copied.sinks());
|
||||||
|
REQUIRE(logger->log_level() == copied.log_level());
|
||||||
|
REQUIRE(logger->flush_level() == copied.flush_level());
|
||||||
|
|
||||||
|
logger->info("Some message 1");
|
||||||
|
copied.info("Some message 2");
|
||||||
|
|
||||||
|
REQUIRE(test_sink->lines().size() == 2);
|
||||||
|
REQUIRE(test_sink->lines()[0] == "Some message 1");
|
||||||
|
REQUIRE(test_sink->lines()[1] == "Some message 2");
|
||||||
|
|
||||||
|
// check that copied custom error handler was indeed copied
|
||||||
|
test_sink->set_exception(std::runtime_error("Some error"));
|
||||||
|
REQUIRE(error_handled == false);
|
||||||
|
copied.error("Some error");
|
||||||
|
REQUIRE(error_handled == true);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("move_ctor", "[move_ctor]") {
|
||||||
|
auto log_level = spdlog::level::critical;
|
||||||
|
auto flush_level = spdlog::level::warn;
|
||||||
|
using spdlog::sinks::test_sink_mt;
|
||||||
|
auto test_sink = std::make_shared<test_sink_mt>();
|
||||||
|
auto logger = std::make_shared<spdlog::logger>("orig", test_sink);
|
||||||
|
logger->flush_on(flush_level);
|
||||||
|
logger->set_level(log_level);
|
||||||
|
logger->set_pattern("%v");
|
||||||
|
bool error_handled = false;
|
||||||
|
logger->set_error_handler([&error_handled](const std::string&) { error_handled = true; });
|
||||||
|
spdlog::logger moved = std::move(*logger);
|
||||||
|
|
||||||
|
REQUIRE(logger->name() == "");
|
||||||
|
REQUIRE(logger->sinks().empty());
|
||||||
|
REQUIRE(moved.name() == "orig");
|
||||||
|
REQUIRE(moved.sinks()[0].get() == test_sink.get());
|
||||||
|
REQUIRE(moved.log_level() == log_level);
|
||||||
|
REQUIRE(moved.flush_level() == flush_level);
|
||||||
|
|
||||||
|
logger->critical("Some message 1");
|
||||||
|
moved.critical("Some message 2");
|
||||||
|
|
||||||
|
REQUIRE(test_sink->lines().size() == 1);
|
||||||
|
REQUIRE(test_sink->lines()[0] == "Some message 2");
|
||||||
|
|
||||||
|
// check that copied custom error handler was indeed copied
|
||||||
|
test_sink->set_exception(std::runtime_error("Some error"));
|
||||||
|
REQUIRE(error_handled == false);
|
||||||
|
moved.critical("Some error");
|
||||||
|
REQUIRE(error_handled == true);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("clone-logger", "[clone]") {
|
TEST_CASE("clone-logger", "[clone]") {
|
||||||
@@ -79,18 +151,27 @@ TEST_CASE("clone-logger", "[clone]") {
|
|||||||
auto test_sink = std::make_shared<test_sink_mt>();
|
auto test_sink = std::make_shared<test_sink_mt>();
|
||||||
auto logger = std::make_shared<spdlog::logger>("orig", test_sink);
|
auto logger = std::make_shared<spdlog::logger>("orig", test_sink);
|
||||||
logger->set_pattern("%v");
|
logger->set_pattern("%v");
|
||||||
|
bool error_handled = false;
|
||||||
|
logger->set_error_handler([&error_handled](const std::string&) { error_handled = true; });
|
||||||
auto cloned = logger->clone("clone");
|
auto cloned = logger->clone("clone");
|
||||||
|
|
||||||
REQUIRE(cloned->name() == "clone");
|
REQUIRE(cloned->name() == "clone");
|
||||||
REQUIRE(logger->sinks() == cloned->sinks());
|
REQUIRE(logger->sinks() == cloned->sinks());
|
||||||
REQUIRE(logger->log_level() == cloned->log_level());
|
REQUIRE(logger->log_level() == cloned->log_level());
|
||||||
REQUIRE(logger->flush_level() == cloned->flush_level());
|
REQUIRE(logger->flush_level() == cloned->flush_level());
|
||||||
|
|
||||||
logger->info("Some message 1");
|
logger->info("Some message 1");
|
||||||
cloned->info("Some message 2");
|
cloned->info("Some message 2");
|
||||||
|
|
||||||
REQUIRE(test_sink->lines().size() == 2);
|
REQUIRE(test_sink->lines().size() == 2);
|
||||||
REQUIRE(test_sink->lines()[0] == "Some message 1");
|
REQUIRE(test_sink->lines()[0] == "Some message 1");
|
||||||
REQUIRE(test_sink->lines()[1] == "Some message 2");
|
REQUIRE(test_sink->lines()[1] == "Some message 2");
|
||||||
|
|
||||||
|
// check that cloned custom error handler was indeed cloned
|
||||||
|
test_sink->set_exception(std::runtime_error("Some error"));
|
||||||
|
REQUIRE(error_handled == false);
|
||||||
|
cloned->error("Some error");
|
||||||
|
REQUIRE(error_handled == true);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("clone async", "[clone]") {
|
TEST_CASE("clone async", "[clone]") {
|
||||||
|
|||||||
@@ -2,6 +2,9 @@
|
|||||||
#include "spdlog/sinks/ostream_sink.h"
|
#include "spdlog/sinks/ostream_sink.h"
|
||||||
#include "test_sink.h"
|
#include "test_sink.h"
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
using spdlog::memory_buf_t;
|
using spdlog::memory_buf_t;
|
||||||
|
|
||||||
// log to str and return it
|
// log to str and return it
|
||||||
@@ -18,6 +21,21 @@ static std::string log_to_str(const std::string &msg, const Args &...args) {
|
|||||||
return oss.str();
|
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]") {
|
TEST_CASE("custom eol", "[pattern_formatter]") {
|
||||||
std::string msg = "Hello custom eol test";
|
std::string msg = "Hello custom eol test";
|
||||||
std::string eol = ";)";
|
std::string eol = ";)";
|
||||||
@@ -52,6 +70,36 @@ TEST_CASE("date MM/DD/YY ", "[pattern_formatter]") {
|
|||||||
REQUIRE(log_to_str("Some message", "%D %v", spdlog::pattern_time_type::local, "\n") == oss.str());
|
REQUIRE(log_to_str("Some message", "%D %v", spdlog::pattern_time_type::local, "\n") == oss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("%z with UTC pattern time", "[pattern_formatter]") {
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
const auto now = std::chrono::system_clock::now();
|
||||||
|
const auto yesterday = now - 24h;
|
||||||
|
|
||||||
|
#ifndef SPDLOG_NO_TZ_OFFSET
|
||||||
|
REQUIRE(log_to_str_with_time(yesterday, "Some message", "%z", spdlog::pattern_time_type::utc, "\n") ==
|
||||||
|
"+00:00\n");
|
||||||
|
#else
|
||||||
|
REQUIRE(log_to_str_with_time(yesterday, "Some message", "%z", spdlog::pattern_time_type::utc, "\n") ==
|
||||||
|
"+??:??\n");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// see test_timezone.cpp for actual UTC offset calculation tests
|
||||||
|
TEST_CASE("UTC offset", "[pattern_formatter]") {
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
const auto now = std::chrono::system_clock::now();
|
||||||
|
std::string result =
|
||||||
|
log_to_str_with_time(now, "Some message", "%z", spdlog::pattern_time_type::local, "\n");
|
||||||
|
|
||||||
|
#ifndef SPDLOG_NO_TZ_OFFSET
|
||||||
|
// Match format: +HH:MM or -HH:MM
|
||||||
|
std::regex re(R"([+-]\d{2}:[0-5]\d\n)");
|
||||||
|
REQUIRE(std::regex_match(result, re));
|
||||||
|
#else
|
||||||
|
REQUIRE(result == "+??:??\n");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("color range test1", "[pattern_formatter]") {
|
TEST_CASE("color range test1", "[pattern_formatter]") {
|
||||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%v%$", spdlog::pattern_time_type::local, "\n");
|
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%v%$", spdlog::pattern_time_type::local, "\n");
|
||||||
|
|
||||||
|
|||||||
@@ -57,15 +57,5 @@ TEST_CASE("test_empty", "[ringbuffer_sink]") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("test_empty_size", "[ringbuffer_sink]") {
|
TEST_CASE("test_empty_size", "[ringbuffer_sink]") {
|
||||||
const size_t sink_size = 0;
|
REQUIRE_THROWS_AS((void)spdlog::sinks::ringbuffer_sink_mt(0), spdlog::spdlog_ex);
|
||||||
auto sink = std::make_shared<spdlog::sinks::ringbuffer_sink_mt>(sink_size);
|
|
||||||
spdlog::logger l("logger", sink);
|
|
||||||
|
|
||||||
for (size_t i = 0; i < sink_size + 1; ++i) {
|
|
||||||
l.info("{}", i);
|
|
||||||
}
|
|
||||||
|
|
||||||
sink->drain([&](std::string_view) {
|
|
||||||
REQUIRE_FALSE(true); // should not be called since the sink size is 0
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,9 +6,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <exception>
|
||||||
|
#include <iterator>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <exception>
|
|
||||||
|
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include "spdlog/details/null_mutex.h"
|
||||||
#include "spdlog/details/os.h"
|
#include "spdlog/details/os.h"
|
||||||
@@ -37,13 +38,9 @@ public:
|
|||||||
delay_ = delay;
|
delay_ = delay;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_exception(const std::runtime_error& ex) {
|
void set_exception(const std::runtime_error& ex) { exception_ptr_ = std::make_exception_ptr(ex); }
|
||||||
exception_ptr_ = std::make_exception_ptr(ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear_exception() {
|
void clear_exception() { exception_ptr_ = nullptr; }
|
||||||
exception_ptr_ = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// return last output without the eol
|
// return last output without the eol
|
||||||
std::vector<std::string> lines() {
|
std::vector<std::string> lines() {
|
||||||
@@ -52,7 +49,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void sink_it_(const details::log_msg &msg) override {
|
void sink_it_(const details::log_msg& msg) override {
|
||||||
if (exception_ptr_) {
|
if (exception_ptr_) {
|
||||||
std::rethrow_exception(exception_ptr_);
|
std::rethrow_exception(exception_ptr_);
|
||||||
}
|
}
|
||||||
@@ -61,7 +58,8 @@ protected:
|
|||||||
// save the line without the eol
|
// save the line without the eol
|
||||||
auto eol_len = strlen(details::os::default_eol);
|
auto eol_len = strlen(details::os::default_eol);
|
||||||
if (lines_.size() < lines_to_save) {
|
if (lines_.size() < lines_to_save) {
|
||||||
lines_.emplace_back(formatted.begin(), formatted.end() - eol_len);
|
using diff_t = typename std::iterator_traits<decltype(formatted.end())>::difference_type;
|
||||||
|
lines_.emplace_back(formatted.begin(), formatted.end() - static_cast<diff_t>(eol_len));
|
||||||
}
|
}
|
||||||
msg_counter_++;
|
msg_counter_++;
|
||||||
std::this_thread::sleep_for(delay_);
|
std::this_thread::sleep_for(delay_);
|
||||||
@@ -78,7 +76,7 @@ protected:
|
|||||||
size_t flush_counter_{0};
|
size_t flush_counter_{0};
|
||||||
std::chrono::milliseconds delay_{std::chrono::milliseconds::zero()};
|
std::chrono::milliseconds delay_{std::chrono::milliseconds::zero()};
|
||||||
std::vector<std::string> lines_;
|
std::vector<std::string> lines_;
|
||||||
std::exception_ptr exception_ptr_; // will be thrown on next log or flush if not null
|
std::exception_ptr exception_ptr_; // will be thrown on next log or flush if not null
|
||||||
};
|
};
|
||||||
|
|
||||||
using test_sink_mt = test_sink<std::mutex>;
|
using test_sink_mt = test_sink<std::mutex>;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
TEST_CASE("stopwatch1", "[stopwatch]") {
|
TEST_CASE("stopwatch1", "[stopwatch]") {
|
||||||
using std::chrono::milliseconds;
|
using std::chrono::milliseconds;
|
||||||
using clock = std::chrono::steady_clock;
|
using clock = std::chrono::steady_clock;
|
||||||
milliseconds wait_ms(200);
|
milliseconds wait_ms(500);
|
||||||
milliseconds tolerance_ms(250);
|
milliseconds tolerance_ms(250);
|
||||||
auto start = clock::now();
|
auto start = clock::now();
|
||||||
spdlog::stopwatch sw;
|
spdlog::stopwatch sw;
|
||||||
@@ -22,7 +22,7 @@ TEST_CASE("stopwatch2", "[stopwatch]") {
|
|||||||
using std::chrono::milliseconds;
|
using std::chrono::milliseconds;
|
||||||
using clock = std::chrono::steady_clock;
|
using clock = std::chrono::steady_clock;
|
||||||
|
|
||||||
clock::duration wait_duration(milliseconds(200));
|
clock::duration wait_duration(milliseconds(500));
|
||||||
clock::duration tolerance_duration(milliseconds(250));
|
clock::duration tolerance_duration(milliseconds(250));
|
||||||
|
|
||||||
auto test_sink = std::make_shared<test_sink_st>();
|
auto test_sink = std::make_shared<test_sink_st>();
|
||||||
|
|||||||
194
tests/test_timezone.cpp
Normal file
194
tests/test_timezone.cpp
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
#ifndef SPDLOG_NO_TZ_OFFSET
|
||||||
|
|
||||||
|
#include "includes.h"
|
||||||
|
#include <ctime>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
// Helper to construct a simple std::tm from components
|
||||||
|
std::tm make_tm(int year, int month, int day, int hour, int minute) {
|
||||||
|
std::tm t;
|
||||||
|
std::memset(&t, 0, sizeof(t));
|
||||||
|
t.tm_year = year - 1900;
|
||||||
|
t.tm_mon = month - 1;
|
||||||
|
t.tm_mday = day;
|
||||||
|
t.tm_hour = hour;
|
||||||
|
t.tm_min = minute;
|
||||||
|
t.tm_sec = 0;
|
||||||
|
t.tm_isdst = -1;
|
||||||
|
std::mktime(&t);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cross-platform RAII Helper to safely set/restore process timezone
|
||||||
|
class ScopedTZ {
|
||||||
|
std::string original_tz_;
|
||||||
|
bool has_original_ = false;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit ScopedTZ(const std::string &tz_name) {
|
||||||
|
// save current TZ
|
||||||
|
#ifdef _WIN32
|
||||||
|
char *buf = nullptr;
|
||||||
|
size_t len = 0;
|
||||||
|
if (_dupenv_s(&buf, &len, "TZ") == 0 && buf != nullptr) {
|
||||||
|
original_tz_ = std::string(buf);
|
||||||
|
has_original_ = true;
|
||||||
|
free(buf);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
const char *tz = std::getenv("TZ");
|
||||||
|
if (tz) {
|
||||||
|
original_tz_ = tz;
|
||||||
|
has_original_ = true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// set new TZ
|
||||||
|
#ifdef _WIN32
|
||||||
|
_putenv_s("TZ", tz_name.c_str());
|
||||||
|
_tzset();
|
||||||
|
#else
|
||||||
|
setenv("TZ", tz_name.c_str(), 1);
|
||||||
|
tzset();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
~ScopedTZ() {
|
||||||
|
// restore original TZ
|
||||||
|
#ifdef _WIN32
|
||||||
|
if (has_original_) {
|
||||||
|
_putenv_s("TZ", original_tz_.c_str());
|
||||||
|
} else {
|
||||||
|
_putenv_s("TZ", "");
|
||||||
|
}
|
||||||
|
_tzset();
|
||||||
|
#else
|
||||||
|
if (has_original_) {
|
||||||
|
setenv("TZ", original_tz_.c_str(), 1);
|
||||||
|
} else {
|
||||||
|
unsetenv("TZ");
|
||||||
|
}
|
||||||
|
tzset();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using spdlog::details::os::utc_minutes_offset;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* POSIX 2024 defines three formats for the TZ environment variable,
|
||||||
|
*
|
||||||
|
* 1. Implementation defined format which always starts with a colon:
|
||||||
|
* ":characters".
|
||||||
|
* 2. A specifier which fully describes the timezone rule in format
|
||||||
|
* "stdoffset[dst[offset][,start[/time],end[/time]]]". Note the
|
||||||
|
* offset and start/end part could be omitted, in which case one hour
|
||||||
|
* is implied, or it's considered implementation-defined when changing
|
||||||
|
* to and from Daylight Saving Time occurs.
|
||||||
|
* 3. Geographical or special timezone from an implementation-defined
|
||||||
|
* timezone database.
|
||||||
|
*
|
||||||
|
* On POSIX-compilant systems, we prefer format 2, and explicitly specify the
|
||||||
|
* DST rules to avoid implementation-defined behavior.
|
||||||
|
*
|
||||||
|
* See also IEEE 1003.1-2024 8.3 Other Environment Variables.
|
||||||
|
*/
|
||||||
|
#ifndef _WIN32
|
||||||
|
/*
|
||||||
|
* Standard time is UTC-5 ("EST"), DST time is UTC-4 ("EDT"). DST is active
|
||||||
|
* from 2:00 on the 2nd Sunday in March, to 2:00 on 1st Sunday in November.
|
||||||
|
*/
|
||||||
|
#define EST5EDT "EST5EDT,M3.2.0,M11.1.0"
|
||||||
|
/*
|
||||||
|
* Standard time is UTC+2 ("IST"), DST time is UTC+3 ("IDT"). DST is active
|
||||||
|
* from 2:00 on following day of the 4th Thursday in March, to 2:00 on the
|
||||||
|
* last Sunday in October.
|
||||||
|
*/
|
||||||
|
#define IST_MINUS2_IDT "IST-2IDT,M3.4.4/26,M10.5.0"
|
||||||
|
#else
|
||||||
|
/*
|
||||||
|
* However, Windows doesn't follow the POSIX rules and only accept a TZ
|
||||||
|
* environment variable in format
|
||||||
|
*
|
||||||
|
* tzn [+|-]hh[:mm[:ss] ][dzn]
|
||||||
|
*
|
||||||
|
* thus we couldn't specify the DST rules. Luckily, Windows C runtime library
|
||||||
|
* assumes the United State's rules for implementing the calculation of DST,
|
||||||
|
* which is fine for our test cases.
|
||||||
|
*
|
||||||
|
* See also https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/tzset?view=msvc-170
|
||||||
|
*/
|
||||||
|
#define EST5EDT "EST5EDT"
|
||||||
|
#define IST_MINUS2_IDT "IST-2IDT"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
TEST_CASE("UTC Offset - Western Hemisphere (USA - Standard Time)", "[timezone][west]") {
|
||||||
|
// EST5EDT: Eastern Standard Time (UTC-5)
|
||||||
|
ScopedTZ tz(EST5EDT);
|
||||||
|
|
||||||
|
// Jan 15th (Winter)
|
||||||
|
auto tm = make_tm(2023, 1, 15, 12, 0);
|
||||||
|
REQUIRE(utc_minutes_offset(tm) == -300);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("UTC Offset - Eastern Hemisphere (Europe/Israel - Standard Time)", "[timezone][east]") {
|
||||||
|
// IST-2IDT: Israel Standard Time (UTC+2)
|
||||||
|
ScopedTZ tz(IST_MINUS2_IDT);
|
||||||
|
|
||||||
|
// Jan 15th (Winter)
|
||||||
|
auto tm = make_tm(2023, 1, 15, 12, 0);
|
||||||
|
REQUIRE(utc_minutes_offset(tm) == 120);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("UTC Offset - Zero Offset (UTC/GMT)", "[timezone][utc]") {
|
||||||
|
ScopedTZ tz("GMT0");
|
||||||
|
|
||||||
|
// Check Winter
|
||||||
|
auto tm_winter = make_tm(2023, 1, 15, 12, 0);
|
||||||
|
REQUIRE(utc_minutes_offset(tm_winter) == 0);
|
||||||
|
|
||||||
|
// Check Summer (GMT never shifts, so this should also be 0)
|
||||||
|
auto tm_summer = make_tm(2023, 7, 15, 12, 0);
|
||||||
|
REQUIRE(utc_minutes_offset(tm_summer) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("UTC Offset - Non-Integer Hour Offsets (India)", "[timezone][partial]") {
|
||||||
|
// IST-5:30: India Standard Time (UTC+5:30)
|
||||||
|
ScopedTZ tz("IST-5:30");
|
||||||
|
|
||||||
|
auto tm = make_tm(2023, 1, 15, 12, 0);
|
||||||
|
REQUIRE(utc_minutes_offset(tm) == 330);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("UTC Offset - Edge Case: Negative Offset Crossing Midnight", "[timezone][edge]") {
|
||||||
|
ScopedTZ tz(EST5EDT);
|
||||||
|
// Late night Dec 31st, 2023
|
||||||
|
auto tm = make_tm(2023, 12, 31, 23, 59);
|
||||||
|
REQUIRE(utc_minutes_offset(tm) == -300);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("UTC Offset - Edge Case: Leap Year", "[timezone][edge]") {
|
||||||
|
ScopedTZ tz(EST5EDT);
|
||||||
|
// Feb 29, 2024 (Leap Day) - Winter
|
||||||
|
auto tm = make_tm(2024, 2, 29, 12, 0);
|
||||||
|
REQUIRE(utc_minutes_offset(tm) == -300);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("UTC Offset - Edge Case: Invalid Date (Pre-Epoch)", "[timezone][edge]") {
|
||||||
|
#ifdef _WIN32
|
||||||
|
// Windows mktime returns -1 for dates before 1970.
|
||||||
|
// We expect the function to safely return 0 (fallback).
|
||||||
|
auto tm = make_tm(1960, 1, 1, 12, 0);
|
||||||
|
REQUIRE(utc_minutes_offset(tm) == 0);
|
||||||
|
#else
|
||||||
|
// Unix mktime handles pre-1970 dates correctly.
|
||||||
|
// We expect the actual historical offset (EST was UTC-5 in 1960).
|
||||||
|
ScopedTZ tz(EST5EDT);
|
||||||
|
auto tm = make_tm(1960, 1, 1, 12, 0);
|
||||||
|
REQUIRE(utc_minutes_offset(tm) == -300);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !SPDLOG_NO_TZ_OFFSET
|
||||||
|
|
||||||
Reference in New Issue
Block a user