From bb0f6b4f1601630d71faf9499f607e567fb1c83f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Langlois?= Date: Tue, 20 Nov 2018 15:36:50 +0100 Subject: [PATCH] refactor:Split main example into several expamples Split the file named "example.cpp" into several examples for "console / basic / daily / rotating logs" to increase the code readability, and add a directory to store binaries, to clean up the "example" directory. --- .gitignore | 12 +++++- example/Makefile | 49 +++++++++++++++++------- example/Makefile.clang | 43 +++++++++++++++------ example/Makefile.mingw | 45 +++++++++++++++------- example/basic_log.cpp | 32 ++++++++++++++++ example/bin/.gitignore | 1 + example/bin/debug/.gitignore | 1 + example/bin/debug/logs/.gitignore | 1 + example/bin/logs/.gitignore | 1 + example/{example.cpp => console_log.cpp} | 18 +-------- example/daily_log.cpp | 34 ++++++++++++++++ example/logs/.gitignore | 1 - example/rotating_log.cpp | 35 +++++++++++++++++ 13 files changed, 215 insertions(+), 58 deletions(-) create mode 100644 example/basic_log.cpp create mode 100644 example/bin/.gitignore create mode 100644 example/bin/debug/.gitignore create mode 100644 example/bin/debug/logs/.gitignore create mode 100644 example/bin/logs/.gitignore rename example/{example.cpp => console_log.cpp} (84%) create mode 100644 example/daily_log.cpp delete mode 100644 example/logs/.gitignore create mode 100644 example/rotating_log.cpp diff --git a/.gitignore b/.gitignore index 2b432ca5..ce6aa6a8 100644 --- a/.gitignore +++ b/.gitignore @@ -39,7 +39,10 @@ build/* # example files example/* -!example/example.cpp +!example/console_log.cpp +!example/basic_log.cpp +!example/daily_log.cpp +!example/rotating_log.cpp !example/bench.cpp !example/utils.h !example/Makefile* @@ -48,6 +51,11 @@ example/* !example/CMakeLists.txt !example/multisink.cpp !example/jni +!example/bin +!example/bin/logs +!example/bin/debug +!example/bin/debug/logs + # generated files generated @@ -68,4 +76,4 @@ install_manifest.txt .idea/ # vscode -.vscode/ \ No newline at end of file +.vscode/ diff --git a/example/Makefile b/example/Makefile index 8b17c7f2..b29f2e4e 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,28 +1,49 @@ -CXX ?= g++ -CXXFLAGS = +CXX ?= g++ +CXXFLAGS = CXX_FLAGS = -Wall -Wshadow -Wextra -pedantic -std=c++11 -pthread -I../include CXX_RELEASE_FLAGS = -O3 -march=native -CXX_DEBUG_FLAGS= -g +CXX_DEBUG_FLAGS = -g +BIN_PATH = bin/ +DEBUG_PATH = $(BIN_PATH)debug/ -all: example bench -debug: example-debug bench-debug -example: example.cpp - $(CXX) example.cpp -o example $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS) +all: basic_log.exe daily_log.exe rotating_log.exe console_log.exe bench.exe +debug: console_log-debug.exe basic_log-debug.exe daily_log-debug.exe rotating_log-debug.exe bench-debug.exe -bench: bench.cpp - $(CXX) bench.cpp -o bench $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS) +basic_log.exe: basic_log.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS) +daily_log.exe: daily_log.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS) -example-debug: example.cpp - $(CXX) example.cpp -o example-debug $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS) +rotating_log.exe: rotating_log.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS) -bench-debug: bench.cpp - $(CXX) bench.cpp -o bench-debug $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS) +console_log.exe: console_log.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS) + +bench.exe: bench.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS) + + +basic_log-debug.exe: basic_log.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS) + +daily_log-debug.exe: daily_log.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS) + +rotating_log-debug.exe: rotating_log.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS) + +console_log-debug.exe: console_log.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS) + +bench-debug.exe: bench.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS) clean: - rm -f *.o logs/*.txt example example-debug bench bench-debug + rm -f *.o $(BIN_PATH)logs/* $(DEBUG_PATH)logs/* $(BIN_PATH)*.exe $(DEBUG_PATH)*-debug.exe rebuild: clean all diff --git a/example/Makefile.clang b/example/Makefile.clang index 78488446..7027aad6 100644 --- a/example/Makefile.clang +++ b/example/Makefile.clang @@ -3,27 +3,48 @@ CXXFLAGS = -march=native -Wall -Wextra -Wshadow -pedantic -std=c++11 -pthread -I CXX_RELEASE_FLAGS = -O2 CXX_DEBUG_FLAGS= -g +BIN_PATH = bin/ +DEBUG_PATH = $(BIN_PATH)debug/ -all: example bench -debug: example-debug bench-debug -example: example.cpp - $(CXX) example.cpp -o example-clang $(CXXFLAGS) $(CXX_RELEASE_FLAGS) +all: basic_log-clang.exe daily_log-clang.exe rotating_log-clang.exe console_log-clang.exe bench-clang.exe +debug: console_log-debug-clang.exe basic_log-debug-clang.exe daily_log-debug-clang.exe rotating_log-debug-clang.exe bench-debug-clang.exe -bench: bench.cpp - $(CXX) bench.cpp -o bench-clang $(CXXFLAGS) $(CXX_RELEASE_FLAGS) +basic_log-clang.exe: basic_log.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXXFLAGS) $(CXX_RELEASE_FLAGS) + +daily_log-clang.exe: daily_log.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXXFLAGS) $(CXX_RELEASE_FLAGS) + +rotating_log-clang.exe: rotating_log.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXXFLAGS) $(CXX_RELEASE_FLAGS) + +console_log-clang.exe: console_log.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXXFLAGS) $(CXX_RELEASE_FLAGS) + +bench-clang.exe: bench.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXXFLAGS) $(CXX_RELEASE_FLAGS) -example-debug: example.cpp - $(CXX) example.cpp -o example-clang-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) +basic_log-debug-clang.exe: basic_log.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXXFLAGS) $(CXX_DEBUG_FLAGS) + +daily_log-debug-clang.exe: daily_log.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXXFLAGS) $(CXX_DEBUG_FLAGS) + +rotating_log-debug-clang.exe: rotating_log.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXXFLAGS) $(CXX_DEBUG_FLAGS) + +console_log-debug-clang.exe: console_log.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXXFLAGS) $(CXX_DEBUG_FLAGS) -bench-debug: bench.cpp - $(CXX) bench.cpp -o bench-clang-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) +bench-clang-debug.exe: bench.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXXFLAGS) $(CXX_DEBUG_FLAGS) clean: - rm -f *.o logs/*.txt example-clang example-clang-debug bench-clang bench-clang-debug + rm -f *.o $(BIN_PATH)logs/* $(DEBUG_PATH)logs/* $(BIN_PATH)*.exe $(DEBUG_PATH)*-debug.exe rebuild: clean all diff --git a/example/Makefile.mingw b/example/Makefile.mingw index 302e0722..35c188f0 100644 --- a/example/Makefile.mingw +++ b/example/Makefile.mingw @@ -3,27 +3,46 @@ CXXFLAGS = -D_WIN32_WINNT=0x600 -march=native -Wall -Wextra -Wshadow -pedantic CXX_RELEASE_FLAGS = -O3 CXX_DEBUG_FLAGS= -g +BIN_PATH = bin/ +DEBUG_PATH = $(BIN_PATH)debug/ -all: example bench -debug: example-debug bench-debug -example: example.cpp - $(CXX) example.cpp -o example $(CXXFLAGS) $(CXX_RELEASE_FLAGS) +all: basic_log.exe daily_log.exe rotating_log.exe console_log.exe bench.exe +debug: console_log-debug.exe basic_log-debug.exe daily_log-debug.exe rotating_log-debug.exe bench-debug.exe -bench: bench.cpp - $(CXX) bench.cpp -o bench $(CXXFLAGS) $(CXX_RELEASE_FLAGS) - +basic_log.exe: basic_log.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXXFLAGS) $(CXX_RELEASE_FLAGS) -example-debug: example.cpp - $(CXX) example.cpp -o example-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) - -bench-debug: bench.cpp - $(CXX) bench.cpp -o bench-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) +daily_log.exe: daily_log.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXXFLAGS) $(CXX_RELEASE_FLAGS) + +rotating_log.exe: rotating_log.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXXFLAGS) $(CXX_RELEASE_FLAGS) +console_log.exe: console_log.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXXFLAGS) $(CXX_RELEASE_FLAGS) + +bench.exe: bench.cpp + $(CXX) $^ -o $(BIN_PATH)$@ $(CXXFLAGS) $(CXX_RELEASE_FLAGS) + +basic_log-debug.exe: basic_log.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXXFLAGS) $(CXX_DEBUG_FLAGS) + +daily_log-debug.exe: daily_log.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXXFLAGS) $(CXX_DEBUG_FLAGS) + +rotating_log-debug.exe: rotating_log.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXXFLAGS) $(CXX_DEBUG_FLAGS) + +console_log-debug.exe: console_log.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXXFLAGS) $(CXX_DEBUG_FLAGS) + +bench-debug.exe: bench.cpp + $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXXFLAGS) $(CXX_DEBUG_FLAGS) clean: - rm -f *.o logs/*.txt example example-debug bench bench-debug + rm -f *.o $(BIN_PATH)logs/* $(DEBUG_PATH)logs/* $(BIN_PATH)*.exe $(DEBUG_PATH)*-debug.exe rebuild: clean all diff --git a/example/basic_log.cpp b/example/basic_log.cpp new file mode 100644 index 00000000..21e28838 --- /dev/null +++ b/example/basic_log.cpp @@ -0,0 +1,32 @@ +// +// Copyright(c) 2015 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// +// +// spdlog usage example +// +// + +#include "spdlog/spdlog.h" + +#include +#include + +namespace spd = spdlog; + +int main(int, char *[]) +{ + try + { + // Create basic file logger (not rotated) + auto my_logger = spd::basic_logger_mt("basic_logger", "logs/basic-log.txt"); + my_logger->info("Some log message"); + } + // Exceptions will only be thrown upon failed logger or sink construction (not during logging) + catch (const spd::spdlog_ex &ex) + { + std::cout << "Log init failed: " << ex.what() << std::endl; + return 1; + } + return 0; +} diff --git a/example/bin/.gitignore b/example/bin/.gitignore new file mode 100644 index 00000000..b883f1fd --- /dev/null +++ b/example/bin/.gitignore @@ -0,0 +1 @@ +*.exe diff --git a/example/bin/debug/.gitignore b/example/bin/debug/.gitignore new file mode 100644 index 00000000..27cef0ff --- /dev/null +++ b/example/bin/debug/.gitignore @@ -0,0 +1 @@ +*-debug.exe diff --git a/example/bin/debug/logs/.gitignore b/example/bin/debug/logs/.gitignore new file mode 100644 index 00000000..397b4a76 --- /dev/null +++ b/example/bin/debug/logs/.gitignore @@ -0,0 +1 @@ +*.log diff --git a/example/bin/logs/.gitignore b/example/bin/logs/.gitignore new file mode 100644 index 00000000..397b4a76 --- /dev/null +++ b/example/bin/logs/.gitignore @@ -0,0 +1 @@ +*.log diff --git a/example/example.cpp b/example/console_log.cpp similarity index 84% rename from example/example.cpp rename to example/console_log.cpp index 1113431e..e6f9d06c 100644 --- a/example/example.cpp +++ b/example/console_log.cpp @@ -13,6 +13,7 @@ #include "spdlog/spdlog.h" #include +#include #include void async_example(); @@ -40,23 +41,6 @@ int main(int, char *[]) spd::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name) function"); - // Create basic file logger (not rotated) - auto my_logger = spd::basic_logger_mt("basic_logger", "logs/basic-log.txt"); - my_logger->info("Some log message"); - - // Create a file rotating logger with 5mb size max and 3 rotated files - auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3); - for (int i = 0; i < 10; ++i) - { - rotating_logger->info("{} * {} equals {:>10}", i, i, i * i); - } - - // Create a daily logger - a new file is created every day on 2:30am - auto daily_logger = spd::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30); - // trigger flush if the log severity is error or higher - daily_logger->flush_on(spd::level::err); - daily_logger->info(123.44); - // Customize msg format for all messages spd::set_pattern("[%^+++%$] [%H:%M:%S %z] [thread %t] %v"); console->info("This an info message with custom format"); diff --git a/example/daily_log.cpp b/example/daily_log.cpp new file mode 100644 index 00000000..c149b6fc --- /dev/null +++ b/example/daily_log.cpp @@ -0,0 +1,34 @@ +// +// Copyright(c) 2015 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// +// +// spdlog usage example +// +// + +#include "spdlog/spdlog.h" + +#include +#include + +namespace spd = spdlog; + +int main(int, char *[]) +{ + try + { + // Create a daily logger - a new file is created every day on 2:30am + auto daily_logger = spd::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30); + // trigger flush if the log severity is error or higher + daily_logger->flush_on(spd::level::err); + daily_logger->info(123.44); + } + // Exceptions will only be thrown upon failed logger or sink construction (not during logging) + catch (const spd::spdlog_ex &ex) + { + std::cout << "Log init failed: " << ex.what() << std::endl; + return 1; + } + return 0; +} diff --git a/example/logs/.gitignore b/example/logs/.gitignore deleted file mode 100644 index 20325135..00000000 --- a/example/logs/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.txt diff --git a/example/rotating_log.cpp b/example/rotating_log.cpp new file mode 100644 index 00000000..d28e600c --- /dev/null +++ b/example/rotating_log.cpp @@ -0,0 +1,35 @@ +// +// Copyright(c) 2015 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// +// +// spdlog usage example +// +// + +#include "spdlog/spdlog.h" + +#include +#include + +namespace spd = spdlog; + +int main(int, char *[]) +{ + try + { + // Create a file rotating logger with 5mb size max and 3 rotated files + auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3); + for (int i = 0; i < 10; ++i) + { + rotating_logger->info("{} * {} equals {:>10}", i, i, i * i); + } + } + // Exceptions will only be thrown upon failed logger or sink construction (not during logging) + catch (const spd::spdlog_ex &ex) + { + std::cout << "Log init failed: " << ex.what() << std::endl; + return 1; + } + return 0; +}