diff --git a/.gitignore b/.gitignore index b1a41919..2b432ca5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ # Auto generated files -build/* +build/* *.slo *.lo *.o @@ -65,4 +65,7 @@ install_manifest.txt /tests/logs/* # idea -.idea/ \ No newline at end of file +.idea/ + +# vscode +.vscode/ \ No newline at end of file diff --git a/README.md b/README.md index 73347139..1146b176 100644 --- a/README.md +++ b/README.md @@ -120,9 +120,9 @@ int main(int, char*[]) daily_logger->info(123.44); // Customize msg format for all messages - spd::set_pattern("*** [%H:%M:%S %z] [thread %t] %v ***"); - rotating_logger->info("This is another message with custom format"); - + spd::set_pattern("[%^+++%$] [%H:%M:%S %z] [thread %t] %v"); + console->info("This an info message with custom format (and custom color range between the '%^' and '%$')"); + console->error("This an error message with custom format (and custom color range between the '%^' and '%$')"); // Runtime log levels spd::set_level(spd::level::info); //Set global log level to info diff --git a/bench/boost-bench-mt.cpp b/bench/boost-bench-mt.cpp index 3a6eeea8..a076e6e4 100644 --- a/bench/boost-bench-mt.cpp +++ b/bench/boost-bench-mt.cpp @@ -82,6 +82,5 @@ int main(int argc, char *argv[]) std::cout << "Delta = " << deltaf << " seconds" << std::endl; std::cout << "Rate = " << rate << "/sec" << std::endl; - return 0; } diff --git a/bench/easylogging-bench-async.cpp b/bench/easylogging-bench-async.cpp index 81985a6a..e9b0dc84 100644 --- a/bench/easylogging-bench-async.cpp +++ b/bench/easylogging-bench-async.cpp @@ -11,8 +11,8 @@ #define ELPP_THREAD_SAFE #define ELPP_EXPERIMENTAL_ASYNC -#include "easylogging++.h" #include "easylogging++.cc" +#include "easylogging++.h" INITIALIZE_EASYLOGGINGPP using namespace std; diff --git a/bench/easylogging-bench-mt.cpp b/bench/easylogging-bench-mt.cpp index cfcf3348..7601d3c0 100644 --- a/bench/easylogging-bench-mt.cpp +++ b/bench/easylogging-bench-mt.cpp @@ -10,8 +10,8 @@ #include #define ELPP_THREAD_SAFE -#include "easylogging++.h" #include "easylogging++.cc" +#include "easylogging++.h" INITIALIZE_EASYLOGGINGPP using namespace std; diff --git a/bench/easylogging-bench.cpp b/bench/easylogging-bench.cpp index 06b66134..15c093d8 100644 --- a/bench/easylogging-bench.cpp +++ b/bench/easylogging-bench.cpp @@ -6,8 +6,8 @@ #include #include -#include "easylogging++.h" #include "easylogging++.cc" +#include "easylogging++.h" INITIALIZE_EASYLOGGINGPP int main(int, char *[]) @@ -21,7 +21,7 @@ int main(int, char *[]) el::Configurations conf("easyl.conf"); el::Loggers::reconfigureLogger("default", conf); - el::Logger* defaultLogger = el::Loggers::getLogger("default"); + el::Logger *defaultLogger = el::Loggers::getLogger("default"); auto start = clock::now(); for (int i = 0; i < howmany; ++i) diff --git a/bench/g3log-async.cpp b/bench/g3log-async.cpp index b5e15a2b..61ed72df 100644 --- a/bench/g3log-async.cpp +++ b/bench/g3log-async.cpp @@ -13,7 +13,8 @@ #include "g3log/logworker.hpp" using namespace std; -template std::string format(const T &value); +template +std::string format(const T &value); int main(int argc, char *argv[]) { @@ -27,7 +28,7 @@ int main(int argc, char *argv[]) int howmany = 1000000; auto worker = g3::LogWorker::createLogWorker(); - auto handle= worker->addDefaultLogger(argv[0], "logs"); + auto handle = worker->addDefaultLogger(argv[0], "logs"); g3::initializeLogging(worker.get()); std::atomic msg_counter{0}; diff --git a/bench/glog-bench.cpp b/bench/glog-bench.cpp index 6b4445a9..7469896c 100644 --- a/bench/glog-bench.cpp +++ b/bench/glog-bench.cpp @@ -8,7 +8,6 @@ #include "glog/logging.h" - int main(int, char *argv[]) { using namespace std::chrono; diff --git a/bench/log4cplus-bench-mt.cpp b/bench/log4cplus-bench-mt.cpp index 6acbf3fa..72e2681c 100644 --- a/bench/log4cplus-bench-mt.cpp +++ b/bench/log4cplus-bench-mt.cpp @@ -10,17 +10,17 @@ #include #include -#include "log4cplus/logger.h" #include "log4cplus/fileappender.h" -#include "log4cplus/layout.h" -#include "log4cplus/ndc.h" #include "log4cplus/helpers/loglog.h" #include "log4cplus/helpers/property.h" +#include "log4cplus/layout.h" +#include "log4cplus/logger.h" #include "log4cplus/loggingmacros.h" +#include "log4cplus/ndc.h" using namespace log4cplus; -int main(int argc, char * argv[]) +int main(int argc, char *argv[]) { using namespace std::chrono; using clock = steady_clock; @@ -32,13 +32,11 @@ int main(int argc, char * argv[]) int howmany = 1000000; log4cplus::initialize(); - SharedFileAppenderPtr append( - new FileAppender(LOG4CPLUS_TEXT("logs/log4cplus-bench-mt.log"), std::ios_base::trunc, - true, true)); + SharedFileAppenderPtr append(new FileAppender(LOG4CPLUS_TEXT("logs/log4cplus-bench-mt.log"), std::ios_base::trunc, true, true)); append->setName(LOG4CPLUS_TEXT("File")); log4cplus::tstring pattern = LOG4CPLUS_TEXT("%d{%Y-%m-%d %H:%M:%S.%Q}: %p - %m %n"); - append->setLayout( std::auto_ptr(new PatternLayout(pattern)) ); + append->setLayout(std::auto_ptr(new PatternLayout(pattern))); append->getloc(); Logger::getRoot().addAppender(SharedAppenderPtr(append.get())); diff --git a/bench/log4cplus-bench.cpp b/bench/log4cplus-bench.cpp index e522e85a..e738aecd 100644 --- a/bench/log4cplus-bench.cpp +++ b/bench/log4cplus-bench.cpp @@ -7,13 +7,13 @@ #include #include -#include "log4cplus/logger.h" #include "log4cplus/fileappender.h" -#include "log4cplus/layout.h" -#include "log4cplus/ndc.h" #include "log4cplus/helpers/loglog.h" #include "log4cplus/helpers/property.h" +#include "log4cplus/layout.h" +#include "log4cplus/logger.h" #include "log4cplus/loggingmacros.h" +#include "log4cplus/ndc.h" using namespace log4cplus; @@ -25,13 +25,11 @@ int main(int, char *[]) int howmany = 1000000; log4cplus::initialize(); - SharedFileAppenderPtr append( - new FileAppender(LOG4CPLUS_TEXT("logs/log4cplus-bench.log"), std::ios_base::trunc, - true, true)); + SharedFileAppenderPtr append(new FileAppender(LOG4CPLUS_TEXT("logs/log4cplus-bench.log"), std::ios_base::trunc, true, true)); append->setName(LOG4CPLUS_TEXT("File")); log4cplus::tstring pattern = LOG4CPLUS_TEXT("%d{%Y-%m-%d %H:%M:%S.%Q}: %p - %m %n"); - append->setLayout( std::auto_ptr(new PatternLayout(pattern)) ); + append->setLayout(std::auto_ptr(new PatternLayout(pattern))); append->getloc(); Logger::getRoot().addAppender(SharedAppenderPtr(append.get())); diff --git a/bench/log4cpp-bench-mt.cpp b/bench/log4cpp-bench-mt.cpp index dc6a5f58..b96b8e1b 100644 --- a/bench/log4cpp-bench-mt.cpp +++ b/bench/log4cpp-bench-mt.cpp @@ -10,15 +10,15 @@ #include #include -#include "log4cpp/Category.hh" #include "log4cpp/Appender.hh" +#include "log4cpp/BasicLayout.hh" +#include "log4cpp/Category.hh" #include "log4cpp/FileAppender.hh" #include "log4cpp/Layout.hh" -#include "log4cpp/BasicLayout.hh" -#include "log4cpp/Priority.hh" #include "log4cpp/PatternLayout.hh" +#include "log4cpp/Priority.hh" -int main(int argc, char * argv[]) +int main(int argc, char *argv[]) { using namespace std::chrono; using clock = steady_clock; @@ -29,12 +29,12 @@ int main(int argc, char * argv[]) int howmany = 1000000; - log4cpp::Appender *appender = new log4cpp::FileAppender("default", "logs/log4cpp-bench-mt.log"); + log4cpp::Appender *appender = new log4cpp::FileAppender("default", "logs/log4cpp-bench-mt.log"); log4cpp::PatternLayout *layout = new log4cpp::PatternLayout(); layout->setConversionPattern("%d{%Y-%m-%d %H:%M:%S.%l}: %p - %m %n"); appender->setLayout(layout); - log4cpp::Category& root = log4cpp::Category::getRoot(); + log4cpp::Category &root = log4cpp::Category::getRoot(); root.addAppender(appender); root.setPriority(log4cpp::Priority::INFO); diff --git a/bench/log4cpp-bench.cpp b/bench/log4cpp-bench.cpp index a21526c8..75029817 100644 --- a/bench/log4cpp-bench.cpp +++ b/bench/log4cpp-bench.cpp @@ -7,13 +7,13 @@ #include #include -#include "log4cpp/Category.hh" #include "log4cpp/Appender.hh" +#include "log4cpp/BasicLayout.hh" +#include "log4cpp/Category.hh" #include "log4cpp/FileAppender.hh" #include "log4cpp/Layout.hh" -#include "log4cpp/BasicLayout.hh" -#include "log4cpp/Priority.hh" #include "log4cpp/PatternLayout.hh" +#include "log4cpp/Priority.hh" int main(int, char *[]) { @@ -22,12 +22,12 @@ int main(int, char *[]) int howmany = 1000000; - log4cpp::Appender *appender = new log4cpp::FileAppender("default", "logs/log4cpp-bench.log"); + log4cpp::Appender *appender = new log4cpp::FileAppender("default", "logs/log4cpp-bench.log"); log4cpp::PatternLayout *layout = new log4cpp::PatternLayout(); layout->setConversionPattern("%d{%Y-%m-%d %H:%M:%S.%l}: %p - %m %n"); appender->setLayout(layout); - log4cpp::Category& root = log4cpp::Category::getRoot(); + log4cpp::Category &root = log4cpp::Category::getRoot(); root.addAppender(appender); root.setPriority(log4cpp::Priority::INFO); diff --git a/bench/p7-bench-mt.cpp b/bench/p7-bench-mt.cpp index a3195208..996922c9 100644 --- a/bench/p7-bench-mt.cpp +++ b/bench/p7-bench-mt.cpp @@ -5,15 +5,14 @@ #include #include +#include #include +#include #include #include -#include -#include #include "P7_Trace.h" - int main(int argc, char *argv[]) { using namespace std::chrono; @@ -27,10 +26,9 @@ int main(int argc, char *argv[]) IP7_Trace::hModule module = NULL; - //create P7 client object - std::unique_ptr> client( - P7_Create_Client(TM("/P7.Pool=1024 /P7.Sink=FileTxt /P7.Dir=logs/p7-bench-mt")), - [&](IP7_Client *ptr){ + // create P7 client object + std::unique_ptr> client( + P7_Create_Client(TM("/P7.Pool=1024 /P7.Sink=FileTxt /P7.Dir=logs/p7-bench-mt")), [&](IP7_Client *ptr) { if (ptr) ptr->Release(); }); @@ -41,10 +39,9 @@ int main(int argc, char *argv[]) return 1; } - //create P7 trace object 1 - std::unique_ptr> trace( - P7_Create_Trace(client.get(), TM("Trace channel 1")), - [&](IP7_Trace *ptr){ + // create P7 trace object 1 + std::unique_ptr> trace( + P7_Create_Trace(client.get(), TM("Trace channel 1")), [&](IP7_Trace *ptr) { if (ptr) ptr->Release(); }); @@ -65,7 +62,7 @@ int main(int argc, char *argv[]) for (int t = 0; t < thread_count; ++t) { threads.push_back(std::thread([&]() { - trace->Register_Thread(TM("Application"), t+1); + trace->Register_Thread(TM("Application"), t + 1); while (true) { int counter = ++msg_counter; @@ -73,7 +70,7 @@ int main(int argc, char *argv[]) break; trace->P7_INFO(module, TM("p7 message #%d: This is some text for your pleasure"), counter); } - trace->Register_Thread(TM("Application"), t+1); + trace->Register_Thread(TM("Application"), t + 1); })); } diff --git a/bench/p7-bench.cpp b/bench/p7-bench.cpp index 15513ca5..25c39b03 100644 --- a/bench/p7-bench.cpp +++ b/bench/p7-bench.cpp @@ -4,13 +4,12 @@ // #include +#include #include #include -#include #include "P7_Trace.h" - int main(int, char *[]) { using namespace std::chrono; @@ -20,10 +19,9 @@ int main(int, char *[]) IP7_Trace::hModule module = NULL; - //create P7 client object - std::unique_ptr> client( - P7_Create_Client(TM("/P7.Pool=1024 /P7.Sink=FileTxt /P7.Dir=logs/p7-bench")), - [&](IP7_Client *ptr){ + // create P7 client object + std::unique_ptr> client( + P7_Create_Client(TM("/P7.Pool=1024 /P7.Sink=FileTxt /P7.Dir=logs/p7-bench")), [&](IP7_Client *ptr) { if (ptr) ptr->Release(); }); @@ -34,10 +32,9 @@ int main(int, char *[]) return 1; } - //create P7 trace object 1 - std::unique_ptr> trace( - P7_Create_Trace(client.get(), TM("Trace channel 1")), - [&](IP7_Trace *ptr){ + // create P7 trace object 1 + std::unique_ptr> trace( + P7_Create_Trace(client.get(), TM("Trace channel 1")), [&](IP7_Trace *ptr) { if (ptr) ptr->Release(); }); @@ -55,7 +52,6 @@ int main(int, char *[]) for (int i = 0; i < howmany; ++i) trace->P7_INFO(module, TM("p7 message #%d: This is some text for your pleasure"), i); - duration delta = clock::now() - start; float deltaf = delta.count(); auto rate = howmany / deltaf; diff --git a/bench/spdlog-async.cpp b/bench/spdlog-async.cpp index e1d1a95f..e8b70009 100644 --- a/bench/spdlog-async.cpp +++ b/bench/spdlog-async.cpp @@ -5,8 +5,8 @@ #include #include -#include #include +#include #include #include @@ -36,7 +36,7 @@ int main(int argc, char *argv[]) run = false; })); - while(run) + while (run) { std::atomic msg_counter{0}; std::vector threads; @@ -68,7 +68,7 @@ int main(int argc, char *argv[]) std::cout << "Threads: " << thread_count << std::endl; std::cout << "Delta = " << std::fixed << deltaf << " seconds" << std::endl; std::cout << "Rate = " << std::fixed << rate << "/sec" << std::endl; - } //while + } // while stoper.join(); diff --git a/bench/spdlog-bench-mt.cpp b/bench/spdlog-bench-mt.cpp index b4948e73..62dd9cc7 100644 --- a/bench/spdlog-bench-mt.cpp +++ b/bench/spdlog-bench-mt.cpp @@ -5,8 +5,8 @@ #include #include -#include #include +#include #include #include diff --git a/example/Makefile b/example/Makefile index 6d169e41..8b17c7f2 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,29 +1,29 @@ -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 - - -all: example bench -debug: example-debug bench-debug - -example: example.cpp - $(CXX) example.cpp -o example $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS) - -bench: bench.cpp - $(CXX) bench.cpp -o bench $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS) - - -example-debug: example.cpp - $(CXX) example.cpp -o example-debug $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS) - -bench-debug: bench.cpp - $(CXX) bench.cpp -o bench-debug $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS) - -clean: - rm -f *.o logs/*.txt example example-debug bench bench-debug - - -rebuild: clean all -rebuild-debug: clean debug +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 + + +all: example bench +debug: example-debug bench-debug + +example: example.cpp + $(CXX) example.cpp -o example $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS) + +bench: bench.cpp + $(CXX) bench.cpp -o bench $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS) + + +example-debug: example.cpp + $(CXX) example.cpp -o example-debug $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS) + +bench-debug: bench.cpp + $(CXX) bench.cpp -o bench-debug $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS) + +clean: + rm -f *.o logs/*.txt example example-debug bench bench-debug + + +rebuild: clean all +rebuild-debug: clean debug diff --git a/example/Makefile.clang b/example/Makefile.clang index 0ed004d0..78488446 100644 --- a/example/Makefile.clang +++ b/example/Makefile.clang @@ -1,32 +1,32 @@ -CXX ?= clang++ -CXXFLAGS = -march=native -Wall -Wextra -Wshadow -pedantic -std=c++11 -pthread -I../include -CXX_RELEASE_FLAGS = -O2 -CXX_DEBUG_FLAGS= -g - - -all: example bench -debug: example-debug bench-debug - -example: example.cpp - $(CXX) example.cpp -o example-clang $(CXXFLAGS) $(CXX_RELEASE_FLAGS) - -bench: bench.cpp - $(CXX) bench.cpp -o bench-clang $(CXXFLAGS) $(CXX_RELEASE_FLAGS) - - -example-debug: example.cpp - $(CXX) example.cpp -o example-clang-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) - -bench-debug: bench.cpp - $(CXX) bench.cpp -o bench-clang-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) - - - -clean: - rm -f *.o logs/*.txt example-clang example-clang-debug bench-clang bench-clang-debug - - -rebuild: clean all -rebuild-debug: clean debug - - +CXX = clang++ +CXXFLAGS = -march=native -Wall -Wextra -Wshadow -pedantic -std=c++11 -pthread -I../include +CXX_RELEASE_FLAGS = -O2 +CXX_DEBUG_FLAGS= -g + + +all: example bench +debug: example-debug bench-debug + +example: example.cpp + $(CXX) example.cpp -o example-clang $(CXXFLAGS) $(CXX_RELEASE_FLAGS) + +bench: bench.cpp + $(CXX) bench.cpp -o bench-clang $(CXXFLAGS) $(CXX_RELEASE_FLAGS) + + +example-debug: example.cpp + $(CXX) example.cpp -o example-clang-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) + +bench-debug: bench.cpp + $(CXX) bench.cpp -o bench-clang-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) + + + +clean: + rm -f *.o logs/*.txt example-clang example-clang-debug bench-clang bench-clang-debug + + +rebuild: clean all +rebuild-debug: clean debug + + diff --git a/example/Makefile.mingw b/example/Makefile.mingw index 5ee5ab6f..302e0722 100644 --- a/example/Makefile.mingw +++ b/example/Makefile.mingw @@ -1,32 +1,32 @@ -CXX ?= g++ -CXXFLAGS = -D_WIN32_WINNT=0x600 -march=native -Wall -Wextra -Wshadow -pedantic -std=gnu++0x -pthread -Wl,--no-as-needed -I../include -CXX_RELEASE_FLAGS = -O3 -CXX_DEBUG_FLAGS= -g - - -all: example bench -debug: example-debug bench-debug - -example: example.cpp - $(CXX) example.cpp -o example $(CXXFLAGS) $(CXX_RELEASE_FLAGS) - -bench: bench.cpp - $(CXX) bench.cpp -o bench $(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) - - - -clean: - rm -f *.o logs/*.txt example example-debug bench bench-debug - - -rebuild: clean all -rebuild-debug: clean debug - - +CXX ?= g++ +CXXFLAGS = -D_WIN32_WINNT=0x600 -march=native -Wall -Wextra -Wshadow -pedantic -std=gnu++0x -pthread -Wl,--no-as-needed -I../include +CXX_RELEASE_FLAGS = -O3 +CXX_DEBUG_FLAGS= -g + + +all: example bench +debug: example-debug bench-debug + +example: example.cpp + $(CXX) example.cpp -o example $(CXXFLAGS) $(CXX_RELEASE_FLAGS) + +bench: bench.cpp + $(CXX) bench.cpp -o bench $(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) + + + +clean: + rm -f *.o logs/*.txt example example-debug bench bench-debug + + +rebuild: clean all +rebuild-debug: clean debug + + diff --git a/example/example.cpp b/example/example.cpp index b1b2e32f..6144c065 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -58,8 +58,9 @@ int main(int, char *[]) daily_logger->info(123.44); // Customize msg format for all messages - spd::set_pattern("*** [%H:%M:%S %z] [thread %t] %v ***"); - rotating_logger->info("This is another message with custom format"); + spd::set_pattern("[%^+++%$] [%H:%M:%S %z] [thread %t] %v"); + console->info("This an info message with custom format"); + console->error("This an error message with custom format"); // Runtime log levels spd::set_level(spd::level::info); // Set global log level to info diff --git a/include/spdlog/contrib/README.md b/include/spdlog/contrib/README.md new file mode 100644 index 00000000..e3abc34d --- /dev/null +++ b/include/spdlog/contrib/README.md @@ -0,0 +1 @@ +Please put here your contribs. Popular contribs will be moved to main tree after stablization diff --git a/include/spdlog/contrib/sinks/.gitignore b/include/spdlog/contrib/sinks/.gitignore new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/include/spdlog/contrib/sinks/.gitignore @@ -0,0 +1 @@ + diff --git a/include/spdlog/contrib/sinks/step_file_sink.h b/include/spdlog/contrib/sinks/step_file_sink.h new file mode 100644 index 00000000..6be4d8ef --- /dev/null +++ b/include/spdlog/contrib/sinks/step_file_sink.h @@ -0,0 +1,161 @@ +#pragma once + +#include "../../details/file_helper.h" +#include "../../details/null_mutex.h" +#include "../../fmt/fmt.h" +#include "../../sinks/base_sink.h" + +#include +#include +#include +#include +#include +#include +#include + +// Example for spdlog.h +// +// Create a file logger which creates new files with a specified time step and fixed file size: +// +// std::shared_ptr step_logger_mt(const std::string &logger_name, const filename_t &filename, unsigned seconds = 60, const filename_t &tmp_ext = ".tmp", unsigned max_file_size = std::numeric_limits::max()); +// std::shared_ptr step_logger_st(const std::string &logger_name, const filename_t &filename, unsigned seconds = 60, const filename_t &tmp_ext = ".tmp", unsigned max_file_size = std::numeric_limits::max();; + +// Example for spdlog_impl.h +// Create a file logger that creates new files with a specified increment +// inline std::shared_ptr spdlog::step_logger_mt( +// const std::string &logger_name, const filename_t &filename_fmt, unsigned seconds, const filename_t &tmp_ext, unsigned max_file_size) +// { +// return create(logger_name, filename_fmt, seconds, tmp_ext, max_file_size); +// } + +// inline std::shared_ptr spdlog::step_logger_st( +// const std::string &logger_name, const filename_t &filename_fmt, unsigned seconds, const filename_t &tmp_ext, unsigned max_file_size) +// { +// return create(logger_name, filename_fmt, seconds, tmp_ext, max_file_size); +// } + +namespace spdlog { +namespace sinks { + +/* + * Default generator of step log file names. + */ +struct default_step_file_name_calculator +{ + // Create filename for the form filename_YYYY-MM-DD_hh-mm-ss.ext + static std::tuple calc_filename(const filename_t &filename, const filename_t &tmp_ext) + { + std::tm tm = spdlog::details::os::localtime(); + filename_t basename, ext; + std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename); + std::conditional::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w; + w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}-{:02d}{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, + tm.tm_hour, tm.tm_min, tm.tm_sec, tmp_ext); + return std::make_tuple(w.str(), ext); + } +}; + +/* + * Rotating file sink based on size and a specified time step + */ +template +class step_file_sink SPDLOG_FINAL : public base_sink +{ +public: + step_file_sink(filename_t base_filename, unsigned step_seconds, filename_t tmp_ext, unsigned max_size) + : _base_filename(std::move(base_filename)) + , _tmp_ext(std::move(tmp_ext)) + , _step_seconds(step_seconds) + , _max_size(max_size) + { + if (step_seconds == 0) + { + throw spdlog_ex("step_file_sink: Invalid time step in ctor"); + } + if (max_size == 0) + { + throw spdlog_ex("step_file_sink: Invalid max log size in ctor"); + } + + _tp = _next_tp(); + std::tie(_current_filename, _ext) = FileNameCalc::calc_filename(_base_filename, _tmp_ext); + + if (_tmp_ext == _ext) + { + throw spdlog_ex("step_file_sink: The temporary extension matches the specified in ctor"); + } + + _file_helper.open(_current_filename); + _current_size = _file_helper.size(); // expensive. called only once + } + + ~step_file_sink() + { + using details::os::filename_to_str; + + filename_t src =_current_filename, target; + std::tie(target, std::ignore) = details::file_helper::split_by_extenstion(src); + target += _ext; + + details::os::rename(src, target); + } + +protected: + void _sink_it(const details::log_msg &msg) override + { + _current_size += msg.formatted.size(); + if (std::chrono::system_clock::now() >= _tp || _current_size > _max_size) + { + close_current_file(); + + std::tie(_current_filename, std::ignore) = FileNameCalc::calc_filename(_base_filename, _tmp_ext); + _file_helper.open(_current_filename); + _tp = _next_tp(); + _current_size = msg.formatted.size(); + } + _file_helper.write(msg); + } + + void _flush() override + { + _file_helper.flush(); + } + +private: + std::chrono::system_clock::time_point _next_tp() + { + return std::chrono::system_clock::now() + _step_seconds; + } + + void close_current_file() + { + using details::os::filename_to_str; + + filename_t src =_current_filename, target; + std::tie(target, std::ignore) = details::file_helper::split_by_extenstion(src); + target += _ext; + + if (details::file_helper::file_exists(src) && details::os::rename(src, target) != 0) + { + throw spdlog_ex("step_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno); + } + } + + const filename_t _base_filename; + const filename_t _tmp_ext; + const std::chrono::seconds _step_seconds; + const unsigned _max_size; + + std::chrono::system_clock::time_point _tp; + filename_t _current_filename; + filename_t _ext; + unsigned _current_size; + + details::file_helper _file_helper; +}; + +using step_file_sink_mt = step_file_sink; +using step_file_sink_st = step_file_sink; + +} // namespace sinks +} // namespace spdlog diff --git a/include/spdlog/details/log_msg.h b/include/spdlog/details/log_msg.h index c4339c74..7a2d3a84 100644 --- a/include/spdlog/details/log_msg.h +++ b/include/spdlog/details/log_msg.h @@ -40,6 +40,9 @@ struct log_msg fmt::BasicMemoryWriter> raw; fmt::BasicMemoryWriter> formatted; size_t msg_id{0}; + // wrap this range with color codes + size_t color_range_start{0}; + size_t color_range_end{0}; }; } // namespace details } // namespace spdlog diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index aa51d573..4fa8c5db 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -6,7 +6,6 @@ #pragma once #include "../logger.h" -#include "../sinks/stdout_sinks.h" #include #include @@ -345,13 +344,11 @@ inline void spdlog::logger::_default_err_handler(const string &msg) { return; } + _last_err_time = now; auto tm_time = details::os::localtime(now); char date_buf[100]; std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time); - details::log_msg err_msg; - err_msg.formatted.write("[*** LOG ERROR ***] [{}] [{}] [{}]{}", name(), msg, date_buf, details::os::default_eol); - sinks::stderr_sink_mt::instance()->log(err_msg); - _last_err_time = now; + fmt::print(stderr, "[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, name(), msg); } inline bool spdlog::logger::_should_flush_on(const details::log_msg &msg) diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 6b75ba69..0d61810e 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -238,15 +238,14 @@ inline size_t filesize(FILE *f) int fd = fileno(f); // 64 bits(but not in osx or cygwin, where fstat64 is deprecated) #if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__)) && !defined(__CYGWIN__) - struct stat64 st; + struct stat64 st; if (fstat64(fd, &st) == 0) { return static_cast(st.st_size); } #else // unix 32 bits or cygwin - struct stat st - { - }; + struct stat st; + if (fstat(fd, &st) == 0) { return static_cast(st.st_size); diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index 0018b7fa..943c13c8 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -423,6 +423,22 @@ private: string _str; }; +// mark the color range. expect it to be in the form of "%^colored text%$" +class color_start_formatter SPDLOG_FINAL : public flag_formatter +{ + void format(details::log_msg &msg, const std::tm &) override + { + msg.color_range_start = msg.formatted.size(); + } +}; +class color_stop_formatter SPDLOG_FINAL : public flag_formatter +{ + void format(details::log_msg &msg, const std::tm &) override + { + msg.color_range_end = msg.formatted.size(); + } +}; + // Full info formatter // pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v class full_formatter SPDLOG_FINAL : public flag_formatter @@ -464,8 +480,12 @@ class full_formatter SPDLOG_FINAL : public flag_formatter msg.formatted << '[' << *msg.logger_name << "] "; #endif - msg.formatted << '[' << level::to_str(msg.level) << "] "; - msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size()); + msg.formatted << '['; + // wrap the level name with color + msg.color_range_start = msg.formatted.size(); + msg.formatted << level::to_str(msg.level); + msg.color_range_end = msg.formatted.size(); + msg.formatted << "] " << fmt::StringRef(msg.raw.data(), msg.raw.size()); } }; @@ -493,6 +513,7 @@ inline void spdlog::pattern_formatter::compile_pattern(const string &pattern) { _formatters.push_back(std::move(user_chars)); } + // if( if (++it != end) { handle_flag(*it); @@ -647,6 +668,14 @@ inline void spdlog::pattern_formatter::handle_flag(char flag) _formatters.emplace_back(make_unique()); break; + case ('^'): + _formatters.emplace_back(new details::color_start_formatter()); + break; + + case ('$'): + _formatters.emplace_back(new details::color_stop_formatter()); + break; + default: // Unknown flag appears as is _formatters.emplace_back(make_unique('%')); _formatters.emplace_back(make_unique(flag)); @@ -676,5 +705,5 @@ inline void spdlog::pattern_formatter::format(details::log_msg &msg) f->format(msg, tm_time); } // write eol - msg.formatted.write(_eol.data(), _eol.size()); + msg.formatted << _eol; } diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 91bda477..64084b10 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -28,12 +28,19 @@ public: : target_file_(file) { should_do_colors_ = details::os::in_terminal(file) && details::os::is_color_terminal(); - colors_[level::trace] = cyan; + colors_[level::trace] = white; colors_[level::debug] = cyan; +<<<<<<< HEAD colors_[level::info] = reset; colors_[level::warn] = string(yellow) + bold; colors_[level::err] = string(red) + bold; colors_[level::critical] = string(bold) + on_red; +======= + colors_[level::info] = green; + colors_[level::warn] = yellow + bold; + colors_[level::err] = red + bold; + colors_[level::critical] = bold + on_red; +>>>>>>> upstream/master colors_[level::off] = reset; } @@ -83,17 +90,20 @@ protected: { // Wrap the originally formatted message in color codes. // If color is not supported in the terminal, log as is instead. - if (should_do_colors_) + if (should_do_colors_ && msg.color_range_end > msg.color_range_start) { - const string& prefix = colors_[msg.level]; - fwrite(prefix.data(), sizeof(char), prefix.size(), target_file_); - fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), target_file_); - fwrite(reset, sizeof(char), sizeof(reset) / sizeof(*reset), target_file_); - fwrite(clear_line, sizeof(char), sizeof(clear_line) / sizeof(*clear_line), target_file_); + // before color range + _print_range(msg, 0, msg.color_range_start); + // in color range + _print_ccode(colors_[msg.level]); + _print_range(msg, msg.color_range_start, msg.color_range_end); + _print_ccode(reset); + // after color range + _print_range(msg, msg.color_range_end, msg.formatted.size()); } else { - fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), target_file_); + _print_range(msg, 0, msg.formatted.size()); } _flush(); } @@ -103,6 +113,15 @@ protected: fflush(target_file_); } +private: + void _print_ccode(const string &color_code) + { + fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_); + } + void _print_range(const details::log_msg &msg, size_t start, size_t end) + { + fwrite(msg.formatted.data() + start, sizeof(char), end - start, target_file_); + } FILE *target_file_; bool should_do_colors_; unordered_map colors_; diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 5afcab43..a616d7c5 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -25,6 +25,7 @@ class wincolor_sink : public base_sink public: const WORD BOLD = FOREGROUND_INTENSITY; const WORD RED = FOREGROUND_RED; + const WORD GREEN = FOREGROUND_GREEN; const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE; const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN; @@ -32,9 +33,9 @@ public: wincolor_sink(HANDLE std_handle) : out_handle_(std_handle) { - colors_[level::trace] = CYAN; + colors_[level::trace] = WHITE; colors_[level::debug] = CYAN; - colors_[level::info] = WHITE | BOLD; + colors_[level::info] = GREEN; colors_[level::warn] = YELLOW | BOLD; colors_[level::err] = RED | BOLD; // red bold colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background @@ -59,10 +60,22 @@ public: protected: void _sink_it(const details::log_msg &msg) override { - auto color = colors_[msg.level]; - auto orig_attribs = set_console_attribs(color); - WriteConsoleA(out_handle_, msg.formatted.data(), static_cast(msg.formatted.size()), nullptr, nullptr); - SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors + if (msg.color_range_end > msg.color_range_start) + { + // before color range + _print_range(msg, 0, msg.color_range_start); + + // in color range + auto orig_attribs = set_console_attribs(colors_[msg.level]); + _print_range(msg, msg.color_range_start, msg.color_range_end); + ::SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors + // after color range + _print_range(msg, msg.color_range_end, msg.formatted.size()); + } + else // print without colors if color range is invalid + { + _print_range(msg, 0, msg.formatted.size()); + } } void _flush() override @@ -86,6 +99,13 @@ private: SetConsoleTextAttribute(out_handle_, attribs | back_color); return orig_buffer_info.wAttributes; // return orig attribs } + + // print a range of formatted message to console + void _print_range(const details::log_msg &msg, size_t start, size_t end) + { + DWORD size = static_cast(end - start); + WriteConsoleA(out_handle_, msg.formatted.data() + start, size, nullptr, nullptr); + } }; // diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index 5c2be1f2..c6700041 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -8,7 +8,9 @@ static std::string log_to_str(const std::string &msg, const std::shared_ptr("%^%v%$", spdlog::pattern_time_type::local, "\n"); + spdlog::details::log_msg msg; + msg.raw << "Hello"; + formatter->format(msg); + REQUIRE(msg.color_range_start == 0); + REQUIRE(msg.color_range_end == 5); + REQUIRE(log_to_str("hello", formatter) == "hello\n"); +} + +TEST_CASE("color range test2", "[pattern_formatter]") +{ + auto formatter = std::make_shared("%^%$", spdlog::pattern_time_type::local, "\n"); + spdlog::details::log_msg msg; + formatter->format(msg); + REQUIRE(msg.color_range_start == 0); + REQUIRE(msg.color_range_end == 0); + REQUIRE(log_to_str("", formatter) == "\n"); +} + +TEST_CASE("color range test3", "[pattern_formatter]") +{ + auto formatter = std::make_shared("%^***%$"); + spdlog::details::log_msg msg; + formatter->format(msg); + REQUIRE(msg.color_range_start == 0); + REQUIRE(msg.color_range_end == 3); +} + +TEST_CASE("color range test4", "[pattern_formatter]") +{ + auto formatter = std::make_shared("XX%^YYY%$", spdlog::pattern_time_type::local, "\n"); + spdlog::details::log_msg msg; + msg.raw << "ignored"; + formatter->format(msg); + REQUIRE(msg.color_range_start == 2); + REQUIRE(msg.color_range_end == 5); + REQUIRE(log_to_str("ignored", formatter) == "XXYYY\n"); +} + +TEST_CASE("color range test5", "[pattern_formatter]") +{ + auto formatter = std::make_shared("**%^"); + spdlog::details::log_msg msg; + formatter->format(msg); + REQUIRE(msg.color_range_start == 2); + REQUIRE(msg.color_range_end == 0); +} + +TEST_CASE("color range test6", "[pattern_formatter]") +{ + auto formatter = std::make_shared("**%$"); + spdlog::details::log_msg msg; + formatter->format(msg); + REQUIRE(msg.color_range_start == 0); + REQUIRE(msg.color_range_end == 2); +} diff --git a/tests/utils.cpp b/tests/utils.cpp index 2c50ce36..5afdb4cf 100644 --- a/tests/utils.cpp +++ b/tests/utils.cpp @@ -8,6 +8,7 @@ void prepare_logdir() system("del /F /Q logs\\*"); #else auto rv = system("mkdir -p logs"); + (void)rv; rv = system("rm -f logs/*"); (void)rv; #endif