diff --git a/.travis.yml b/.travis.yml index c65e84d4..85273844 100644 --- a/.travis.yml +++ b/.travis.yml @@ -85,6 +85,7 @@ script: - ./"${BIN}" - valgrind --trace-children=yes --leak-check=full ./"${BIN}" - cd $CHECKOUT_PATH/tests; make rebuild; ./tests + - cd $CHECKOUT_PATH/tests; STYLE=printf make rebuild; ./tests notifications: email: false diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 16222909..dbbba16f 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -8,6 +8,14 @@ #include "spdlog/logger.h" #include "spdlog/sinks/stdout_sinks.h" +#if defined(SPDLOG_FMT_PRINTF) +#if !defined(SPDLOG_FMT_EXTERNAL) +#include "spdlog/fmt/bundled/printf.h" +#else //external fmtlib +#include +#endif +#endif + #include #include @@ -58,7 +66,6 @@ inline void spdlog::logger::set_pattern(const std::string& pattern, pattern_time _set_pattern(pattern, pattern_time); } - template inline void spdlog::logger::log(level::level_enum lvl, const char* fmt, const Args&... args) { @@ -67,7 +74,12 @@ inline void spdlog::logger::log(level::level_enum lvl, const char* fmt, const Ar try { details::log_msg log_msg(&_name, lvl); + +#if defined(SPDLOG_FMT_PRINTF) + fmt::printf(log_msg.raw, fmt, args...); +#else log_msg.raw.write(fmt, args...); +#endif _sink_it(log_msg); } catch (const std::exception &ex) diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h index 8d5d9485..8c424497 100644 --- a/include/spdlog/tweakme.h +++ b/include/spdlog/tweakme.h @@ -96,6 +96,14 @@ /////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to use printf-style messages in your logs instead of the usual +// format-style used by default. +// +// #define SPDLOG_FMT_PRINTF +/////////////////////////////////////////////////////////////////////////////// + + /////////////////////////////////////////////////////////////////////////////// // Uncomment to enable syslog (disabled by default) // diff --git a/tests/Makefile b/tests/Makefile index 97871bac..b1935e75 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,5 +1,11 @@ CXX ?= g++ -CXXFLAGS = -Wall -pedantic -std=c++11 -pthread -O2 -I../include +ifeq ($(STYLE),printf) + $(info *** PRINTF STYLE ***) + CXXFLAGS = -DSPDLOG_FMT_PRINTF -Wall -pedantic -std=c++11 -pthread -O2 -I../include +else + $(info *** FORMAT STYLE ***) + CXXFLAGS = -Wall -pedantic -std=c++11 -pthread -O2 -I../include +endif LDPFALGS = -pthread CPP_FILES := $(wildcard *.cpp) diff --git a/tests/cond_logging.cpp b/tests/cond_logging.cpp index dd5a6ced..d501664a 100644 --- a/tests/cond_logging.cpp +++ b/tests/cond_logging.cpp @@ -135,6 +135,7 @@ TEST_CASE("conditional_trace_varargs", "[conditional_trace_varargs]") //const char for (auto i = 0; i < 2; i++) { +#if !defined(SPDLOG_FMT_PRINTF) REQUIRE(conditional_log_varags(spdlog::level::trace, (i % 2 == 0), "Hello {}", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); REQUIRE(conditional_log_varags(spdlog::level::debug, (i % 2 == 0), "Hello {}", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); REQUIRE(conditional_log_varags(spdlog::level::info, (i % 2 == 0), "Hello {}", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); @@ -150,5 +151,24 @@ TEST_CASE("conditional_trace_varargs", "[conditional_trace_varargs]") REQUIRE(conditional_log_varags(spdlog::level::err, (i % 2 == 0), L"Hello {}", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); REQUIRE(conditional_log_varags(spdlog::level::critical, (i % 2 == 0), L"Hello {}", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT + +#else + REQUIRE(conditional_log_varags(spdlog::level::trace, (i % 2 == 0), "Hello %d", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); + REQUIRE(conditional_log_varags(spdlog::level::debug, (i % 2 == 0), "Hello %d", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); + REQUIRE(conditional_log_varags(spdlog::level::info, (i % 2 == 0), "Hello %d", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); + REQUIRE(conditional_log_varags(spdlog::level::warn, (i % 2 == 0), "Hello %d", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); + REQUIRE(conditional_log_varags(spdlog::level::err, (i % 2 == 0), "Hello %d", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); + REQUIRE(conditional_log_varags(spdlog::level::critical, (i % 2 == 0), "Hello %d", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); + +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + REQUIRE(conditional_log_varags(spdlog::level::trace, (i % 2 == 0), L"Hello %d", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); + REQUIRE(conditional_log_varags(spdlog::level::debug, (i % 2 == 0), L"Hello %d", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); + REQUIRE(conditional_log_varags(spdlog::level::info, (i % 2 == 0), L"Hello %d", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); + REQUIRE(conditional_log_varags(spdlog::level::warn, (i % 2 == 0), L"Hello %d", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); + REQUIRE(conditional_log_varags(spdlog::level::err, (i % 2 == 0), L"Hello %d", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); + REQUIRE(conditional_log_varags(spdlog::level::critical, (i % 2 == 0), L"Hello %d", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); +#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT + +#endif // !defined(SPDLOG_FMT_PRINTF) } } \ No newline at end of file diff --git a/tests/errors.cpp b/tests/errors.cpp index 75de900a..83af2051 100644 --- a/tests/errors.cpp +++ b/tests/errors.cpp @@ -26,8 +26,13 @@ TEST_CASE("default_error_handler", "[errors]]") auto logger = spdlog::create("logger", filename, true); logger->set_pattern("%v"); +#if !defined(SPDLOG_FMT_PRINTF) logger->info("Test message {} {}", 1); logger->info("Test message {}", 2); +#else + logger->info("Test message %d %d", 1); + logger->info("Test message %d", 2); +#endif logger->flush(); REQUIRE(file_contents(filename) == std::string("Test message 2\n")); @@ -50,7 +55,11 @@ TEST_CASE("custom_error_handler", "[errors]]") throw custom_ex(); }); logger->info("Good message #1"); +#if !defined(SPDLOG_FMT_PRINTF) REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex); +#else + REQUIRE_THROWS_AS(logger->info("Bad format msg %s %s", "xxx"), custom_ex); +#endif logger->info("Good message #2"); REQUIRE(count_lines(filename) == 2); } @@ -81,7 +90,11 @@ TEST_CASE("async_error_handler", "[errors]]") ofs << err_msg; }); logger->info("Good message #1"); +#if !defined(SPDLOG_FMT_PRINTF) logger->info("Bad format msg {} {}", "xxx"); +#else + logger->info("Bad format msg %s %s", "xxx"); +#endif logger->info("Good message #2"); spdlog::drop("logger"); //force logger to drain the queue and shutdown spdlog::set_sync_mode(); diff --git a/tests/file_log.cpp b/tests/file_log.cpp index 45f6e8c1..ff29a898 100644 --- a/tests/file_log.cpp +++ b/tests/file_log.cpp @@ -12,9 +12,13 @@ TEST_CASE("simple_file_logger", "[simple_logger]]") auto logger = spdlog::create("logger", filename); logger->set_pattern("%v"); - +#if !defined(SPDLOG_FMT_PRINTF) logger->info("Test message {}", 1); logger->info("Test message {}", 2); +#else + logger->info("Test message %d", 1); + logger->info("Test message %d", 2); +#endif logger->flush(); REQUIRE(file_contents(filename) == std::string("Test message 1\nTest message 2\n")); REQUIRE(count_lines(filename) == 2); @@ -33,8 +37,13 @@ TEST_CASE("flush_on", "[flush_on]]") logger->trace("Should not be flushed"); REQUIRE(count_lines(filename) == 0); +#if !defined(SPDLOG_FMT_PRINTF) logger->info("Test message {}", 1); logger->info("Test message {}", 2); +#else + logger->info("Test message %d", 1); + logger->info("Test message %d", 2); +#endif logger->flush(); REQUIRE(file_contents(filename) == std::string("Should not be flushed\nTest message 1\nTest message 2\n")); REQUIRE(count_lines(filename) == 3); @@ -47,7 +56,13 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]") auto logger = spdlog::rotating_logger_mt("logger", basename, 1024, 0); for (int i = 0; i < 10; ++i) + { +#if !defined(SPDLOG_FMT_PRINTF) logger->info("Test message {}", i); +#else + logger->info("Test message %d", i); +#endif + } logger->flush(); auto filename = basename; @@ -67,7 +82,13 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]") auto filename = basename; REQUIRE(count_lines(filename) == 10); for (int i = 0; i < 1000; i++) + { +#if !defined(SPDLOG_FMT_PRINTF) logger->info("Test message {}", i); +#else + logger->info("Test message %d", i); +#endif + } logger->flush(); REQUIRE(get_filesize(filename) <= 1024); @@ -88,7 +109,13 @@ TEST_CASE("daily_logger", "[daily_logger]]") auto logger = spdlog::daily_logger_mt("logger", basename, 0, 0); logger->flush_on(spdlog::level::info); for (int i = 0; i < 10; ++i) + { +#if !defined(SPDLOG_FMT_PRINTF) logger->info("Test message {}", i); +#else + logger->info("Test message %d", i); +#endif + } auto filename = w.str(); REQUIRE(count_lines(filename) == 10); @@ -110,7 +137,13 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]") auto logger = spdlog::create("logger", basename, 0, 0); for (int i = 0; i < 10; ++i) + { +#if !defined(SPDLOG_FMT_PRINTF) logger->info("Test message {}", i); +#else + logger->info("Test message %d", i); +#endif + } logger->flush(); auto filename = w.str(); REQUIRE(count_lines(filename) == 10); @@ -142,7 +175,13 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]") auto logger = spdlog::create("logger", basename, 0, 0); for (int i = 0; i < 10; ++i) + { +#if !defined(SPDLOG_FMT_PRINTF) logger->info("Test message {}", i); +#else + logger->info("Test message %d", i); +#endif + } logger->flush(); auto filename = w.str();