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.
pull/921/head
Benoît Langlois 7 years ago
parent 7088644d3f
commit bb0f6b4f16
No known key found for this signature in database
GPG Key ID: A2B011ADDA5FE3CE

12
.gitignore vendored

@ -39,7 +39,10 @@ build/*
# example files # example files
example/* example/*
!example/example.cpp !example/console_log.cpp
!example/basic_log.cpp
!example/daily_log.cpp
!example/rotating_log.cpp
!example/bench.cpp !example/bench.cpp
!example/utils.h !example/utils.h
!example/Makefile* !example/Makefile*
@ -48,6 +51,11 @@ example/*
!example/CMakeLists.txt !example/CMakeLists.txt
!example/multisink.cpp !example/multisink.cpp
!example/jni !example/jni
!example/bin
!example/bin/logs
!example/bin/debug
!example/bin/debug/logs
# generated files # generated files
generated generated
@ -68,4 +76,4 @@ install_manifest.txt
.idea/ .idea/
# vscode # vscode
.vscode/ .vscode/

@ -1,28 +1,49 @@
CXX ?= g++ CXX ?= g++
CXXFLAGS = CXXFLAGS =
CXX_FLAGS = -Wall -Wshadow -Wextra -pedantic -std=c++11 -pthread -I../include CXX_FLAGS = -Wall -Wshadow -Wextra -pedantic -std=c++11 -pthread -I../include
CXX_RELEASE_FLAGS = -O3 -march=native 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 all: basic_log.exe daily_log.exe rotating_log.exe console_log.exe bench.exe
$(CXX) example.cpp -o example $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS) debug: console_log-debug.exe basic_log-debug.exe daily_log-debug.exe rotating_log-debug.exe bench-debug.exe
bench: bench.cpp basic_log.exe: basic_log.cpp
$(CXX) bench.cpp -o bench $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS) $(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 rotating_log.exe: rotating_log.cpp
$(CXX) example.cpp -o example-debug $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS) $(CXX) $^ -o $(BIN_PATH)$@ $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS)
bench-debug: bench.cpp console_log.exe: console_log.cpp
$(CXX) bench.cpp -o bench-debug $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS) $(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: 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 rebuild: clean all

@ -3,27 +3,48 @@ CXXFLAGS = -march=native -Wall -Wextra -Wshadow -pedantic -std=c++11 -pthread -I
CXX_RELEASE_FLAGS = -O2 CXX_RELEASE_FLAGS = -O2
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 all: basic_log-clang.exe daily_log-clang.exe rotating_log-clang.exe console_log-clang.exe bench-clang.exe
$(CXX) example.cpp -o example-clang $(CXXFLAGS) $(CXX_RELEASE_FLAGS) 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 basic_log-clang.exe: basic_log.cpp
$(CXX) bench.cpp -o bench-clang $(CXXFLAGS) $(CXX_RELEASE_FLAGS) $(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 basic_log-debug-clang.exe: basic_log.cpp
$(CXX) example.cpp -o example-clang-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) $(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 bench-clang-debug.exe: bench.cpp
$(CXX) bench.cpp -o bench-clang-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) $(CXX) $^ -o $(DEBUG_PATH)$@ $(CXXFLAGS) $(CXX_DEBUG_FLAGS)
clean: 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 rebuild: clean all

@ -3,27 +3,46 @@ CXXFLAGS = -D_WIN32_WINNT=0x600 -march=native -Wall -Wextra -Wshadow -pedantic
CXX_RELEASE_FLAGS = -O3 CXX_RELEASE_FLAGS = -O3
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 all: basic_log.exe daily_log.exe rotating_log.exe console_log.exe bench.exe
$(CXX) example.cpp -o example $(CXXFLAGS) $(CXX_RELEASE_FLAGS) debug: console_log-debug.exe basic_log-debug.exe daily_log-debug.exe rotating_log-debug.exe bench-debug.exe
bench: bench.cpp basic_log.exe: basic_log.cpp
$(CXX) bench.cpp -o bench $(CXXFLAGS) $(CXX_RELEASE_FLAGS) $(CXX) $^ -o $(BIN_PATH)$@ $(CXXFLAGS) $(CXX_RELEASE_FLAGS)
example-debug: example.cpp daily_log.exe: daily_log.cpp
$(CXX) example.cpp -o example-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) $(CXX) $^ -o $(BIN_PATH)$@ $(CXXFLAGS) $(CXX_RELEASE_FLAGS)
bench-debug: bench.cpp rotating_log.exe: rotating_log.cpp
$(CXX) bench.cpp -o bench-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) $(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: 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 rebuild: clean all

@ -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 <iostream>
#include <string>
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;
}

@ -0,0 +1 @@
*.exe

@ -0,0 +1 @@
*-debug.exe

@ -0,0 +1 @@
*.log

@ -13,6 +13,7 @@
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include <iostream> #include <iostream>
#include <string>
#include <memory> #include <memory>
void async_example(); 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"); 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 // Customize msg format for all messages
spd::set_pattern("[%^+++%$] [%H:%M:%S %z] [thread %t] %v"); spd::set_pattern("[%^+++%$] [%H:%M:%S %z] [thread %t] %v");
console->info("This an info message with custom format"); console->info("This an info message with custom format");

@ -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 <iostream>
#include <string>
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;
}

@ -1 +0,0 @@
*.txt

@ -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 <iostream>
#include <string>
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;
}
Loading…
Cancel
Save