Skip to content

Commit 1cd32fc

Browse files
committed
spdlog added
1 parent 14a7ac8 commit 1cd32fc

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ option(ENABLE_TRACY "enable the code profiling with tracy" OFF)
3030
option(ENABLE_XML "enable the xml example with tinyxml2" ON)
3131
option(ENABLE_TESTING "Enable testing" OFF)
3232
option(ENABLE_SANITIZE "Add sanitizer flags" OFF)
33+
option(ENABLE_SPDLOG "Add spdlog for logging" ON)
3334

3435

3536
add_executable(pointers src/pointers/pointers.cpp)
@@ -538,6 +539,50 @@ message("fast-cpp-csv-parser is not enabled")
538539
endif()
539540

540541

542+
if(ENABLE_SPDLOG)
543+
message("\n########################################## spdlog ##########################################\n")
544+
# include(FetchContent)
545+
# FetchContent_Declare(spdlog
546+
# GIT_REPOSITORY https://github.com/gabime/spdlog.git
547+
# GIT_TAG master
548+
# GIT_SHALLOW TRUE
549+
# GIT_PROGRESS TRUE
550+
# )
551+
552+
# FetchContent_MakeAvailable(spdlog)
553+
554+
# add_executable(spdlog_example src/spdlog_example.cpp)
555+
# target_compile_definitions(spdlog_example PRIVATE CMAKE_CURRENT_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
556+
557+
# # Add spdlog's include directory
558+
# target_link_libraries(spdlog_example PRIVATE spdlog::spdlog)
559+
560+
561+
562+
# Fetch spdlog
563+
FetchContent_Declare(
564+
spdlog
565+
GIT_REPOSITORY https://github.com/gabime/spdlog.git
566+
GIT_TAG v1.14.1 # Use a specific tag, branch, or commit
567+
)
568+
569+
FetchContent_MakeAvailable(spdlog)
570+
571+
# Create an executable or library
572+
add_executable(spdlog_example src/spdlog_example.cpp)
573+
574+
# Link spdlog to your target
575+
target_link_libraries(spdlog_example PRIVATE spdlog::spdlog)
576+
577+
# Optionally, if you want to disable spdlog's automatic formatting, add the following:
578+
# target_compile_definitions(MyExecutable PRIVATE SPDLOG_FMT_EXTERNAL)
579+
580+
else()
581+
message("spdlog is not enabled")
582+
endif()
583+
584+
585+
541586
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp")
542587
add_executable(main src/main.cpp)
543588
endif()

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ This change ensures that VSCode uses the "Ninja Multi-Config" generator by defau
315315
* [YAML](docs/yaml-cpp.md)
316316
* [JSON](docs/json.md)
317317
* [XML](docs/tinyxml2.md)
318+
319+
## [Logging With C++](#)
320+
* [spdlog](docs/spdlog.md)
321+
318322
## [Code Benchmarking](#)
319323
* [Google Benchmark](docs/google_benchmark.md)
320324
## [C++ Coding Standards and Style Guide](#)

docs/spdlog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# spdlog
2+
3+
Refs [1](https://github.com/gabime/spdlog)

src/spdlog_example.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "spdlog/spdlog.h"
2+
3+
int main() {
4+
spdlog::info("Welcome to spdlog!");
5+
spdlog::error("Some error message with arg: {}", 1);
6+
7+
spdlog::warn("Easy padding in numbers like {:08d}", 12);
8+
spdlog::critical(
9+
"Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
10+
spdlog::info("Support for floats {:03.2f}", 1.23456);
11+
spdlog::info("Positional args are {1} {0}..", "too", "supported");
12+
spdlog::info("{:<30}", "left aligned");
13+
14+
spdlog::set_level(spdlog::level::debug); // Set global log level to debug
15+
spdlog::debug("This message should be displayed..");
16+
17+
// change log pattern
18+
spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v");
19+
20+
// Compile time log levels
21+
// Note that this does not change the current log level, it will only
22+
// remove (depending on SPDLOG_ACTIVE_LEVEL) the call on the release code.
23+
SPDLOG_TRACE("Some trace message with param {}", 42);
24+
SPDLOG_DEBUG("Some debug message");
25+
}

0 commit comments

Comments
 (0)