Skip to content

Commit

Permalink
V2.x no std format (#3271)
Browse files Browse the repository at this point in the history
* Removed SPDLOG_USE_STD_FORMAT

* Removed SPDLOG_USE_STD_FORMAT

* clang-format

* Fix windows.yml ci

* Fix ci
  • Loading branch information
gabime authored Nov 29, 2024
1 parent 9fea0e5 commit 63535f1
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 274 deletions.
24 changes: 1 addition & 23 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,4 @@ jobs:
-DSPDLOG_SANITIZE_ADDRESS=${{ matrix.config.asan || 'OFF' }} \
-DSPDLOG_SANITIZE_THREAD=${{ matrix.config.tsan || 'OFF' }}
make -j 4
ctest -j 4 --output-on-failure
# -----------------------------------------------------------------------
# OS X build matrix
# -----------------------------------------------------------------------
build_osx:
runs-on: macOS-latest
name: "OS X Clang (C++17, Release)"
steps:
- uses: actions/checkout@v4
- name: Build
run: |
mkdir -p build && cd build
cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CXX_STANDARD=17 \
-DSPDLOG_BUILD_EXAMPLE=ON \
-DSPDLOG_BUILD_WARNINGS=ON \
-DSPDLOG_BUILD_BENCH=OFF \
-DSPDLOG_BUILD_TESTS=ON \
-DSPDLOG_SANITIZE_ADDRESS=OFF
make -j 4
ctest -j 4 --output-on-failure
ctest -j 4 --output-on-failure
2 changes: 0 additions & 2 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ jobs:
-D SPDLOG_BUILD_EXAMPLE=${{ matrix.config.BUILD_EXAMPLE }} `
-D SPDLOG_BUILD_TESTS=ON `
-D SPDLOG_BUILD_WARNINGS=${{ matrix.config.FATAL_ERRORS }} `
-D SPDLOG_USE_STD_FORMAT=${{ matrix.config.USE_STD_FORMAT }} `
-D CMAKE_CXX_STANDARD=${{ matrix.config.CXX_STANDARD }} ..
- name: Build
Expand Down Expand Up @@ -118,7 +117,6 @@ jobs:
-D SPDLOG_BUILD_EXAMPLE=${{ matrix.config.BUILD_EXAMPLE }} `
-D SPDLOG_BUILD_TESTS=ON `
-D SPDLOG_BUILD_WARNINGS=${{ matrix.config.FATAL_ERRORS }} `
-D SPDLOG_USE_STD_FORMAT=${{ matrix.config.USE_STD_FORMAT }} `
-D CMAKE_CXX_STANDARD=${{ matrix.config.CXX_STANDARD }} ..
- name: Build
Expand Down
89 changes: 33 additions & 56 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ elseif(CMAKE_CXX_STANDARD LESS 17)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)


set(CMAKE_CXX_EXTENSIONS OFF)

if(CMAKE_SYSTEM_NAME MATCHES "CYGWIN" OR CMAKE_SYSTEM_NAME MATCHES "MSYS" OR CMAKE_SYSTEM_NAME MATCHES "MINGW")
Expand All @@ -58,7 +57,6 @@ option(SPDLOG_BUILD_ALL "Build all artifacts" OFF)
# build shared option
option(SPDLOG_BUILD_SHARED "Build shared library" OFF)


# example options
option(SPDLOG_BUILD_EXAMPLE "Build example" ${SPDLOG_MASTER_PROJECT})

Expand All @@ -77,19 +75,9 @@ option(SPDLOG_BUILD_WARNINGS "Enable compiler warnings" OFF)
# install options
option(SPDLOG_SYSTEM_INCLUDES "Include as system headers (skip for clang-tidy)." OFF)
option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT})
option(SPDLOG_USE_STD_FORMAT "Use std::format instead of fmt library." OFF)
option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of of fetching from gitub." OFF)

option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF)

if(SPDLOG_USE_STD_FORMAT AND CMAKE_CXX_STANDARD LESS 20)
message(FATAL_ERROR "SPDLOG_USE_STD_FORMAT must be used with CMAKE_CXX_STANDARD >= 20")
endif()

if(SPDLOG_USE_STD_FORMAT AND SPDLOG_FMT_EXTERNAL)
message(FATAL_ERROR "SPDLOG_USE_STD_FORMAT and SPDLOG_FMT_EXTERNAL are mutually exclusive")
endif()

# misc tweakme options
if(WIN32)
option(SPDLOG_WCHAR_FILENAMES "Support wchar filenames" OFF)
Expand Down Expand Up @@ -126,35 +114,31 @@ if(SPDLOG_BUILD_SHARED)
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# place dlls and libs and executables in the same directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)

# make sure __cplusplus is defined
add_compile_options(/Zc:__cplusplus)
# enable parallel build for the solution
add_compile_options(/MP)
# place dlls and libs and executables in the same directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)

# make sure __cplusplus is defined
add_compile_options(/Zc:__cplusplus)
# enable parallel build for the solution
add_compile_options(/MP)
endif()


message(STATUS "spdlog version: ${SPDLOG_VERSION}")
message(STATUS "spdlog build type: " ${CMAKE_BUILD_TYPE})
message(STATUS "spdlog build shared: " ${BUILD_SHARED_LIBS})
message(STATUS "spdlog use std format: " ${SPDLOG_USE_STD_FORMAT})
message(STATUS "spdlog fmt external: " ${SPDLOG_FMT_EXTERNAL})

# ---------------------------------------------------------------------------------------
# Find {fmt} library if not using std::format
# Find {fmt} library
# ---------------------------------------------------------------------------------------
if(NOT SPDLOG_USE_STD_FORMAT)
if (SPDLOG_FMT_EXTERNAL)
find_package(fmt REQUIRED)
message(STATUS "Using external fmt lib version: ${fmt_VERSION}")
else()
include(cmake/fmtlib.cmake)
endif()
if(SPDLOG_FMT_EXTERNAL)
find_package(fmt REQUIRED)
message(STATUS "Using external fmt lib version: ${fmt_VERSION}")
else()
include(cmake/fmtlib.cmake)
endif()

# ---------------------------------------------------------------------------------------
Expand Down Expand Up @@ -217,8 +201,7 @@ set(SPDLOG_HEADERS
"include/spdlog/sinks/syslog_sink.h"
"include/spdlog/sinks/systemd_sink.h"
"include/spdlog/sinks/tcp_sink.h"
"include/spdlog/sinks/udp_sink.h"
)
"include/spdlog/sinks/udp_sink.h")

set(SPDLOG_SRCS
"src/async_logger.cpp"
Expand All @@ -242,20 +225,18 @@ set(SPDLOG_SRCS
"src/sinks/stdout_sinks.cpp")

if(WIN32)
list(APPEND SPDLOG_SRCS
"src/details/os_windows.cpp"
"src/sinks/wincolor_sink.cpp")
list(APPEND SPDLOG_HEADERS
"include/spdlog/details/tcp_client_windows.h"
"include/spdlog/details/udp_client_windows.h"
"include/spdlog/details/windows_include.h"
"include/spdlog/sinks/win_eventlog_sink.h"
"include/spdlog/sinks/wincolor_sink.h")
list(APPEND SPDLOG_SRCS "src/details/os_windows.cpp" "src/sinks/wincolor_sink.cpp")
list(
APPEND
SPDLOG_HEADERS
"include/spdlog/details/tcp_client_windows.h"
"include/spdlog/details/udp_client_windows.h"
"include/spdlog/details/windows_include.h"
"include/spdlog/sinks/win_eventlog_sink.h"
"include/spdlog/sinks/wincolor_sink.h")
else()
list(APPEND SPDLOG_SRCS "src/details/os_unix.cpp")
list(APPEND SPDLOG_HEADERS
"include/spdlog/details/tcp_client_unix.h"
"include/spdlog/details/udp_client_unix.h")
list(APPEND SPDLOG_HEADERS "include/spdlog/details/tcp_client_unix.h" "include/spdlog/details/udp_client_unix.h")
endif()

# Generate spdlog_config.h based on the current configuration
Expand Down Expand Up @@ -287,16 +268,14 @@ endif()

set_target_properties(spdlog PROPERTIES PUBLIC_HEADER "${SPDLOG_HEADERS}")

target_include_directories(spdlog ${SPDLOG_INCLUDES_LEVEL} PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
target_include_directories(spdlog ${SPDLOG_INCLUDES_LEVEL} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")

target_link_libraries(spdlog PUBLIC Threads::Threads)
if(NOT SPDLOG_USE_STD_FORMAT)
target_link_libraries(spdlog PUBLIC fmt::fmt)
endif()
target_link_libraries(spdlog PUBLIC fmt::fmt)
spdlog_enable_warnings(spdlog)
set_target_properties(spdlog PROPERTIES VERSION ${SPDLOG_VERSION} SOVERSION ${SPDLOG_VERSION_MAJOR}.${SPDLOG_VERSION_MINOR})
set_target_properties(spdlog PROPERTIES VERSION ${SPDLOG_VERSION} SOVERSION
${SPDLOG_VERSION_MAJOR}.${SPDLOG_VERSION_MINOR})
set_target_properties(spdlog PROPERTIES DEBUG_POSTFIX d)

# set source groups for visual studio
Expand All @@ -313,7 +292,6 @@ if(ANDROID)
target_link_libraries(spdlog PUBLIC log)
endif()


# ---------------------------------------------------------------------------------------
# If exceptions are disabled, disable them in the bundled fmt as well
# ---------------------------------------------------------------------------------------
Expand Down Expand Up @@ -403,8 +381,7 @@ if(SPDLOG_INSTALL)
# ---------------------------------------------------------------------------------------
include(cmake/spdlogCPack.cmake)

# Install spdlog_config.h file
# Assume your tweakme.h.in is located in the same directory as CMakeLists.txt
# Install spdlog_config.h file Assume your tweakme.h.in is located in the same directory as CMakeLists.txt
set(TWEAKME_IN "${CMAKE_CURRENT_SOURCE_DIR}/tweakme.h.in")
set(TWEAKME_OUT "${CMAKE_CURRENT_BINARY_DIR}/include/spdlog/tweakme.h")
endif()
2 changes: 1 addition & 1 deletion bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if(NOT benchmark_FOUND)
# Do not build and run googlebenchmark tests
FetchContent_Declare(googlebenchmark GIT_REPOSITORY https://github.com/google/benchmark.git GIT_TAG v1.6.0)
FetchContent_MakeAvailable(googlebenchmark)
set_target_properties(benchmark PROPERTIES FOLDER "third-party")
set_target_properties(benchmark PROPERTIES FOLDER "third-party")
endif()

add_executable(bench bench.cpp)
Expand Down
17 changes: 6 additions & 11 deletions bench/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,19 @@
//
// bench.cpp : spdlog benchmarks
//
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/null_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/spdlog.h"

#if defined(SPDLOG_USE_STD_FORMAT)
#include <format>
#else
#include <fmt/format.h>
#endif
#include <fmt/format.h>

#include <atomic>
#include <cstdlib> // EXIT_FAILURE
#include <memory>
#include <string>
#include <thread>

#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/null_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/spdlog.h"
#include "utils.h"

void bench(int howmany, std::shared_ptr<spdlog::logger> log);
Expand Down
10 changes: 4 additions & 6 deletions cmake/fmtlib.cmake
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
include(FetchContent)

FetchContent_Declare(
fmt
DOWNLOAD_EXTRACT_TIMESTAMP FALSE
URL https://github.com/fmtlib/fmt/archive/refs/tags/11.0.2.tar.gz
URL_HASH SHA256=6cb1e6d37bdcb756dbbe59be438790db409cdb4868c66e888d5df9f13f7c027f)
fmt DOWNLOAD_EXTRACT_TIMESTAMP FALSE URL https://github.com/fmtlib/fmt/archive/refs/tags/11.0.2.tar.gz
URL_HASH SHA256=6cb1e6d37bdcb756dbbe59be438790db409cdb4868c66e888d5df9f13f7c027f)
FetchContent_GetProperties(fmt)
if(NOT fmt_POPULATED)
FetchContent_Populate(fmt)
# We do not require os features of fmt
set(FMT_OS OFF CACHE BOOL "Disable FMT_OS" FORCE)
add_subdirectory(${fmt_SOURCE_DIR} ${fmt_BINARY_DIR})
set_target_properties(fmt PROPERTIES FOLDER "third-party")
endif ()
set_target_properties(fmt PROPERTIES FOLDER "third-party")
endif()
5 changes: 1 addition & 4 deletions cmake/spdlogConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ include(CMakeFindDependencyMacro)

find_package(Threads REQUIRED)

set(SPDLOG_USE_STD_FORMAT @SPDLOG_USE_STD_FORMAT@)
if(NOT SPDLOG_USE_STD_FORMAT)
find_dependency(fmt CONFIG)
endif()
find_dependency(fmt CONFIG)

set(config_targets_file @config_targets_file@)
include("${CMAKE_CURRENT_LIST_DIR}/${config_targets_file}")
Expand Down
4 changes: 0 additions & 4 deletions cmake/spdlog_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,5 @@
// Disable default logger creation
#cmakedefine SPDLOG_DISABLE_DEFAULT_LOGGER

// Use std::format instead of fmtlib
#cmakedefine SPDLOG_USE_STD_FORMAT

// Use external fmtlib instead of bundled
#cmakedefine SPDLOG_FMT_EXTERNAL

2 changes: 1 addition & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ if(NOT TARGET spdlog)
endif()

add_executable(example example.cpp)
target_link_libraries(example PRIVATE spdlog::spdlog $<$<BOOL:${MINGW}>:ws2_32>)
target_link_libraries(example PRIVATE spdlog::spdlog $<$<BOOL:${MINGW}>:ws2_32>)
27 changes: 2 additions & 25 deletions example/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ void async_example() {
// {:p} - don't print the position on each line start.
// {:n} - don't split the output to lines.

#if !defined SPDLOG_USE_STD_FORMAT || defined(_MSC_VER)
#include "spdlog/fmt/bin_to_hex.h"
#include "spdlog/fmt/bin_to_hex.h"
void binary_example() {
std::vector<char> buf;
for (int i = 0; i < 80; i++) {
Expand All @@ -177,26 +176,14 @@ void binary_example() {
// logger->info("hexdump style: {:a}", spdlog::to_hex(buf));
// logger->info("hexdump style, 20 chars per line {:a}", spdlog::to_hex(buf, 20));
}
#else
void binary_example() {
// not supported with std::format yet
}
#endif

// Log a vector of numbers
#ifndef SPDLOG_USE_STD_FORMAT
#include "fmt/ranges.h"
#include "fmt/ranges.h"
void vector_example() {
std::vector<int> vec = {1, 2, 3};
spdlog::info("Vector example: {}", vec);
}

#else
void vector_example() {}
#endif

// ! DSPDLOG_USE_STD_FORMAT

// Compile time log levels.
// define SPDLOG_ACTIVE_LEVEL to required level (e.g. SPDLOG_LEVEL_TRACE)
void trace_example() {
Expand Down Expand Up @@ -250,23 +237,13 @@ struct my_type {
: i(i) {}
};

#ifndef SPDLOG_USE_STD_FORMAT // when using fmtlib
template <>
struct fmt::formatter<my_type> : fmt::formatter<std::string> {
auto format(my_type my, format_context &ctx) const -> decltype(ctx.out()) {
return fmt::format_to(ctx.out(), "[my_type i={}]", my.i);
}
};

#else // when using std::format
template <>
struct std::formatter<my_type> : std::formatter<std::string> {
auto format(my_type my, format_context &ctx) const -> decltype(ctx.out()) {
return format_to(ctx.out(), "[my_type i={}]", my.i);
}
};
#endif

void user_defined_example() { spdlog::info("user defined type: {}", my_type(14)); }

// Custom error handler. Will be triggered on log failure.
Expand Down
Loading

0 comments on commit 63535f1

Please sign in to comment.