mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 16:35:45 +08:00
allow printf style logging
* tests updated for printf style * tests makefile support printf style
This commit is contained in:
parent
552d6214e0
commit
ee525f9bef
@ -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
|
||||
|
@ -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 <fmt/printf.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
@ -58,7 +66,6 @@ inline void spdlog::logger::set_pattern(const std::string& pattern, pattern_time
|
||||
_set_pattern(pattern, pattern_time);
|
||||
}
|
||||
|
||||
|
||||
template <typename... Args>
|
||||
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)
|
||||
|
@ -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)
|
||||
//
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
@ -26,8 +26,13 @@ TEST_CASE("default_error_handler", "[errors]]")
|
||||
|
||||
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("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();
|
||||
|
@ -12,9 +12,13 @@ TEST_CASE("simple_file_logger", "[simple_logger]]")
|
||||
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("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<sink_type>("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<sink_type>("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();
|
||||
|
Loading…
Reference in New Issue
Block a user