mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-24 17:41:34 +08:00
Updated clang-format
Merged with origin
This commit is contained in:
parent
218e859867
commit
dcd5904bdc
@ -6,12 +6,13 @@ Standard: c++17
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
UseTab: Never
|
||||
ColumnLimit: 120
|
||||
ColumnLimit: 100
|
||||
AlignAfterOpenBracket: Align
|
||||
BinPackParameters: false
|
||||
AlignEscapedNewlines: Left
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
PackConstructorInitializers: Never
|
||||
BreakConstructorInitializersBeforeComma: true
|
||||
IndentPPDirectives: BeforeHash
|
||||
SortIncludes: Never
|
||||
...
|
||||
|
@ -11,11 +11,11 @@
|
||||
#include "spdlog/sinks/basic_file_sink.h"
|
||||
|
||||
#if defined(SPDLOG_USE_STD_FORMAT)
|
||||
# include <format>
|
||||
#include <format>
|
||||
#elif defined(SPDLOG_FMT_EXTERNAL)
|
||||
# include <fmt/format.h>
|
||||
#include <fmt/format.h>
|
||||
#else
|
||||
# include "spdlog/fmt/bundled/format.h"
|
||||
#include "spdlog/fmt/bundled/format.h"
|
||||
#endif
|
||||
|
||||
#include "utils.h"
|
||||
@ -34,17 +34,15 @@ using namespace utils;
|
||||
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable : 4996) // disable fopen warning under msvc
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4996) // disable fopen warning under msvc
|
||||
#endif // _MSC_VER
|
||||
|
||||
int count_lines(const char *filename)
|
||||
{
|
||||
int count_lines(const char *filename) {
|
||||
int counter = 0;
|
||||
auto *infile = fopen(filename, "r");
|
||||
int ch;
|
||||
while (EOF != (ch = getc(infile)))
|
||||
{
|
||||
while (EOF != (ch = getc(infile))) {
|
||||
if ('\n' == ch)
|
||||
counter++;
|
||||
}
|
||||
@ -53,35 +51,31 @@ int count_lines(const char *filename)
|
||||
return counter;
|
||||
}
|
||||
|
||||
void verify_file(const char *filename, int expected_count)
|
||||
{
|
||||
void verify_file(const char *filename, int expected_count) {
|
||||
spdlog::info("Verifying {} to contain {} line..", filename, expected_count);
|
||||
auto count = count_lines(filename);
|
||||
if (count != expected_count)
|
||||
{
|
||||
spdlog::error("Test failed. {} has {} lines instead of {}", filename, count, expected_count);
|
||||
if (count != expected_count) {
|
||||
spdlog::error("Test failed. {} has {} lines instead of {}", filename, count,
|
||||
expected_count);
|
||||
exit(1);
|
||||
}
|
||||
spdlog::info("Line count OK ({})\n", count);
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning(pop)
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
int howmany = 1000000;
|
||||
int queue_size = std::min(howmany + 2, 8192);
|
||||
int threads = 10;
|
||||
int iters = 3;
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
spdlog::set_pattern("[%^%l%$] %v");
|
||||
if (argc == 1)
|
||||
{
|
||||
if (argc == 1) {
|
||||
spdlog::info("Usage: {} <message_count> <threads> <q_size> <iterations>", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
@ -90,11 +84,9 @@ int main(int argc, char *argv[])
|
||||
howmany = atoi(argv[1]);
|
||||
if (argc > 2)
|
||||
threads = atoi(argv[2]);
|
||||
if (argc > 3)
|
||||
{
|
||||
if (argc > 3) {
|
||||
queue_size = atoi(argv[3]);
|
||||
if (queue_size > 500000)
|
||||
{
|
||||
if (queue_size > 500000) {
|
||||
spdlog::error("Max queue size allowed: 500,000");
|
||||
exit(1);
|
||||
}
|
||||
@ -108,7 +100,8 @@ int main(int argc, char *argv[])
|
||||
spdlog::info("Messages : {:L}", howmany);
|
||||
spdlog::info("Threads : {:L}", threads);
|
||||
spdlog::info("Queue : {:L} slots", queue_size);
|
||||
spdlog::info("Queue memory : {:L} x {:L} = {:L} KB ", queue_size, slot_size, (queue_size * slot_size) / 1024);
|
||||
spdlog::info("Queue memory : {:L} x {:L} = {:L} KB ", queue_size, slot_size,
|
||||
(queue_size * slot_size) / 1024);
|
||||
spdlog::info("Total iters : {:L}", iters);
|
||||
spdlog::info("-------------------------------------------------");
|
||||
|
||||
@ -117,11 +110,11 @@ int main(int argc, char *argv[])
|
||||
spdlog::info("*********************************");
|
||||
spdlog::info("Queue Overflow Policy: block");
|
||||
spdlog::info("*********************************");
|
||||
for (int i = 0; i < iters; i++)
|
||||
{
|
||||
for (int i = 0; i < iters; i++) {
|
||||
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
||||
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
|
||||
auto logger = std::make_shared<async_logger>("async_logger", std::move(file_sink), std::move(tp), async_overflow_policy::block);
|
||||
auto logger = std::make_shared<async_logger>(
|
||||
"async_logger", std::move(file_sink), std::move(tp), async_overflow_policy::block);
|
||||
bench_mt(howmany, std::move(logger), threads);
|
||||
// verify_file(filename, howmany);
|
||||
}
|
||||
@ -132,18 +125,16 @@ int main(int argc, char *argv[])
|
||||
spdlog::info("*********************************");
|
||||
// do same test but discard oldest if queue is full instead of blocking
|
||||
filename = "logs/basic_async-overrun.log";
|
||||
for (int i = 0; i < iters; i++)
|
||||
{
|
||||
for (int i = 0; i < iters; i++) {
|
||||
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
||||
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
|
||||
auto logger =
|
||||
std::make_shared<async_logger>("async_logger", std::move(file_sink), std::move(tp), async_overflow_policy::overrun_oldest);
|
||||
std::make_shared<async_logger>("async_logger", std::move(file_sink), std::move(tp),
|
||||
async_overflow_policy::overrun_oldest);
|
||||
bench_mt(howmany, std::move(logger), threads);
|
||||
}
|
||||
spdlog::shutdown();
|
||||
}
|
||||
catch (std::exception &ex)
|
||||
{
|
||||
} catch (std::exception &ex) {
|
||||
std::cerr << "Error: " << ex.what() << std::endl;
|
||||
perror("Last error");
|
||||
return 1;
|
||||
@ -151,32 +142,28 @@ int main(int argc, char *argv[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
void thread_fun(std::shared_ptr<spdlog::logger> logger, int howmany)
|
||||
{
|
||||
for (int i = 0; i < howmany; i++)
|
||||
{
|
||||
void thread_fun(std::shared_ptr<spdlog::logger> logger, int howmany) {
|
||||
for (int i = 0; i < howmany; i++) {
|
||||
logger->info("Hello logger: msg number {}", i);
|
||||
}
|
||||
}
|
||||
|
||||
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> logger, int thread_count)
|
||||
{
|
||||
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> logger, int thread_count) {
|
||||
using std::chrono::high_resolution_clock;
|
||||
vector<std::thread> threads;
|
||||
auto start = high_resolution_clock::now();
|
||||
|
||||
int msgs_per_thread = howmany / thread_count;
|
||||
int msgs_per_thread_mod = howmany % thread_count;
|
||||
for (int t = 0; t < thread_count; ++t)
|
||||
{
|
||||
for (int t = 0; t < thread_count; ++t) {
|
||||
if (t == 0 && msgs_per_thread_mod)
|
||||
threads.push_back(std::thread(thread_fun, logger, msgs_per_thread + msgs_per_thread_mod));
|
||||
threads.push_back(
|
||||
std::thread(thread_fun, logger, msgs_per_thread + msgs_per_thread_mod));
|
||||
else
|
||||
threads.push_back(std::thread(thread_fun, logger, msgs_per_thread));
|
||||
}
|
||||
|
||||
for (auto &t : threads)
|
||||
{
|
||||
for (auto &t : threads) {
|
||||
t.join();
|
||||
};
|
||||
|
||||
|
@ -13,11 +13,11 @@
|
||||
#include "spdlog/sinks/rotating_file_sink.h"
|
||||
|
||||
#if defined(SPDLOG_USE_STD_FORMAT)
|
||||
# include <format>
|
||||
#include <format>
|
||||
#elif defined(SPDLOG_FMT_EXTERNAL)
|
||||
# include <fmt/format.h>
|
||||
#include <fmt/format.h>
|
||||
#else
|
||||
# include "spdlog/fmt/bundled/format.h"
|
||||
#include "spdlog/fmt/bundled/format.h"
|
||||
#endif
|
||||
|
||||
#include "utils.h"
|
||||
@ -37,17 +37,18 @@ static const size_t file_size = 30 * 1024 * 1024;
|
||||
static const size_t rotating_files = 5;
|
||||
static const int max_threads = 1000;
|
||||
|
||||
void bench_threaded_logging(size_t threads, int iters)
|
||||
{
|
||||
void bench_threaded_logging(size_t threads, int iters) {
|
||||
spdlog::info("**************************************************************");
|
||||
spdlog::info(spdlog::fmt_lib::format(std::locale("en_US.UTF-8"), "Multi threaded: {:L} threads, {:L} messages", threads, iters));
|
||||
spdlog::info(spdlog::fmt_lib::format(
|
||||
std::locale("en_US.UTF-8"), "Multi threaded: {:L} threads, {:L} messages", threads, iters));
|
||||
spdlog::info("**************************************************************");
|
||||
|
||||
auto basic_mt = spdlog::basic_logger_mt("basic_mt", "logs/basic_mt.log", true);
|
||||
bench_mt(iters, std::move(basic_mt), threads);
|
||||
|
||||
spdlog::info("");
|
||||
auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "logs/rotating_mt.log", file_size, rotating_files);
|
||||
auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "logs/rotating_mt.log", file_size,
|
||||
rotating_files);
|
||||
bench_mt(iters, std::move(rotating_mt), threads);
|
||||
|
||||
spdlog::info("");
|
||||
@ -60,17 +61,18 @@ void bench_threaded_logging(size_t threads, int iters)
|
||||
bench(iters, empty_logger);
|
||||
}
|
||||
|
||||
void bench_single_threaded(int iters)
|
||||
{
|
||||
void bench_single_threaded(int iters) {
|
||||
spdlog::info("**************************************************************");
|
||||
spdlog::info(spdlog::fmt_lib::format(std::locale("en_US.UTF-8"), "Single threaded: {} messages", iters));
|
||||
spdlog::info(
|
||||
spdlog::fmt_lib::format(std::locale("en_US.UTF-8"), "Single threaded: {} messages", iters));
|
||||
spdlog::info("**************************************************************");
|
||||
|
||||
auto basic_st = spdlog::basic_logger_st("basic_st", "logs/basic_st.log", true);
|
||||
bench(iters, std::move(basic_st));
|
||||
|
||||
spdlog::info("");
|
||||
auto rotating_st = spdlog::rotating_logger_st("rotating_st", "logs/rotating_st.log", file_size, rotating_files);
|
||||
auto rotating_st = spdlog::rotating_logger_st("rotating_st", "logs/rotating_st.log", file_size,
|
||||
rotating_files);
|
||||
bench(iters, std::move(rotating_st));
|
||||
|
||||
spdlog::info("");
|
||||
@ -83,63 +85,55 @@ void bench_single_threaded(int iters)
|
||||
bench(iters, empty_logger);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
spdlog::set_automatic_registration(false);
|
||||
spdlog::default_logger()->set_pattern("[%^%l%$] %v");
|
||||
int iters = 250000;
|
||||
size_t threads = 4;
|
||||
try
|
||||
{
|
||||
try {
|
||||
|
||||
if (argc > 1)
|
||||
{
|
||||
if (argc > 1) {
|
||||
iters = std::stoi(argv[1]);
|
||||
}
|
||||
if (argc > 2)
|
||||
{
|
||||
if (argc > 2) {
|
||||
threads = std::stoul(argv[2]);
|
||||
}
|
||||
|
||||
if (threads > max_threads)
|
||||
{
|
||||
throw std::runtime_error(spdlog::fmt_lib::format("Number of threads exceeds maximum({})", max_threads));
|
||||
if (threads > max_threads) {
|
||||
throw std::runtime_error(
|
||||
spdlog::fmt_lib::format("Number of threads exceeds maximum({})", max_threads));
|
||||
}
|
||||
|
||||
bench_single_threaded(iters);
|
||||
bench_threaded_logging(1, iters);
|
||||
bench_threaded_logging(threads, iters);
|
||||
}
|
||||
catch (std::exception &ex)
|
||||
{
|
||||
} catch (std::exception &ex) {
|
||||
spdlog::error(ex.what());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void bench(int howmany, std::shared_ptr<spdlog::logger> log)
|
||||
{
|
||||
void bench(int howmany, std::shared_ptr<spdlog::logger> log) {
|
||||
using std::chrono::duration;
|
||||
using std::chrono::duration_cast;
|
||||
using std::chrono::high_resolution_clock;
|
||||
|
||||
auto start = high_resolution_clock::now();
|
||||
for (auto i = 0; i < howmany; ++i)
|
||||
{
|
||||
for (auto i = 0; i < howmany; ++i) {
|
||||
log->info("Hello logger: msg number {}", i);
|
||||
}
|
||||
|
||||
auto delta = high_resolution_clock::now() - start;
|
||||
auto delta_d = duration_cast<duration<double>>(delta).count();
|
||||
|
||||
spdlog::info(spdlog::fmt_lib::format(
|
||||
std::locale("en_US.UTF-8"), "{:<30} Elapsed: {:0.2f} secs {:>16L}/sec", log->name(), delta_d, size_t(howmany / delta_d)));
|
||||
spdlog::info(spdlog::fmt_lib::format(std::locale("en_US.UTF-8"),
|
||||
"{:<30} Elapsed: {:0.2f} secs {:>16L}/sec", log->name(),
|
||||
delta_d, size_t(howmany / delta_d)));
|
||||
spdlog::drop(log->name());
|
||||
}
|
||||
|
||||
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, size_t thread_count)
|
||||
{
|
||||
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, size_t thread_count) {
|
||||
using std::chrono::duration;
|
||||
using std::chrono::duration_cast;
|
||||
using std::chrono::high_resolution_clock;
|
||||
@ -147,25 +141,23 @@ void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, size_t thread_co
|
||||
std::vector<std::thread> threads;
|
||||
threads.reserve(thread_count);
|
||||
auto start = high_resolution_clock::now();
|
||||
for (size_t t = 0; t < thread_count; ++t)
|
||||
{
|
||||
for (size_t t = 0; t < thread_count; ++t) {
|
||||
threads.emplace_back([&]() {
|
||||
for (int j = 0; j < howmany / static_cast<int>(thread_count); j++)
|
||||
{
|
||||
for (int j = 0; j < howmany / static_cast<int>(thread_count); j++) {
|
||||
log->info("Hello logger: msg number {}", j);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for (auto &t : threads)
|
||||
{
|
||||
for (auto &t : threads) {
|
||||
t.join();
|
||||
};
|
||||
|
||||
auto delta = high_resolution_clock::now() - start;
|
||||
auto delta_d = duration_cast<duration<double>>(delta).count();
|
||||
spdlog::info(spdlog::fmt_lib::format(
|
||||
std::locale("en_US.UTF-8"), "{:<30} Elapsed: {:0.2f} secs {:>16L}/sec", log->name(), delta_d, size_t(howmany / delta_d)));
|
||||
spdlog::info(spdlog::fmt_lib::format(std::locale("en_US.UTF-8"),
|
||||
"{:<30} Elapsed: {:0.2f} secs {:>16L}/sec", log->name(),
|
||||
delta_d, size_t(howmany / delta_d)));
|
||||
spdlog::drop(log->name());
|
||||
}
|
||||
|
||||
@ -188,7 +180,8 @@ void bench_default_api(int howmany, std::shared_ptr<spdlog::logger> log)
|
||||
auto delta_d = duration_cast<duration<double>>(delta).count();
|
||||
spdlog::drop(log->name());
|
||||
spdlog::set_default_logger(std::move(orig_default));
|
||||
spdlog::info("{:<30} Elapsed: {:0.2f} secs {:>16}/sec", log->name(), delta_d, int(howmany / delta_d));
|
||||
spdlog::info("{:<30} Elapsed: {:0.2f} secs {:>16}/sec", log->name(), delta_d, int(howmany /
|
||||
delta_d));
|
||||
}
|
||||
|
||||
void bench_c_string(int howmany, std::shared_ptr<spdlog::logger> log)
|
||||
@ -197,11 +190,12 @@ void bench_c_string(int howmany, std::shared_ptr<spdlog::logger> log)
|
||||
using std::chrono::duration;
|
||||
using std::chrono::duration_cast;
|
||||
|
||||
const char *msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra metus cursus "
|
||||
"lacus placerat congue. Nulla egestas, mauris a tincidunt tempus, enim lectus volutpat mi, eu consequat sem "
|
||||
"libero nec massa. In dapibus ipsum a diam rhoncus gravida. Etiam non dapibus eros. Donec fringilla dui sed "
|
||||
"augue pretium, nec scelerisque est maximus. Nullam convallis, sem nec blandit maximus, nisi turpis ornare "
|
||||
"nisl, sit amet volutpat neque massa eu odio. Maecenas malesuada quam ex, posuere congue nibh turpis duis.";
|
||||
const char *msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra
|
||||
metus cursus " "lacus placerat congue. Nulla egestas, mauris a tincidunt tempus, enim lectus
|
||||
volutpat mi, eu consequat sem " "libero nec massa. In dapibus ipsum a diam rhoncus gravida. Etiam
|
||||
non dapibus eros. Donec fringilla dui sed " "augue pretium, nec scelerisque est maximus. Nullam
|
||||
convallis, sem nec blandit maximus, nisi turpis ornare " "nisl, sit amet volutpat neque massa eu
|
||||
odio. Maecenas malesuada quam ex, posuere congue nibh turpis duis.";
|
||||
|
||||
auto orig_default = spdlog::default_logger();
|
||||
spdlog::set_default_logger(log);
|
||||
@ -215,7 +209,8 @@ void bench_c_string(int howmany, std::shared_ptr<spdlog::logger> log)
|
||||
auto delta_d = duration_cast<duration<double>>(delta).count();
|
||||
spdlog::drop(log->name());
|
||||
spdlog::set_default_logger(std::move(orig_default));
|
||||
spdlog::info("{:<30} Elapsed: {:0.2f} secs {:>16}/sec", log->name(), delta_d, int(howmany / delta_d));
|
||||
spdlog::info("{:<30} Elapsed: {:0.2f} secs {:>16}/sec", log->name(), delta_d, int(howmany /
|
||||
delta_d));
|
||||
}
|
||||
|
||||
*/
|
@ -8,31 +8,28 @@
|
||||
#include "spdlog/spdlog.h"
|
||||
#include "spdlog/pattern_formatter.h"
|
||||
|
||||
void bench_formatter(benchmark::State &state, std::string pattern)
|
||||
{
|
||||
void bench_formatter(benchmark::State &state, std::string pattern) {
|
||||
auto formatter = std::make_unique<spdlog::pattern_formatter>(pattern);
|
||||
spdlog::memory_buf_t dest;
|
||||
std::string logger_name = "logger-name";
|
||||
const char *text = "Hello. This is some message with length of 80 ";
|
||||
const char *text =
|
||||
"Hello. This is some message with length of 80 ";
|
||||
|
||||
spdlog::source_loc source_loc{"a/b/c/d/myfile.cpp", 123, "some_func()"};
|
||||
spdlog::details::log_msg msg(source_loc, logger_name, spdlog::level::info, text);
|
||||
|
||||
for (auto _ : state)
|
||||
{
|
||||
for (auto _ : state) {
|
||||
dest.clear();
|
||||
formatter->format(msg, dest);
|
||||
benchmark::DoNotOptimize(dest);
|
||||
}
|
||||
}
|
||||
|
||||
void bench_formatters()
|
||||
{
|
||||
void bench_formatters() {
|
||||
// basic patterns(single flag)
|
||||
std::string all_flags = "+vtPnlLaAbBcCYDmdHIMSefFprRTXzEisg@luioO%";
|
||||
std::vector<std::string> basic_patterns;
|
||||
for (auto &flag : all_flags)
|
||||
{
|
||||
for (auto &flag : all_flags) {
|
||||
auto pattern = std::string("%") + flag;
|
||||
benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern);
|
||||
|
||||
@ -50,29 +47,24 @@ void bench_formatters()
|
||||
"[%Y-%m-%d %H:%M:%S.%e] [%l] [%n] %v",
|
||||
"[%Y-%m-%d %H:%M:%S.%e] [%l] [%n] [%t] %v",
|
||||
};
|
||||
for (auto &pattern : patterns)
|
||||
{
|
||||
benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern)->Iterations(2500000);
|
||||
for (auto &pattern : patterns) {
|
||||
benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern)
|
||||
->Iterations(2500000);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
spdlog::set_pattern("[%^%l%$] %v");
|
||||
if (argc != 2)
|
||||
{
|
||||
if (argc != 2) {
|
||||
spdlog::error("Usage: {} <pattern> (or \"all\" to bench all)", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
std::string pattern = argv[1];
|
||||
if (pattern == "all")
|
||||
{
|
||||
if (pattern == "all") {
|
||||
bench_formatters();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern);
|
||||
}
|
||||
benchmark::Initialize(&argc, argv);
|
||||
|
@ -16,76 +16,72 @@
|
||||
#include "spdlog/sinks/null_sink.h"
|
||||
#include "spdlog/sinks/rotating_file_sink.h"
|
||||
|
||||
void bench_c_string(benchmark::State &state, std::shared_ptr<spdlog::logger> logger)
|
||||
{
|
||||
const char *msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra metus cursus "
|
||||
"lacus placerat congue. Nulla egestas, mauris a tincidunt tempus, enim lectus volutpat mi, eu consequat sem "
|
||||
"libero nec massa. In dapibus ipsum a diam rhoncus gravida. Etiam non dapibus eros. Donec fringilla dui sed "
|
||||
"augue pretium, nec scelerisque est maximus. Nullam convallis, sem nec blandit maximus, nisi turpis ornare "
|
||||
"nisl, sit amet volutpat neque massa eu odio. Maecenas malesuada quam ex, posuere congue nibh turpis duis.";
|
||||
void bench_c_string(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
|
||||
const char *msg =
|
||||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra metus cursus "
|
||||
"lacus placerat congue. Nulla egestas, mauris a tincidunt tempus, enim lectus volutpat mi, "
|
||||
"eu consequat sem "
|
||||
"libero nec massa. In dapibus ipsum a diam rhoncus gravida. Etiam non dapibus eros. Donec "
|
||||
"fringilla dui sed "
|
||||
"augue pretium, nec scelerisque est maximus. Nullam convallis, sem nec blandit maximus, "
|
||||
"nisi turpis ornare "
|
||||
"nisl, sit amet volutpat neque massa eu odio. Maecenas malesuada quam ex, posuere congue "
|
||||
"nibh turpis duis.";
|
||||
|
||||
for (auto _ : state)
|
||||
{
|
||||
for (auto _ : state) {
|
||||
logger->info(msg);
|
||||
}
|
||||
}
|
||||
|
||||
void bench_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logger)
|
||||
{
|
||||
void bench_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
|
||||
int i = 0;
|
||||
for (auto _ : state)
|
||||
{
|
||||
for (auto _ : state) {
|
||||
logger->info("Hello logger: msg number {}...............", ++i);
|
||||
}
|
||||
}
|
||||
void bench_global_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logger)
|
||||
{
|
||||
void bench_global_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
|
||||
spdlog::set_default_logger(std::move(logger));
|
||||
int i = 0;
|
||||
for (auto _ : state)
|
||||
{
|
||||
for (auto _ : state) {
|
||||
spdlog::info("Hello logger: msg number {}...............", ++i);
|
||||
}
|
||||
}
|
||||
|
||||
void bench_disabled_macro(benchmark::State &state, std::shared_ptr<spdlog::logger> logger)
|
||||
{
|
||||
void bench_disabled_macro(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) {
|
||||
int i = 0;
|
||||
benchmark::DoNotOptimize(i); // prevent unused warnings
|
||||
benchmark::DoNotOptimize(logger); // prevent unused warnings
|
||||
for (auto _ : state)
|
||||
{
|
||||
for (auto _ : state) {
|
||||
SPDLOG_LOGGER_DEBUG(logger, "Hello logger: msg number {}...............", i++);
|
||||
}
|
||||
}
|
||||
|
||||
void bench_disabled_macro_global_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logger)
|
||||
{
|
||||
void bench_disabled_macro_global_logger(benchmark::State &state,
|
||||
std::shared_ptr<spdlog::logger> logger) {
|
||||
spdlog::set_default_logger(std::move(logger));
|
||||
int i = 0;
|
||||
benchmark::DoNotOptimize(i); // prevent unused warnings
|
||||
benchmark::DoNotOptimize(logger); // prevent unused warnings
|
||||
for (auto _ : state)
|
||||
{
|
||||
for (auto _ : state) {
|
||||
SPDLOG_DEBUG("Hello logger: msg number {}...............", i++);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
void bench_dev_null()
|
||||
{
|
||||
void bench_dev_null() {
|
||||
auto dev_null_st = spdlog::basic_logger_st("/dev/null_st", "/dev/null");
|
||||
benchmark::RegisterBenchmark("/dev/null_st", bench_logger, std::move(dev_null_st))->UseRealTime();
|
||||
benchmark::RegisterBenchmark("/dev/null_st", bench_logger, std::move(dev_null_st))
|
||||
->UseRealTime();
|
||||
spdlog::drop("/dev/null_st");
|
||||
|
||||
auto dev_null_mt = spdlog::basic_logger_mt("/dev/null_mt", "/dev/null");
|
||||
benchmark::RegisterBenchmark("/dev/null_mt", bench_logger, std::move(dev_null_mt))->UseRealTime();
|
||||
benchmark::RegisterBenchmark("/dev/null_mt", bench_logger, std::move(dev_null_mt))
|
||||
->UseRealTime();
|
||||
spdlog::drop("/dev/null_mt");
|
||||
}
|
||||
#endif // __linux__
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
using spdlog::sinks::null_sink_mt;
|
||||
using spdlog::sinks::null_sink_st;
|
||||
|
||||
@ -96,32 +92,39 @@ int main(int argc, char *argv[])
|
||||
auto full_bench = argc > 1 && std::string(argv[1]) == "full";
|
||||
|
||||
// disabled loggers
|
||||
auto disabled_logger = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_mt>());
|
||||
auto disabled_logger =
|
||||
std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_mt>());
|
||||
disabled_logger->set_level(spdlog::level::off);
|
||||
benchmark::RegisterBenchmark("disabled-at-compile-time", bench_disabled_macro, disabled_logger);
|
||||
benchmark::RegisterBenchmark("disabled-at-compile-time (global logger)", bench_disabled_macro_global_logger, disabled_logger);
|
||||
benchmark::RegisterBenchmark("disabled-at-compile-time (global logger)",
|
||||
bench_disabled_macro_global_logger, disabled_logger);
|
||||
benchmark::RegisterBenchmark("disabled-at-runtime", bench_logger, disabled_logger);
|
||||
benchmark::RegisterBenchmark("disabled-at-runtime (global logger)", bench_global_logger, disabled_logger);
|
||||
benchmark::RegisterBenchmark("disabled-at-runtime (global logger)", bench_global_logger,
|
||||
disabled_logger);
|
||||
|
||||
auto null_logger_st = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_st>());
|
||||
benchmark::RegisterBenchmark("null_sink_st (500_bytes c_str)", bench_c_string, std::move(null_logger_st));
|
||||
auto null_logger_st =
|
||||
std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_st>());
|
||||
benchmark::RegisterBenchmark("null_sink_st (500_bytes c_str)", bench_c_string,
|
||||
std::move(null_logger_st));
|
||||
benchmark::RegisterBenchmark("null_sink_st", bench_logger, null_logger_st);
|
||||
benchmark::RegisterBenchmark("null_sink_st (global logger)", bench_global_logger, null_logger_st);
|
||||
benchmark::RegisterBenchmark("null_sink_st (global logger)", bench_global_logger,
|
||||
null_logger_st);
|
||||
|
||||
#ifdef __linux
|
||||
bench_dev_null();
|
||||
#endif // __linux__
|
||||
|
||||
if (full_bench)
|
||||
{
|
||||
if (full_bench) {
|
||||
// basic_st
|
||||
auto basic_st = spdlog::basic_logger_st("basic_st", "latency_logs/basic_st.log", true);
|
||||
benchmark::RegisterBenchmark("basic_st", bench_logger, std::move(basic_st))->UseRealTime();
|
||||
spdlog::drop("basic_st");
|
||||
|
||||
// rotating st
|
||||
auto rotating_st = spdlog::rotating_logger_st("rotating_st", "latency_logs/rotating_st.log", file_size, rotating_files);
|
||||
benchmark::RegisterBenchmark("rotating_st", bench_logger, std::move(rotating_st))->UseRealTime();
|
||||
auto rotating_st = spdlog::rotating_logger_st("rotating_st", "latency_logs/rotating_st.log",
|
||||
file_size, rotating_files);
|
||||
benchmark::RegisterBenchmark("rotating_st", bench_logger, std::move(rotating_st))
|
||||
->UseRealTime();
|
||||
spdlog::drop("rotating_st");
|
||||
|
||||
// daily st
|
||||
@ -132,22 +135,32 @@ int main(int argc, char *argv[])
|
||||
//
|
||||
// Multi threaded bench, 10 loggers using same logger concurrently
|
||||
//
|
||||
auto null_logger_mt = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_mt>());
|
||||
benchmark::RegisterBenchmark("null_sink_mt", bench_logger, null_logger_mt)->Threads(n_threads)->UseRealTime();
|
||||
auto null_logger_mt =
|
||||
std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_mt>());
|
||||
benchmark::RegisterBenchmark("null_sink_mt", bench_logger, null_logger_mt)
|
||||
->Threads(n_threads)
|
||||
->UseRealTime();
|
||||
|
||||
// basic_mt
|
||||
auto basic_mt = spdlog::basic_logger_mt("basic_mt", "latency_logs/basic_mt.log", true);
|
||||
benchmark::RegisterBenchmark("basic_mt", bench_logger, std::move(basic_mt))->Threads(n_threads)->UseRealTime();
|
||||
benchmark::RegisterBenchmark("basic_mt", bench_logger, std::move(basic_mt))
|
||||
->Threads(n_threads)
|
||||
->UseRealTime();
|
||||
spdlog::drop("basic_mt");
|
||||
|
||||
// rotating mt
|
||||
auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "latency_logs/rotating_mt.log", file_size, rotating_files);
|
||||
benchmark::RegisterBenchmark("rotating_mt", bench_logger, std::move(rotating_mt))->Threads(n_threads)->UseRealTime();
|
||||
auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "latency_logs/rotating_mt.log",
|
||||
file_size, rotating_files);
|
||||
benchmark::RegisterBenchmark("rotating_mt", bench_logger, std::move(rotating_mt))
|
||||
->Threads(n_threads)
|
||||
->UseRealTime();
|
||||
spdlog::drop("rotating_mt");
|
||||
|
||||
// daily mt
|
||||
auto daily_mt = spdlog::daily_logger_mt("daily_mt", "latency_logs/daily_mt.log");
|
||||
benchmark::RegisterBenchmark("daily_mt", bench_logger, std::move(daily_mt))->Threads(n_threads)->UseRealTime();
|
||||
benchmark::RegisterBenchmark("daily_mt", bench_logger, std::move(daily_mt))
|
||||
->Threads(n_threads)
|
||||
->UseRealTime();
|
||||
spdlog::drop("daily_mt");
|
||||
}
|
||||
|
||||
@ -155,8 +168,11 @@ int main(int argc, char *argv[])
|
||||
auto queue_size = 1024 * 1024 * 3;
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto async_logger = std::make_shared<spdlog::async_logger>(
|
||||
"async_logger", std::make_shared<null_sink_mt>(), std::move(tp), spdlog::async_overflow_policy::overrun_oldest);
|
||||
benchmark::RegisterBenchmark("async_logger", bench_logger, async_logger)->Threads(n_threads)->UseRealTime();
|
||||
"async_logger", std::make_shared<null_sink_mt>(), std::move(tp),
|
||||
spdlog::async_overflow_policy::overrun_oldest);
|
||||
benchmark::RegisterBenchmark("async_logger", bench_logger, async_logger)
|
||||
->Threads(n_threads)
|
||||
->UseRealTime();
|
||||
benchmark::Initialize(&argc, argv);
|
||||
benchmark::RunSpecifiedBenchmarks();
|
||||
}
|
||||
|
@ -11,9 +11,8 @@
|
||||
|
||||
namespace utils {
|
||||
|
||||
template<typename T>
|
||||
inline std::string format(const T &value)
|
||||
{
|
||||
template <typename T>
|
||||
inline std::string format(const T &value) {
|
||||
static std::locale loc("");
|
||||
std::stringstream ss;
|
||||
ss.imbue(loc);
|
||||
@ -21,9 +20,8 @@ inline std::string format(const T &value)
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
template<>
|
||||
inline std::string format(const double &value)
|
||||
{
|
||||
template <>
|
||||
inline std::string format(const double &value) {
|
||||
static std::locale loc("");
|
||||
std::stringstream ss;
|
||||
ss.imbue(loc);
|
||||
|
@ -34,12 +34,12 @@ void replace_default_logger_example();
|
||||
#include "spdlog/spdlog.h"
|
||||
#include "spdlog/cfg/env.h" // support for loading levels from the environment variable
|
||||
|
||||
int main(int, char *[])
|
||||
{
|
||||
int main(int, char *[]) {
|
||||
// Log levels can be loaded from argv/env using "SPDLOG_LEVEL"
|
||||
load_levels_example();
|
||||
|
||||
spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
|
||||
spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR,
|
||||
SPDLOG_VER_PATCH);
|
||||
spdlog::warn("Easy padding in numbers like {:08d}", 12);
|
||||
spdlog::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
|
||||
spdlog::info("Support for floats {:03.2f}", 1.23456);
|
||||
@ -58,8 +58,7 @@ int main(int, char *[])
|
||||
spdlog::set_pattern("%+"); // back to default format
|
||||
spdlog::set_level(spdlog::level::info);
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
stdout_logger_example();
|
||||
basic_example();
|
||||
rotating_example();
|
||||
@ -91,8 +90,7 @@ int main(int, char *[])
|
||||
}
|
||||
|
||||
// Exceptions will only be thrown upon failed logger or sink construction (not during logging).
|
||||
catch (const spdlog::spdlog_ex &ex)
|
||||
{
|
||||
catch (const spdlog::spdlog_ex &ex) {
|
||||
std::printf("Log initialization failed: %s\n", ex.what());
|
||||
return 1;
|
||||
}
|
||||
@ -100,8 +98,7 @@ int main(int, char *[])
|
||||
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
// or #include "spdlog/sinks/stdout_sinks.h" if no colors needed.
|
||||
void stdout_logger_example()
|
||||
{
|
||||
void stdout_logger_example() {
|
||||
// Create color multi threaded logger.
|
||||
auto console = spdlog::stdout_color_mt("console");
|
||||
// or for stderr:
|
||||
@ -109,37 +106,34 @@ void stdout_logger_example()
|
||||
}
|
||||
|
||||
#include "spdlog/sinks/basic_file_sink.h"
|
||||
void basic_example()
|
||||
{
|
||||
void basic_example() {
|
||||
// Create basic file logger (not rotated).
|
||||
auto my_logger = spdlog::basic_logger_mt("file_logger", "logs/basic-log.txt", true);
|
||||
}
|
||||
|
||||
#include "spdlog/sinks/rotating_file_sink.h"
|
||||
void rotating_example()
|
||||
{
|
||||
void rotating_example() {
|
||||
// Create a file rotating logger with 5mb size max and 3 rotated files.
|
||||
auto rotating_logger = spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3);
|
||||
auto rotating_logger =
|
||||
spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3);
|
||||
}
|
||||
|
||||
#include "spdlog/sinks/daily_file_sink.h"
|
||||
void daily_example()
|
||||
{
|
||||
void daily_example() {
|
||||
// Create a daily logger - a new file is created every day on 2:30am.
|
||||
auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
|
||||
}
|
||||
|
||||
#include "spdlog/sinks/callback_sink.h"
|
||||
void callback_example()
|
||||
{
|
||||
void callback_example() {
|
||||
// Create the logger
|
||||
auto logger = spdlog::callback_logger_mt("custom_callback_logger", [](const spdlog::details::log_msg & /*msg*/) {
|
||||
auto logger = spdlog::callback_logger_mt("custom_callback_logger",
|
||||
[](const spdlog::details::log_msg & /*msg*/) {
|
||||
// do what you need to do with msg
|
||||
});
|
||||
}
|
||||
|
||||
void load_levels_example()
|
||||
{
|
||||
void load_levels_example() {
|
||||
// Set the log level to "info" and mylogger to "trace":
|
||||
// SPDLOG_LEVEL=info,mylogger=trace && ./example
|
||||
// must #include "spdlog/cfg/env.h"
|
||||
@ -151,16 +145,17 @@ void load_levels_example()
|
||||
}
|
||||
|
||||
#include "spdlog/async.h"
|
||||
void async_example()
|
||||
{
|
||||
void async_example() {
|
||||
// Default thread pool settings can be modified *before* creating the async logger:
|
||||
// spdlog::init_thread_pool(32768, 1); // queue with max 32k items 1 backing thread.
|
||||
auto async_file = spdlog::basic_logger_mt<spdlog::async_factory>("async_file_logger", "logs/async_log.txt");
|
||||
auto async_file =
|
||||
spdlog::basic_logger_mt<spdlog::async_factory>("async_file_logger", "logs/async_log.txt");
|
||||
// alternatively:
|
||||
// auto async_file = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("async_file_logger", "logs/async_log.txt");
|
||||
// auto async_file =
|
||||
// spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("async_file_logger",
|
||||
// "logs/async_log.txt");
|
||||
|
||||
for (int i = 1; i < 101; ++i)
|
||||
{
|
||||
for (int i = 1; i < 101; ++i) {
|
||||
async_file->info("Async message #{}", i);
|
||||
}
|
||||
}
|
||||
@ -175,16 +170,15 @@ void async_example()
|
||||
// {:n} - don't split the output to lines.
|
||||
|
||||
#if !defined SPDLOG_USE_STD_FORMAT || defined(_MSC_VER)
|
||||
# include "spdlog/fmt/bin_to_hex.h"
|
||||
void binary_example()
|
||||
{
|
||||
#include "spdlog/fmt/bin_to_hex.h"
|
||||
void binary_example() {
|
||||
std::vector<char> buf(80);
|
||||
for (int i = 0; i < 80; i++)
|
||||
{
|
||||
for (int i = 0; i < 80; i++) {
|
||||
buf.push_back(static_cast<char>(i & 0xff));
|
||||
}
|
||||
spdlog::info("Binary example: {}", spdlog::to_hex(buf));
|
||||
spdlog::info("Another binary example:{:n}", spdlog::to_hex(std::begin(buf), std::begin(buf) + 10));
|
||||
spdlog::info("Another binary example:{:n}",
|
||||
spdlog::to_hex(std::begin(buf), std::begin(buf) + 10));
|
||||
// more examples:
|
||||
// logger->info("uppercase: {:X}", spdlog::to_hex(buf));
|
||||
// logger->info("uppercase, no delimiters: {:Xs}", spdlog::to_hex(buf));
|
||||
@ -193,17 +187,15 @@ void binary_example()
|
||||
// logger->info("hexdump style, 20 chars per line {:a}", spdlog::to_hex(buf, 20));
|
||||
}
|
||||
#else
|
||||
void binary_example()
|
||||
{
|
||||
void binary_example() {
|
||||
// not supported with std::format yet
|
||||
}
|
||||
#endif
|
||||
|
||||
// Log a vector of numbers
|
||||
#ifndef SPDLOG_USE_STD_FORMAT
|
||||
# include "spdlog/fmt/ranges.h"
|
||||
void vector_example()
|
||||
{
|
||||
#include "spdlog/fmt/ranges.h"
|
||||
void vector_example() {
|
||||
std::vector<int> vec = {1, 2, 3};
|
||||
spdlog::info("Vector example: {}", vec);
|
||||
}
|
||||
@ -216,8 +208,7 @@ void vector_example() {}
|
||||
|
||||
// Compile time log levels.
|
||||
// define SPDLOG_ACTIVE_LEVEL to required level (e.g. SPDLOG_LEVEL_TRACE)
|
||||
void trace_example()
|
||||
{
|
||||
void trace_example() {
|
||||
// trace from default logger
|
||||
SPDLOG_TRACE("Some trace message.. {} ,{}", 1, 3.23);
|
||||
// debug from default logger
|
||||
@ -231,16 +222,14 @@ void trace_example()
|
||||
// stopwatch example
|
||||
#include "spdlog/stopwatch.h"
|
||||
#include <thread>
|
||||
void stopwatch_example()
|
||||
{
|
||||
void stopwatch_example() {
|
||||
spdlog::stopwatch sw;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(123));
|
||||
spdlog::info("Stopwatch: {} seconds", sw);
|
||||
}
|
||||
|
||||
#include "spdlog/sinks/udp_sink.h"
|
||||
void udp_example()
|
||||
{
|
||||
void udp_example() {
|
||||
spdlog::sinks::udp_sink_config cfg("127.0.0.1", 11091);
|
||||
auto my_logger = spdlog::udp_logger_mt("udplog", cfg);
|
||||
my_logger->set_level(spdlog::level::debug);
|
||||
@ -248,13 +237,13 @@ void udp_example()
|
||||
}
|
||||
|
||||
// A logger with multiple sinks (stdout and file) - each with a different format and log level.
|
||||
void multi_sink_example()
|
||||
{
|
||||
void multi_sink_example() {
|
||||
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
||||
console_sink->set_level(spdlog::level::warn);
|
||||
console_sink->set_pattern("[multi_sink_example] [%^%l%$] %v");
|
||||
|
||||
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/multisink.txt", true);
|
||||
auto file_sink =
|
||||
std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/multisink.txt", true);
|
||||
file_sink->set_level(spdlog::level::trace);
|
||||
|
||||
spdlog::logger logger("multi_sink", {console_sink, file_sink});
|
||||
@ -264,51 +253,43 @@ void multi_sink_example()
|
||||
}
|
||||
|
||||
// User defined types logging
|
||||
struct my_type
|
||||
{
|
||||
struct my_type {
|
||||
int i = 0;
|
||||
explicit my_type(int i)
|
||||
: i(i){};
|
||||
};
|
||||
|
||||
#ifndef SPDLOG_USE_STD_FORMAT // when using fmtlib
|
||||
template<>
|
||||
struct fmt::formatter<my_type> : fmt::formatter<std::string>
|
||||
{
|
||||
auto format(my_type my, format_context &ctx) -> decltype(ctx.out())
|
||||
{
|
||||
template <>
|
||||
struct fmt::formatter<my_type> : fmt::formatter<std::string> {
|
||||
auto format(my_type my, format_context &ctx) -> decltype(ctx.out()) {
|
||||
return fmt::format_to(ctx.out(), "[my_type i={}]", my.i);
|
||||
}
|
||||
};
|
||||
|
||||
#else // when using std::format
|
||||
template<>
|
||||
struct std::formatter<my_type> : std::formatter<std::string>
|
||||
{
|
||||
auto format(my_type my, format_context &ctx) const -> decltype(ctx.out())
|
||||
{
|
||||
template <>
|
||||
struct std::formatter<my_type> : std::formatter<std::string> {
|
||||
auto format(my_type my, format_context &ctx) const -> decltype(ctx.out()) {
|
||||
return format_to(ctx.out(), "[my_type i={}]", my.i);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
void user_defined_example()
|
||||
{
|
||||
spdlog::info("user defined type: {}", my_type(14));
|
||||
}
|
||||
void user_defined_example() { spdlog::info("user defined type: {}", my_type(14)); }
|
||||
|
||||
// Custom error handler. Will be triggered on log failure.
|
||||
void err_handler_example()
|
||||
{
|
||||
void err_handler_example() {
|
||||
// can be set globally or per logger(logger->set_error_handler(..))
|
||||
spdlog::set_error_handler([](const std::string &msg) { printf("*** Custom log error handler: %s ***\n", msg.c_str()); });
|
||||
spdlog::set_error_handler([](const std::string &msg) {
|
||||
printf("*** Custom log error handler: %s ***\n", msg.c_str());
|
||||
});
|
||||
}
|
||||
|
||||
// syslog example (linux/osx/freebsd)
|
||||
#ifndef _WIN32
|
||||
# include "spdlog/sinks/syslog_sink.h"
|
||||
void syslog_example()
|
||||
{
|
||||
#include "spdlog/sinks/syslog_sink.h"
|
||||
void syslog_example() {
|
||||
std::string ident = "spdlog-example";
|
||||
auto syslog_logger = spdlog::syslog_logger_mt("syslog", ident, LOG_PID);
|
||||
syslog_logger->warn("This is warning that will end up in syslog.");
|
||||
@ -317,9 +298,8 @@ void syslog_example()
|
||||
|
||||
// Android example.
|
||||
#if defined(__ANDROID__)
|
||||
# include "spdlog/sinks/android_sink.h"
|
||||
void android_example()
|
||||
{
|
||||
#include "spdlog/sinks/android_sink.h"
|
||||
void android_example() {
|
||||
std::string tag = "spdlog-android";
|
||||
auto android_logger = spdlog::android_logger_mt("android", tag);
|
||||
android_logger->critical("Use \"adb shell logcat\" to view this message.");
|
||||
@ -329,34 +309,32 @@ void android_example()
|
||||
// Log patterns can contain custom flags.
|
||||
// this will add custom flag '%*' which will be bound to a <my_formatter_flag> instance
|
||||
#include "spdlog/pattern_formatter.h"
|
||||
class my_formatter_flag : public spdlog::custom_flag_formatter
|
||||
{
|
||||
class my_formatter_flag : public spdlog::custom_flag_formatter {
|
||||
public:
|
||||
void format(const spdlog::details::log_msg &, const std::tm &, spdlog::memory_buf_t &dest) override
|
||||
{
|
||||
void
|
||||
format(const spdlog::details::log_msg &, const std::tm &, spdlog::memory_buf_t &dest) override {
|
||||
std::string some_txt = "custom-flag";
|
||||
dest.append(some_txt.data(), some_txt.data() + some_txt.size());
|
||||
}
|
||||
|
||||
std::unique_ptr<custom_flag_formatter> clone() const override
|
||||
{
|
||||
std::unique_ptr<custom_flag_formatter> clone() const override {
|
||||
return std::make_unique<my_formatter_flag>();
|
||||
}
|
||||
};
|
||||
|
||||
void custom_flags_example()
|
||||
{
|
||||
void custom_flags_example() {
|
||||
auto formatter = std::make_unique<spdlog::pattern_formatter>();
|
||||
formatter->add_flag<my_formatter_flag>('*').set_pattern("[%n] [%*] [%^%l%$] %v");
|
||||
// set the new formatter using spdlog::set_formatter(formatter) or logger->set_formatter(formatter)
|
||||
// spdlog::set_formatter(std::move(formatter));
|
||||
// set the new formatter using spdlog::set_formatter(formatter) or
|
||||
// logger->set_formatter(formatter) spdlog::set_formatter(std::move(formatter));
|
||||
}
|
||||
|
||||
void file_events_example()
|
||||
{
|
||||
void file_events_example() {
|
||||
// pass the spdlog::file_event_handlers to file sinks for open/close log file notifications
|
||||
spdlog::file_event_handlers handlers;
|
||||
handlers.before_open = [](spdlog::filename_t filename) { spdlog::info("Before opening {}", filename); };
|
||||
handlers.before_open = [](spdlog::filename_t filename) {
|
||||
spdlog::info("Before opening {}", filename);
|
||||
};
|
||||
handlers.after_open = [](spdlog::filename_t filename, std::FILE *fstream) {
|
||||
spdlog::info("After opening {}", filename);
|
||||
fputs("After opening\n", fstream);
|
||||
@ -365,18 +343,21 @@ void file_events_example()
|
||||
spdlog::info("Before closing {}", filename);
|
||||
fputs("Before closing\n", fstream);
|
||||
};
|
||||
handlers.after_close = [](spdlog::filename_t filename) { spdlog::info("After closing {}", filename); };
|
||||
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/events-sample.txt", true, handlers);
|
||||
handlers.after_close = [](spdlog::filename_t filename) {
|
||||
spdlog::info("After closing {}", filename);
|
||||
};
|
||||
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/events-sample.txt",
|
||||
true, handlers);
|
||||
spdlog::logger my_logger("some_logger", file_sink);
|
||||
my_logger.info("Some log line");
|
||||
}
|
||||
|
||||
void replace_default_logger_example()
|
||||
{
|
||||
void replace_default_logger_example() {
|
||||
// store the old logger so we don't break other examples.
|
||||
auto old_logger = spdlog::default_logger();
|
||||
|
||||
auto new_logger = spdlog::basic_logger_mt("new_default_logger", "logs/new-default-log.txt", true);
|
||||
auto new_logger =
|
||||
spdlog::basic_logger_mt("new_default_logger", "logs/new-default-log.txt", true);
|
||||
spdlog::set_default_logger(new_logger);
|
||||
spdlog::set_level(spdlog::level::info);
|
||||
spdlog::debug("This message should not be displayed!");
|
||||
|
@ -48,8 +48,8 @@ struct async_factory_impl {
|
||||
}
|
||||
|
||||
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
|
||||
auto new_logger =
|
||||
std::make_shared<async_logger>(std::move(logger_name), std::move(sink), std::move(tp), OverflowPolicy);
|
||||
auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink),
|
||||
std::move(tp), OverflowPolicy);
|
||||
registry_inst.initialize_logger(new_logger);
|
||||
return new_logger;
|
||||
}
|
||||
@ -59,13 +59,17 @@ using async_factory = async_factory_impl<async_overflow_policy::block>;
|
||||
using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
|
||||
|
||||
template <typename Sink, typename... SinkArgs>
|
||||
inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&...sink_args) {
|
||||
return async_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
|
||||
inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name,
|
||||
SinkArgs &&...sink_args) {
|
||||
return async_factory::create<Sink>(std::move(logger_name),
|
||||
std::forward<SinkArgs>(sink_args)...);
|
||||
}
|
||||
|
||||
template <typename Sink, typename... SinkArgs>
|
||||
inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&...sink_args) {
|
||||
return async_factory_nonblock::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
|
||||
inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name,
|
||||
SinkArgs &&...sink_args) {
|
||||
return async_factory_nonblock::create<Sink>(std::move(logger_name),
|
||||
std::forward<SinkArgs>(sink_args)...);
|
||||
}
|
||||
|
||||
// set global thread pool.
|
||||
@ -73,11 +77,13 @@ inline void init_thread_pool(size_t q_size,
|
||||
size_t thread_count,
|
||||
std::function<void()> on_thread_start,
|
||||
std::function<void()> on_thread_stop) {
|
||||
auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start, on_thread_stop);
|
||||
auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start,
|
||||
on_thread_stop);
|
||||
details::registry::instance().set_tp(std::move(tp));
|
||||
}
|
||||
|
||||
inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start) {
|
||||
inline void
|
||||
init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start) {
|
||||
init_thread_pool(q_size, thread_count, on_thread_start, [] {});
|
||||
}
|
||||
|
||||
@ -87,5 +93,7 @@ inline void init_thread_pool(size_t q_size, size_t thread_count) {
|
||||
}
|
||||
|
||||
// get the global thread pool.
|
||||
inline std::shared_ptr<spdlog::details::thread_pool> thread_pool() { return details::registry::instance().get_tp(); }
|
||||
inline std::shared_ptr<spdlog::details::thread_pool> thread_pool() {
|
||||
return details::registry::instance().get_tp();
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@ -30,7 +30,8 @@ namespace details {
|
||||
class thread_pool;
|
||||
}
|
||||
|
||||
class SPDLOG_API async_logger final : public std::enable_shared_from_this<async_logger>, public logger {
|
||||
class SPDLOG_API async_logger final : public std::enable_shared_from_this<async_logger>,
|
||||
public logger {
|
||||
friend class details::thread_pool;
|
||||
|
||||
public:
|
||||
@ -40,9 +41,9 @@ public:
|
||||
It end,
|
||||
std::weak_ptr<details::thread_pool> tp,
|
||||
async_overflow_policy overflow_policy = async_overflow_policy::block)
|
||||
: logger(std::move(logger_name), begin, end),
|
||||
thread_pool_(std::move(tp)),
|
||||
overflow_policy_(overflow_policy) {}
|
||||
: logger(std::move(logger_name), begin, end)
|
||||
, thread_pool_(std::move(tp))
|
||||
, overflow_policy_(overflow_policy) {}
|
||||
|
||||
async_logger(std::string logger_name,
|
||||
sinks_init_list sinks_list,
|
||||
|
@ -32,7 +32,9 @@ inline void load_argv_levels(int argc, const char **argv) {
|
||||
}
|
||||
}
|
||||
|
||||
inline void load_argv_levels(int argc, char **argv) { load_argv_levels(argc, const_cast<const char **>(argv)); }
|
||||
inline void load_argv_levels(int argc, char **argv) {
|
||||
load_argv_levels(argc, const_cast<const char **>(argv));
|
||||
}
|
||||
|
||||
} // namespace cfg
|
||||
} // namespace spdlog
|
||||
|
@ -53,7 +53,8 @@
|
||||
|
||||
#include <spdlog/fmt/fmt.h>
|
||||
|
||||
#if !defined(SPDLOG_USE_STD_FORMAT) && FMT_VERSION >= 80000 // backward compatibility with fmt versions older than 8
|
||||
#if !defined(SPDLOG_USE_STD_FORMAT) && \
|
||||
FMT_VERSION >= 80000 // backward compatibility with fmt versions older than 8
|
||||
#define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string)
|
||||
#if defined(SPDLOG_WCHAR_FILENAMES)
|
||||
#include <spdlog/fmt/xchar.h>
|
||||
@ -176,7 +177,9 @@ using atomic_level_t = std::atomic<level>;
|
||||
{ "T", "D", "I", "W", "E", "C", "O" }
|
||||
#endif
|
||||
|
||||
[[nodiscard]] constexpr size_t level_to_number(level lvl) noexcept { return static_cast<size_t>(lvl); }
|
||||
[[nodiscard]] constexpr size_t level_to_number(level lvl) noexcept {
|
||||
return static_cast<size_t>(lvl);
|
||||
}
|
||||
constexpr auto levels_count = level_to_number(level::n_levels);
|
||||
constexpr std::array<string_view_t, levels_count> level_string_views SPDLOG_LEVEL_NAMES;
|
||||
constexpr std::array<const char *, levels_count> short_level_names SPDLOG_SHORT_LEVEL_NAMES;
|
||||
@ -223,19 +226,24 @@ private:
|
||||
|
||||
struct source_loc {
|
||||
constexpr source_loc() = default;
|
||||
constexpr source_loc(const char *filename_in, std::uint_least32_t line_in, const char *funcname_in)
|
||||
: filename{filename_in},
|
||||
line{line_in},
|
||||
funcname{funcname_in} {}
|
||||
constexpr source_loc(const char *filename_in,
|
||||
std::uint_least32_t line_in,
|
||||
const char *funcname_in)
|
||||
: filename{filename_in}
|
||||
, line{line_in}
|
||||
, funcname{funcname_in} {}
|
||||
|
||||
#ifdef SPDLOG_HAVE_STD_SOURCE_LOCATION
|
||||
static constexpr source_loc current(const std::source_location source_location = std::source_location::current()) {
|
||||
return source_loc{source_location.file_name(), source_location.line(), source_location.function_name()};
|
||||
static constexpr source_loc
|
||||
current(const std::source_location source_location = std::source_location::current()) {
|
||||
return source_loc{source_location.file_name(), source_location.line(),
|
||||
source_location.function_name()};
|
||||
}
|
||||
#elif defined(SPDLOG_HAVE_EXPERIMENTAL_SOURCE_LOCATION)
|
||||
static constexpr source_loc
|
||||
current(const std::experimental::source_location source_location = std::experimental::source_location::current()) {
|
||||
return source_loc{source_location.file_name(), source_location.line(), source_location.function_name()};
|
||||
static constexpr source_loc current(const std::experimental::source_location source_location =
|
||||
std::experimental::source_location::current()) {
|
||||
return source_loc{source_location.file_name(), source_location.line(),
|
||||
source_location.function_name()};
|
||||
}
|
||||
#else // no source location support
|
||||
static constexpr source_loc current() { return source_loc{}; }
|
||||
@ -254,22 +262,23 @@ struct loc_with_fmt {
|
||||
string_view_t fmt_string;
|
||||
template <typename S, typename = is_convertible_to_sv<S>>
|
||||
constexpr loc_with_fmt(S fmt_str, source_loc loc = source_loc::current()) noexcept
|
||||
: loc(loc),
|
||||
fmt_string(fmt_str) {}
|
||||
: loc(loc)
|
||||
, fmt_string(fmt_str) {}
|
||||
|
||||
#ifndef SPDLOG_USE_STD_FORMAT
|
||||
constexpr loc_with_fmt(fmt::runtime_format_string<char> fmt_str, source_loc loc = source_loc::current()) noexcept
|
||||
: loc(loc),
|
||||
fmt_string(fmt_str.str) {}
|
||||
constexpr loc_with_fmt(fmt::runtime_format_string<char> fmt_str,
|
||||
source_loc loc = source_loc::current()) noexcept
|
||||
: loc(loc)
|
||||
, fmt_string(fmt_str.str) {}
|
||||
#endif
|
||||
};
|
||||
|
||||
struct file_event_handlers {
|
||||
file_event_handlers()
|
||||
: before_open(nullptr),
|
||||
after_open(nullptr),
|
||||
before_close(nullptr),
|
||||
after_close(nullptr) {}
|
||||
: before_open(nullptr)
|
||||
, after_open(nullptr)
|
||||
, before_close(nullptr)
|
||||
, after_close(nullptr) {}
|
||||
|
||||
std::function<void(const filename_t &filename)> before_open;
|
||||
std::function<void(const filename_t &filename, std::FILE *file_stream)> after_open;
|
||||
@ -285,32 +294,39 @@ namespace details {
|
||||
return spdlog::string_view_t{buf.data(), buf.size()};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr spdlog::string_view_t to_string_view(spdlog::string_view_t str) noexcept { return str; }
|
||||
[[nodiscard]] constexpr spdlog::string_view_t to_string_view(spdlog::string_view_t str) noexcept {
|
||||
return str;
|
||||
}
|
||||
|
||||
#if defined(SPDLOG_WCHAR_FILENAMES)
|
||||
[[nodiscard]] constexpr spdlog::wstring_view_t to_string_view(const wmemory_buf_t &buf) noexcept {
|
||||
return spdlog::wstring_view_t{buf.data(), buf.size()};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) noexcept { return str; }
|
||||
[[nodiscard]] constexpr spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) noexcept {
|
||||
return str;
|
||||
}
|
||||
#endif
|
||||
|
||||
// convert format_string<...> to string_view depending on format lib versions
|
||||
#if defined(SPDLOG_USE_STD_FORMAT)
|
||||
#if __cpp_lib_format >= 202207L // std::format and __cpp_lib_format >= 202207L
|
||||
template <typename T, typename... Args>
|
||||
[[nodiscard]] constexpr std::basic_string_view<T> to_string_view(std::basic_format_string<T, Args...> fmt) noexcept {
|
||||
[[nodiscard]] constexpr std::basic_string_view<T>
|
||||
to_string_view(std::basic_format_string<T, Args...> fmt) noexcept {
|
||||
return fmt.get();
|
||||
}
|
||||
#else // std::format and __cpp_lib_format < 202207L
|
||||
template <typename T, typename... Args>
|
||||
[[nodiscard]] constexpr std::basic_string_view<T> to_string_view(std::basic_format_string<T, Args...> fmt) noexcept {
|
||||
[[nodiscard]] constexpr std::basic_string_view<T>
|
||||
to_string_view(std::basic_format_string<T, Args...> fmt) noexcept {
|
||||
return fmt;
|
||||
}
|
||||
#endif
|
||||
#else // {fmt} version
|
||||
template <typename T, typename... Args>
|
||||
[[nodiscard]] constexpr fmt::basic_string_view<T> to_string_view(fmt::basic_format_string<T, Args...> fmt) noexcept {
|
||||
[[nodiscard]] constexpr fmt::basic_string_view<T>
|
||||
to_string_view(fmt::basic_format_string<T, Args...> fmt) noexcept {
|
||||
return fmt;
|
||||
}
|
||||
#endif
|
||||
|
@ -25,8 +25,7 @@ public:
|
||||
|
||||
explicit circular_q(size_t max_items)
|
||||
: max_items_(max_items + 1) // one item is reserved as marker for full q
|
||||
,
|
||||
v_(max_items_) {}
|
||||
, v_(max_items_) {}
|
||||
|
||||
circular_q(const circular_q &) = default;
|
||||
circular_q &operator=(const circular_q &) = default;
|
||||
|
@ -68,7 +68,8 @@ constexpr unsigned int count_digits_fallback(T n) {
|
||||
|
||||
template <typename T>
|
||||
inline unsigned int count_digits(T n) {
|
||||
using count_type = typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type;
|
||||
using count_type =
|
||||
typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type;
|
||||
#ifdef SPDLOG_USE_STD_FORMAT
|
||||
return count_digits_fallback(static_cast<count_type>(n));
|
||||
#else
|
||||
|
@ -10,7 +10,11 @@ namespace spdlog {
|
||||
namespace details {
|
||||
struct SPDLOG_API log_msg {
|
||||
log_msg() = default;
|
||||
log_msg(log_clock::time_point log_time, source_loc loc, string_view_t logger_name, level lvl, string_view_t msg);
|
||||
log_msg(log_clock::time_point log_time,
|
||||
source_loc loc,
|
||||
string_view_t logger_name,
|
||||
level lvl,
|
||||
string_view_t msg);
|
||||
log_msg(source_loc loc, string_view_t logger_name, level lvl, string_view_t msg);
|
||||
log_msg(string_view_t logger_name, level lvl, string_view_t msg);
|
||||
log_msg(const log_msg &other) = default;
|
||||
|
@ -41,7 +41,8 @@ constexpr static const char *default_eol = SPDLOG_EOL;
|
||||
#endif
|
||||
|
||||
constexpr static const char folder_seps[] = SPDLOG_FOLDER_SEPS;
|
||||
constexpr static const filename_t::value_type folder_seps_filename[] = SPDLOG_FILENAME_T(SPDLOG_FOLDER_SEPS);
|
||||
constexpr static const filename_t::value_type folder_seps_filename[] =
|
||||
SPDLOG_FILENAME_T(SPDLOG_FOLDER_SEPS);
|
||||
|
||||
// fopen_s on non windows for writing
|
||||
SPDLOG_API bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode);
|
||||
|
@ -7,7 +7,8 @@
|
||||
//
|
||||
// RAII over the owned thread:
|
||||
// creates the thread on construction.
|
||||
// stops and joins the thread on destruction (if the thread is executing a callback, wait for it to finish first).
|
||||
// stops and joins the thread on destruction (if the thread is executing a callback, wait for it
|
||||
// to finish first).
|
||||
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
@ -23,7 +24,8 @@ namespace details {
|
||||
class SPDLOG_API periodic_worker {
|
||||
public:
|
||||
template <typename Rep, typename Period>
|
||||
periodic_worker(const std::function<void()> &callback_fun, std::chrono::duration<Rep, Period> interval) {
|
||||
periodic_worker(const std::function<void()> &callback_fun,
|
||||
std::chrono::duration<Rep, Period> interval) {
|
||||
active_ = (interval > std::chrono::duration<Rep, Period>::zero());
|
||||
if (!active_) {
|
||||
return;
|
||||
|
@ -38,7 +38,8 @@ public:
|
||||
// Return raw ptr to the default logger.
|
||||
// To be used directly by the spdlog default api (e.g. spdlog::info)
|
||||
// This make the default API faster, but cannot be used concurrently with set_default_logger().
|
||||
// e.g do not call set_default_logger() from one thread while calling spdlog::info() from another.
|
||||
// e.g do not call set_default_logger() from one thread while calling spdlog::info() from
|
||||
// another.
|
||||
logger *get_default_raw();
|
||||
|
||||
// set default logger.
|
||||
|
@ -34,8 +34,9 @@ class tcp_client {
|
||||
|
||||
static void throw_winsock_error_(const std::string &msg, int last_error) {
|
||||
char buf[512];
|
||||
::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, last_error,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, (sizeof(buf) / sizeof(char)), NULL);
|
||||
::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
|
||||
last_error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf,
|
||||
(sizeof(buf) / sizeof(char)), NULL);
|
||||
|
||||
throw_spdlog_ex(fmt_lib::format("tcp_sink - {}: {}", msg, buf));
|
||||
}
|
||||
@ -104,7 +105,8 @@ public:
|
||||
|
||||
// set TCP_NODELAY
|
||||
int enable_flag = 1;
|
||||
::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&enable_flag), sizeof(enable_flag));
|
||||
::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&enable_flag),
|
||||
sizeof(enable_flag));
|
||||
}
|
||||
|
||||
// Send exactly n_bytes of the given data.
|
||||
@ -113,7 +115,8 @@ public:
|
||||
size_t bytes_sent = 0;
|
||||
while (bytes_sent < n_bytes) {
|
||||
const int send_flags = 0;
|
||||
auto write_result = ::send(socket_, data + bytes_sent, (int)(n_bytes - bytes_sent), send_flags);
|
||||
auto write_result =
|
||||
::send(socket_, data + bytes_sent, (int)(n_bytes - bytes_sent), send_flags);
|
||||
if (write_result == SOCKET_ERROR) {
|
||||
int last_error = ::WSAGetLastError();
|
||||
close();
|
||||
|
@ -84,11 +84,13 @@ public:
|
||||
|
||||
// set TCP_NODELAY
|
||||
int enable_flag = 1;
|
||||
::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&enable_flag), sizeof(enable_flag));
|
||||
::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&enable_flag),
|
||||
sizeof(enable_flag));
|
||||
|
||||
// prevent sigpipe on systems where MSG_NOSIGNAL is not available
|
||||
#if defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL)
|
||||
::setsockopt(socket_, SOL_SOCKET, SO_NOSIGPIPE, reinterpret_cast<char *>(&enable_flag), sizeof(enable_flag));
|
||||
::setsockopt(socket_, SOL_SOCKET, SO_NOSIGPIPE, reinterpret_cast<char *>(&enable_flag),
|
||||
sizeof(enable_flag));
|
||||
#endif
|
||||
|
||||
#if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL)
|
||||
@ -106,7 +108,8 @@ public:
|
||||
#else
|
||||
const int send_flags = 0;
|
||||
#endif
|
||||
auto write_result = ::send(socket_, data + bytes_sent, n_bytes - bytes_sent, send_flags);
|
||||
auto write_result =
|
||||
::send(socket_, data + bytes_sent, n_bytes - bytes_sent, send_flags);
|
||||
if (write_result < 0) {
|
||||
close();
|
||||
throw_spdlog_ex("write(2) failed", errno);
|
||||
|
@ -38,9 +38,9 @@ struct async_msg : log_msg_buffer {
|
||||
// support for vs2013 move
|
||||
#if defined(_MSC_VER) && _MSC_VER <= 1800
|
||||
async_msg(async_msg &&other)
|
||||
: log_msg_buffer(std::move(other)),
|
||||
msg_type(other.msg_type),
|
||||
worker_ptr(std::move(other.worker_ptr)) {}
|
||||
: log_msg_buffer(std::move(other))
|
||||
, msg_type(other.msg_type)
|
||||
, worker_ptr(std::move(other.worker_ptr)) {}
|
||||
|
||||
async_msg &operator=(async_msg &&other) {
|
||||
*static_cast<log_msg_buffer *>(this) = std::move(other);
|
||||
@ -55,14 +55,14 @@ struct async_msg : log_msg_buffer {
|
||||
|
||||
// construct from log_msg with given type
|
||||
async_msg(async_logger_ptr &&worker, async_msg_type the_type, const details::log_msg &m)
|
||||
: log_msg_buffer{m},
|
||||
msg_type{the_type},
|
||||
worker_ptr{std::move(worker)} {}
|
||||
: log_msg_buffer{m}
|
||||
, msg_type{the_type}
|
||||
, worker_ptr{std::move(worker)} {}
|
||||
|
||||
async_msg(async_logger_ptr &&worker, async_msg_type the_type)
|
||||
: log_msg_buffer{},
|
||||
msg_type{the_type},
|
||||
worker_ptr{std::move(worker)} {}
|
||||
: log_msg_buffer{}
|
||||
, msg_type{the_type}
|
||||
, worker_ptr{std::move(worker)} {}
|
||||
|
||||
explicit async_msg(async_msg_type the_type)
|
||||
: async_msg{nullptr, the_type} {}
|
||||
@ -86,7 +86,9 @@ public:
|
||||
thread_pool(const thread_pool &) = delete;
|
||||
thread_pool &operator=(thread_pool &&) = delete;
|
||||
|
||||
void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy);
|
||||
void post_log(async_logger_ptr &&worker_ptr,
|
||||
const details::log_msg &msg,
|
||||
async_overflow_policy overflow_policy);
|
||||
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
|
||||
size_t overrun_counter();
|
||||
void reset_overrun_counter();
|
||||
|
@ -38,8 +38,9 @@ class udp_client {
|
||||
|
||||
static void throw_winsock_error_(const std::string &msg, int last_error) {
|
||||
char buf[512];
|
||||
::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, last_error,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, (sizeof(buf) / sizeof(char)), NULL);
|
||||
::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
|
||||
last_error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf,
|
||||
(sizeof(buf) / sizeof(char)), NULL);
|
||||
|
||||
throw_spdlog_ex(fmt_lib::format("udp_sink - {}: {}", msg, buf));
|
||||
}
|
||||
@ -73,8 +74,8 @@ public:
|
||||
}
|
||||
|
||||
int option_value = TX_BUFFER_SIZE;
|
||||
if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<const char *>(&option_value),
|
||||
sizeof(option_value)) < 0) {
|
||||
if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF,
|
||||
reinterpret_cast<const char *>(&option_value), sizeof(option_value)) < 0) {
|
||||
int last_error = ::WSAGetLastError();
|
||||
cleanup_();
|
||||
throw_winsock_error_("error: setsockopt(SO_SNDBUF) Failed!", last_error);
|
||||
@ -87,7 +88,8 @@ public:
|
||||
|
||||
void send(const char *data, size_t n_bytes) {
|
||||
socklen_t tolen = sizeof(struct sockaddr);
|
||||
if (::sendto(socket_, data, static_cast<int>(n_bytes), 0, (struct sockaddr *)&addr_, tolen) == -1) {
|
||||
if (::sendto(socket_, data, static_cast<int>(n_bytes), 0, (struct sockaddr *)&addr_,
|
||||
tolen) == -1) {
|
||||
throw_spdlog_ex("sendto(2) failed", errno);
|
||||
}
|
||||
}
|
||||
|
@ -45,8 +45,8 @@ public:
|
||||
}
|
||||
|
||||
int option_value = TX_BUFFER_SIZE;
|
||||
if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<const char *>(&option_value),
|
||||
sizeof(option_value)) < 0) {
|
||||
if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF,
|
||||
reinterpret_cast<const char *>(&option_value), sizeof(option_value)) < 0) {
|
||||
cleanup_();
|
||||
throw_spdlog_ex("error: setsockopt(SO_SNDBUF) Failed!");
|
||||
}
|
||||
@ -71,7 +71,8 @@ public:
|
||||
void send(const char *data, size_t n_bytes) {
|
||||
ssize_t toslen = 0;
|
||||
socklen_t tolen = sizeof(struct sockaddr);
|
||||
if ((toslen = ::sendto(socket_, data, n_bytes, 0, (struct sockaddr *)&sockAddr_, tolen)) == -1) {
|
||||
if ((toslen = ::sendto(socket_, data, n_bytes, 0, (struct sockaddr *)&sockAddr_, tolen)) ==
|
||||
-1) {
|
||||
throw_spdlog_ex("sendto(2) failed", errno);
|
||||
}
|
||||
}
|
||||
|
@ -43,9 +43,9 @@ template <typename It>
|
||||
class dump_info {
|
||||
public:
|
||||
dump_info(It range_begin, It range_end, size_t size_per_line)
|
||||
: begin_(range_begin),
|
||||
end_(range_end),
|
||||
size_per_line_(size_per_line) {}
|
||||
: begin_(range_begin)
|
||||
, end_(range_end)
|
||||
, size_per_line_(size_per_line) {}
|
||||
|
||||
// do not use begin() and end() to avoid collision with fmt/ranges
|
||||
It get_begin() const { return begin_; }
|
||||
@ -62,7 +62,8 @@ private:
|
||||
template <typename Container>
|
||||
inline details::dump_info<typename Container::const_iterator> to_hex(const Container &container,
|
||||
size_t size_per_line = 32) {
|
||||
static_assert(sizeof(typename Container::value_type) == 1, "sizeof(Container::value_type) != 1");
|
||||
static_assert(sizeof(typename Container::value_type) == 1,
|
||||
"sizeof(Container::value_type) != 1");
|
||||
using Iter = typename Container::const_iterator;
|
||||
return details::dump_info<Iter>(std::begin(container), std::end(container), size_per_line);
|
||||
}
|
||||
@ -70,10 +71,11 @@ inline details::dump_info<typename Container::const_iterator> to_hex(const Conta
|
||||
#if __cpp_lib_span >= 202002L
|
||||
|
||||
template <typename Value, size_t Extent>
|
||||
inline details::dump_info<typename std::span<Value, Extent>::iterator> to_hex(const std::span<Value, Extent> &container,
|
||||
size_t size_per_line = 32) {
|
||||
inline details::dump_info<typename std::span<Value, Extent>::iterator>
|
||||
to_hex(const std::span<Value, Extent> &container, size_t size_per_line = 32) {
|
||||
using Container = std::span<Value, Extent>;
|
||||
static_assert(sizeof(typename Container::value_type) == 1, "sizeof(Container::value_type) != 1");
|
||||
static_assert(sizeof(typename Container::value_type) == 1,
|
||||
"sizeof(Container::value_type) != 1");
|
||||
using Iter = typename Container::iterator;
|
||||
return details::dump_info<Iter>(std::begin(container), std::end(container), size_per_line);
|
||||
}
|
||||
@ -82,7 +84,8 @@ inline details::dump_info<typename std::span<Value, Extent>::iterator> to_hex(co
|
||||
|
||||
// create dump_info from ranges
|
||||
template <typename It>
|
||||
inline details::dump_info<It> to_hex(const It range_begin, const It range_end, size_t size_per_line = 32) {
|
||||
inline details::dump_info<It>
|
||||
to_hex(const It range_begin, const It range_end, size_t size_per_line = 32) {
|
||||
return details::dump_info<It>(range_begin, range_end, size_per_line);
|
||||
}
|
||||
|
||||
@ -155,7 +158,8 @@ struct formatter<spdlog::details::dump_info<T>, char> {
|
||||
for (auto i = the_range.get_begin(); i != the_range.get_end(); i++) {
|
||||
auto ch = static_cast<unsigned char>(*i);
|
||||
|
||||
if (put_newlines && (i == the_range.get_begin() || i - start_of_line >= size_per_line)) {
|
||||
if (put_newlines &&
|
||||
(i == the_range.get_begin() || i - start_of_line >= size_per_line)) {
|
||||
if (show_ascii && i != the_range.get_begin()) {
|
||||
*inserter++ = delimiter;
|
||||
*inserter++ = delimiter;
|
||||
|
@ -5,8 +5,8 @@
|
||||
|
||||
#pragma once
|
||||
//
|
||||
// include bundled or external copy of fmtlib's std support (for formatting e.g. std::filesystem::path, std::thread::id,
|
||||
// std::monostate, std::variant, ...)
|
||||
// include bundled or external copy of fmtlib's std support (for formatting e.g.
|
||||
// std::filesystem::path, std::thread::id, std::monostate, std::variant, ...)
|
||||
//
|
||||
|
||||
#if !defined(SPDLOG_USE_STD_FORMAT)
|
||||
|
@ -25,7 +25,8 @@
|
||||
#define SPDLOG_LOGGER_CATCH(location) \
|
||||
catch (const std::exception &ex) { \
|
||||
if (!location.empty()) { \
|
||||
err_handler_(fmt_lib::format("{} [{}({})]", ex.what(), location.filename, location.line)); \
|
||||
err_handler_( \
|
||||
fmt_lib::format("{} [{}({})]", ex.what(), location.filename, location.line)); \
|
||||
} else { \
|
||||
err_handler_(ex.what()); \
|
||||
} \
|
||||
@ -49,8 +50,8 @@ public:
|
||||
// Logger with range on sinks
|
||||
template <typename It>
|
||||
logger(std::string name, It begin, It end)
|
||||
: name_(std::move(name)),
|
||||
sinks_(begin, end) {}
|
||||
: name_(std::move(name))
|
||||
, sinks_(begin, end) {}
|
||||
|
||||
// Logger with single sink
|
||||
logger(std::string name, sink_ptr single_sink)
|
||||
@ -76,7 +77,8 @@ public:
|
||||
template <typename... Args>
|
||||
void log(level lvl, format_string_t<Args...> fmt, Args &&...args) {
|
||||
if (should_log(lvl)) {
|
||||
log_with_format_(source_loc{}, lvl, details::to_string_view(fmt), std::forward<Args>(args)...);
|
||||
log_with_format_(source_loc{}, lvl, details::to_string_view(fmt),
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
}
|
||||
|
||||
@ -140,17 +142,29 @@ public:
|
||||
|
||||
// log functions with no format string, just string
|
||||
|
||||
void trace(string_view_t msg, source_loc loc = source_loc::current()) { log(loc, level::trace, msg); }
|
||||
void trace(string_view_t msg, source_loc loc = source_loc::current()) {
|
||||
log(loc, level::trace, msg);
|
||||
}
|
||||
|
||||
void debug(string_view_t msg, source_loc loc = source_loc::current()) { log(loc, level::debug, msg); }
|
||||
void debug(string_view_t msg, source_loc loc = source_loc::current()) {
|
||||
log(loc, level::debug, msg);
|
||||
}
|
||||
|
||||
void info(string_view_t msg, source_loc loc = source_loc::current()) { log(loc, level::info, msg); }
|
||||
void info(string_view_t msg, source_loc loc = source_loc::current()) {
|
||||
log(loc, level::info, msg);
|
||||
}
|
||||
|
||||
void warn(string_view_t msg, source_loc loc = source_loc::current()) { log(loc, level::warn, msg); }
|
||||
void warn(string_view_t msg, source_loc loc = source_loc::current()) {
|
||||
log(loc, level::warn, msg);
|
||||
}
|
||||
|
||||
void error(string_view_t msg, source_loc loc = source_loc::current()) { log(loc, level::err, msg); }
|
||||
void error(string_view_t msg, source_loc loc = source_loc::current()) {
|
||||
log(loc, level::err, msg);
|
||||
}
|
||||
|
||||
void critical(string_view_t msg, source_loc loc = source_loc::current()) { log(loc, level::critical, msg); }
|
||||
void critical(string_view_t msg, source_loc loc = source_loc::current()) {
|
||||
log(loc, level::critical, msg);
|
||||
}
|
||||
#else
|
||||
template <typename... Args>
|
||||
void trace(format_string_t<Args...> fmt, Args &&...args) {
|
||||
@ -197,7 +211,9 @@ public:
|
||||
#endif
|
||||
|
||||
// return true if logging is enabled for the given level.
|
||||
[[nodiscard]] bool should_log(level msg_level) const { return msg_level >= level_.load(std::memory_order_relaxed); }
|
||||
[[nodiscard]] bool should_log(level msg_level) const {
|
||||
return msg_level >= level_.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
// set the level of logging
|
||||
void set_level(level level);
|
||||
@ -241,7 +257,8 @@ protected:
|
||||
spdlog::atomic_level_t flush_level_{level::off};
|
||||
err_handler custom_err_handler_{nullptr};
|
||||
|
||||
// common implementation for after templated public api has been resolved to format string and args
|
||||
// common implementation for after templated public api has been resolved to format string and
|
||||
// args
|
||||
template <typename... Args>
|
||||
void log_with_format_(source_loc loc, level lvl, string_view_t fmt, Args &&...args) {
|
||||
assert(should_log(lvl));
|
||||
|
@ -25,10 +25,10 @@ struct padding_info {
|
||||
|
||||
padding_info() = default;
|
||||
padding_info(size_t width, padding_info::pad_side side, bool truncate)
|
||||
: width_(width),
|
||||
side_(side),
|
||||
truncate_(truncate),
|
||||
enabled_(true) {}
|
||||
: width_(width)
|
||||
, side_(side)
|
||||
, truncate_(truncate)
|
||||
, enabled_(true) {}
|
||||
|
||||
bool enabled() const { return enabled_; }
|
||||
size_t width_ = 0;
|
||||
@ -43,7 +43,8 @@ public:
|
||||
: padinfo_(padinfo) {}
|
||||
flag_formatter() = default;
|
||||
virtual ~flag_formatter() = default;
|
||||
virtual void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) = 0;
|
||||
virtual void
|
||||
format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) = 0;
|
||||
|
||||
protected:
|
||||
padding_info padinfo_;
|
||||
@ -55,7 +56,9 @@ class SPDLOG_API custom_flag_formatter : public details::flag_formatter {
|
||||
public:
|
||||
virtual std::unique_ptr<custom_flag_formatter> clone() const = 0;
|
||||
|
||||
void set_padding_info(const details::padding_info &padding) { flag_formatter::padinfo_ = padding; }
|
||||
void set_padding_info(const details::padding_info &padding) {
|
||||
flag_formatter::padinfo_ = padding;
|
||||
}
|
||||
};
|
||||
|
||||
class SPDLOG_API pattern_formatter final : public formatter {
|
||||
@ -102,7 +105,8 @@ private:
|
||||
// Extract given pad spec (e.g. %8X)
|
||||
// Advance the given it pass the end of the padding spec found (if any)
|
||||
// Return padding.
|
||||
static details::padding_info handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end);
|
||||
static details::padding_info handle_padspec_(std::string::const_iterator &it,
|
||||
std::string::const_iterator end);
|
||||
|
||||
void compile_pattern_(const std::string &pattern);
|
||||
};
|
||||
|
@ -27,14 +27,15 @@ namespace sinks {
|
||||
|
||||
/*
|
||||
* Android sink
|
||||
* (logging using __android_log_write or __android_log_buf_write depending on the specified BufferID)
|
||||
* (logging using __android_log_write or __android_log_buf_write depending on the specified
|
||||
* BufferID)
|
||||
*/
|
||||
template <typename Mutex, int BufferID = log_id::LOG_ID_MAIN>
|
||||
class android_sink final : public base_sink<Mutex> {
|
||||
public:
|
||||
explicit android_sink(std::string tag = "spdlog", bool use_raw_msg = false)
|
||||
: tag_(std::move(tag)),
|
||||
use_raw_msg_(use_raw_msg) {}
|
||||
: tag_(std::move(tag))
|
||||
, use_raw_msg_(use_raw_msg) {}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override {
|
||||
@ -68,10 +69,10 @@ protected:
|
||||
void flush_() override {}
|
||||
|
||||
private:
|
||||
// There might be liblog versions used, that do not support __android_log_buf_write. So we only compile and link
|
||||
// against
|
||||
// __android_log_buf_write, if user explicitly provides a non-default log buffer. Otherwise, when using the default
|
||||
// log buffer, always log via __android_log_write.
|
||||
// There might be liblog versions used, that do not support __android_log_buf_write. So we only
|
||||
// compile and link against
|
||||
// __android_log_buf_write, if user explicitly provides a non-default log buffer. Otherwise,
|
||||
// when using the default log buffer, always log via __android_log_write.
|
||||
template <int ID = BufferID>
|
||||
typename std::enable_if<ID == static_cast<int>(log_id::LOG_ID_MAIN), int>::type
|
||||
android_log(int prio, const char *tag, const char *text) {
|
||||
@ -120,12 +121,14 @@ using android_sink_buf_st = android_sink<details::null_mutex, BufferId>;
|
||||
// Create and register android syslog logger
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> android_logger_mt(const std::string &logger_name, const std::string &tag = "spdlog") {
|
||||
inline std::shared_ptr<logger> android_logger_mt(const std::string &logger_name,
|
||||
const std::string &tag = "spdlog") {
|
||||
return Factory::template create<sinks::android_sink_mt>(logger_name, tag);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> android_logger_st(const std::string &logger_name, const std::string &tag = "spdlog") {
|
||||
inline std::shared_ptr<logger> android_logger_st(const std::string &logger_name,
|
||||
const std::string &tag = "spdlog") {
|
||||
return Factory::template create<sinks::android_sink_st>(logger_name, tag);
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,8 @@ inline std::shared_ptr<logger> basic_logger_mt(const std::string &logger_name,
|
||||
const filename_t &filename,
|
||||
bool truncate = false,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
return Factory::template create<sinks::basic_file_sink_mt>(logger_name, filename, truncate, event_handlers);
|
||||
return Factory::template create<sinks::basic_file_sink_mt>(logger_name, filename, truncate,
|
||||
event_handlers);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
@ -53,7 +54,8 @@ inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name,
|
||||
const filename_t &filename,
|
||||
bool truncate = false,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
return Factory::template create<sinks::basic_file_sink_st>(logger_name, filename, truncate, event_handlers);
|
||||
return Factory::template create<sinks::basic_file_sink_st>(logger_name, filename, truncate,
|
||||
event_handlers);
|
||||
}
|
||||
|
||||
} // namespace spdlog
|
||||
|
@ -42,12 +42,14 @@ using callback_sink_st = callback_sink<details::null_mutex>;
|
||||
// factory functions
|
||||
//
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> callback_logger_mt(const std::string &logger_name, const custom_log_callback &callback) {
|
||||
inline std::shared_ptr<logger> callback_logger_mt(const std::string &logger_name,
|
||||
const custom_log_callback &callback) {
|
||||
return Factory::template create<sinks::callback_sink_mt>(logger_name, callback);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> callback_logger_st(const std::string &logger_name, const custom_log_callback &callback) {
|
||||
inline std::shared_ptr<logger> callback_logger_st(const std::string &logger_name,
|
||||
const custom_log_callback &callback) {
|
||||
return Factory::template create<sinks::callback_sink_st>(logger_name, callback);
|
||||
}
|
||||
|
||||
|
@ -31,16 +31,18 @@ struct daily_filename_calculator {
|
||||
static filename_t calc_filename(const filename_t &filename, const tm &now_tm) {
|
||||
filename_t basename, ext;
|
||||
std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
|
||||
return fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, now_tm.tm_year + 1900,
|
||||
now_tm.tm_mon + 1, now_tm.tm_mday, ext);
|
||||
return fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename,
|
||||
now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, ext);
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Generator of daily log file names with strftime format.
|
||||
* Usages:
|
||||
* auto sink = std::make_shared<spdlog::sinks::daily_file_format_sink_mt>("myapp-%Y-%m-%d:%H:%M:%S.log", hour,
|
||||
* minute);" auto logger = spdlog::daily_logger_format_mt("loggername, "myapp-%Y-%m-%d:%X.log", hour, minute)"
|
||||
* auto sink =
|
||||
* std::make_shared<spdlog::sinks::daily_file_format_sink_mt>("myapp-%Y-%m-%d:%H:%M:%S.log", hour,
|
||||
* minute);" auto logger = spdlog::daily_logger_format_mt("loggername, "myapp-%Y-%m-%d:%X.log",
|
||||
* hour, minute)"
|
||||
*
|
||||
*/
|
||||
struct daily_filename_format_calculator {
|
||||
@ -70,14 +72,15 @@ public:
|
||||
bool truncate = false,
|
||||
uint16_t max_files = 0,
|
||||
const file_event_handlers &event_handlers = {})
|
||||
: base_filename_(std::move(base_filename)),
|
||||
rotation_h_(rotation_hour),
|
||||
rotation_m_(rotation_minute),
|
||||
file_helper_{event_handlers},
|
||||
truncate_(truncate),
|
||||
max_files_(max_files),
|
||||
filenames_q_() {
|
||||
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59) {
|
||||
: base_filename_(std::move(base_filename))
|
||||
, rotation_h_(rotation_hour)
|
||||
, rotation_m_(rotation_minute)
|
||||
, file_helper_{event_handlers}
|
||||
, truncate_(truncate)
|
||||
, max_files_(max_files)
|
||||
, filenames_q_() {
|
||||
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 ||
|
||||
rotation_minute > 59) {
|
||||
throw_spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
|
||||
}
|
||||
|
||||
@ -168,7 +171,8 @@ private:
|
||||
bool ok = remove_if_exists(old_filename) == 0;
|
||||
if (!ok) {
|
||||
filenames_q_.push_back(std::move(current_file));
|
||||
throw_spdlog_ex("Failed removing daily file " + filename_to_str(old_filename), errno);
|
||||
throw_spdlog_ex("Failed removing daily file " + filename_to_str(old_filename),
|
||||
errno);
|
||||
}
|
||||
}
|
||||
filenames_q_.push_back(std::move(current_file));
|
||||
@ -187,7 +191,8 @@ private:
|
||||
using daily_file_sink_mt = daily_file_sink<std::mutex>;
|
||||
using daily_file_sink_st = daily_file_sink<details::null_mutex>;
|
||||
using daily_file_format_sink_mt = daily_file_sink<std::mutex, daily_filename_format_calculator>;
|
||||
using daily_file_format_sink_st = daily_file_sink<details::null_mutex, daily_filename_format_calculator>;
|
||||
using daily_file_format_sink_st =
|
||||
daily_file_sink<details::null_mutex, daily_filename_format_calculator>;
|
||||
|
||||
} // namespace sinks
|
||||
|
||||
@ -202,20 +207,21 @@ inline std::shared_ptr<logger> daily_logger_mt(const std::string &logger_name,
|
||||
bool truncate = false,
|
||||
uint16_t max_files = 0,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate, max_files,
|
||||
event_handlers);
|
||||
return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute,
|
||||
truncate, max_files, event_handlers);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> daily_logger_format_mt(const std::string &logger_name,
|
||||
inline std::shared_ptr<logger>
|
||||
daily_logger_format_mt(const std::string &logger_name,
|
||||
const filename_t &filename,
|
||||
int hour = 0,
|
||||
int minute = 0,
|
||||
bool truncate = false,
|
||||
uint16_t max_files = 0,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
return Factory::template create<sinks::daily_file_format_sink_mt>(logger_name, filename, hour, minute, truncate,
|
||||
max_files, event_handlers);
|
||||
return Factory::template create<sinks::daily_file_format_sink_mt>(
|
||||
logger_name, filename, hour, minute, truncate, max_files, event_handlers);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
@ -226,19 +232,20 @@ inline std::shared_ptr<logger> daily_logger_st(const std::string &logger_name,
|
||||
bool truncate = false,
|
||||
uint16_t max_files = 0,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate, max_files,
|
||||
event_handlers);
|
||||
return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute,
|
||||
truncate, max_files, event_handlers);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> daily_logger_format_st(const std::string &logger_name,
|
||||
inline std::shared_ptr<logger>
|
||||
daily_logger_format_st(const std::string &logger_name,
|
||||
const filename_t &filename,
|
||||
int hour = 0,
|
||||
int minute = 0,
|
||||
bool truncate = false,
|
||||
uint16_t max_files = 0,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
return Factory::template create<sinks::daily_file_format_sink_st>(logger_name, filename, hour, minute, truncate,
|
||||
max_files, event_handlers);
|
||||
return Factory::template create<sinks::daily_file_format_sink_st>(
|
||||
logger_name, filename, hour, minute, truncate, max_files, event_handlers);
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@ -20,8 +20,8 @@
|
||||
// #include <spdlog/sinks/dup_filter_sink.h>
|
||||
//
|
||||
// int main() {
|
||||
// auto dup_filter = std::make_shared<dup_filter_sink_st>(std::chrono::seconds(5), level::info);
|
||||
// dup_filter->add_sink(std::make_shared<stdout_color_sink_mt>());
|
||||
// auto dup_filter = std::make_shared<dup_filter_sink_st>(std::chrono::seconds(5),
|
||||
// level::info); dup_filter->add_sink(std::make_shared<stdout_color_sink_mt>());
|
||||
// spdlog::logger l("logger", dup_filter);
|
||||
// l.info("Hello");
|
||||
// l.info("Hello");
|
||||
@ -42,8 +42,8 @@ public:
|
||||
template <class Rep, class Period>
|
||||
explicit dup_filter_sink(std::chrono::duration<Rep, Period> max_skip_duration,
|
||||
level notification_level = level::info)
|
||||
: max_skip_duration_{max_skip_duration},
|
||||
log_level_{notification_level} {}
|
||||
: max_skip_duration_{max_skip_duration}
|
||||
, log_level_{notification_level} {}
|
||||
|
||||
protected:
|
||||
std::chrono::microseconds max_skip_duration_;
|
||||
@ -62,8 +62,8 @@ protected:
|
||||
// log the "skipped.." message
|
||||
if (skip_counter_ > 0) {
|
||||
char buf[64];
|
||||
auto msg_size =
|
||||
::snprintf(buf, sizeof(buf), "Skipped %u duplicate messages..", static_cast<unsigned>(skip_counter_));
|
||||
auto msg_size = ::snprintf(buf, sizeof(buf), "Skipped %u duplicate messages..",
|
||||
static_cast<unsigned>(skip_counter_));
|
||||
if (msg_size > 0 && static_cast<size_t>(msg_size) < sizeof(buf)) {
|
||||
details::log_msg skipped_msg{msg.source, msg.logger_name, log_level_,
|
||||
string_view_t{buf, static_cast<size_t>(msg_size)}};
|
||||
|
@ -29,8 +29,9 @@ struct hourly_filename_calculator {
|
||||
static filename_t calc_filename(const filename_t &filename, const tm &now_tm) {
|
||||
filename_t basename, ext;
|
||||
std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
|
||||
return fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}_{:02d}{}"), basename, now_tm.tm_year + 1900,
|
||||
now_tm.tm_mon + 1, now_tm.tm_mday, now_tm.tm_hour, ext);
|
||||
return fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}_{:02d}{}"), basename,
|
||||
now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday,
|
||||
now_tm.tm_hour, ext);
|
||||
}
|
||||
};
|
||||
|
||||
@ -47,11 +48,11 @@ public:
|
||||
bool truncate = false,
|
||||
uint16_t max_files = 0,
|
||||
const file_event_handlers &event_handlers = {})
|
||||
: base_filename_(std::move(base_filename)),
|
||||
file_helper_{event_handlers},
|
||||
truncate_(truncate),
|
||||
max_files_(max_files),
|
||||
filenames_q_() {
|
||||
: base_filename_(std::move(base_filename))
|
||||
, file_helper_{event_handlers}
|
||||
, truncate_(truncate)
|
||||
, max_files_(max_files)
|
||||
, filenames_q_() {
|
||||
auto now = log_clock::now();
|
||||
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
|
||||
file_helper_.open(filename, truncate_);
|
||||
@ -144,7 +145,8 @@ private:
|
||||
bool ok = remove_if_exists(old_filename) == 0;
|
||||
if (!ok) {
|
||||
filenames_q_.push_back(std::move(current_file));
|
||||
SPDLOG_THROW(spdlog_ex("Failed removing hourly file " + filename_to_str(old_filename), errno));
|
||||
SPDLOG_THROW(spdlog_ex(
|
||||
"Failed removing hourly file " + filename_to_str(old_filename), errno));
|
||||
}
|
||||
}
|
||||
filenames_q_.push_back(std::move(current_file));
|
||||
@ -173,8 +175,8 @@ inline std::shared_ptr<logger> hourly_logger_mt(const std::string &logger_name,
|
||||
bool truncate = false,
|
||||
uint16_t max_files = 0,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
return Factory::template create<sinks::hourly_file_sink_mt>(logger_name, filename, truncate, max_files,
|
||||
event_handlers);
|
||||
return Factory::template create<sinks::hourly_file_sink_mt>(logger_name, filename, truncate,
|
||||
max_files, event_handlers);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
@ -183,7 +185,7 @@ inline std::shared_ptr<logger> hourly_logger_st(const std::string &logger_name,
|
||||
bool truncate = false,
|
||||
uint16_t max_files = 0,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
return Factory::template create<sinks::hourly_file_sink_st>(logger_name, filename, truncate, max_files,
|
||||
event_handlers);
|
||||
return Factory::template create<sinks::hourly_file_sink_st>(logger_name, filename, truncate,
|
||||
max_files, event_handlers);
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@ -30,9 +30,9 @@ struct kafka_sink_config {
|
||||
int32_t flush_timeout_ms = 1000;
|
||||
|
||||
kafka_sink_config(std::string addr, std::string topic, int flush_timeout_ms = 1000)
|
||||
: server_addr{std::move(addr)},
|
||||
produce_topic{std::move(topic)},
|
||||
flush_timeout_ms(flush_timeout_ms) {}
|
||||
: server_addr{std::move(addr)}
|
||||
, produce_topic{std::move(topic)}
|
||||
, flush_timeout_ms(flush_timeout_ms) {}
|
||||
};
|
||||
|
||||
template <typename Mutex>
|
||||
@ -43,9 +43,11 @@ public:
|
||||
try {
|
||||
std::string errstr;
|
||||
conf_.reset(RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL));
|
||||
RdKafka::Conf::ConfResult confRes = conf_->set("bootstrap.servers", config_.server_addr, errstr);
|
||||
RdKafka::Conf::ConfResult confRes =
|
||||
conf_->set("bootstrap.servers", config_.server_addr, errstr);
|
||||
if (confRes != RdKafka::Conf::CONF_OK) {
|
||||
throw_spdlog_ex(fmt_lib::format("conf set bootstrap.servers failed err:{}", errstr));
|
||||
throw_spdlog_ex(
|
||||
fmt_lib::format("conf set bootstrap.servers failed err:{}", errstr));
|
||||
}
|
||||
|
||||
tconf_.reset(RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC));
|
||||
@ -57,7 +59,8 @@ public:
|
||||
if (producer_ == nullptr) {
|
||||
throw_spdlog_ex(fmt_lib::format("create producer failed err:{}", errstr));
|
||||
}
|
||||
topic_.reset(RdKafka::Topic::create(producer_.get(), config_.produce_topic, tconf_.get(), errstr));
|
||||
topic_.reset(RdKafka::Topic::create(producer_.get(), config_.produce_topic,
|
||||
tconf_.get(), errstr));
|
||||
if (topic_ == nullptr) {
|
||||
throw_spdlog_ex(fmt_lib::format("create topic failed err:{}", errstr));
|
||||
}
|
||||
@ -70,8 +73,8 @@ public:
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override {
|
||||
producer_->produce(topic_.get(), 0, RdKafka::Producer::RK_MSG_COPY, (void *)msg.payload.data(),
|
||||
msg.payload.size(), NULL, NULL);
|
||||
producer_->produce(topic_.get(), 0, RdKafka::Producer::RK_MSG_COPY,
|
||||
(void *)msg.payload.data(), msg.payload.size(), NULL, NULL);
|
||||
}
|
||||
|
||||
void flush_() override { producer_->flush(config_.flush_timeout_ms); }
|
||||
@ -102,14 +105,14 @@ inline std::shared_ptr<logger> kafka_logger_st(const std::string &logger_name,
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::async_factory>
|
||||
inline std::shared_ptr<spdlog::logger> kafka_logger_async_mt(std::string logger_name,
|
||||
spdlog::sinks::kafka_sink_config config) {
|
||||
inline std::shared_ptr<spdlog::logger>
|
||||
kafka_logger_async_mt(std::string logger_name, spdlog::sinks::kafka_sink_config config) {
|
||||
return Factory::template create<sinks::kafka_sink_mt>(logger_name, config);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::async_factory>
|
||||
inline std::shared_ptr<spdlog::logger> kafka_logger_async_st(std::string logger_name,
|
||||
spdlog::sinks::kafka_sink_config config) {
|
||||
inline std::shared_ptr<spdlog::logger>
|
||||
kafka_logger_async_st(std::string logger_name, spdlog::sinks::kafka_sink_config config) {
|
||||
return Factory::template create<sinks::kafka_sink_st>(logger_name, config);
|
||||
}
|
||||
|
||||
|
@ -40,9 +40,9 @@ public:
|
||||
const std::string &db_name,
|
||||
const std::string &collection_name,
|
||||
const std::string &uri = "mongodb://localhost:27017")
|
||||
: instance_(std::move(instance)),
|
||||
db_name_(db_name),
|
||||
coll_name_(collection_name) {
|
||||
: instance_(std::move(instance))
|
||||
, db_name_(db_name)
|
||||
, coll_name_(collection_name) {
|
||||
try {
|
||||
client_ = spdlog::std::make_unique<mongocxx::client>(mongocxx::uri{uri});
|
||||
} catch (const std::exception &e) {
|
||||
@ -59,10 +59,12 @@ protected:
|
||||
|
||||
if (client_ != nullptr) {
|
||||
auto doc = document{} << "timestamp" << bsoncxx::types::b_date(msg.time) << "level"
|
||||
<< level::to_string_view(msg.log_level).data() << "level_num" << msg.log_level
|
||||
<< "message" << std::string(msg.payload.begin(), msg.payload.end()) << "logger_name"
|
||||
<< std::string(msg.logger_name.begin(), msg.logger_name.end()) << "thread_id"
|
||||
<< static_cast<int>(msg.thread_id) << finalize;
|
||||
<< level::to_string_view(msg.log_level).data() << "level_num"
|
||||
<< msg.log_level << "message"
|
||||
<< std::string(msg.payload.begin(), msg.payload.end())
|
||||
<< "logger_name"
|
||||
<< std::string(msg.logger_name.begin(), msg.logger_name.end())
|
||||
<< "thread_id" << static_cast<int>(msg.thread_id) << finalize;
|
||||
client_->database(db_name_).collection(coll_name_).insert_one(doc.view());
|
||||
}
|
||||
}
|
||||
@ -84,19 +86,23 @@ using mongo_sink_st = mongo_sink<spdlog::details::null_mutex>;
|
||||
} // namespace sinks
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> mongo_logger_mt(const std::string &logger_name,
|
||||
inline std::shared_ptr<logger>
|
||||
mongo_logger_mt(const std::string &logger_name,
|
||||
const std::string &db_name,
|
||||
const std::string &collection_name,
|
||||
const std::string &uri = "mongodb://localhost:27017") {
|
||||
return Factory::template create<sinks::mongo_sink_mt>(logger_name, db_name, collection_name, uri);
|
||||
return Factory::template create<sinks::mongo_sink_mt>(logger_name, db_name, collection_name,
|
||||
uri);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> mongo_logger_st(const std::string &logger_name,
|
||||
inline std::shared_ptr<logger>
|
||||
mongo_logger_st(const std::string &logger_name,
|
||||
const std::string &db_name,
|
||||
const std::string &collection_name,
|
||||
const std::string &uri = "mongodb://localhost:27017") {
|
||||
return Factory::template create<sinks::mongo_sink_st>(logger_name, db_name, collection_name, uri);
|
||||
return Factory::template create<sinks::mongo_sink_st>(logger_name, db_name, collection_name,
|
||||
uri);
|
||||
}
|
||||
|
||||
} // namespace spdlog
|
||||
|
@ -15,8 +15,8 @@ template <typename Mutex>
|
||||
class ostream_sink final : public base_sink<Mutex> {
|
||||
public:
|
||||
explicit ostream_sink(std::ostream &os, bool force_flush = false)
|
||||
: ostream_(os),
|
||||
force_flush_(force_flush) {}
|
||||
: ostream_(os)
|
||||
, force_flush_(force_flush) {}
|
||||
ostream_sink(const ostream_sink &) = delete;
|
||||
ostream_sink &operator=(const ostream_sink &) = delete;
|
||||
|
||||
|
@ -8,8 +8,8 @@
|
||||
// etc) Building and using requires Qt library.
|
||||
//
|
||||
// Warning: the qt_sink won't be notified if the target widget is destroyed.
|
||||
// If the widget's lifetime can be shorter than the logger's one, you should provide some permanent QObject,
|
||||
// and then use a standard signal/slot.
|
||||
// If the widget's lifetime can be shorter than the logger's one, you should provide some permanent
|
||||
// QObject, and then use a standard signal/slot.
|
||||
//
|
||||
|
||||
#include "spdlog/common.h"
|
||||
@ -30,8 +30,8 @@ template <typename Mutex>
|
||||
class qt_sink : public base_sink<Mutex> {
|
||||
public:
|
||||
qt_sink(QObject *qt_object, std::string meta_method)
|
||||
: qt_object_(qt_object),
|
||||
meta_method_(std::move(meta_method)) {
|
||||
: qt_object_(qt_object)
|
||||
, meta_method_(std::move(meta_method)) {
|
||||
if (!qt_object_) {
|
||||
throw_spdlog_ex("qt_sink: qt_object is null");
|
||||
}
|
||||
@ -59,15 +59,19 @@ private:
|
||||
// QT color sink to QTextEdit.
|
||||
// Color location is determined by the sink log pattern like in the rest of spdlog sinks.
|
||||
// Colors can be modified if needed using sink->set_color(level, qtTextCharFormat).
|
||||
// max_lines is the maximum number of lines that the sink will hold before removing the oldest lines.
|
||||
// By default, only ascii (latin1) is supported by this sink. Set is_utf8 to true if utf8 support is needed.
|
||||
// max_lines is the maximum number of lines that the sink will hold before removing the oldest
|
||||
// lines. By default, only ascii (latin1) is supported by this sink. Set is_utf8 to true if utf8
|
||||
// support is needed.
|
||||
template <typename Mutex>
|
||||
class qt_color_sink : public base_sink<Mutex> {
|
||||
public:
|
||||
qt_color_sink(QTextEdit *qt_text_edit, int max_lines, bool dark_colors = false, bool is_utf8 = false)
|
||||
: qt_text_edit_(qt_text_edit),
|
||||
max_lines_(max_lines),
|
||||
is_utf8_(is_utf8) {
|
||||
qt_color_sink(QTextEdit *qt_text_edit,
|
||||
int max_lines,
|
||||
bool dark_colors = false,
|
||||
bool is_utf8 = false)
|
||||
: qt_text_edit_(qt_text_edit)
|
||||
, max_lines_(max_lines)
|
||||
, is_utf8_(is_utf8) {
|
||||
if (!qt_text_edit_) {
|
||||
throw_spdlog_ex("qt_color_text_sink: text_edit is null");
|
||||
}
|
||||
@ -127,13 +131,13 @@ protected:
|
||||
QTextCharFormat level_color,
|
||||
int color_range_start,
|
||||
int color_range_end)
|
||||
: max_lines(max_lines),
|
||||
q_text_edit(q_text_edit),
|
||||
payload(std::move(payload)),
|
||||
default_color(default_color),
|
||||
level_color(level_color),
|
||||
color_range_start(color_range_start),
|
||||
color_range_end(color_range_end) {}
|
||||
: max_lines(max_lines)
|
||||
, q_text_edit(q_text_edit)
|
||||
, payload(std::move(payload))
|
||||
, default_color(default_color)
|
||||
, level_color(level_color)
|
||||
, color_range_start(color_range_start)
|
||||
, color_range_end(color_range_end) {}
|
||||
int max_lines;
|
||||
QTextEdit *q_text_edit;
|
||||
QString payload;
|
||||
@ -178,8 +182,8 @@ protected:
|
||||
void flush_() override {}
|
||||
|
||||
// Add colored text to the text edit widget. This method is invoked in the GUI thread.
|
||||
// It is a static method to ensure that it is handled correctly even if the sink is destroyed prematurely
|
||||
// before it is invoked.
|
||||
// It is a static method to ensure that it is handled correctly even if the sink is destroyed
|
||||
// prematurely before it is invoked.
|
||||
|
||||
static void invoke_method_(invoke_params params) {
|
||||
auto *document = params.q_text_edit->document();
|
||||
@ -206,8 +210,8 @@ protected:
|
||||
|
||||
// insert the colorized text
|
||||
cursor.setCharFormat(params.level_color);
|
||||
cursor.insertText(
|
||||
params.payload.mid(params.color_range_start, params.color_range_end - params.color_range_start));
|
||||
cursor.insertText(params.payload.mid(params.color_range_start,
|
||||
params.color_range_end - params.color_range_start));
|
||||
|
||||
// insert the text after the color range with default format
|
||||
cursor.setCharFormat(params.default_color);
|
||||
@ -236,14 +240,16 @@ using qt_color_sink_st = qt_color_sink<details::null_mutex>;
|
||||
|
||||
// log to QTextEdit
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
qt_logger_mt(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append") {
|
||||
inline std::shared_ptr<logger> qt_logger_mt(const std::string &logger_name,
|
||||
QTextEdit *qt_object,
|
||||
const std::string &meta_method = "append") {
|
||||
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
qt_logger_st(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append") {
|
||||
inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name,
|
||||
QTextEdit *qt_object,
|
||||
const std::string &meta_method = "append") {
|
||||
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
|
||||
}
|
||||
|
||||
@ -276,15 +282,21 @@ qt_logger_st(const std::string &logger_name, QObject *qt_object, const std::stri
|
||||
|
||||
// log to QTextEdit with colorize output
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
qt_color_logger_mt(const std::string &logger_name, QTextEdit *qt_text_edit, int max_lines, bool is_utf8 = false) {
|
||||
return Factory::template create<sinks::qt_color_sink_mt>(logger_name, qt_text_edit, max_lines, false, is_utf8);
|
||||
inline std::shared_ptr<logger> qt_color_logger_mt(const std::string &logger_name,
|
||||
QTextEdit *qt_text_edit,
|
||||
int max_lines,
|
||||
bool is_utf8 = false) {
|
||||
return Factory::template create<sinks::qt_color_sink_mt>(logger_name, qt_text_edit, max_lines,
|
||||
false, is_utf8);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
qt_color_logger_st(const std::string &logger_name, QTextEdit *qt_text_edit, int max_lines, bool is_utf8 = false) {
|
||||
return Factory::template create<sinks::qt_color_sink_st>(logger_name, qt_text_edit, max_lines, false, is_utf8);
|
||||
inline std::shared_ptr<logger> qt_color_logger_st(const std::string &logger_name,
|
||||
QTextEdit *qt_text_edit,
|
||||
int max_lines,
|
||||
bool is_utf8 = false) {
|
||||
return Factory::template create<sinks::qt_color_sink_st>(logger_name, qt_text_edit, max_lines,
|
||||
false, is_utf8);
|
||||
}
|
||||
|
||||
} // namespace spdlog
|
||||
|
@ -16,10 +16,10 @@
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
/*
|
||||
* Ring buffer sink. Holds fixed amount of log messages in memory. When the buffer is full, new messages override the
|
||||
* old ones. Useful for storing debug data in memory in case of error. Example: auto rb_sink =
|
||||
* std::make_shared<spdlog::sinks::ringbuffer_sink_mt>(128); spdlog::logger logger("rb_logger", rb_sink);
|
||||
* rb->drain([](const std::string_view msg) { process(msg);});
|
||||
* Ring buffer sink. Holds fixed amount of log messages in memory. When the buffer is full, new
|
||||
* messages override the old ones. Useful for storing debug data in memory in case of error.
|
||||
* Example: auto rb_sink = std::make_shared<spdlog::sinks::ringbuffer_sink_mt>(128); spdlog::logger
|
||||
* logger("rb_logger", rb_sink); rb->drain([](const std::string_view msg) { process(msg);});
|
||||
*/
|
||||
template <typename Mutex>
|
||||
class ringbuffer_sink final : public base_sink<Mutex> {
|
||||
@ -47,7 +47,9 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override { q_.push_back(details::log_msg_buffer{msg}); }
|
||||
void sink_it_(const details::log_msg &msg) override {
|
||||
q_.push_back(details::log_msg_buffer{msg});
|
||||
}
|
||||
void flush_() override {}
|
||||
|
||||
private:
|
||||
|
@ -14,7 +14,6 @@
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
//
|
||||
// Rotating file sink based on size
|
||||
//
|
||||
@ -26,6 +25,7 @@ public:
|
||||
std::size_t max_files,
|
||||
bool rotate_on_open = false,
|
||||
const file_event_handlers &event_handlers = {});
|
||||
|
||||
static filename_t calc_filename(const filename_t &filename, std::size_t index);
|
||||
filename_t filename();
|
||||
|
||||
@ -68,8 +68,8 @@ inline std::shared_ptr<logger> rotating_logger_mt(const std::string &logger_name
|
||||
size_t max_files,
|
||||
bool rotate_on_open = false,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
return Factory::template create<sinks::rotating_file_sink_mt>(logger_name, filename, max_file_size, max_files,
|
||||
rotate_on_open, event_handlers);
|
||||
return Factory::template create<sinks::rotating_file_sink_mt>(
|
||||
logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
@ -79,7 +79,7 @@ inline std::shared_ptr<logger> rotating_logger_st(const std::string &logger_name
|
||||
size_t max_files,
|
||||
bool rotate_on_open = false,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
return Factory::template create<sinks::rotating_file_sink_st>(logger_name, filename, max_file_size, max_files,
|
||||
rotate_on_open, event_handlers);
|
||||
return Factory::template create<sinks::rotating_file_sink_st>(
|
||||
logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@ -30,15 +30,19 @@ using stderr_color_sink_st = ansicolor_stderr_sink_st;
|
||||
// template instantations
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||
std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name,
|
||||
color_mode mode = color_mode::automatic);
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
std::shared_ptr<logger> stdout_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||
std::shared_ptr<logger> stdout_color_st(const std::string &logger_name,
|
||||
color_mode mode = color_mode::automatic);
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||
std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name,
|
||||
color_mode mode = color_mode::automatic);
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
std::shared_ptr<logger> stderr_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic);
|
||||
std::shared_ptr<logger> stderr_color_st(const std::string &logger_name,
|
||||
color_mode mode = color_mode::automatic);
|
||||
|
||||
} // namespace spdlog
|
||||
|
@ -21,15 +21,15 @@ class syslog_sink : public base_sink<Mutex> {
|
||||
|
||||
public:
|
||||
syslog_sink(std::string ident, int syslog_option, int syslog_facility, bool enable_formatting)
|
||||
: enable_formatting_{enable_formatting},
|
||||
syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
|
||||
: enable_formatting_{enable_formatting}
|
||||
, syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
|
||||
/* spdlog::level::debug */ LOG_DEBUG,
|
||||
/* spdlog::level::info */ LOG_INFO,
|
||||
/* spdlog::level::warn */ LOG_WARNING,
|
||||
/* spdlog::level::err */ LOG_ERR,
|
||||
/* spdlog::level::critical */ LOG_CRIT,
|
||||
/* spdlog::level::off */ LOG_INFO}},
|
||||
ident_{std::move(ident)} {
|
||||
/* spdlog::level::off */ LOG_INFO}}
|
||||
, ident_{std::move(ident)} {
|
||||
// set ident to be program name if empty
|
||||
::openlog(ident_.empty() ? nullptr : ident_.c_str(), syslog_option, syslog_facility);
|
||||
}
|
||||
@ -88,8 +88,8 @@ inline std::shared_ptr<logger> syslog_logger_mt(const std::string &logger_name,
|
||||
int syslog_option = 0,
|
||||
int syslog_facility = LOG_USER,
|
||||
bool enable_formatting = false) {
|
||||
return Factory::template create<sinks::syslog_sink_mt>(logger_name, syslog_ident, syslog_option, syslog_facility,
|
||||
enable_formatting);
|
||||
return Factory::template create<sinks::syslog_sink_mt>(logger_name, syslog_ident, syslog_option,
|
||||
syslog_facility, enable_formatting);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
@ -98,7 +98,7 @@ inline std::shared_ptr<logger> syslog_logger_st(const std::string &logger_name,
|
||||
int syslog_option = 0,
|
||||
int syslog_facility = LOG_USER,
|
||||
bool enable_formatting = false) {
|
||||
return Factory::template create<sinks::syslog_sink_st>(logger_name, syslog_ident, syslog_option, syslog_facility,
|
||||
enable_formatting);
|
||||
return Factory::template create<sinks::syslog_sink_st>(logger_name, syslog_ident, syslog_option,
|
||||
syslog_facility, enable_formatting);
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@ -24,9 +24,9 @@ template <typename Mutex>
|
||||
class systemd_sink : public base_sink<Mutex> {
|
||||
public:
|
||||
systemd_sink(std::string ident = "", bool enable_formatting = false)
|
||||
: ident_{std::move(ident)},
|
||||
enable_formatting_{enable_formatting},
|
||||
syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
|
||||
: ident_{std::move(ident)}
|
||||
, enable_formatting_{enable_formatting}
|
||||
, syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG,
|
||||
/* spdlog::level::debug */ LOG_DEBUG,
|
||||
/* spdlog::level::info */ LOG_INFO,
|
||||
/* spdlog::level::warn */ LOG_WARNING,
|
||||
@ -67,22 +67,25 @@ protected:
|
||||
// Do not send source location if not available
|
||||
if (msg.source.empty()) {
|
||||
// Note: function call inside '()' to avoid macro expansion
|
||||
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d",
|
||||
syslog_level(msg.log_level),
|
||||
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(),
|
||||
"PRIORITY=%d", syslog_level(msg.log_level),
|
||||
#ifndef SPDLOG_NO_THREAD_ID
|
||||
"TID=%zu", details::os::thread_id(),
|
||||
#endif
|
||||
"SYSLOG_IDENTIFIER=%.*s", static_cast<int>(syslog_identifier.size()),
|
||||
"SYSLOG_IDENTIFIER=%.*s",
|
||||
static_cast<int>(syslog_identifier.size()),
|
||||
syslog_identifier.data(), nullptr);
|
||||
} else {
|
||||
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(), "PRIORITY=%d",
|
||||
syslog_level(msg.log_level),
|
||||
err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), payload.data(),
|
||||
"PRIORITY=%d", syslog_level(msg.log_level),
|
||||
#ifndef SPDLOG_NO_THREAD_ID
|
||||
"TID=%zu", details::os::thread_id(),
|
||||
#endif
|
||||
"SYSLOG_IDENTIFIER=%.*s", static_cast<int>(syslog_identifier.size()),
|
||||
syslog_identifier.data(), "CODE_FILE=%s", msg.source.filename, "CODE_LINE=%d",
|
||||
msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr);
|
||||
"SYSLOG_IDENTIFIER=%.*s",
|
||||
static_cast<int>(syslog_identifier.size()),
|
||||
syslog_identifier.data(), "CODE_FILE=%s", msg.source.filename,
|
||||
"CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s",
|
||||
msg.source.funcname, nullptr);
|
||||
}
|
||||
|
||||
if (err) {
|
||||
@ -101,14 +104,16 @@ using systemd_sink_st = systemd_sink<details::null_mutex>;
|
||||
|
||||
// Create and register a syslog logger
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
systemd_logger_mt(const std::string &logger_name, const std::string &ident = "", bool enable_formatting = false) {
|
||||
inline std::shared_ptr<logger> systemd_logger_mt(const std::string &logger_name,
|
||||
const std::string &ident = "",
|
||||
bool enable_formatting = false) {
|
||||
return Factory::template create<sinks::systemd_sink_mt>(logger_name, ident, enable_formatting);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger>
|
||||
systemd_logger_st(const std::string &logger_name, const std::string &ident = "", bool enable_formatting = false) {
|
||||
inline std::shared_ptr<logger> systemd_logger_st(const std::string &logger_name,
|
||||
const std::string &ident = "",
|
||||
bool enable_formatting = false) {
|
||||
return Factory::template create<sinks::systemd_sink_st>(logger_name, ident, enable_formatting);
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
@ -22,7 +22,8 @@
|
||||
// Simple tcp client sink
|
||||
// Connects to remote address and send the formatted log.
|
||||
// Will attempt to reconnect if connection drops.
|
||||
// If more complicated behaviour is needed (i.e get responses), you can inherit it and override the sink_it_ method.
|
||||
// If more complicated behaviour is needed (i.e get responses), you can inherit it and override the
|
||||
// sink_it_ method.
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
@ -33,8 +34,8 @@ struct tcp_sink_config {
|
||||
bool lazy_connect = false; // if true connect on first log call instead of on construction
|
||||
|
||||
tcp_sink_config(std::string host, int port)
|
||||
: server_host{std::move(host)},
|
||||
server_port{port} {}
|
||||
: server_host{std::move(host)}
|
||||
, server_port{port} {}
|
||||
};
|
||||
|
||||
template <typename Mutex>
|
||||
|
@ -28,8 +28,8 @@ struct udp_sink_config {
|
||||
uint16_t server_port;
|
||||
|
||||
udp_sink_config(std::string host, uint16_t port)
|
||||
: server_host{std::move(host)},
|
||||
server_port{port} {}
|
||||
: server_host{std::move(host)}
|
||||
, server_port{port} {}
|
||||
};
|
||||
|
||||
template <typename Mutex>
|
||||
@ -61,7 +61,8 @@ using udp_sink_st = udp_sink<spdlog::details::null_mutex>;
|
||||
// factory functions
|
||||
//
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> udp_logger_mt(const std::string &logger_name, sinks::udp_sink_config skin_config) {
|
||||
inline std::shared_ptr<logger> udp_logger_mt(const std::string &logger_name,
|
||||
sinks::udp_sink_config skin_config) {
|
||||
return Factory::template create<sinks::udp_sink_mt>(logger_name, skin_config);
|
||||
}
|
||||
|
||||
|
@ -1,17 +1,20 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
// Writing to Windows Event Log requires the registry entries below to be present, with the following modifications:
|
||||
// Writing to Windows Event Log requires the registry entries below to be present, with the
|
||||
// following modifications:
|
||||
// 1. <log_name> should be replaced with your log name (e.g. your application name)
|
||||
// 2. <source_name> should be replaced with the specific source name and the key should be duplicated for
|
||||
// 2. <source_name> should be replaced with the specific source name and the key should be
|
||||
// duplicated for
|
||||
// each source used in the application
|
||||
//
|
||||
// Since typically modifications of this kind require elevation, it's better to do it as a part of setup procedure.
|
||||
// The snippet below uses mscoree.dll as the message file as it exists on most of the Windows systems anyway and
|
||||
// happens to contain the needed resource.
|
||||
// Since typically modifications of this kind require elevation, it's better to do it as a part of
|
||||
// setup procedure. The snippet below uses mscoree.dll as the message file as it exists on most of
|
||||
// the Windows systems anyway and happens to contain the needed resource.
|
||||
//
|
||||
// You can also specify a custom message file if needed.
|
||||
// Please refer to Event Log functions descriptions in MSDN for more details on custom message files.
|
||||
// Please refer to Event Log functions descriptions in MSDN for more details on custom message
|
||||
// files.
|
||||
|
||||
/*---------------------------------------------------------------------------------------
|
||||
|
||||
@ -70,9 +73,11 @@ struct win32_error : public spdlog_ex {
|
||||
std::string system_message;
|
||||
|
||||
local_alloc_t format_message_result{};
|
||||
auto format_message_succeeded = ::FormatMessageA(
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
|
||||
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&format_message_result.hlocal_, 0, nullptr);
|
||||
auto format_message_succeeded =
|
||||
::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPSTR)&format_message_result.hlocal_, 0, nullptr);
|
||||
|
||||
if (format_message_succeeded && format_message_result.hlocal_) {
|
||||
system_message = fmt_lib::format(" ({})", (LPSTR)format_message_result.hlocal_);
|
||||
@ -125,18 +130,21 @@ public:
|
||||
|
||||
~process_token_t() { ::CloseHandle(token_handle_); }
|
||||
|
||||
} current_process_token(::GetCurrentProcess()); // GetCurrentProcess returns pseudohandle, no leak here!
|
||||
} current_process_token(
|
||||
::GetCurrentProcess()); // GetCurrentProcess returns pseudohandle, no leak here!
|
||||
|
||||
// Get the required size, this is expected to fail with ERROR_INSUFFICIENT_BUFFER and return the token size
|
||||
// Get the required size, this is expected to fail with ERROR_INSUFFICIENT_BUFFER and return
|
||||
// the token size
|
||||
DWORD tusize = 0;
|
||||
if (::GetTokenInformation(current_process_token.token_handle_, TokenUser, NULL, 0, &tusize)) {
|
||||
if (::GetTokenInformation(current_process_token.token_handle_, TokenUser, NULL, 0,
|
||||
&tusize)) {
|
||||
SPDLOG_THROW(win32_error("GetTokenInformation should fail"));
|
||||
}
|
||||
|
||||
// get user token
|
||||
std::vector<unsigned char> buffer(static_cast<size_t>(tusize));
|
||||
if (!::GetTokenInformation(current_process_token.token_handle_, TokenUser, (LPVOID)buffer.data(), tusize,
|
||||
&tusize)) {
|
||||
if (!::GetTokenInformation(current_process_token.token_handle_, TokenUser,
|
||||
(LPVOID)buffer.data(), tusize, &tusize)) {
|
||||
SPDLOG_THROW(win32_error("GetTokenInformation"));
|
||||
}
|
||||
|
||||
@ -205,9 +213,9 @@ protected:
|
||||
formatted.push_back('\0');
|
||||
|
||||
LPCSTR lp_str = formatted.data();
|
||||
succeeded = static_cast<bool>(::ReportEventA(event_log_handle(), eventlog::get_event_type(msg),
|
||||
eventlog::get_event_category(msg), event_id_,
|
||||
current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr));
|
||||
succeeded = static_cast<bool>(::ReportEventA(
|
||||
event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg),
|
||||
event_id_, current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr));
|
||||
|
||||
if (!succeeded) {
|
||||
SPDLOG_THROW(win32_error("ReportEvent"));
|
||||
@ -217,14 +225,15 @@ protected:
|
||||
void flush_() override {}
|
||||
|
||||
public:
|
||||
win_eventlog_sink(std::string const &source, DWORD event_id = 1000 /* according to mscoree.dll */)
|
||||
: source_(source),
|
||||
event_id_(event_id) {
|
||||
win_eventlog_sink(std::string const &source,
|
||||
DWORD event_id = 1000 /* according to mscoree.dll */)
|
||||
: source_(source)
|
||||
, event_id_(event_id) {
|
||||
try {
|
||||
current_user_sid_ = internal::sid_t::get_current_user_sid();
|
||||
} catch (...) {
|
||||
// get_current_user_sid() is unlikely to fail and if it does, we can still proceed without
|
||||
// current_user_sid but in the event log the record will have no user name
|
||||
// get_current_user_sid() is unlikely to fail and if it does, we can still proceed
|
||||
// without current_user_sid but in the event log the record will have no user name
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,8 @@ using default_factory = synchronous_factory;
|
||||
// spdlog::create<daily_file_sink_st>("logger_name", "dailylog_filename", 11, 59);
|
||||
template <typename Sink, typename... SinkArgs>
|
||||
inline std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&...sink_args) {
|
||||
return default_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
|
||||
return default_factory::create<Sink>(std::move(logger_name),
|
||||
std::forward<SinkArgs>(sink_args)...);
|
||||
}
|
||||
|
||||
// Initialize and register a logger,
|
||||
@ -55,7 +56,8 @@ SPDLOG_API void set_formatter(std::unique_ptr<spdlog::formatter> formatter);
|
||||
|
||||
// Set global format string.
|
||||
// example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v");
|
||||
SPDLOG_API void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
|
||||
SPDLOG_API void set_pattern(std::string pattern,
|
||||
pattern_time_type time_type = pattern_time_type::local);
|
||||
|
||||
// Get global logging level
|
||||
SPDLOG_API level get_level();
|
||||
@ -181,17 +183,29 @@ inline void critical(loc_with_fmt fmt, Args &&...args) {
|
||||
}
|
||||
|
||||
// log functions with no format string, just string
|
||||
inline void trace(string_view_t msg, source_loc loc = source_loc::current()) { log(loc, level::trace, msg); }
|
||||
inline void trace(string_view_t msg, source_loc loc = source_loc::current()) {
|
||||
log(loc, level::trace, msg);
|
||||
}
|
||||
|
||||
inline void debug(string_view_t msg, source_loc loc = source_loc::current()) { log(loc, level::debug, msg); }
|
||||
inline void debug(string_view_t msg, source_loc loc = source_loc::current()) {
|
||||
log(loc, level::debug, msg);
|
||||
}
|
||||
|
||||
inline void info(string_view_t msg, source_loc loc = source_loc::current()) { log(loc, level::info, msg); }
|
||||
inline void info(string_view_t msg, source_loc loc = source_loc::current()) {
|
||||
log(loc, level::info, msg);
|
||||
}
|
||||
|
||||
inline void warn(string_view_t msg, source_loc loc = source_loc::current()) { log(loc, spdlog::level::warn, msg); }
|
||||
inline void warn(string_view_t msg, source_loc loc = source_loc::current()) {
|
||||
log(loc, spdlog::level::warn, msg);
|
||||
}
|
||||
|
||||
inline void error(string_view_t msg, source_loc loc = source_loc::current()) { log(loc, level::err, msg); }
|
||||
inline void error(string_view_t msg, source_loc loc = source_loc::current()) {
|
||||
log(loc, level::err, msg);
|
||||
}
|
||||
|
||||
inline void critical(string_view_t msg, source_loc loc = source_loc::current()) { log(loc, level::critical, msg); }
|
||||
inline void critical(string_view_t msg, source_loc loc = source_loc::current()) {
|
||||
log(loc, level::critical, msg);
|
||||
}
|
||||
#else
|
||||
template <typename... Args>
|
||||
inline void trace(format_string_t<Args...> fmt, Args &&...args) {
|
||||
@ -256,11 +270,13 @@ inline void critical(string_view_t msg) { log(level::critical, msg); }
|
||||
#define SPDLOG_LOGGER_CALL(logger, level, ...) \
|
||||
(logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__)
|
||||
#else
|
||||
#define SPDLOG_LOGGER_CALL(logger, level, ...) (logger)->log(spdlog::source_loc{}, level, __VA_ARGS__)
|
||||
#define SPDLOG_LOGGER_CALL(logger, level, ...) \
|
||||
(logger)->log(spdlog::source_loc{}, level, __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
|
||||
#define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__)
|
||||
#define SPDLOG_LOGGER_TRACE(logger, ...) \
|
||||
SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__)
|
||||
#define SPDLOG_TRACE(...) SPDLOG_LOGGER_TRACE(spdlog::default_logger_raw(), __VA_ARGS__)
|
||||
#else
|
||||
#define SPDLOG_LOGGER_TRACE(logger, ...) (void)0
|
||||
@ -268,7 +284,8 @@ inline void critical(string_view_t msg) { log(level::critical, msg); }
|
||||
#endif
|
||||
|
||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
|
||||
#define SPDLOG_LOGGER_DEBUG(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__)
|
||||
#define SPDLOG_LOGGER_DEBUG(logger, ...) \
|
||||
SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__)
|
||||
#define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::default_logger_raw(), __VA_ARGS__)
|
||||
#else
|
||||
#define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0
|
||||
@ -276,7 +293,8 @@ inline void critical(string_view_t msg) { log(level::critical, msg); }
|
||||
#endif
|
||||
|
||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
|
||||
#define SPDLOG_LOGGER_INFO(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__)
|
||||
#define SPDLOG_LOGGER_INFO(logger, ...) \
|
||||
SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__)
|
||||
#define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__)
|
||||
#else
|
||||
#define SPDLOG_LOGGER_INFO(logger, ...) (void)0
|
||||
@ -284,7 +302,8 @@ inline void critical(string_view_t msg) { log(level::critical, msg); }
|
||||
#endif
|
||||
|
||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
|
||||
#define SPDLOG_LOGGER_WARN(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__)
|
||||
#define SPDLOG_LOGGER_WARN(logger, ...) \
|
||||
SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__)
|
||||
#define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::default_logger_raw(), __VA_ARGS__)
|
||||
#else
|
||||
#define SPDLOG_LOGGER_WARN(logger, ...) (void)0
|
||||
@ -292,7 +311,8 @@ inline void critical(string_view_t msg) { log(level::critical, msg); }
|
||||
#endif
|
||||
|
||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
|
||||
#define SPDLOG_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__)
|
||||
#define SPDLOG_LOGGER_ERROR(logger, ...) \
|
||||
SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__)
|
||||
#define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::default_logger_raw(), __VA_ARGS__)
|
||||
#else
|
||||
#define SPDLOG_LOGGER_ERROR(logger, ...) (void)0
|
||||
@ -300,7 +320,8 @@ inline void critical(string_view_t msg) { log(level::critical, msg); }
|
||||
#endif
|
||||
|
||||
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
|
||||
#define SPDLOG_LOGGER_CRITICAL(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__)
|
||||
#define SPDLOG_LOGGER_CRITICAL(logger, ...) \
|
||||
SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__)
|
||||
#define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::default_logger_raw(), __VA_ARGS__)
|
||||
#else
|
||||
#define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0
|
||||
|
@ -35,7 +35,9 @@ public:
|
||||
stopwatch()
|
||||
: start_tp_{clock::now()} {}
|
||||
|
||||
std::chrono::duration<double> elapsed() const { return std::chrono::duration<double>(clock::now() - start_tp_); }
|
||||
std::chrono::duration<double> elapsed() const {
|
||||
return std::chrono::duration<double>(clock::now() - start_tp_);
|
||||
}
|
||||
|
||||
void reset() { start_tp_ = clock::now(); }
|
||||
};
|
||||
|
@ -85,7 +85,8 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Uncomment to customize level names (e.g. "MY TRACE")
|
||||
//
|
||||
// #define SPDLOG_LEVEL_NAMES { "MY TRACE", "MY DEBUG", "MY INFO", "MY WARNING", "MY ERROR", "MY CRITICAL", "OFF" }
|
||||
// #define SPDLOG_LEVEL_NAMES { "MY TRACE", "MY DEBUG", "MY INFO", "MY WARNING", "MY ERROR", "MY
|
||||
// CRITICAL", "OFF" }
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -12,13 +12,18 @@ spdlog::async_logger::async_logger(std::string logger_name,
|
||||
sinks_init_list sinks_list,
|
||||
std::weak_ptr<details::thread_pool> tp,
|
||||
async_overflow_policy overflow_policy)
|
||||
: async_logger(std::move(logger_name), sinks_list.begin(), sinks_list.end(), std::move(tp), overflow_policy) {}
|
||||
: async_logger(std::move(logger_name),
|
||||
sinks_list.begin(),
|
||||
sinks_list.end(),
|
||||
std::move(tp),
|
||||
overflow_policy) {}
|
||||
|
||||
spdlog::async_logger::async_logger(std::string logger_name,
|
||||
sink_ptr single_sink,
|
||||
std::weak_ptr<details::thread_pool> tp,
|
||||
async_overflow_policy overflow_policy)
|
||||
: async_logger(std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy) {}
|
||||
: async_logger(
|
||||
std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy) {}
|
||||
|
||||
// send the log message to the thread pool
|
||||
void spdlog::async_logger::sink_it_(const details::log_msg &msg) {
|
||||
|
@ -16,8 +16,9 @@ namespace helpers {
|
||||
|
||||
// inplace convert to lowercase
|
||||
inline std::string &to_lower_(std::string &str) {
|
||||
std::transform(str.begin(), str.end(), str.begin(),
|
||||
[](char ch) { return static_cast<char>((ch >= 'A' && ch <= 'Z') ? ch + ('a' - 'A') : ch); });
|
||||
std::transform(str.begin(), str.end(), str.begin(), [](char ch) {
|
||||
return static_cast<char>((ch >= 'A' && ch <= 'Z') ? ch + ('a' - 'A') : ch);
|
||||
});
|
||||
return str;
|
||||
}
|
||||
|
||||
@ -91,7 +92,8 @@ void load_levels(const std::string &input) {
|
||||
}
|
||||
}
|
||||
|
||||
details::registry::instance().set_levels(std::move(levels), global_level_found ? &global_level : nullptr);
|
||||
details::registry::instance().set_levels(std::move(levels),
|
||||
global_level_found ? &global_level : nullptr);
|
||||
}
|
||||
|
||||
} // namespace helpers
|
||||
|
@ -38,7 +38,9 @@ spdlog_ex::spdlog_ex(const std::string &msg, int last_errno) {
|
||||
|
||||
const char *spdlog_ex::what() const noexcept { return msg_.c_str(); }
|
||||
|
||||
void throw_spdlog_ex(const std::string &msg, int last_errno) { SPDLOG_THROW(spdlog_ex(msg, last_errno)); }
|
||||
void throw_spdlog_ex(const std::string &msg, int last_errno) {
|
||||
SPDLOG_THROW(spdlog_ex(msg, last_errno));
|
||||
}
|
||||
|
||||
void throw_spdlog_ex(std::string msg) { SPDLOG_THROW(spdlog_ex(std::move(msg))); }
|
||||
|
||||
|
@ -52,7 +52,8 @@ void file_helper::open(const filename_t &fname, bool truncate) {
|
||||
details::os::sleep_for_millis(open_interval_);
|
||||
}
|
||||
|
||||
throw_spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing", errno);
|
||||
throw_spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing",
|
||||
errno);
|
||||
}
|
||||
|
||||
void file_helper::reopen(bool truncate) {
|
||||
|
@ -12,19 +12,20 @@ log_msg::log_msg(spdlog::log_clock::time_point log_time,
|
||||
string_view_t a_logger_name,
|
||||
spdlog::level lvl,
|
||||
spdlog::string_view_t msg)
|
||||
: logger_name(a_logger_name),
|
||||
log_level(lvl),
|
||||
time(log_time)
|
||||
: logger_name(a_logger_name)
|
||||
, log_level(lvl)
|
||||
, time(log_time)
|
||||
#ifndef SPDLOG_NO_THREAD_ID
|
||||
,
|
||||
thread_id(os::thread_id())
|
||||
, thread_id(os::thread_id())
|
||||
#endif
|
||||
,
|
||||
source(loc),
|
||||
payload(msg) {
|
||||
, source(loc)
|
||||
, payload(msg) {
|
||||
}
|
||||
|
||||
log_msg::log_msg(spdlog::source_loc loc, string_view_t a_logger_name, spdlog::level lvl, spdlog::string_view_t msg)
|
||||
log_msg::log_msg(spdlog::source_loc loc,
|
||||
string_view_t a_logger_name,
|
||||
spdlog::level lvl,
|
||||
spdlog::string_view_t msg)
|
||||
: log_msg(os::now(), loc, a_logger_name, lvl, msg) {}
|
||||
|
||||
log_msg::log_msg(string_view_t a_logger_name, spdlog::level lvl, spdlog::string_view_t msg)
|
||||
|
@ -21,8 +21,8 @@ log_msg_buffer::log_msg_buffer(const log_msg_buffer &other)
|
||||
}
|
||||
|
||||
log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) noexcept
|
||||
: log_msg{other},
|
||||
buffer{std::move(other.buffer)} {
|
||||
: log_msg{other}
|
||||
, buffer{std::move(other.buffer)} {
|
||||
update_string_views();
|
||||
}
|
||||
|
||||
|
@ -73,8 +73,8 @@ spdlog::log_clock::time_point now() noexcept {
|
||||
timespec ts;
|
||||
::clock_gettime(CLOCK_REALTIME_COARSE, &ts);
|
||||
return std::chrono::time_point<log_clock, typename log_clock::duration>(
|
||||
std::chrono::duration_cast<typename log_clock::duration>(std::chrono::seconds(ts.tv_sec) +
|
||||
std::chrono::nanoseconds(ts.tv_nsec)));
|
||||
std::chrono::duration_cast<typename log_clock::duration>(
|
||||
std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec)));
|
||||
|
||||
#else
|
||||
return log_clock::now();
|
||||
@ -134,7 +134,8 @@ bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) {
|
||||
#else // unix
|
||||
#if defined(SPDLOG_PREVENT_CHILD_FD)
|
||||
const int mode_flag = mode == SPDLOG_FILENAME_T("ab") ? O_APPEND : O_TRUNC;
|
||||
const int fd = ::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644));
|
||||
const int fd =
|
||||
::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644));
|
||||
if (fd == -1) {
|
||||
return true;
|
||||
}
|
||||
@ -158,7 +159,9 @@ int remove(const filename_t &filename) noexcept {
|
||||
#endif
|
||||
}
|
||||
|
||||
int remove_if_exists(const filename_t &filename) noexcept { return path_exists(filename) ? remove(filename) : 0; }
|
||||
int remove_if_exists(const filename_t &filename) noexcept {
|
||||
return path_exists(filename) ? remove(filename) : 0;
|
||||
}
|
||||
|
||||
int rename(const filename_t &filename1, const filename_t &filename2) noexcept {
|
||||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||
@ -261,7 +264,8 @@ int utc_minutes_offset(const std::tm &tm) {
|
||||
return offset;
|
||||
#else
|
||||
|
||||
#if defined(sun) || defined(__sun) || defined(_AIX) || (defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
|
||||
#if defined(sun) || defined(__sun) || defined(_AIX) || \
|
||||
(defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
|
||||
(!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE))
|
||||
// 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
|
||||
struct helper {
|
||||
@ -396,17 +400,18 @@ bool is_color_terminal() noexcept {
|
||||
return true;
|
||||
}
|
||||
|
||||
static constexpr std::array<const char *, 16> terms = {{"ansi", "color", "console", "cygwin", "gnome",
|
||||
"konsole", "kterm", "linux", "msys", "putty", "rxvt",
|
||||
"screen", "vt100", "xterm", "alacritty", "vt102"}};
|
||||
static constexpr std::array<const char *, 16> terms = {
|
||||
{"ansi", "color", "console", "cygwin", "gnome", "konsole", "kterm", "linux", "msys",
|
||||
"putty", "rxvt", "screen", "vt100", "xterm", "alacritty", "vt102"}};
|
||||
|
||||
const char *env_term_p = std::getenv("TERM");
|
||||
if (env_term_p == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return std::any_of(terms.begin(), terms.end(),
|
||||
[&](const char *term) { return std::strstr(env_term_p, term) != nullptr; });
|
||||
return std::any_of(terms.begin(), terms.end(), [&](const char *term) {
|
||||
return std::strstr(env_term_p, term) != nullptr;
|
||||
});
|
||||
}();
|
||||
|
||||
return result;
|
||||
@ -438,12 +443,14 @@ void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) {
|
||||
|
||||
int result_size = static_cast<int>(target.capacity());
|
||||
if ((wstr_size + 1) * 2 > result_size) {
|
||||
result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL);
|
||||
result_size =
|
||||
::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL);
|
||||
}
|
||||
|
||||
if (result_size > 0) {
|
||||
target.resize(result_size);
|
||||
result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(), result_size, NULL, NULL);
|
||||
result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(),
|
||||
result_size, NULL, NULL);
|
||||
|
||||
if (result_size > 0) {
|
||||
target.resize(result_size);
|
||||
@ -451,7 +458,8 @@ void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) {
|
||||
}
|
||||
}
|
||||
|
||||
throw_spdlog_ex(fmt_lib::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
|
||||
throw_spdlog_ex(
|
||||
fmt_lib::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
|
||||
}
|
||||
|
||||
void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) {
|
||||
@ -466,19 +474,21 @@ void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) {
|
||||
}
|
||||
|
||||
// find the size to allocate for the result buffer
|
||||
int result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0);
|
||||
int result_size =
|
||||
::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0);
|
||||
|
||||
if (result_size > 0) {
|
||||
target.resize(result_size);
|
||||
result_size =
|
||||
::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, target.data(), result_size);
|
||||
result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size,
|
||||
target.data(), result_size);
|
||||
if (result_size > 0) {
|
||||
assert(result_size == target.size());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw_spdlog_ex(fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError()));
|
||||
throw_spdlog_ex(
|
||||
fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError()));
|
||||
}
|
||||
#endif // defined(SPDLOG_WCHAR_FILENAMES) && defined(_WIN32)
|
||||
|
||||
|
@ -26,7 +26,9 @@ thread_pool::thread_pool(size_t q_max_items,
|
||||
}
|
||||
}
|
||||
|
||||
thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start)
|
||||
thread_pool::thread_pool(size_t q_max_items,
|
||||
size_t threads_n,
|
||||
std::function<void()> on_thread_start)
|
||||
: thread_pool(q_max_items, threads_n, on_thread_start, [] {}) {}
|
||||
|
||||
thread_pool::thread_pool(size_t q_max_items, size_t threads_n)
|
||||
|
@ -23,7 +23,8 @@ template FMT_API auto decimal_point_impl(locale_ref) -> char;
|
||||
|
||||
template FMT_API void buffer<char>::append(const char *, const char *);
|
||||
|
||||
template FMT_API void vformat_to(buffer<char> &, string_view, typename vformat_args<>::type, locale_ref);
|
||||
template FMT_API void
|
||||
vformat_to(buffer<char> &, string_view, typename vformat_args<>::type, locale_ref);
|
||||
|
||||
// Explicit instantiations for wchar_t.
|
||||
|
||||
|
@ -12,18 +12,18 @@ namespace spdlog {
|
||||
|
||||
// public methods
|
||||
logger::logger(const logger &other) noexcept
|
||||
: name_(other.name_),
|
||||
sinks_(other.sinks_),
|
||||
level_(other.level_.load(std::memory_order_relaxed)),
|
||||
flush_level_(other.flush_level_.load(std::memory_order_relaxed)),
|
||||
custom_err_handler_(other.custom_err_handler_) {}
|
||||
: name_(other.name_)
|
||||
, sinks_(other.sinks_)
|
||||
, level_(other.level_.load(std::memory_order_relaxed))
|
||||
, flush_level_(other.flush_level_.load(std::memory_order_relaxed))
|
||||
, custom_err_handler_(other.custom_err_handler_) {}
|
||||
|
||||
logger::logger(logger &&other) noexcept
|
||||
: name_(std::move(other.name_)),
|
||||
sinks_(std::move(other.sinks_)),
|
||||
level_(other.level_.load(std::memory_order_relaxed)),
|
||||
flush_level_(other.flush_level_.load(std::memory_order_relaxed)),
|
||||
custom_err_handler_(std::move(other.custom_err_handler_)) {}
|
||||
: name_(std::move(other.name_))
|
||||
, sinks_(std::move(other.sinks_))
|
||||
, level_(other.level_.load(std::memory_order_relaxed))
|
||||
, flush_level_(other.flush_level_.load(std::memory_order_relaxed))
|
||||
, custom_err_handler_(std::move(other.custom_err_handler_)) {}
|
||||
|
||||
void logger::set_level(level level) { level_.store(level); }
|
||||
|
||||
@ -103,10 +103,11 @@ void logger::err_handler_(const std::string &msg) {
|
||||
char date_buf[64];
|
||||
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
||||
#if defined(USING_R) && defined(R_R_H) // if in R environment
|
||||
REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(), msg.c_str());
|
||||
#else
|
||||
std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(),
|
||||
REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(),
|
||||
msg.c_str());
|
||||
#else
|
||||
std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf,
|
||||
name().c_str(), msg.c_str());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@ -27,8 +27,8 @@ namespace details {
|
||||
class scoped_padder {
|
||||
public:
|
||||
scoped_padder(size_t wrapped_size, const padding_info &padinfo, memory_buf_t &dest)
|
||||
: padinfo_(padinfo),
|
||||
dest_(dest) {
|
||||
: padinfo_(padinfo)
|
||||
, dest_(dest) {
|
||||
remaining_pad_ = static_cast<long>(padinfo.width_) - static_cast<long>(wrapped_size);
|
||||
if (remaining_pad_ <= 0) {
|
||||
return;
|
||||
@ -61,7 +61,8 @@ public:
|
||||
|
||||
private:
|
||||
void pad_it(long count) {
|
||||
fmt_helper::append_string_view(string_view_t(spaces_.data(), static_cast<size_t>(count)), dest_);
|
||||
fmt_helper::append_string_view(string_view_t(spaces_.data(), static_cast<size_t>(count)),
|
||||
dest_);
|
||||
}
|
||||
|
||||
const padding_info &padinfo_;
|
||||
@ -71,7 +72,9 @@ private:
|
||||
};
|
||||
|
||||
struct null_scoped_padder {
|
||||
null_scoped_padder(size_t /*wrapped_size*/, const padding_info & /*padinfo*/, memory_buf_t & /*dest*/) {}
|
||||
null_scoped_padder(size_t /*wrapped_size*/,
|
||||
const padding_info & /*padinfo*/,
|
||||
memory_buf_t & /*dest*/) {}
|
||||
|
||||
template <typename T>
|
||||
static unsigned int count_digits(T /* number */) {
|
||||
@ -178,8 +181,9 @@ public:
|
||||
};
|
||||
|
||||
// Full month name
|
||||
static const std::array<const char *, 12> full_months{{"January", "February", "March", "April", "May", "June", "July",
|
||||
"August", "September", "October", "November", "December"}};
|
||||
static const std::array<const char *, 12> full_months{{"January", "February", "March", "April",
|
||||
"May", "June", "July", "August", "September",
|
||||
"October", "November", "December"}};
|
||||
|
||||
template <typename ScopedPadder>
|
||||
class B_formatter final : public flag_formatter {
|
||||
@ -576,7 +580,9 @@ public:
|
||||
explicit ch_formatter(char ch)
|
||||
: ch_(ch) {}
|
||||
|
||||
void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override { dest.push_back(ch_); }
|
||||
void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override {
|
||||
dest.push_back(ch_);
|
||||
}
|
||||
|
||||
private:
|
||||
char ch_;
|
||||
@ -633,8 +639,8 @@ public:
|
||||
size_t text_size;
|
||||
if (padinfo_.enabled()) {
|
||||
// calc text size for padding based on "filename:line"
|
||||
text_size =
|
||||
std::char_traits<char>::length(msg.source.filename) + ScopedPadder::count_digits(msg.source.line) + 1;
|
||||
text_size = std::char_traits<char>::length(msg.source.filename) +
|
||||
ScopedPadder::count_digits(msg.source.line) + 1;
|
||||
} else {
|
||||
text_size = 0;
|
||||
}
|
||||
@ -658,7 +664,8 @@ public:
|
||||
ScopedPadder p(0, padinfo_, dest);
|
||||
return;
|
||||
}
|
||||
size_t text_size = padinfo_.enabled() ? std::char_traits<char>::length(msg.source.filename) : 0;
|
||||
size_t text_size =
|
||||
padinfo_.enabled() ? std::char_traits<char>::length(msg.source.filename) : 0;
|
||||
ScopedPadder p(text_size, padinfo_, dest);
|
||||
fmt_helper::append_string_view(msg.source.filename, dest);
|
||||
}
|
||||
@ -684,7 +691,8 @@ public:
|
||||
const std::reverse_iterator<const char *> begin(filename + std::strlen(filename));
|
||||
const std::reverse_iterator<const char *> end(filename);
|
||||
|
||||
const auto it = std::find_first_of(begin, end, std::begin(os::folder_seps), std::end(os::folder_seps) - 1);
|
||||
const auto it = std::find_first_of(begin, end, std::begin(os::folder_seps),
|
||||
std::end(os::folder_seps) - 1);
|
||||
return it != end ? it.base() : filename;
|
||||
}
|
||||
}
|
||||
@ -734,7 +742,8 @@ public:
|
||||
ScopedPadder p(0, padinfo_, dest);
|
||||
return;
|
||||
}
|
||||
size_t text_size = padinfo_.enabled() ? std::char_traits<char>::length(msg.source.funcname) : 0;
|
||||
size_t text_size =
|
||||
padinfo_.enabled() ? std::char_traits<char>::length(msg.source.funcname) : 0;
|
||||
ScopedPadder p(text_size, padinfo_, dest);
|
||||
fmt_helper::append_string_view(msg.source.funcname, dest);
|
||||
}
|
||||
@ -747,8 +756,8 @@ public:
|
||||
using DurationUnits = Units;
|
||||
|
||||
explicit elapsed_formatter(padding_info padinfo)
|
||||
: flag_formatter(padinfo),
|
||||
last_message_time_(log_clock::now()) {}
|
||||
: flag_formatter(padinfo)
|
||||
, last_message_time_(log_clock::now()) {}
|
||||
|
||||
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override {
|
||||
auto delta = (std::max)(msg.time - last_message_time_, log_clock::duration::zero());
|
||||
@ -831,7 +840,8 @@ public:
|
||||
if (!msg.source.empty()) {
|
||||
dest.push_back('[');
|
||||
const char *filename =
|
||||
details::short_filename_formatter<details::null_scoped_padder>::basename(msg.source.filename);
|
||||
details::short_filename_formatter<details::null_scoped_padder>::basename(
|
||||
msg.source.filename);
|
||||
fmt_helper::append_string_view(filename, dest);
|
||||
dest.push_back(':');
|
||||
fmt_helper::append_int(msg.source.line, dest);
|
||||
@ -853,23 +863,23 @@ pattern_formatter::pattern_formatter(std::string pattern,
|
||||
pattern_time_type time_type,
|
||||
std::string eol,
|
||||
custom_flags custom_user_flags)
|
||||
: pattern_(std::move(pattern)),
|
||||
eol_(std::move(eol)),
|
||||
pattern_time_type_(time_type),
|
||||
need_localtime_(false),
|
||||
last_log_secs_(0),
|
||||
custom_handlers_(std::move(custom_user_flags)) {
|
||||
: pattern_(std::move(pattern))
|
||||
, eol_(std::move(eol))
|
||||
, pattern_time_type_(time_type)
|
||||
, need_localtime_(false)
|
||||
, last_log_secs_(0)
|
||||
, custom_handlers_(std::move(custom_user_flags)) {
|
||||
std::memset(&cached_tm_, 0, sizeof(cached_tm_));
|
||||
compile_pattern_(pattern_);
|
||||
}
|
||||
|
||||
// use by default full formatter for if pattern is not given
|
||||
pattern_formatter::pattern_formatter(pattern_time_type time_type, std::string eol)
|
||||
: pattern_("%+"),
|
||||
eol_(std::move(eol)),
|
||||
pattern_time_type_(time_type),
|
||||
need_localtime_(true),
|
||||
last_log_secs_(0) {
|
||||
: pattern_("%+")
|
||||
, eol_(std::move(eol))
|
||||
, pattern_time_type_(time_type)
|
||||
, need_localtime_(true)
|
||||
, last_log_secs_(0) {
|
||||
std::memset(&cached_tm_, 0, sizeof(cached_tm_));
|
||||
formatters_.push_back(std::make_unique<details::full_formatter>(details::padding_info{}));
|
||||
}
|
||||
@ -879,8 +889,8 @@ std::unique_ptr<formatter> pattern_formatter::clone() const {
|
||||
for (auto &it : custom_handlers_) {
|
||||
cloned_custom_formatters[it.first] = it.second->clone();
|
||||
}
|
||||
auto cloned =
|
||||
std::make_unique<pattern_formatter>(pattern_, pattern_time_type_, eol_, std::move(cloned_custom_formatters));
|
||||
auto cloned = std::make_unique<pattern_formatter>(pattern_, pattern_time_type_, eol_,
|
||||
std::move(cloned_custom_formatters));
|
||||
cloned->need_localtime(need_localtime_);
|
||||
#if defined(__GNUC__) && __GNUC__ < 5
|
||||
return std::move(cloned);
|
||||
@ -891,7 +901,8 @@ std::unique_ptr<formatter> pattern_formatter::clone() const {
|
||||
|
||||
void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest) {
|
||||
if (need_localtime_) {
|
||||
const auto secs = std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch());
|
||||
const auto secs =
|
||||
std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch());
|
||||
if (secs != last_log_secs_) {
|
||||
cached_tm_ = get_time_(msg);
|
||||
last_log_secs_ = secs;
|
||||
@ -1085,7 +1096,8 @@ void pattern_formatter::handle_flag_(char flag, details::padding_info padding) {
|
||||
break;
|
||||
|
||||
case ('@'): // source location (filename:filenumber)
|
||||
formatters_.push_back(std::make_unique<details::source_location_formatter<Padder>>(padding));
|
||||
formatters_.push_back(
|
||||
std::make_unique<details::source_location_formatter<Padder>>(padding));
|
||||
break;
|
||||
|
||||
case ('s'): // short source filename - without directory name
|
||||
@ -1093,7 +1105,8 @@ void pattern_formatter::handle_flag_(char flag, details::padding_info padding) {
|
||||
break;
|
||||
|
||||
case ('g'): // full source filename
|
||||
formatters_.push_back(std::make_unique<details::source_filename_formatter<Padder>>(padding));
|
||||
formatters_.push_back(
|
||||
std::make_unique<details::source_filename_formatter<Padder>>(padding));
|
||||
break;
|
||||
|
||||
case ('#'): // source line number
|
||||
@ -1101,7 +1114,8 @@ void pattern_formatter::handle_flag_(char flag, details::padding_info padding) {
|
||||
break;
|
||||
|
||||
case ('!'): // source funcname
|
||||
formatters_.push_back(std::make_unique<details::source_funcname_formatter<Padder>>(padding));
|
||||
formatters_.push_back(
|
||||
std::make_unique<details::source_funcname_formatter<Padder>>(padding));
|
||||
break;
|
||||
|
||||
case ('%'): // % char
|
||||
@ -1109,19 +1123,26 @@ void pattern_formatter::handle_flag_(char flag, details::padding_info padding) {
|
||||
break;
|
||||
|
||||
case ('u'): // elapsed time since last log message in nanos
|
||||
formatters_.push_back(std::make_unique<details::elapsed_formatter<Padder, std::chrono::nanoseconds>>(padding));
|
||||
formatters_.push_back(
|
||||
std::make_unique<details::elapsed_formatter<Padder, std::chrono::nanoseconds>>(
|
||||
padding));
|
||||
break;
|
||||
|
||||
case ('i'): // elapsed time since last log message in micros
|
||||
formatters_.push_back(std::make_unique<details::elapsed_formatter<Padder, std::chrono::microseconds>>(padding));
|
||||
formatters_.push_back(
|
||||
std::make_unique<details::elapsed_formatter<Padder, std::chrono::microseconds>>(
|
||||
padding));
|
||||
break;
|
||||
|
||||
case ('o'): // elapsed time since last log message in millis
|
||||
formatters_.push_back(std::make_unique<details::elapsed_formatter<Padder, std::chrono::milliseconds>>(padding));
|
||||
formatters_.push_back(
|
||||
std::make_unique<details::elapsed_formatter<Padder, std::chrono::milliseconds>>(
|
||||
padding));
|
||||
break;
|
||||
|
||||
case ('O'): // elapsed time since last log message in seconds
|
||||
formatters_.push_back(std::make_unique<details::elapsed_formatter<Padder, std::chrono::seconds>>(padding));
|
||||
formatters_.push_back(
|
||||
std::make_unique<details::elapsed_formatter<Padder, std::chrono::seconds>>(padding));
|
||||
break;
|
||||
|
||||
default: // Unknown flag appears as is
|
||||
@ -1132,12 +1153,13 @@ void pattern_formatter::handle_flag_(char flag, details::padding_info padding) {
|
||||
unknown_flag->add_ch(flag);
|
||||
formatters_.push_back((std::move(unknown_flag)));
|
||||
}
|
||||
// fix issue #1617 (prev char was '!' and should have been treated as funcname flag instead of truncating flag)
|
||||
// spdlog::set_pattern("[%10!] %v") => "[ main] some message"
|
||||
// fix issue #1617 (prev char was '!' and should have been treated as funcname flag instead
|
||||
// of truncating flag) spdlog::set_pattern("[%10!] %v") => "[ main] some message"
|
||||
// spdlog::set_pattern("[%3!!] %v") => "[mai] some message"
|
||||
else {
|
||||
padding.truncate_ = false;
|
||||
formatters_.push_back(std::make_unique<details::source_funcname_formatter<Padder>>(padding));
|
||||
formatters_.push_back(
|
||||
std::make_unique<details::source_funcname_formatter<Padder>>(padding));
|
||||
unknown_flag->add_ch(flag);
|
||||
formatters_.push_back((std::move(unknown_flag)));
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ namespace sinks {
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, color_mode mode)
|
||||
: target_file_(target_file),
|
||||
mutex_(ConsoleMutex::mutex()),
|
||||
formatter_(std::make_unique<spdlog::pattern_formatter>())
|
||||
: target_file_(target_file)
|
||||
, mutex_(ConsoleMutex::mutex())
|
||||
, formatter_(std::make_unique<spdlog::pattern_formatter>())
|
||||
|
||||
{
|
||||
set_color_mode(mode);
|
||||
@ -70,7 +70,8 @@ void ansicolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern) {
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
void ansicolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
void ansicolor_sink<ConsoleMutex>::set_formatter(
|
||||
std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
@ -87,7 +88,8 @@ void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode) {
|
||||
should_do_colors_ = true;
|
||||
return;
|
||||
case color_mode::automatic:
|
||||
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
||||
should_do_colors_ =
|
||||
details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
||||
return;
|
||||
case color_mode::never:
|
||||
should_do_colors_ = false;
|
||||
@ -103,7 +105,9 @@ void ansicolor_sink<ConsoleMutex>::print_ccode_(const string_view_t &color_code)
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) {
|
||||
void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted,
|
||||
size_t start,
|
||||
size_t end) {
|
||||
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
|
||||
}
|
||||
|
||||
|
@ -35,7 +35,8 @@ void spdlog::sinks::base_sink<Mutex>::set_pattern(const std::string &pattern) {
|
||||
}
|
||||
|
||||
template <typename Mutex>
|
||||
void spdlog::sinks::base_sink<Mutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
void spdlog::sinks::base_sink<Mutex>::set_formatter(
|
||||
std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
set_formatter_(std::move(sink_formatter));
|
||||
}
|
||||
@ -46,7 +47,8 @@ void spdlog::sinks::base_sink<Mutex>::set_pattern_(const std::string &pattern) {
|
||||
}
|
||||
|
||||
template <typename Mutex>
|
||||
void spdlog::sinks::base_sink<Mutex>::set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
void spdlog::sinks::base_sink<Mutex>::set_formatter_(
|
||||
std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
|
||||
|
@ -22,10 +22,10 @@ rotating_file_sink<Mutex>::rotating_file_sink(filename_t base_filename,
|
||||
std::size_t max_files,
|
||||
bool rotate_on_open,
|
||||
const file_event_handlers &event_handlers)
|
||||
: base_filename_(std::move(base_filename)),
|
||||
max_size_(max_size),
|
||||
max_files_(max_files),
|
||||
file_helper_{event_handlers} {
|
||||
: base_filename_(std::move(base_filename))
|
||||
, max_size_(max_size)
|
||||
, max_files_(max_files)
|
||||
, file_helper_{event_handlers} {
|
||||
if (max_size == 0) {
|
||||
throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero");
|
||||
}
|
||||
@ -109,10 +109,11 @@ void rotating_file_sink<Mutex>::rotate_() {
|
||||
// rates can cause the rename to fail with permission denied (because of antivirus?).
|
||||
details::os::sleep_for_millis(100);
|
||||
if (!rename_file_(src, target)) {
|
||||
file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit!
|
||||
file_helper_.reopen(
|
||||
true); // truncate the log file anyway to prevent it to grow beyond its limit!
|
||||
current_size_ = 0;
|
||||
throw_spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " +
|
||||
filename_to_str(target),
|
||||
throw_spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) +
|
||||
" to " + filename_to_str(target),
|
||||
errno);
|
||||
}
|
||||
}
|
||||
@ -123,7 +124,8 @@ void rotating_file_sink<Mutex>::rotate_() {
|
||||
// delete the target if exists, and rename the src file to target
|
||||
// return true on success, false otherwise.
|
||||
template <typename Mutex>
|
||||
bool rotating_file_sink<Mutex>::rename_file_(const filename_t &src_filename, const filename_t &target_filename) {
|
||||
bool rotating_file_sink<Mutex>::rename_file_(const filename_t &src_filename,
|
||||
const filename_t &target_filename) {
|
||||
// try to delete the target file in case it already exists.
|
||||
(void)details::os::remove(target_filename);
|
||||
return details::os::rename(src_filename, target_filename) == 0;
|
||||
|
@ -32,16 +32,20 @@ std::shared_ptr<logger> stderr_color_st(const std::string &logger_name, color_mo
|
||||
|
||||
// template instantiations
|
||||
template SPDLOG_API std::shared_ptr<spdlog::logger>
|
||||
spdlog::stdout_color_mt<spdlog::synchronous_factory>(const std::string &logger_name, color_mode mode);
|
||||
spdlog::stdout_color_mt<spdlog::synchronous_factory>(const std::string &logger_name,
|
||||
color_mode mode);
|
||||
|
||||
template SPDLOG_API std::shared_ptr<spdlog::logger>
|
||||
spdlog::stdout_color_st<spdlog::synchronous_factory>(const std::string &logger_name, color_mode mode);
|
||||
spdlog::stdout_color_st<spdlog::synchronous_factory>(const std::string &logger_name,
|
||||
color_mode mode);
|
||||
|
||||
template SPDLOG_API std::shared_ptr<spdlog::logger>
|
||||
spdlog::stderr_color_mt<spdlog::synchronous_factory>(const std::string &logger_name, color_mode mode);
|
||||
spdlog::stderr_color_mt<spdlog::synchronous_factory>(const std::string &logger_name,
|
||||
color_mode mode);
|
||||
|
||||
template SPDLOG_API std::shared_ptr<spdlog::logger>
|
||||
spdlog::stderr_color_st<spdlog::synchronous_factory>(const std::string &logger_name, color_mode mode);
|
||||
spdlog::stderr_color_st<spdlog::synchronous_factory>(const std::string &logger_name,
|
||||
color_mode mode);
|
||||
|
||||
template SPDLOG_API std::shared_ptr<spdlog::logger>
|
||||
spdlog::stdout_color_mt<spdlog::async_factory>(const std::string &logger_name, color_mode mode);
|
||||
|
@ -24,9 +24,9 @@ namespace sinks {
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
|
||||
: mutex_(ConsoleMutex::mutex()),
|
||||
file_(file),
|
||||
formatter_(std::make_unique<spdlog::pattern_formatter>()) {
|
||||
: mutex_(ConsoleMutex::mutex())
|
||||
, file_(file)
|
||||
, formatter_(std::make_unique<spdlog::pattern_formatter>()) {
|
||||
#ifdef _WIN32
|
||||
// get windows handle from the FILE* object
|
||||
|
||||
@ -54,7 +54,8 @@ void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg) {
|
||||
DWORD bytes_written = 0;
|
||||
bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0;
|
||||
if (!ok) {
|
||||
throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " + std::to_string(::GetLastError()));
|
||||
throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " +
|
||||
std::to_string(::GetLastError()));
|
||||
}
|
||||
#else
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
@ -78,7 +79,8 @@ void stdout_sink_base<ConsoleMutex>::set_pattern(const std::string &pattern) {
|
||||
}
|
||||
|
||||
template <typename ConsoleMutex>
|
||||
void stdout_sink_base<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
void stdout_sink_base<ConsoleMutex>::set_formatter(
|
||||
std::unique_ptr<spdlog::formatter> sink_formatter) {
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
|
@ -13,19 +13,20 @@ namespace spdlog {
|
||||
namespace sinks {
|
||||
template <typename ConsoleMutex>
|
||||
wincolor_sink<ConsoleMutex>::wincolor_sink(void *out_handle, color_mode mode)
|
||||
: out_handle_(out_handle),
|
||||
mutex_(ConsoleMutex::mutex()),
|
||||
formatter_(std::make_unique<spdlog::pattern_formatter>()) {
|
||||
: out_handle_(out_handle)
|
||||
, mutex_(ConsoleMutex::mutex())
|
||||
, formatter_(std::make_unique<spdlog::pattern_formatter>()) {
|
||||
set_color_mode_impl(mode);
|
||||
// set level colors
|
||||
colors_.at(level_to_number(level::trace)) = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white
|
||||
colors_.at(level_to_number(level::trace)) =
|
||||
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white
|
||||
colors_.at(level_to_number(level::debug)) = FOREGROUND_GREEN | FOREGROUND_BLUE; // cyan
|
||||
colors_.at(level_to_number(level::info)) = FOREGROUND_GREEN; // green
|
||||
colors_.at(level_to_number(level::warn)) =
|
||||
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; // intense yellow
|
||||
colors_.at(level_to_number(level::err)) = FOREGROUND_RED | FOREGROUND_INTENSITY; // intense red
|
||||
colors_.at(level_to_number(level::critical)) = BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN |
|
||||
FOREGROUND_BLUE |
|
||||
colors_.at(level_to_number(level::critical)) =
|
||||
BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE |
|
||||
FOREGROUND_INTENSITY; // intense white on red background
|
||||
colors_.at(level_to_number(level::off)) = 0;
|
||||
}
|
||||
@ -57,7 +58,8 @@ void wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg) {
|
||||
// before color range
|
||||
print_range_(formatted, 0, msg.color_range_start);
|
||||
// in color range
|
||||
auto orig_attribs = static_cast<WORD>(set_foreground_color_(colors_[static_cast<size_t>(msg.log_level)]));
|
||||
auto orig_attribs =
|
||||
static_cast<WORD>(set_foreground_color_(colors_[static_cast<size_t>(msg.log_level)]));
|
||||
print_range_(formatted, msg.color_range_start, msg.color_range_end);
|
||||
// reset to orig colors
|
||||
::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), orig_attribs);
|
||||
@ -114,18 +116,21 @@ std::uint16_t wincolor_sink<ConsoleMutex>::set_foreground_color_(std::uint16_t a
|
||||
|
||||
// change only the foreground bits (lowest 4 bits)
|
||||
auto new_attribs = static_cast<WORD>(attribs) | (orig_buffer_info.wAttributes & 0xfff0);
|
||||
auto ignored = ::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), static_cast<WORD>(new_attribs));
|
||||
auto ignored =
|
||||
::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), static_cast<WORD>(new_attribs));
|
||||
(void)(ignored);
|
||||
return static_cast<std::uint16_t>(orig_buffer_info.wAttributes); // return orig attribs
|
||||
}
|
||||
|
||||
// print a range of formatted message to console
|
||||
template <typename ConsoleMutex>
|
||||
void wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) {
|
||||
void wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted,
|
||||
size_t start,
|
||||
size_t end) {
|
||||
if (end > start) {
|
||||
auto size = static_cast<DWORD>(end - start);
|
||||
auto ignored =
|
||||
::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start, size, nullptr, nullptr);
|
||||
auto ignored = ::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start,
|
||||
size, nullptr, nullptr);
|
||||
(void)(ignored);
|
||||
}
|
||||
}
|
||||
@ -134,7 +139,8 @@ template <typename ConsoleMutex>
|
||||
void wincolor_sink<ConsoleMutex>::write_to_file_(const memory_buf_t &formatted) {
|
||||
auto size = static_cast<DWORD>(formatted.size());
|
||||
DWORD bytes_written = 0;
|
||||
auto ignored = ::WriteFile(static_cast<HANDLE>(out_handle_), formatted.data(), size, &bytes_written, nullptr);
|
||||
auto ignored = ::WriteFile(static_cast<HANDLE>(out_handle_), formatted.data(), size,
|
||||
&bytes_written, nullptr);
|
||||
(void)(ignored);
|
||||
}
|
||||
|
||||
|
@ -11,14 +11,17 @@ void initialize_logger(std::shared_ptr<logger> logger) {
|
||||
details::registry::instance().initialize_logger(std::move(logger));
|
||||
}
|
||||
|
||||
std::shared_ptr<logger> get(const std::string &name) { return details::registry::instance().get(name); }
|
||||
std::shared_ptr<logger> get(const std::string &name) {
|
||||
return details::registry::instance().get(name);
|
||||
}
|
||||
|
||||
void set_formatter(std::unique_ptr<spdlog::formatter> formatter) {
|
||||
details::registry::instance().set_formatter(std::move(formatter));
|
||||
}
|
||||
|
||||
void set_pattern(std::string pattern, pattern_time_type time_type) {
|
||||
set_formatter(std::unique_ptr<spdlog::formatter>(new pattern_formatter(std::move(pattern), time_type)));
|
||||
set_formatter(
|
||||
std::unique_ptr<spdlog::formatter>(new pattern_formatter(std::move(pattern), time_type)));
|
||||
}
|
||||
|
||||
level get_level() { return default_logger_raw()->log_level(); }
|
||||
@ -51,7 +54,9 @@ void set_automatic_registration(bool automatic_registration) {
|
||||
details::registry::instance().set_automatic_registration(automatic_registration);
|
||||
}
|
||||
|
||||
std::shared_ptr<spdlog::logger> default_logger() { return details::registry::instance().default_logger(); }
|
||||
std::shared_ptr<spdlog::logger> default_logger() {
|
||||
return details::registry::instance().default_logger();
|
||||
}
|
||||
|
||||
spdlog::logger *default_logger_raw() { return details::registry::instance().get_default_raw(); }
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ == 12
|
||||
# pragma GCC diagnostic push
|
||||
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // Workaround for GCC 12
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // Workaround for GCC 12
|
||||
#endif
|
||||
#include <catch2/catch_all.hpp>
|
||||
#if defined(__GNUC__) && __GNUC__ == 12
|
||||
# pragma GCC diagnostic pop
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
#include "utils.h"
|
||||
|
@ -1,10 +1,10 @@
|
||||
#if defined(__GNUC__) && __GNUC__ == 12
|
||||
# pragma GCC diagnostic push
|
||||
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // Workaround for GCC 12
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // Workaround for GCC 12
|
||||
#endif
|
||||
|
||||
#include <catch2/catch_all.hpp>
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ == 12
|
||||
# pragma GCC diagnostic pop
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
@ -5,17 +5,16 @@
|
||||
|
||||
#define TEST_FILENAME "test_logs/async_test.log"
|
||||
|
||||
TEST_CASE("basic async test ", "[async]")
|
||||
{
|
||||
TEST_CASE("basic async test ", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
size_t overrun_counter = 0;
|
||||
size_t queue_size = 128;
|
||||
size_t messages = 256;
|
||||
{
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
for (size_t i = 0; i < messages; i++)
|
||||
{
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp,
|
||||
spdlog::async_overflow_policy::block);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message #{}", i);
|
||||
}
|
||||
logger->flush();
|
||||
@ -26,42 +25,39 @@ TEST_CASE("basic async test ", "[async]")
|
||||
REQUIRE(overrun_counter == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("discard policy ", "[async]")
|
||||
{
|
||||
TEST_CASE("discard policy ", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
test_sink->set_delay(std::chrono::milliseconds(1));
|
||||
size_t queue_size = 4;
|
||||
size_t messages = 1024;
|
||||
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::overrun_oldest);
|
||||
for (size_t i = 0; i < messages; i++)
|
||||
{
|
||||
auto logger = std::make_shared<spdlog::async_logger>(
|
||||
"as", test_sink, tp, spdlog::async_overflow_policy::overrun_oldest);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message");
|
||||
}
|
||||
REQUIRE(test_sink->msg_counter() < messages);
|
||||
REQUIRE(tp->overrun_counter() > 0);
|
||||
}
|
||||
|
||||
TEST_CASE("discard policy discard_new ", "[async]")
|
||||
{
|
||||
TEST_CASE("discard policy discard_new ", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
test_sink->set_delay(std::chrono::milliseconds(1));
|
||||
size_t queue_size = 4;
|
||||
size_t messages = 1024;
|
||||
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::discard_new);
|
||||
for (size_t i = 0; i < messages; i++)
|
||||
{
|
||||
auto logger = std::make_shared<spdlog::async_logger>(
|
||||
"as", test_sink, tp, spdlog::async_overflow_policy::discard_new);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message");
|
||||
}
|
||||
REQUIRE(test_sink->msg_counter() < messages);
|
||||
REQUIRE(tp->discard_counter() > 0);
|
||||
}
|
||||
|
||||
TEST_CASE("discard policy using factory ", "[async]")
|
||||
{
|
||||
TEST_CASE("discard policy using factory ", "[async]") {
|
||||
size_t queue_size = 4;
|
||||
size_t messages = 1024;
|
||||
spdlog::init_thread_pool(queue_size, 1);
|
||||
@ -70,8 +66,7 @@ TEST_CASE("discard policy using factory ", "[async]")
|
||||
auto test_sink = std::static_pointer_cast<spdlog::sinks::test_sink_mt>(logger->sinks()[0]);
|
||||
test_sink->set_delay(std::chrono::milliseconds(3));
|
||||
|
||||
for (size_t i = 0; i < messages; i++)
|
||||
{
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message");
|
||||
}
|
||||
|
||||
@ -79,16 +74,15 @@ TEST_CASE("discard policy using factory ", "[async]")
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("flush", "[async]")
|
||||
{
|
||||
TEST_CASE("flush", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
size_t queue_size = 256;
|
||||
size_t messages = 256;
|
||||
{
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
for (size_t i = 0; i < messages; i++)
|
||||
{
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp,
|
||||
spdlog::async_overflow_policy::block);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message #{}", i);
|
||||
}
|
||||
|
||||
@ -99,8 +93,7 @@ TEST_CASE("flush", "[async]")
|
||||
REQUIRE(test_sink->flush_counter() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("async periodic flush", "[async]")
|
||||
{
|
||||
TEST_CASE("async periodic flush", "[async]") {
|
||||
|
||||
auto logger = spdlog::create_async<spdlog::sinks::test_sink_mt>("as");
|
||||
auto test_sink = std::static_pointer_cast<spdlog::sinks::test_sink_mt>(logger->sinks()[0]);
|
||||
@ -112,16 +105,15 @@ TEST_CASE("async periodic flush", "[async]")
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("tp->wait_empty() ", "[async]")
|
||||
{
|
||||
TEST_CASE("tp->wait_empty() ", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
test_sink->set_delay(std::chrono::milliseconds(5));
|
||||
size_t messages = 100;
|
||||
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, 2);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
for (size_t i = 0; i < messages; i++)
|
||||
{
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp,
|
||||
spdlog::async_overflow_policy::block);
|
||||
for (size_t i = 0; i < messages; i++) {
|
||||
logger->info("Hello message #{}", i);
|
||||
}
|
||||
logger->flush();
|
||||
@ -131,30 +123,27 @@ TEST_CASE("tp->wait_empty() ", "[async]")
|
||||
REQUIRE(test_sink->flush_counter() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("multi threads", "[async]")
|
||||
{
|
||||
TEST_CASE("multi threads", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
size_t queue_size = 128;
|
||||
size_t messages = 256;
|
||||
size_t n_threads = 10;
|
||||
{
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(queue_size, 1);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp, spdlog::async_overflow_policy::block);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, tp,
|
||||
spdlog::async_overflow_policy::block);
|
||||
|
||||
std::vector<std::thread> threads;
|
||||
for (size_t i = 0; i < n_threads; i++)
|
||||
{
|
||||
for (size_t i = 0; i < n_threads; i++) {
|
||||
threads.emplace_back([logger, messages] {
|
||||
for (size_t j = 0; j < messages; j++)
|
||||
{
|
||||
for (size_t j = 0; j < messages; j++) {
|
||||
logger->info("Hello message #{}", j);
|
||||
}
|
||||
});
|
||||
logger->flush();
|
||||
}
|
||||
|
||||
for (auto &t : threads)
|
||||
{
|
||||
for (auto &t : threads) {
|
||||
t.join();
|
||||
}
|
||||
}
|
||||
@ -163,8 +152,7 @@ TEST_CASE("multi threads", "[async]")
|
||||
REQUIRE(test_sink->flush_counter() == n_threads);
|
||||
}
|
||||
|
||||
TEST_CASE("to_file", "[async]")
|
||||
{
|
||||
TEST_CASE("to_file", "[async]") {
|
||||
prepare_logdir();
|
||||
size_t messages = 1024;
|
||||
size_t tp_threads = 1;
|
||||
@ -172,10 +160,10 @@ TEST_CASE("to_file", "[async]")
|
||||
{
|
||||
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));
|
||||
auto logger =
|
||||
std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));
|
||||
|
||||
for (size_t j = 0; j < messages; j++)
|
||||
{
|
||||
for (size_t j = 0; j < messages; j++) {
|
||||
logger->info("Hello message #{}", j);
|
||||
}
|
||||
}
|
||||
@ -186,8 +174,7 @@ TEST_CASE("to_file", "[async]")
|
||||
REQUIRE(ends_with(contents, spdlog::fmt_lib::format("Hello message #1023{}", default_eol)));
|
||||
}
|
||||
|
||||
TEST_CASE("to_file multi-workers", "[async]")
|
||||
{
|
||||
TEST_CASE("to_file multi-workers", "[async]") {
|
||||
prepare_logdir();
|
||||
size_t messages = 1024 * 10;
|
||||
size_t tp_threads = 10;
|
||||
@ -195,18 +182,17 @@ TEST_CASE("to_file multi-workers", "[async]")
|
||||
{
|
||||
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true);
|
||||
auto tp = std::make_shared<spdlog::details::thread_pool>(messages, tp_threads);
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));
|
||||
auto logger =
|
||||
std::make_shared<spdlog::async_logger>("as", std::move(file_sink), std::move(tp));
|
||||
|
||||
for (size_t j = 0; j < messages; j++)
|
||||
{
|
||||
for (size_t j = 0; j < messages; j++) {
|
||||
logger->info("Hello message #{}", j);
|
||||
}
|
||||
}
|
||||
require_message_count(TEST_FILENAME, messages);
|
||||
}
|
||||
|
||||
TEST_CASE("bad_tp", "[async]")
|
||||
{
|
||||
TEST_CASE("bad_tp", "[async]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_mt>();
|
||||
std::shared_ptr<spdlog::details::thread_pool> const empty_tp;
|
||||
auto logger = std::make_shared<spdlog::async_logger>("as", test_sink, empty_tp);
|
||||
|
@ -2,8 +2,7 @@
|
||||
#include "test_sink.h"
|
||||
#include "spdlog/fmt/bin_to_hex.h"
|
||||
|
||||
TEST_CASE("to_hex", "[to_hex]")
|
||||
{
|
||||
TEST_CASE("to_hex", "[to_hex]") {
|
||||
std::ostringstream oss;
|
||||
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
@ -12,11 +11,11 @@ TEST_CASE("to_hex", "[to_hex]")
|
||||
oss_logger.info("{}", spdlog::to_hex(v));
|
||||
|
||||
auto output = oss.str();
|
||||
REQUIRE(ends_with(output, "0000: 09 0a 0b 0c ff ff" + std::string(spdlog::details::os::default_eol)));
|
||||
REQUIRE(ends_with(output,
|
||||
"0000: 09 0a 0b 0c ff ff" + std::string(spdlog::details::os::default_eol)));
|
||||
}
|
||||
|
||||
TEST_CASE("to_hex_upper", "[to_hex]")
|
||||
{
|
||||
TEST_CASE("to_hex_upper", "[to_hex]") {
|
||||
std::ostringstream oss;
|
||||
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
@ -25,11 +24,11 @@ TEST_CASE("to_hex_upper", "[to_hex]")
|
||||
oss_logger.info("{:X}", spdlog::to_hex(v));
|
||||
|
||||
auto output = oss.str();
|
||||
REQUIRE(ends_with(output, "0000: 09 0A 0B 0C FF FF" + std::string(spdlog::details::os::default_eol)));
|
||||
REQUIRE(ends_with(output,
|
||||
"0000: 09 0A 0B 0C FF FF" + std::string(spdlog::details::os::default_eol)));
|
||||
}
|
||||
|
||||
TEST_CASE("to_hex_no_delimiter", "[to_hex]")
|
||||
{
|
||||
TEST_CASE("to_hex_no_delimiter", "[to_hex]") {
|
||||
std::ostringstream oss;
|
||||
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
@ -38,11 +37,11 @@ TEST_CASE("to_hex_no_delimiter", "[to_hex]")
|
||||
oss_logger.info("{:sX}", spdlog::to_hex(v));
|
||||
|
||||
auto output = oss.str();
|
||||
REQUIRE(ends_with(output, "0000: 090A0B0CFFFF" + std::string(spdlog::details::os::default_eol)));
|
||||
REQUIRE(
|
||||
ends_with(output, "0000: 090A0B0CFFFF" + std::string(spdlog::details::os::default_eol)));
|
||||
}
|
||||
|
||||
TEST_CASE("to_hex_show_ascii", "[to_hex]")
|
||||
{
|
||||
TEST_CASE("to_hex_show_ascii", "[to_hex]") {
|
||||
std::ostringstream oss;
|
||||
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
@ -50,11 +49,11 @@ TEST_CASE("to_hex_show_ascii", "[to_hex]")
|
||||
std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
|
||||
oss_logger.info("{:Xsa}", spdlog::to_hex(v, 8));
|
||||
|
||||
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." + std::string(spdlog::details::os::default_eol)));
|
||||
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." +
|
||||
std::string(spdlog::details::os::default_eol)));
|
||||
}
|
||||
|
||||
TEST_CASE("to_hex_different_size_per_line", "[to_hex]")
|
||||
{
|
||||
TEST_CASE("to_hex_different_size_per_line", "[to_hex]") {
|
||||
std::ostringstream oss;
|
||||
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
@ -62,22 +61,25 @@ TEST_CASE("to_hex_different_size_per_line", "[to_hex]")
|
||||
std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
|
||||
|
||||
oss_logger.info("{:Xsa}", spdlog::to_hex(v, 10));
|
||||
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." + std::string(spdlog::details::os::default_eol)));
|
||||
|
||||
oss_logger.info("{:Xs}", spdlog::to_hex(v, 10));
|
||||
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
|
||||
|
||||
oss_logger.info("{:Xsa}", spdlog::to_hex(v, 6));
|
||||
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4B ...A.K" + std::string(spdlog::details::os::default_eol) + "0006: FFFF .." +
|
||||
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." +
|
||||
std::string(spdlog::details::os::default_eol)));
|
||||
|
||||
oss_logger.info("{:Xs}", spdlog::to_hex(v, 10));
|
||||
REQUIRE(ends_with(oss.str(),
|
||||
"0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
|
||||
|
||||
oss_logger.info("{:Xsa}", spdlog::to_hex(v, 6));
|
||||
REQUIRE(ends_with(
|
||||
oss.str(), "0000: 090A0B410C4B ...A.K" + std::string(spdlog::details::os::default_eol) +
|
||||
"0006: FFFF .." + std::string(spdlog::details::os::default_eol)));
|
||||
|
||||
oss_logger.info("{:Xs}", spdlog::to_hex(v, 6));
|
||||
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4B" + std::string(spdlog::details::os::default_eol) + "0006: FFFF" +
|
||||
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4B" +
|
||||
std::string(spdlog::details::os::default_eol) + "0006: FFFF" +
|
||||
std::string(spdlog::details::os::default_eol)));
|
||||
}
|
||||
|
||||
TEST_CASE("to_hex_no_ascii", "[to_hex]")
|
||||
{
|
||||
TEST_CASE("to_hex_no_ascii", "[to_hex]") {
|
||||
std::ostringstream oss;
|
||||
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
@ -85,9 +87,11 @@ TEST_CASE("to_hex_no_ascii", "[to_hex]")
|
||||
std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
|
||||
oss_logger.info("{:Xs}", spdlog::to_hex(v, 8));
|
||||
|
||||
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
|
||||
REQUIRE(ends_with(oss.str(),
|
||||
"0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
|
||||
|
||||
oss_logger.info("{:Xsna}", spdlog::to_hex(v, 8));
|
||||
|
||||
REQUIRE(ends_with(oss.str(), "090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
|
||||
REQUIRE(
|
||||
ends_with(oss.str(), "090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
|
||||
}
|
||||
|
@ -9,8 +9,7 @@ using spdlog::cfg::load_argv_levels;
|
||||
using spdlog::cfg::load_env_levels;
|
||||
using spdlog::sinks::test_sink_st;
|
||||
|
||||
TEST_CASE("env", "[cfg]")
|
||||
{
|
||||
TEST_CASE("env", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
auto l1 = spdlog::create<test_sink_st>("l1");
|
||||
#ifdef CATCH_PLATFORM_WINDOWS
|
||||
@ -24,8 +23,7 @@ TEST_CASE("env", "[cfg]")
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("argv1", "[cfg]")
|
||||
{
|
||||
TEST_CASE("argv1", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=warn"};
|
||||
load_argv_levels(2, argv);
|
||||
@ -34,8 +32,7 @@ TEST_CASE("argv1", "[cfg]")
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("argv2", "[cfg]")
|
||||
{
|
||||
TEST_CASE("argv2", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=warn,trace"};
|
||||
load_argv_levels(2, argv);
|
||||
@ -44,8 +41,7 @@ TEST_CASE("argv2", "[cfg]")
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::trace);
|
||||
}
|
||||
|
||||
TEST_CASE("argv3", "[cfg]")
|
||||
{
|
||||
TEST_CASE("argv3", "[cfg]") {
|
||||
spdlog::set_level(spdlog::level::trace);
|
||||
|
||||
spdlog::drop("l1");
|
||||
@ -56,8 +52,7 @@ TEST_CASE("argv3", "[cfg]")
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::trace);
|
||||
}
|
||||
|
||||
TEST_CASE("argv4", "[cfg]")
|
||||
{
|
||||
TEST_CASE("argv4", "[cfg]") {
|
||||
spdlog::set_level(spdlog::level::info);
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=junk"};
|
||||
@ -66,8 +61,7 @@ TEST_CASE("argv4", "[cfg]")
|
||||
REQUIRE(l1->log_level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("argv5", "[cfg]")
|
||||
{
|
||||
TEST_CASE("argv5", "[cfg]") {
|
||||
spdlog::set_level(spdlog::level::info);
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", "ignore", "SPDLOG_LEVEL=l1=warn,trace"};
|
||||
@ -78,8 +72,7 @@ TEST_CASE("argv5", "[cfg]")
|
||||
spdlog::set_level(spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("argv6", "[cfg]")
|
||||
{
|
||||
TEST_CASE("argv6", "[cfg]") {
|
||||
spdlog::set_level(spdlog::level::err);
|
||||
const char *argv[] = {""};
|
||||
load_argv_levels(1, argv);
|
||||
@ -87,8 +80,7 @@ TEST_CASE("argv6", "[cfg]")
|
||||
spdlog::set_level(spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("argv7", "[cfg]")
|
||||
{
|
||||
TEST_CASE("argv7", "[cfg]") {
|
||||
spdlog::set_level(spdlog::level::err);
|
||||
const char *argv[] = {""};
|
||||
load_argv_levels(0, argv);
|
||||
@ -96,8 +88,7 @@ TEST_CASE("argv7", "[cfg]")
|
||||
spdlog::set_level(spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("level-not-set-test1", "[cfg]")
|
||||
{
|
||||
TEST_CASE("level-not-set-test1", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
const char *argv[] = {"ignore", ""};
|
||||
load_argv_levels(2, argv);
|
||||
@ -107,8 +98,7 @@ TEST_CASE("level-not-set-test1", "[cfg]")
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("level-not-set-test2", "[cfg]")
|
||||
{
|
||||
TEST_CASE("level-not-set-test2", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
spdlog::drop("l2");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace"};
|
||||
@ -125,8 +115,7 @@ TEST_CASE("level-not-set-test2", "[cfg]")
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("level-not-set-test3", "[cfg]")
|
||||
{
|
||||
TEST_CASE("level-not-set-test3", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
spdlog::drop("l2");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace"};
|
||||
@ -141,8 +130,7 @@ TEST_CASE("level-not-set-test3", "[cfg]")
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::info);
|
||||
}
|
||||
|
||||
TEST_CASE("level-not-set-test4", "[cfg]")
|
||||
{
|
||||
TEST_CASE("level-not-set-test4", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
spdlog::drop("l2");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace,warn"};
|
||||
@ -157,8 +145,7 @@ TEST_CASE("level-not-set-test4", "[cfg]")
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::warn);
|
||||
}
|
||||
|
||||
TEST_CASE("level-not-set-test5", "[cfg]")
|
||||
{
|
||||
TEST_CASE("level-not-set-test5", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
spdlog::drop("l2");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=junk,warn"};
|
||||
@ -173,8 +160,7 @@ TEST_CASE("level-not-set-test5", "[cfg]")
|
||||
REQUIRE(spdlog::default_logger()->log_level() == spdlog::level::warn);
|
||||
}
|
||||
|
||||
TEST_CASE("restore-to-default", "[cfg]")
|
||||
{
|
||||
TEST_CASE("restore-to-default", "[cfg]") {
|
||||
spdlog::drop("l1");
|
||||
spdlog::drop("l2");
|
||||
const char *argv[] = {"ignore", "SPDLOG_LEVEL=info"};
|
||||
|
@ -3,15 +3,13 @@
|
||||
|
||||
using q_type = spdlog::details::circular_q<size_t>;
|
||||
|
||||
TEST_CASE("test_size", "[circular_q]")
|
||||
{
|
||||
TEST_CASE("test_size", "[circular_q]") {
|
||||
const size_t q_size = 4;
|
||||
q_type q(q_size);
|
||||
REQUIRE(q.size() == 0);
|
||||
REQUIRE(q.empty() == true);
|
||||
REQUIRE(q.full() == false);
|
||||
for (size_t i = 0; i < q_size; i++)
|
||||
{
|
||||
for (size_t i = 0; i < q_size; i++) {
|
||||
q.push_back(10);
|
||||
}
|
||||
REQUIRE(q.size() == q_size);
|
||||
@ -19,13 +17,11 @@ TEST_CASE("test_size", "[circular_q]")
|
||||
REQUIRE(q.size() == q_size);
|
||||
}
|
||||
|
||||
TEST_CASE("test_rolling", "[circular_q]")
|
||||
{
|
||||
TEST_CASE("test_rolling", "[circular_q]") {
|
||||
const size_t q_size = 4;
|
||||
q_type q(q_size);
|
||||
|
||||
for (size_t i = 0; i < q_size + 2; i++)
|
||||
{
|
||||
for (size_t i = 0; i < q_size + 2; i++) {
|
||||
auto val = i;
|
||||
q.push_back(std::move(val)); // arg to push_back must be r value
|
||||
}
|
||||
@ -50,8 +46,7 @@ TEST_CASE("test_rolling", "[circular_q]")
|
||||
REQUIRE(q.front() == 6);
|
||||
}
|
||||
|
||||
TEST_CASE("test_empty", "[circular_q]")
|
||||
{
|
||||
TEST_CASE("test_empty", "[circular_q]") {
|
||||
q_type q1(0);
|
||||
REQUIRE(q1.empty());
|
||||
REQUIRE(q1.full()); // q with capacity 0 is considered full
|
||||
@ -66,8 +61,7 @@ TEST_CASE("test_empty", "[circular_q]")
|
||||
REQUIRE(q2.empty());
|
||||
}
|
||||
|
||||
TEST_CASE("test_full", "[circular_q]")
|
||||
{
|
||||
TEST_CASE("test_full", "[circular_q]") {
|
||||
q_type q1(0);
|
||||
REQUIRE(q1.full());
|
||||
|
||||
@ -81,8 +75,7 @@ TEST_CASE("test_full", "[circular_q]")
|
||||
REQUIRE(q2.full());
|
||||
}
|
||||
|
||||
TEST_CASE("test_operator[]", "[circular_q]")
|
||||
{
|
||||
TEST_CASE("test_operator[]", "[circular_q]") {
|
||||
q_type q(2);
|
||||
q.push_back(100);
|
||||
q.push_back(200);
|
||||
@ -90,8 +83,7 @@ TEST_CASE("test_operator[]", "[circular_q]")
|
||||
REQUIRE(q[1] == 200);
|
||||
}
|
||||
|
||||
TEST_CASE("test_operator=", "[circular_q]")
|
||||
{
|
||||
TEST_CASE("test_operator=", "[circular_q]") {
|
||||
q_type q1(2);
|
||||
q1.push_back(100);
|
||||
q1.push_back(200);
|
||||
@ -101,20 +93,17 @@ TEST_CASE("test_operator=", "[circular_q]")
|
||||
REQUIRE(q2[1] == 200);
|
||||
}
|
||||
|
||||
TEST_CASE("test_front", "[circular_q]")
|
||||
{
|
||||
TEST_CASE("test_front", "[circular_q]") {
|
||||
q_type q(2);
|
||||
q.push_back(100);
|
||||
q.push_back(200);
|
||||
REQUIRE(q.front() == 100);
|
||||
}
|
||||
|
||||
TEST_CASE("test_overrun_counter", "[circular_q]")
|
||||
{
|
||||
TEST_CASE("test_overrun_counter", "[circular_q]") {
|
||||
q_type q(2);
|
||||
REQUIRE(q.overrun_counter() == 0);
|
||||
for (size_t i = 0; i < 10; i++)
|
||||
{
|
||||
for (size_t i = 0; i < 10; i++) {
|
||||
q.push_back(100);
|
||||
}
|
||||
REQUIRE(q.overrun_counter() == 8);
|
||||
@ -123,8 +112,7 @@ TEST_CASE("test_overrun_counter", "[circular_q]")
|
||||
REQUIRE(q.overrun_counter() == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("test_move", "[circular_q]")
|
||||
{
|
||||
TEST_CASE("test_move", "[circular_q]") {
|
||||
q_type q1(2);
|
||||
q1.push_back(100);
|
||||
q1.push_back(200);
|
||||
|
@ -1,40 +1,46 @@
|
||||
/*
|
||||
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
* This content is released under the MIT License as specified in
|
||||
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
*/
|
||||
#include "includes.h"
|
||||
|
||||
using spdlog::details::os::create_dir;
|
||||
using spdlog::details::os::path_exists;
|
||||
|
||||
bool try_create_dir(const spdlog::filename_t &path, const spdlog::filename_t &normalized_path)
|
||||
{
|
||||
bool try_create_dir(const spdlog::filename_t &path, const spdlog::filename_t &normalized_path) {
|
||||
auto rv = create_dir(path);
|
||||
REQUIRE(rv == true);
|
||||
return path_exists(normalized_path);
|
||||
}
|
||||
|
||||
TEST_CASE("create_dir", "[create_dir]")
|
||||
{
|
||||
TEST_CASE("create_dir", "[create_dir]") {
|
||||
prepare_logdir();
|
||||
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/dir1/dir1"), SPDLOG_FILENAME_T("test_logs/dir1/dir1")));
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/dir1/dir1"), SPDLOG_FILENAME_T("test_logs/dir1/dir1"))); // test existing
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/dir1///dir2//"), SPDLOG_FILENAME_T("test_logs/dir1/dir2")));
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("./test_logs/dir1/dir3"), SPDLOG_FILENAME_T("test_logs/dir1/dir3")));
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/../test_logs/dir1/dir4"), SPDLOG_FILENAME_T("test_logs/dir1/dir4")));
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/dir1/dir1"),
|
||||
SPDLOG_FILENAME_T("test_logs/dir1/dir1")));
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/dir1/dir1"),
|
||||
SPDLOG_FILENAME_T("test_logs/dir1/dir1"))); // test existing
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/dir1///dir2//"),
|
||||
SPDLOG_FILENAME_T("test_logs/dir1/dir2")));
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("./test_logs/dir1/dir3"),
|
||||
SPDLOG_FILENAME_T("test_logs/dir1/dir3")));
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs/../test_logs/dir1/dir4"),
|
||||
SPDLOG_FILENAME_T("test_logs/dir1/dir4")));
|
||||
|
||||
#ifdef WIN32
|
||||
// test backslash folder separator
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs\\dir1\\dir222"), SPDLOG_FILENAME_T("test_logs\\dir1\\dir222")));
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs\\dir1\\dir223\\"), SPDLOG_FILENAME_T("test_logs\\dir1\\dir223\\")));
|
||||
REQUIRE(
|
||||
try_create_dir(SPDLOG_FILENAME_T(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23"), SPDLOG_FILENAME_T("test_logs\\dir1\\dir2\\dir23")));
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs\\..\\test_logs\\dir1\\dir5"), SPDLOG_FILENAME_T("test_logs\\dir1\\dir5")));
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs\\dir1\\dir222"),
|
||||
SPDLOG_FILENAME_T("test_logs\\dir1\\dir222")));
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs\\dir1\\dir223\\"),
|
||||
SPDLOG_FILENAME_T("test_logs\\dir1\\dir223\\")));
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T(".\\test_logs\\dir1\\dir2\\dir99\\..\\dir23"),
|
||||
SPDLOG_FILENAME_T("test_logs\\dir1\\dir2\\dir23")));
|
||||
REQUIRE(try_create_dir(SPDLOG_FILENAME_T("test_logs\\..\\test_logs\\dir1\\dir5"),
|
||||
SPDLOG_FILENAME_T("test_logs\\dir1\\dir5")));
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("create_invalid_dir", "[create_dir]")
|
||||
{
|
||||
TEST_CASE("create_invalid_dir", "[create_dir]") {
|
||||
REQUIRE(create_dir(SPDLOG_FILENAME_T("")) == false);
|
||||
REQUIRE(create_dir(spdlog::filename_t{}) == false);
|
||||
#ifdef __linux__
|
||||
@ -42,8 +48,7 @@ TEST_CASE("create_invalid_dir", "[create_dir]")
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("dir_name", "[create_dir]")
|
||||
{
|
||||
TEST_CASE("dir_name", "[create_dir]") {
|
||||
using spdlog::details::os::dir_name;
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("")).empty());
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir")).empty());
|
||||
@ -55,13 +60,16 @@ TEST_CASE("dir_name", "[create_dir]")
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir/file)")) == SPDLOG_FILENAME_T("dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\file.txt)")) == SPDLOG_FILENAME_T("dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir/file)")) == SPDLOG_FILENAME_T("dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\file.txt\)")) == SPDLOG_FILENAME_T(R"(dir\file.txt)"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(dir\file.txt\)")) ==
|
||||
SPDLOG_FILENAME_T(R"(dir\file.txt)"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(\dir\file.txt)")) == SPDLOG_FILENAME_T(R"(\dir)"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(\\dir\file.txt)")) == SPDLOG_FILENAME_T(R"(\\dir)"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(..\file.txt)")) == SPDLOG_FILENAME_T(".."));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(.\file.txt)")) == SPDLOG_FILENAME_T("."));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(c:\\a\b\c\d\file.txt)")) == SPDLOG_FILENAME_T(R"(c:\\a\b\c\d)"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(c://a/b/c/d/file.txt)")) == SPDLOG_FILENAME_T(R"(c://a/b/c/d)"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(c:\\a\b\c\d\file.txt)")) ==
|
||||
SPDLOG_FILENAME_T(R"(c:\\a\b\c\d)"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T(R"(c://a/b/c/d/file.txt)")) ==
|
||||
SPDLOG_FILENAME_T(R"(c://a/b/c/d)"));
|
||||
#endif
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir/")) == SPDLOG_FILENAME_T("dir"));
|
||||
REQUIRE(dir_name(SPDLOG_FILENAME_T("dir///")) == SPDLOG_FILENAME_T("dir//"));
|
||||
|
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
* This content is released under the MIT License as specified in
|
||||
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
*/
|
||||
#include "includes.h"
|
||||
#include "test_sink.h"
|
||||
@ -7,11 +8,11 @@
|
||||
#include "spdlog/async.h"
|
||||
#include "spdlog/common.h"
|
||||
|
||||
TEST_CASE("custom_callback_logger", "[custom_callback_logger]")
|
||||
{
|
||||
TEST_CASE("custom_callback_logger", "[custom_callback_logger]") {
|
||||
std::vector<std::string> lines;
|
||||
spdlog::pattern_formatter formatter;
|
||||
auto callback_logger = std::make_shared<spdlog::sinks::callback_sink_st>([&](const spdlog::details::log_msg &msg) {
|
||||
auto callback_logger =
|
||||
std::make_shared<spdlog::sinks::callback_sink_st>([&](const spdlog::details::log_msg &msg) {
|
||||
spdlog::memory_buf_t formatted;
|
||||
formatter.format(msg, formatted);
|
||||
auto eol_len = strlen(spdlog::details::os::default_eol);
|
||||
|
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
* This content is released under the MIT License as specified in
|
||||
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
*/
|
||||
#include "includes.h"
|
||||
|
||||
@ -10,22 +11,20 @@ using filename_memory_buf_t = fmt::basic_memory_buffer<spdlog::filename_t::value
|
||||
#endif
|
||||
|
||||
#ifdef SPDLOG_WCHAR_FILENAMES
|
||||
std::string filename_buf_to_utf8string(const filename_memory_buf_t &w)
|
||||
{
|
||||
std::string filename_buf_to_utf8string(const filename_memory_buf_t &w) {
|
||||
spdlog::memory_buf_t buf;
|
||||
spdlog::details::os::wstr_to_utf8buf(spdlog::wstring_view_t(w.data(), w.size()), buf);
|
||||
return SPDLOG_BUF_TO_STRING(buf);
|
||||
}
|
||||
#else
|
||||
std::string filename_buf_to_utf8string(const filename_memory_buf_t &w)
|
||||
{
|
||||
std::string filename_buf_to_utf8string(const filename_memory_buf_t &w) {
|
||||
return SPDLOG_BUF_TO_STRING(w);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
|
||||
{
|
||||
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::daily_filename_calculator>;
|
||||
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") {
|
||||
using sink_type =
|
||||
spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::daily_filename_calculator>;
|
||||
|
||||
prepare_logdir();
|
||||
|
||||
@ -33,12 +32,11 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
|
||||
spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_dateonly");
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
filename_memory_buf_t w;
|
||||
spdlog::fmt_lib::format_to(
|
||||
std::back_inserter(w), SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
||||
spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}"),
|
||||
basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
||||
|
||||
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
|
||||
logger->info("Test message {}", i);
|
||||
}
|
||||
@ -47,20 +45,18 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
|
||||
require_message_count(filename_buf_to_utf8string(w), 10);
|
||||
}
|
||||
|
||||
struct custom_daily_file_name_calculator
|
||||
{
|
||||
static spdlog::filename_t calc_filename(const spdlog::filename_t &basename, const tm &now_tm)
|
||||
{
|
||||
struct custom_daily_file_name_calculator {
|
||||
static spdlog::filename_t calc_filename(const spdlog::filename_t &basename, const tm &now_tm) {
|
||||
filename_memory_buf_t w;
|
||||
spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename, now_tm.tm_year + 1900,
|
||||
now_tm.tm_mon + 1, now_tm.tm_mday);
|
||||
spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"),
|
||||
basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1,
|
||||
now_tm.tm_mday);
|
||||
|
||||
return SPDLOG_BUF_TO_STRING(w);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE("daily_logger with custom calculator", "[daily_logger]")
|
||||
{
|
||||
TEST_CASE("daily_logger with custom calculator", "[daily_logger]") {
|
||||
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, custom_daily_file_name_calculator>;
|
||||
|
||||
prepare_logdir();
|
||||
@ -69,12 +65,11 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]")
|
||||
spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_dateonly");
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
filename_memory_buf_t w;
|
||||
spdlog::fmt_lib::format_to(
|
||||
std::back_inserter(w), SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
||||
spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"),
|
||||
basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
||||
|
||||
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
logger->info("Test message {}", i);
|
||||
}
|
||||
|
||||
@ -87,34 +82,33 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]")
|
||||
* File name calculations
|
||||
*/
|
||||
|
||||
TEST_CASE("rotating_file_sink::calc_filename1", "[rotating_file_sink]")
|
||||
{
|
||||
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename(SPDLOG_FILENAME_T("rotated.txt"), 3);
|
||||
TEST_CASE("rotating_file_sink::calc_filename1", "[rotating_file_sink]") {
|
||||
auto filename =
|
||||
spdlog::sinks::rotating_file_sink_st::calc_filename(SPDLOG_FILENAME_T("rotated.txt"), 3);
|
||||
REQUIRE(filename == SPDLOG_FILENAME_T("rotated.3.txt"));
|
||||
}
|
||||
|
||||
TEST_CASE("rotating_file_sink::calc_filename2", "[rotating_file_sink]")
|
||||
{
|
||||
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename(SPDLOG_FILENAME_T("rotated"), 3);
|
||||
TEST_CASE("rotating_file_sink::calc_filename2", "[rotating_file_sink]") {
|
||||
auto filename =
|
||||
spdlog::sinks::rotating_file_sink_st::calc_filename(SPDLOG_FILENAME_T("rotated"), 3);
|
||||
REQUIRE(filename == SPDLOG_FILENAME_T("rotated.3"));
|
||||
}
|
||||
|
||||
TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]")
|
||||
{
|
||||
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename(SPDLOG_FILENAME_T("rotated.txt"), 0);
|
||||
TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]") {
|
||||
auto filename =
|
||||
spdlog::sinks::rotating_file_sink_st::calc_filename(SPDLOG_FILENAME_T("rotated.txt"), 0);
|
||||
REQUIRE(filename == SPDLOG_FILENAME_T("rotated.txt"));
|
||||
}
|
||||
|
||||
// regex supported only from gcc 4.9 and above
|
||||
#if defined(_MSC_VER) || !(__GNUC__ <= 4 && __GNUC_MINOR__ < 9)
|
||||
|
||||
# include <regex>
|
||||
#include <regex>
|
||||
|
||||
TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]")
|
||||
{
|
||||
TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]") {
|
||||
// daily_YYYY-MM-DD_hh-mm.txt
|
||||
auto filename =
|
||||
spdlog::sinks::daily_filename_calculator::calc_filename(SPDLOG_FILENAME_T("daily.txt"), spdlog::details::os::localtime());
|
||||
auto filename = spdlog::sinks::daily_filename_calculator::calc_filename(
|
||||
SPDLOG_FILENAME_T("daily.txt"), spdlog::details::os::localtime());
|
||||
// date regex based on https://www.regular-expressions.info/dates.html
|
||||
std::basic_regex<spdlog::filename_t::value_type> re(
|
||||
SPDLOG_FILENAME_T(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)"));
|
||||
@ -123,27 +117,26 @@ TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]")
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE("daily_file_sink::daily_filename_format_calculator", "[daily_file_sink]")
|
||||
{
|
||||
TEST_CASE("daily_file_sink::daily_filename_format_calculator", "[daily_file_sink]") {
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
// example-YYYY-MM-DD.log
|
||||
auto filename = spdlog::sinks::daily_filename_format_calculator::calc_filename(SPDLOG_FILENAME_T("example-%Y-%m-%d.log"), tm);
|
||||
auto filename = spdlog::sinks::daily_filename_format_calculator::calc_filename(
|
||||
SPDLOG_FILENAME_T("example-%Y-%m-%d.log"), tm);
|
||||
|
||||
REQUIRE(filename ==
|
||||
spdlog::fmt_lib::format(SPDLOG_FILENAME_T("example-{:04d}-{:02d}-{:02d}.log"), tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday));
|
||||
spdlog::fmt_lib::format(SPDLOG_FILENAME_T("example-{:04d}-{:02d}-{:02d}.log"),
|
||||
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday));
|
||||
}
|
||||
|
||||
/* Test removal of old files */
|
||||
static spdlog::details::log_msg create_msg(std::chrono::seconds offset)
|
||||
{
|
||||
static spdlog::details::log_msg create_msg(std::chrono::seconds offset) {
|
||||
using spdlog::log_clock;
|
||||
spdlog::details::log_msg msg{"test", spdlog::level::info, "Hello Message"};
|
||||
msg.time = log_clock::now() + offset;
|
||||
return msg;
|
||||
}
|
||||
|
||||
static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_files)
|
||||
{
|
||||
static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_files) {
|
||||
using spdlog::log_clock;
|
||||
using spdlog::details::log_msg;
|
||||
using spdlog::sinks::daily_file_sink_st;
|
||||
@ -155,8 +148,7 @@ static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_
|
||||
|
||||
// simulate messages with 24 intervals
|
||||
|
||||
for (int i = 0; i < days_to_run; i++)
|
||||
{
|
||||
for (int i = 0; i < days_to_run; i++) {
|
||||
auto offset = std::chrono::seconds{24 * 3600 * i};
|
||||
sink.log(create_msg(offset));
|
||||
}
|
||||
@ -164,8 +156,7 @@ static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_
|
||||
REQUIRE(count_files("test_logs") == static_cast<size_t>(expected_n_files));
|
||||
}
|
||||
|
||||
TEST_CASE("daily_logger rotate", "[daily_file_sink]")
|
||||
{
|
||||
TEST_CASE("daily_logger rotate", "[daily_file_sink]") {
|
||||
int days_to_run = 1;
|
||||
test_rotate(days_to_run, 0, 1);
|
||||
test_rotate(days_to_run, 1, 1);
|
||||
|
@ -2,8 +2,7 @@
|
||||
#include "spdlog/sinks/dup_filter_sink.h"
|
||||
#include "test_sink.h"
|
||||
|
||||
TEST_CASE("dup_filter_test1", "[dup_filter_sink]")
|
||||
{
|
||||
TEST_CASE("dup_filter_test1", "[dup_filter_sink]") {
|
||||
using spdlog::sinks::dup_filter_sink_st;
|
||||
using spdlog::sinks::test_sink_mt;
|
||||
|
||||
@ -11,16 +10,14 @@ TEST_CASE("dup_filter_test1", "[dup_filter_sink]")
|
||||
auto test_sink = std::make_shared<test_sink_mt>();
|
||||
dup_sink.add_sink(test_sink);
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
for (int i = 0; i < 10; i++) {
|
||||
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::info, "message1"});
|
||||
}
|
||||
|
||||
REQUIRE(test_sink->msg_counter() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("dup_filter_test2", "[dup_filter_sink]")
|
||||
{
|
||||
TEST_CASE("dup_filter_test2", "[dup_filter_sink]") {
|
||||
using spdlog::sinks::dup_filter_sink_st;
|
||||
using spdlog::sinks::test_sink_mt;
|
||||
|
||||
@ -28,8 +25,7 @@ TEST_CASE("dup_filter_test2", "[dup_filter_sink]")
|
||||
auto test_sink = std::make_shared<test_sink_mt>();
|
||||
dup_sink.add_sink(test_sink);
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
for (int i = 0; i < 10; i++) {
|
||||
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::info, "message1"});
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
}
|
||||
@ -37,8 +33,7 @@ TEST_CASE("dup_filter_test2", "[dup_filter_sink]")
|
||||
REQUIRE(test_sink->msg_counter() == 10);
|
||||
}
|
||||
|
||||
TEST_CASE("dup_filter_test3", "[dup_filter_sink]")
|
||||
{
|
||||
TEST_CASE("dup_filter_test3", "[dup_filter_sink]") {
|
||||
using spdlog::sinks::dup_filter_sink_st;
|
||||
using spdlog::sinks::test_sink_mt;
|
||||
|
||||
@ -46,8 +41,7 @@ TEST_CASE("dup_filter_test3", "[dup_filter_sink]")
|
||||
auto test_sink = std::make_shared<test_sink_mt>();
|
||||
dup_sink.add_sink(test_sink);
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
for (int i = 0; i < 10; i++) {
|
||||
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::info, "message1"});
|
||||
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::info, "message2"});
|
||||
}
|
||||
@ -55,8 +49,7 @@ TEST_CASE("dup_filter_test3", "[dup_filter_sink]")
|
||||
REQUIRE(test_sink->msg_counter() == 20);
|
||||
}
|
||||
|
||||
TEST_CASE("dup_filter_test4", "[dup_filter_sink]")
|
||||
{
|
||||
TEST_CASE("dup_filter_test4", "[dup_filter_sink]") {
|
||||
using spdlog::sinks::dup_filter_sink_mt;
|
||||
using spdlog::sinks::test_sink_mt;
|
||||
|
||||
@ -70,8 +63,7 @@ TEST_CASE("dup_filter_test4", "[dup_filter_sink]")
|
||||
REQUIRE(test_sink->msg_counter() == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("dup_filter_test5", "[dup_filter_sink]")
|
||||
{
|
||||
TEST_CASE("dup_filter_test5", "[dup_filter_sink]") {
|
||||
using spdlog::sinks::dup_filter_sink_mt;
|
||||
using spdlog::sinks::test_sink_mt;
|
||||
|
||||
@ -85,6 +77,7 @@ TEST_CASE("dup_filter_test5", "[dup_filter_sink]")
|
||||
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::info, "message1"});
|
||||
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::info, "message2"});
|
||||
|
||||
REQUIRE(test_sink->msg_counter() == 3); // skip 2 messages but log the "skipped.." message before message2
|
||||
REQUIRE(test_sink->msg_counter() ==
|
||||
3); // skip 2 messages but log the "skipped.." message before message2
|
||||
REQUIRE(test_sink->lines()[1] == "Skipped 2 duplicate messages..");
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
* This content is released under the MIT License as specified in
|
||||
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
*/
|
||||
#include "includes.h"
|
||||
|
||||
@ -8,25 +9,18 @@
|
||||
#define SIMPLE_LOG "test_logs/simple_log.txt"
|
||||
#define SIMPLE_ASYNC_LOG "test_logs/simple_async_log.txt"
|
||||
|
||||
class failing_sink : public spdlog::sinks::base_sink<std::mutex>
|
||||
{
|
||||
class failing_sink : public spdlog::sinks::base_sink<std::mutex> {
|
||||
protected:
|
||||
void sink_it_(const spdlog::details::log_msg &) final
|
||||
{
|
||||
void sink_it_(const spdlog::details::log_msg &) final {
|
||||
throw std::runtime_error("some error happened during log");
|
||||
}
|
||||
|
||||
void flush_() final
|
||||
{
|
||||
throw std::runtime_error("some error happened during flush");
|
||||
}
|
||||
void flush_() final { throw std::runtime_error("some error happened during flush"); }
|
||||
};
|
||||
struct custom_ex
|
||||
{};
|
||||
struct custom_ex {};
|
||||
|
||||
#if !defined(SPDLOG_USE_STD_FORMAT) // std formt doesn't fully support runtime format strings
|
||||
TEST_CASE("default_error_handler", "[errors]")
|
||||
{
|
||||
TEST_CASE("default_error_handler", "[errors]") {
|
||||
prepare_logdir();
|
||||
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
|
||||
|
||||
@ -40,8 +34,7 @@ TEST_CASE("default_error_handler", "[errors]")
|
||||
REQUIRE(count_lines(SIMPLE_LOG) == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("custom_error_handler", "[errors]")
|
||||
{
|
||||
TEST_CASE("custom_error_handler", "[errors]") {
|
||||
prepare_logdir();
|
||||
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
|
||||
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
|
||||
@ -55,16 +48,14 @@ TEST_CASE("custom_error_handler", "[errors]")
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE("default_error_handler2", "[errors]")
|
||||
{
|
||||
TEST_CASE("default_error_handler2", "[errors]") {
|
||||
spdlog::drop_all();
|
||||
auto logger = spdlog::create<failing_sink>("failed_logger");
|
||||
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
||||
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
|
||||
}
|
||||
|
||||
TEST_CASE("flush_error_handler", "[errors]")
|
||||
{
|
||||
TEST_CASE("flush_error_handler", "[errors]") {
|
||||
spdlog::drop_all();
|
||||
auto logger = spdlog::create<failing_sink>("failed_logger");
|
||||
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
||||
@ -72,19 +63,18 @@ TEST_CASE("flush_error_handler", "[errors]")
|
||||
}
|
||||
|
||||
#if !defined(SPDLOG_USE_STD_FORMAT) // std formt doesn't fully support runtime format strings
|
||||
TEST_CASE("async_error_handler", "[errors]")
|
||||
{
|
||||
TEST_CASE("async_error_handler", "[errors]") {
|
||||
prepare_logdir();
|
||||
std::string err_msg("log failed with some msg");
|
||||
|
||||
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_ASYNC_LOG);
|
||||
{
|
||||
spdlog::init_thread_pool(128, 1);
|
||||
auto logger = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
|
||||
auto logger =
|
||||
spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
|
||||
logger->set_error_handler([=](const std::string &) {
|
||||
std::ofstream ofs("test_logs/custom_err.txt");
|
||||
if (!ofs)
|
||||
{
|
||||
if (!ofs) {
|
||||
throw std::runtime_error("Failed open test_logs/custom_err.txt");
|
||||
}
|
||||
ofs << err_msg;
|
||||
@ -101,8 +91,7 @@ TEST_CASE("async_error_handler", "[errors]")
|
||||
#endif
|
||||
|
||||
// Make sure async error handler is executed
|
||||
TEST_CASE("async_error_handler2", "[errors]")
|
||||
{
|
||||
TEST_CASE("async_error_handler2", "[errors]") {
|
||||
prepare_logdir();
|
||||
std::string err_msg("This is async handler error message");
|
||||
{
|
||||
|
@ -1,26 +1,25 @@
|
||||
#if _WIN32
|
||||
|
||||
# include "includes.h"
|
||||
# include "test_sink.h"
|
||||
#include "includes.h"
|
||||
#include "test_sink.h"
|
||||
|
||||
# include "spdlog/sinks/win_eventlog_sink.h"
|
||||
#include "spdlog/sinks/win_eventlog_sink.h"
|
||||
|
||||
static const LPCSTR TEST_SOURCE = "spdlog_test";
|
||||
|
||||
static void test_single_print(std::function<void(std::string const &)> do_log, std::string const &expected_contents, WORD expected_ev_type)
|
||||
{
|
||||
static void test_single_print(std::function<void(std::string const &)> do_log,
|
||||
std::string const &expected_contents,
|
||||
WORD expected_ev_type) {
|
||||
using namespace std::chrono;
|
||||
do_log(expected_contents);
|
||||
const auto expected_time_generated = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
|
||||
const auto expected_time_generated =
|
||||
duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
|
||||
|
||||
struct handle_t
|
||||
{
|
||||
struct handle_t {
|
||||
HANDLE handle_;
|
||||
|
||||
~handle_t()
|
||||
{
|
||||
if (handle_)
|
||||
{
|
||||
~handle_t() {
|
||||
if (handle_) {
|
||||
REQUIRE(CloseEventLog(handle_));
|
||||
}
|
||||
}
|
||||
@ -29,16 +28,16 @@ static void test_single_print(std::function<void(std::string const &)> do_log, s
|
||||
REQUIRE(event_log.handle_);
|
||||
|
||||
DWORD read_bytes{}, size_needed{};
|
||||
auto ok = ::ReadEventLogA(
|
||||
event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, &read_bytes, 0, &read_bytes, &size_needed);
|
||||
auto ok = ::ReadEventLogA(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ,
|
||||
0, &read_bytes, 0, &read_bytes, &size_needed);
|
||||
REQUIRE(!ok);
|
||||
REQUIRE(::GetLastError() == ERROR_INSUFFICIENT_BUFFER);
|
||||
|
||||
std::vector<char> record_buffer(size_needed);
|
||||
PEVENTLOGRECORD record = (PEVENTLOGRECORD)record_buffer.data();
|
||||
|
||||
ok = ::ReadEventLogA(
|
||||
event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, record, size_needed, &read_bytes, &size_needed);
|
||||
ok = ::ReadEventLogA(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0,
|
||||
record, size_needed, &read_bytes, &size_needed);
|
||||
REQUIRE(ok);
|
||||
|
||||
REQUIRE(record->NumStrings == 1);
|
||||
@ -49,8 +48,7 @@ static void test_single_print(std::function<void(std::string const &)> do_log, s
|
||||
REQUIRE(message_in_log == expected_contents + spdlog::details::os::default_eol);
|
||||
}
|
||||
|
||||
TEST_CASE("eventlog", "[eventlog]")
|
||||
{
|
||||
TEST_CASE("eventlog", "[eventlog]") {
|
||||
using namespace spdlog;
|
||||
|
||||
auto test_sink = std::make_shared<sinks::win_eventlog_sink_mt>(TEST_SOURCE);
|
||||
@ -60,12 +58,18 @@ TEST_CASE("eventlog", "[eventlog]")
|
||||
|
||||
test_sink->set_pattern("%v");
|
||||
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.trace(msg); }, "my trace message", EVENTLOG_SUCCESS);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.debug(msg); }, "my debug message", EVENTLOG_SUCCESS);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.info(msg); }, "my info message", EVENTLOG_INFORMATION_TYPE);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.warn(msg); }, "my warn message", EVENTLOG_WARNING_TYPE);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.error(msg); }, "my error message", EVENTLOG_ERROR_TYPE);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.critical(msg); }, "my critical message", EVENTLOG_ERROR_TYPE);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.trace(msg); },
|
||||
"my trace message", EVENTLOG_SUCCESS);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.debug(msg); },
|
||||
"my debug message", EVENTLOG_SUCCESS);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.info(msg); },
|
||||
"my info message", EVENTLOG_INFORMATION_TYPE);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.warn(msg); },
|
||||
"my warn message", EVENTLOG_WARNING_TYPE);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.error(msg); },
|
||||
"my error message", EVENTLOG_ERROR_TYPE);
|
||||
test_single_print([&test_logger](std::string const &msg) { test_logger.critical(msg); },
|
||||
"my critical message", EVENTLOG_ERROR_TYPE);
|
||||
}
|
||||
|
||||
#endif //_WIN32
|
||||
|
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
* This content is released under the MIT License as specified in
|
||||
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
*/
|
||||
#include "includes.h"
|
||||
|
||||
@ -7,16 +8,14 @@
|
||||
|
||||
using spdlog::details::file_helper;
|
||||
|
||||
static void write_with_helper(file_helper &helper, size_t howmany)
|
||||
{
|
||||
static void write_with_helper(file_helper &helper, size_t howmany) {
|
||||
spdlog::memory_buf_t formatted;
|
||||
spdlog::fmt_lib::format_to(std::back_inserter(formatted), "{}", std::string(howmany, '1'));
|
||||
helper.write(formatted);
|
||||
helper.flush();
|
||||
}
|
||||
|
||||
TEST_CASE("file_helper_filename", "[file_helper::filename()]")
|
||||
{
|
||||
TEST_CASE("file_helper_filename", "[file_helper::filename()]") {
|
||||
prepare_logdir();
|
||||
|
||||
file_helper helper;
|
||||
@ -25,8 +24,7 @@ TEST_CASE("file_helper_filename", "[file_helper::filename()]")
|
||||
REQUIRE(helper.filename() == target_filename);
|
||||
}
|
||||
|
||||
TEST_CASE("file_helper_size", "[file_helper::size()]")
|
||||
{
|
||||
TEST_CASE("file_helper_size", "[file_helper::size()]") {
|
||||
prepare_logdir();
|
||||
spdlog::filename_t target_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
|
||||
size_t expected_size = 123;
|
||||
@ -39,8 +37,7 @@ TEST_CASE("file_helper_size", "[file_helper::size()]")
|
||||
REQUIRE(get_filesize(TEST_FILENAME) == expected_size);
|
||||
}
|
||||
|
||||
TEST_CASE("file_helper_reopen", "[file_helper::reopen()]")
|
||||
{
|
||||
TEST_CASE("file_helper_reopen", "[file_helper::reopen()]") {
|
||||
prepare_logdir();
|
||||
spdlog::filename_t target_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
|
||||
file_helper helper;
|
||||
@ -51,8 +48,7 @@ TEST_CASE("file_helper_reopen", "[file_helper::reopen()]")
|
||||
REQUIRE(helper.size() == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]")
|
||||
{
|
||||
TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]") {
|
||||
prepare_logdir();
|
||||
spdlog::filename_t target_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
|
||||
size_t expected_size = 14;
|
||||
@ -64,9 +60,9 @@ TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]")
|
||||
REQUIRE(helper.size() == expected_size);
|
||||
}
|
||||
|
||||
static void test_split_ext(const spdlog::filename_t::value_type *fname, const spdlog::filename_t::value_type *expect_base,
|
||||
const spdlog::filename_t::value_type *expect_ext)
|
||||
{
|
||||
static void test_split_ext(const spdlog::filename_t::value_type *fname,
|
||||
const spdlog::filename_t::value_type *expect_base,
|
||||
const spdlog::filename_t::value_type *expect_ext) {
|
||||
spdlog::filename_t filename(fname);
|
||||
spdlog::filename_t expected_base(expect_base);
|
||||
spdlog::filename_t expected_ext(expect_ext);
|
||||
@ -78,37 +74,43 @@ static void test_split_ext(const spdlog::filename_t::value_type *fname, const sp
|
||||
REQUIRE(ext == expected_ext);
|
||||
}
|
||||
|
||||
TEST_CASE("file_helper_split_by_extension", "[file_helper::split_by_extension()]")
|
||||
{
|
||||
test_split_ext(SPDLOG_FILENAME_T("mylog.txt"), SPDLOG_FILENAME_T("mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T(".mylog.txt"), SPDLOG_FILENAME_T(".mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
TEST_CASE("file_helper_split_by_extension", "[file_helper::split_by_extension()]") {
|
||||
test_split_ext(SPDLOG_FILENAME_T("mylog.txt"), SPDLOG_FILENAME_T("mylog"),
|
||||
SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T(".mylog.txt"), SPDLOG_FILENAME_T(".mylog"),
|
||||
SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T(".mylog"), SPDLOG_FILENAME_T(".mylog"), SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("/aaa/bb.d/mylog"), SPDLOG_FILENAME_T("/aaa/bb.d/mylog"), SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("/aaa/bb.d/mylog.txt"), SPDLOG_FILENAME_T("/aaa/bb.d/mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog.txt"), SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog."), SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog."), SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/.mylog.txt"), SPDLOG_FILENAME_T("aaa/bbb/ccc/.mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("/aaa/bbb/ccc/mylog.txt"), SPDLOG_FILENAME_T("/aaa/bbb/ccc/mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("/aaa/bbb/ccc/.mylog"), SPDLOG_FILENAME_T("/aaa/bbb/ccc/.mylog"), SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("../mylog.txt"), SPDLOG_FILENAME_T("../mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T(".././mylog.txt"), SPDLOG_FILENAME_T(".././mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T(".././mylog.txt/xxx"), SPDLOG_FILENAME_T(".././mylog.txt/xxx"), SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("/mylog.txt"), SPDLOG_FILENAME_T("/mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("//mylog.txt"), SPDLOG_FILENAME_T("//mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("/aaa/bb.d/mylog"), SPDLOG_FILENAME_T("/aaa/bb.d/mylog"),
|
||||
SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("/aaa/bb.d/mylog.txt"), SPDLOG_FILENAME_T("/aaa/bb.d/mylog"),
|
||||
SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog.txt"),
|
||||
SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog."), SPDLOG_FILENAME_T("aaa/bbb/ccc/mylog."),
|
||||
SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("aaa/bbb/ccc/.mylog.txt"),
|
||||
SPDLOG_FILENAME_T("aaa/bbb/ccc/.mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("/aaa/bbb/ccc/mylog.txt"),
|
||||
SPDLOG_FILENAME_T("/aaa/bbb/ccc/mylog"), SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("/aaa/bbb/ccc/.mylog"),
|
||||
SPDLOG_FILENAME_T("/aaa/bbb/ccc/.mylog"), SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("../mylog.txt"), SPDLOG_FILENAME_T("../mylog"),
|
||||
SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T(".././mylog.txt"), SPDLOG_FILENAME_T(".././mylog"),
|
||||
SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T(".././mylog.txt/xxx"), SPDLOG_FILENAME_T(".././mylog.txt/xxx"),
|
||||
SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("/mylog.txt"), SPDLOG_FILENAME_T("/mylog"),
|
||||
SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T("//mylog.txt"), SPDLOG_FILENAME_T("//mylog"),
|
||||
SPDLOG_FILENAME_T(".txt"));
|
||||
test_split_ext(SPDLOG_FILENAME_T(""), SPDLOG_FILENAME_T(""), SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("."), SPDLOG_FILENAME_T("."), SPDLOG_FILENAME_T(""));
|
||||
test_split_ext(SPDLOG_FILENAME_T("..txt"), SPDLOG_FILENAME_T("."), SPDLOG_FILENAME_T(".txt"));
|
||||
}
|
||||
|
||||
TEST_CASE("file_event_handlers", "[file_helper]")
|
||||
{
|
||||
enum class flags
|
||||
{
|
||||
before_open,
|
||||
after_open,
|
||||
before_close,
|
||||
after_close
|
||||
};
|
||||
TEST_CASE("file_event_handlers", "[file_helper]") {
|
||||
enum class flags { before_open, after_open, before_close, after_close };
|
||||
prepare_logdir();
|
||||
|
||||
spdlog::filename_t test_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
|
||||
@ -155,8 +157,7 @@ TEST_CASE("file_event_handlers", "[file_helper]")
|
||||
REQUIRE(file_contents(TEST_FILENAME) == "after_open\nbefore_close\n");
|
||||
}
|
||||
|
||||
TEST_CASE("file_helper_open", "[file_helper]")
|
||||
{
|
||||
TEST_CASE("file_helper_open", "[file_helper]") {
|
||||
prepare_logdir();
|
||||
spdlog::filename_t target_filename = SPDLOG_FILENAME_T(TEST_FILENAME);
|
||||
file_helper helper;
|
||||
|
@ -1,13 +1,13 @@
|
||||
/*
|
||||
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
* This content is released under the MIT License as specified in
|
||||
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
*/
|
||||
#include "includes.h"
|
||||
|
||||
#define SIMPLE_LOG "test_logs/simple_log"
|
||||
#define ROTATING_LOG "test_logs/rotating_log"
|
||||
|
||||
TEST_CASE("simple_file_logger", "[simple_logger]")
|
||||
{
|
||||
TEST_CASE("simple_file_logger", "[simple_logger]") {
|
||||
prepare_logdir();
|
||||
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
|
||||
|
||||
@ -20,11 +20,11 @@ TEST_CASE("simple_file_logger", "[simple_logger]")
|
||||
logger->flush();
|
||||
require_message_count(SIMPLE_LOG, 2);
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(file_contents(SIMPLE_LOG) == spdlog::fmt_lib::format("Test message 1{}Test message 2{}", default_eol, default_eol));
|
||||
REQUIRE(file_contents(SIMPLE_LOG) ==
|
||||
spdlog::fmt_lib::format("Test message 1{}Test message 2{}", default_eol, default_eol));
|
||||
}
|
||||
|
||||
TEST_CASE("flush_on", "[flush_on]")
|
||||
{
|
||||
TEST_CASE("flush_on", "[flush_on]") {
|
||||
prepare_logdir();
|
||||
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
|
||||
|
||||
@ -41,18 +41,17 @@ TEST_CASE("flush_on", "[flush_on]")
|
||||
require_message_count(SIMPLE_LOG, 3);
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(file_contents(SIMPLE_LOG) ==
|
||||
spdlog::fmt_lib::format("Should not be flushed{}Test message 1{}Test message 2{}", default_eol, default_eol, default_eol));
|
||||
spdlog::fmt_lib::format("Should not be flushed{}Test message 1{}Test message 2{}",
|
||||
default_eol, default_eol, default_eol));
|
||||
}
|
||||
|
||||
TEST_CASE("rotating_file_logger1", "[rotating_logger]")
|
||||
{
|
||||
TEST_CASE("rotating_file_logger1", "[rotating_logger]") {
|
||||
prepare_logdir();
|
||||
size_t max_size = 1024 * 10;
|
||||
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
||||
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 0);
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
logger->info("Test message {}", i);
|
||||
}
|
||||
|
||||
@ -60,8 +59,7 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]")
|
||||
require_message_count(ROTATING_LOG, 10);
|
||||
}
|
||||
|
||||
TEST_CASE("rotating_file_logger2", "[rotating_logger]")
|
||||
{
|
||||
TEST_CASE("rotating_file_logger2", "[rotating_logger]") {
|
||||
prepare_logdir();
|
||||
size_t max_size = 1024 * 10;
|
||||
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
||||
@ -69,8 +67,7 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]")
|
||||
{
|
||||
// make an initial logger to create the first output file
|
||||
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
logger->info("Test message {}", i);
|
||||
}
|
||||
// drop causes the logger destructor to be called, which is required so the
|
||||
@ -79,8 +76,7 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]")
|
||||
}
|
||||
|
||||
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
logger->info("Test message {}", i);
|
||||
}
|
||||
|
||||
@ -88,8 +84,7 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]")
|
||||
|
||||
require_message_count(ROTATING_LOG, 10);
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
|
||||
logger->info("Test message {}", i);
|
||||
}
|
||||
@ -100,10 +95,10 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]")
|
||||
}
|
||||
|
||||
// test that passing max_size=0 throws
|
||||
TEST_CASE("rotating_file_logger3", "[rotating_logger]")
|
||||
{
|
||||
TEST_CASE("rotating_file_logger3", "[rotating_logger]") {
|
||||
prepare_logdir();
|
||||
size_t max_size = 0;
|
||||
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
||||
REQUIRE_THROWS_AS(spdlog::rotating_logger_mt("logger", basename, max_size, 0), spdlog::spdlog_ex);
|
||||
REQUIRE_THROWS_AS(spdlog::rotating_logger_mt("logger", basename, max_size, 0),
|
||||
spdlog::spdlog_ex);
|
||||
}
|
||||
|
@ -4,40 +4,35 @@
|
||||
using spdlog::memory_buf_t;
|
||||
using spdlog::details::to_string_view;
|
||||
|
||||
void test_pad2(int n, const char *expected)
|
||||
{
|
||||
void test_pad2(int n, const char *expected) {
|
||||
memory_buf_t buf;
|
||||
spdlog::details::fmt_helper::pad2(n, buf);
|
||||
|
||||
REQUIRE(to_string_view(buf) == expected);
|
||||
}
|
||||
|
||||
void test_pad3(uint32_t n, const char *expected)
|
||||
{
|
||||
void test_pad3(uint32_t n, const char *expected) {
|
||||
memory_buf_t buf;
|
||||
spdlog::details::fmt_helper::pad3(n, buf);
|
||||
|
||||
REQUIRE(to_string_view(buf) == expected);
|
||||
}
|
||||
|
||||
void test_pad6(std::size_t n, const char *expected)
|
||||
{
|
||||
void test_pad6(std::size_t n, const char *expected) {
|
||||
memory_buf_t buf;
|
||||
spdlog::details::fmt_helper::pad6(n, buf);
|
||||
|
||||
REQUIRE(to_string_view(buf) == expected);
|
||||
}
|
||||
|
||||
void test_pad9(std::size_t n, const char *expected)
|
||||
{
|
||||
void test_pad9(std::size_t n, const char *expected) {
|
||||
memory_buf_t buf;
|
||||
spdlog::details::fmt_helper::pad9(n, buf);
|
||||
|
||||
REQUIRE(to_string_view(buf) == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("pad2", "[fmt_helper]")
|
||||
{
|
||||
TEST_CASE("pad2", "[fmt_helper]") {
|
||||
test_pad2(0, "00");
|
||||
test_pad2(3, "03");
|
||||
test_pad2(10, "10");
|
||||
@ -49,8 +44,7 @@ TEST_CASE("pad2", "[fmt_helper]")
|
||||
test_pad2(-5, "-5");
|
||||
}
|
||||
|
||||
TEST_CASE("pad3", "[fmt_helper]")
|
||||
{
|
||||
TEST_CASE("pad3", "[fmt_helper]") {
|
||||
test_pad3(0, "000");
|
||||
test_pad3(3, "003");
|
||||
test_pad3(10, "010");
|
||||
@ -63,8 +57,7 @@ TEST_CASE("pad3", "[fmt_helper]")
|
||||
test_pad3(1234, "1234");
|
||||
}
|
||||
|
||||
TEST_CASE("pad6", "[fmt_helper]")
|
||||
{
|
||||
TEST_CASE("pad6", "[fmt_helper]") {
|
||||
test_pad6(0, "000000");
|
||||
test_pad6(3, "000003");
|
||||
test_pad6(23, "000023");
|
||||
@ -74,8 +67,7 @@ TEST_CASE("pad6", "[fmt_helper]")
|
||||
test_pad6(123456, "123456");
|
||||
}
|
||||
|
||||
TEST_CASE("pad9", "[fmt_helper]")
|
||||
{
|
||||
TEST_CASE("pad9", "[fmt_helper]") {
|
||||
test_pad9(0, "000000000");
|
||||
test_pad9(3, "000000003");
|
||||
test_pad9(23, "000000023");
|
||||
|
@ -3,8 +3,7 @@
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
TEST_CASE("test_default_level", "[log_level]")
|
||||
{
|
||||
TEST_CASE("test_default_level", "[log_level]") {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_st>();
|
||||
REQUIRE(test_sink->log_level() == spdlog::level::trace);
|
||||
spdlog::logger logger("test-level", test_sink);
|
||||
@ -14,10 +13,8 @@ TEST_CASE("test_default_level", "[log_level]")
|
||||
|
||||
// test that logger log only messages with level bigger or equal to its level
|
||||
void test_logger_level(spdlog::level level);
|
||||
TEST_CASE("test_logger_levels", "[log_level]")
|
||||
{
|
||||
for (size_t i = 0; i < spdlog::levels_count; i++)
|
||||
{
|
||||
TEST_CASE("test_logger_levels", "[log_level]") {
|
||||
for (size_t i = 0; i < spdlog::levels_count; i++) {
|
||||
auto level = static_cast<spdlog::level>(i);
|
||||
test_logger_level(level);
|
||||
}
|
||||
@ -25,32 +22,35 @@ TEST_CASE("test_logger_levels", "[log_level]")
|
||||
|
||||
// test that logger log all messages with level bigger or equal to its level
|
||||
void test_sink_level(spdlog::level level);
|
||||
TEST_CASE("test_sink_levels", "[log_level]")
|
||||
{
|
||||
for (size_t i = 0; i < spdlog::levels_count; i++)
|
||||
{
|
||||
TEST_CASE("test_sink_levels", "[log_level]") {
|
||||
for (size_t i = 0; i < spdlog::levels_count; i++) {
|
||||
auto level = static_cast<spdlog::level>(i);
|
||||
test_sink_level(level);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// test helpers to check that logger/sink displays only messages with level bigger or equal to its level
|
||||
// test helpers to check that logger/sink displays only messages with level bigger or equal to its
|
||||
// level
|
||||
//
|
||||
auto get_expected_messages(spdlog::level level)
|
||||
{
|
||||
auto get_expected_messages(spdlog::level level) {
|
||||
// expected messages for each level
|
||||
static const std::map<spdlog::level, std::vector<std::string>> messages = {
|
||||
{spdlog::level::trace, {"trace hello", "debug hello", "info hello", "warning hello", "error hello", "critical hello"}},
|
||||
{spdlog::level::debug, {"debug hello", "info hello", "warning hello", "error hello", "critical hello"}},
|
||||
{spdlog::level::trace,
|
||||
{"trace hello", "debug hello", "info hello", "warning hello", "error hello",
|
||||
"critical hello"}},
|
||||
{spdlog::level::debug,
|
||||
{"debug hello", "info hello", "warning hello", "error hello", "critical hello"}},
|
||||
{spdlog::level::info, {"info hello", "warning hello", "error hello", "critical hello"}},
|
||||
{spdlog::level::warn, {"warning hello", "error hello", "critical hello"}}, {spdlog::level::err, {"error hello", "critical hello"}},
|
||||
{spdlog::level::critical, {"critical hello"}}, {spdlog::level::off, {}}, {spdlog::level::n_levels, {}}};
|
||||
{spdlog::level::warn, {"warning hello", "error hello", "critical hello"}},
|
||||
{spdlog::level::err, {"error hello", "critical hello"}},
|
||||
{spdlog::level::critical, {"critical hello"}},
|
||||
{spdlog::level::off, {}},
|
||||
{spdlog::level::n_levels, {}}};
|
||||
return messages.at(level);
|
||||
}
|
||||
|
||||
void test_logger_level(spdlog::level level)
|
||||
{
|
||||
void test_logger_level(spdlog::level level) {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_st>();
|
||||
spdlog::logger logger("test-level", test_sink);
|
||||
logger.set_level(level);
|
||||
@ -70,8 +70,7 @@ void test_logger_level(spdlog::level level)
|
||||
}
|
||||
|
||||
// test that sink displays all messages with level bigger or equal to its level
|
||||
void test_sink_level(spdlog::level level)
|
||||
{
|
||||
void test_sink_level(spdlog::level level) {
|
||||
auto test_sink = std::make_shared<spdlog::sinks::test_sink_st>();
|
||||
spdlog::logger logger("test-level", test_sink);
|
||||
logger.set_level(spdlog::level::trace);
|
||||
|
@ -1,17 +1,17 @@
|
||||
/*
|
||||
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
* This content is released under the MIT License as specified in
|
||||
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
*/
|
||||
|
||||
#include "includes.h"
|
||||
|
||||
#if SPDLOG_ACTIVE_LEVEL != SPDLOG_LEVEL_DEBUG
|
||||
# error "Invalid SPDLOG_ACTIVE_LEVEL in test. Should be SPDLOG_LEVEL_DEBUG"
|
||||
#error "Invalid SPDLOG_ACTIVE_LEVEL in test. Should be SPDLOG_LEVEL_DEBUG"
|
||||
#endif
|
||||
|
||||
#define TEST_FILENAME "test_logs/simple_log"
|
||||
|
||||
TEST_CASE("debug and trace w/o format string", "[macros]")
|
||||
{
|
||||
TEST_CASE("debug and trace w/o format string", "[macros]") {
|
||||
|
||||
prepare_logdir();
|
||||
spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME);
|
||||
@ -25,7 +25,8 @@ TEST_CASE("debug and trace w/o format string", "[macros]")
|
||||
logger->flush();
|
||||
|
||||
using spdlog::details::os::default_eol;
|
||||
REQUIRE(ends_with(file_contents(TEST_FILENAME), spdlog::fmt_lib::format("Test message 2{}", default_eol)));
|
||||
REQUIRE(ends_with(file_contents(TEST_FILENAME),
|
||||
spdlog::fmt_lib::format("Test message 2{}", default_eol)));
|
||||
REQUIRE(count_lines(TEST_FILENAME) == 1);
|
||||
|
||||
auto orig_default_logger = spdlog::default_logger();
|
||||
@ -36,17 +37,16 @@ TEST_CASE("debug and trace w/o format string", "[macros]")
|
||||
logger->flush();
|
||||
|
||||
require_message_count(TEST_FILENAME, 2);
|
||||
REQUIRE(ends_with(file_contents(TEST_FILENAME), spdlog::fmt_lib::format("Test message 4{}", default_eol)));
|
||||
REQUIRE(ends_with(file_contents(TEST_FILENAME),
|
||||
spdlog::fmt_lib::format("Test message 4{}", default_eol)));
|
||||
spdlog::set_default_logger(std::move(orig_default_logger));
|
||||
}
|
||||
|
||||
TEST_CASE("disable param evaluation", "[macros]")
|
||||
{
|
||||
TEST_CASE("disable param evaluation", "[macros]") {
|
||||
SPDLOG_TRACE("Test message {}", throw std::runtime_error("Should not be evaluated"));
|
||||
}
|
||||
|
||||
TEST_CASE("pass logger pointer", "[macros]")
|
||||
{
|
||||
TEST_CASE("pass logger pointer", "[macros]") {
|
||||
auto logger = spdlog::create<spdlog::sinks::null_sink_mt>("refmacro");
|
||||
auto &ref = *logger;
|
||||
SPDLOG_LOGGER_TRACE(&ref, "Test message 1");
|
||||
|
@ -1,9 +1,8 @@
|
||||
#include "includes.h"
|
||||
#include "test_sink.h"
|
||||
|
||||
template<class T>
|
||||
std::string log_info(const T &what, spdlog::level logger_level = spdlog::level::info)
|
||||
{
|
||||
template <class T>
|
||||
std::string log_info(const T &what, spdlog::level logger_level = spdlog::level::info) {
|
||||
|
||||
std::ostringstream oss;
|
||||
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
@ -16,8 +15,7 @@ std::string log_info(const T &what, spdlog::level logger_level = spdlog::level::
|
||||
return oss.str().substr(0, oss.str().length() - strlen(spdlog::details::os::default_eol));
|
||||
}
|
||||
|
||||
TEST_CASE("basic_logging ", "[basic_logging]")
|
||||
{
|
||||
TEST_CASE("basic_logging ", "[basic_logging]") {
|
||||
// const char
|
||||
REQUIRE(log_info("Hello") == "Hello");
|
||||
REQUIRE(log_info("").empty());
|
||||
@ -27,8 +25,7 @@ TEST_CASE("basic_logging ", "[basic_logging]")
|
||||
REQUIRE(log_info(std::string()).empty());
|
||||
}
|
||||
|
||||
TEST_CASE("log_levels", "[log_levels]")
|
||||
{
|
||||
TEST_CASE("log_levels", "[log_levels]") {
|
||||
REQUIRE(log_info("Hello", spdlog::level::err).empty());
|
||||
REQUIRE(log_info("Hello", spdlog::level::critical).empty());
|
||||
REQUIRE(log_info("Hello", spdlog::level::info) == "Hello");
|
||||
@ -36,8 +33,7 @@ TEST_CASE("log_levels", "[log_levels]")
|
||||
REQUIRE(log_info("Hello", spdlog::level::trace) == "Hello");
|
||||
}
|
||||
|
||||
TEST_CASE("level_to_string_view", "[convert_to_string_view]")
|
||||
{
|
||||
TEST_CASE("level_to_string_view", "[convert_to_string_view]") {
|
||||
REQUIRE(spdlog::to_string_view(spdlog::level::trace) == "trace");
|
||||
REQUIRE(spdlog::to_string_view(spdlog::level::debug) == "debug");
|
||||
REQUIRE(spdlog::to_string_view(spdlog::level::info) == "info");
|
||||
@ -47,8 +43,7 @@ TEST_CASE("level_to_string_view", "[convert_to_string_view]")
|
||||
REQUIRE(spdlog::to_string_view(spdlog::level::off) == "off");
|
||||
}
|
||||
|
||||
TEST_CASE("to_short_c_str", "[convert_to_short_c_str]")
|
||||
{
|
||||
TEST_CASE("to_short_c_str", "[convert_to_short_c_str]") {
|
||||
REQUIRE(std::string(spdlog::to_short_c_str(spdlog::level::trace)) == "T");
|
||||
REQUIRE(std::string(spdlog::to_short_c_str(spdlog::level::debug)) == "D");
|
||||
REQUIRE(std::string(spdlog::to_short_c_str(spdlog::level::info)) == "I");
|
||||
@ -58,8 +53,7 @@ TEST_CASE("to_short_c_str", "[convert_to_short_c_str]")
|
||||
REQUIRE(std::string(spdlog::to_short_c_str(spdlog::level::off)) == "O");
|
||||
}
|
||||
|
||||
TEST_CASE("to_level_enum", "[convert_to_level_enum]")
|
||||
{
|
||||
TEST_CASE("to_level_enum", "[convert_to_level_enum]") {
|
||||
REQUIRE(spdlog::level_from_str("trace") == spdlog::level::trace);
|
||||
REQUIRE(spdlog::level_from_str("debug") == spdlog::level::debug);
|
||||
REQUIRE(spdlog::level_from_str("info") == spdlog::level::info);
|
||||
@ -71,8 +65,7 @@ TEST_CASE("to_level_enum", "[convert_to_level_enum]")
|
||||
REQUIRE(spdlog::level_from_str("null") == spdlog::level::off);
|
||||
}
|
||||
|
||||
TEST_CASE("periodic flush", "[periodic_flush]")
|
||||
{
|
||||
TEST_CASE("periodic flush", "[periodic_flush]") {
|
||||
using spdlog::sinks::test_sink_mt;
|
||||
auto logger = spdlog::create<test_sink_mt>("periodic_flush");
|
||||
auto test_sink = std::static_pointer_cast<test_sink_mt>(logger->sinks()[0]);
|
||||
@ -84,8 +77,7 @@ TEST_CASE("periodic flush", "[periodic_flush]")
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("clone-logger", "[clone]")
|
||||
{
|
||||
TEST_CASE("clone-logger", "[clone]") {
|
||||
using spdlog::sinks::test_sink_mt;
|
||||
auto test_sink = std::make_shared<test_sink_mt>();
|
||||
auto logger = std::make_shared<spdlog::logger>("orig", test_sink);
|
||||
@ -106,8 +98,7 @@ TEST_CASE("clone-logger", "[clone]")
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("clone async", "[clone]")
|
||||
{
|
||||
TEST_CASE("clone async", "[clone]") {
|
||||
using spdlog::sinks::test_sink_st;
|
||||
spdlog::init_thread_pool(4, 1);
|
||||
auto test_sink = std::make_shared<test_sink_st>();
|
||||
@ -132,8 +123,7 @@ TEST_CASE("clone async", "[clone]")
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("default logger API", "[default logger]")
|
||||
{
|
||||
TEST_CASE("default logger API", "[default logger]") {
|
||||
std::ostringstream oss;
|
||||
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
|
||||
|
@ -3,12 +3,10 @@
|
||||
using std::chrono::milliseconds;
|
||||
using test_clock = std::chrono::high_resolution_clock;
|
||||
|
||||
static milliseconds millis_from(const test_clock::time_point &tp0)
|
||||
{
|
||||
static milliseconds millis_from(const test_clock::time_point &tp0) {
|
||||
return std::chrono::duration_cast<milliseconds>(test_clock::now() - tp0);
|
||||
}
|
||||
TEST_CASE("dequeue-empty-nowait", "[mpmc_blocking_q]")
|
||||
{
|
||||
TEST_CASE("dequeue-empty-nowait", "[mpmc_blocking_q]") {
|
||||
size_t q_size = 100;
|
||||
milliseconds tolerance_wait(20);
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
@ -23,8 +21,7 @@ TEST_CASE("dequeue-empty-nowait", "[mpmc_blocking_q]")
|
||||
REQUIRE(delta_ms <= tolerance_wait);
|
||||
}
|
||||
|
||||
TEST_CASE("dequeue-empty-wait", "[mpmc_blocking_q]")
|
||||
{
|
||||
TEST_CASE("dequeue-empty-wait", "[mpmc_blocking_q]") {
|
||||
|
||||
size_t q_size = 100;
|
||||
milliseconds wait_ms(250);
|
||||
@ -43,8 +40,7 @@ TEST_CASE("dequeue-empty-wait", "[mpmc_blocking_q]")
|
||||
REQUIRE(delta_ms <= wait_ms + tolerance_wait);
|
||||
}
|
||||
|
||||
TEST_CASE("dequeue-full-nowait", "[mpmc_blocking_q]")
|
||||
{
|
||||
TEST_CASE("dequeue-full-nowait", "[mpmc_blocking_q]") {
|
||||
spdlog::details::mpmc_blocking_queue<int> q(1);
|
||||
q.enqueue(42);
|
||||
|
||||
@ -53,8 +49,7 @@ TEST_CASE("dequeue-full-nowait", "[mpmc_blocking_q]")
|
||||
REQUIRE(item == 42);
|
||||
}
|
||||
|
||||
TEST_CASE("dequeue-full-wait", "[mpmc_blocking_q]")
|
||||
{
|
||||
TEST_CASE("dequeue-full-wait", "[mpmc_blocking_q]") {
|
||||
spdlog::details::mpmc_blocking_queue<int> q(1);
|
||||
q.enqueue(42);
|
||||
|
||||
@ -63,8 +58,7 @@ TEST_CASE("dequeue-full-wait", "[mpmc_blocking_q]")
|
||||
REQUIRE(item == 42);
|
||||
}
|
||||
|
||||
TEST_CASE("enqueue_nowait", "[mpmc_blocking_q]")
|
||||
{
|
||||
TEST_CASE("enqueue_nowait", "[mpmc_blocking_q]") {
|
||||
|
||||
size_t q_size = 1;
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
@ -82,8 +76,7 @@ TEST_CASE("enqueue_nowait", "[mpmc_blocking_q]")
|
||||
REQUIRE(q.overrun_counter() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("bad_queue", "[mpmc_blocking_q]")
|
||||
{
|
||||
TEST_CASE("bad_queue", "[mpmc_blocking_q]") {
|
||||
size_t q_size = 0;
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
q.enqueue_nowait(1);
|
||||
@ -92,28 +85,25 @@ TEST_CASE("bad_queue", "[mpmc_blocking_q]")
|
||||
REQUIRE(q.dequeue_for(i, milliseconds(0)) == false);
|
||||
}
|
||||
|
||||
TEST_CASE("empty_queue", "[mpmc_blocking_q]")
|
||||
{
|
||||
TEST_CASE("empty_queue", "[mpmc_blocking_q]") {
|
||||
size_t q_size = 10;
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
int i = 0;
|
||||
REQUIRE(q.dequeue_for(i, milliseconds(10)) == false);
|
||||
}
|
||||
|
||||
TEST_CASE("full_queue", "[mpmc_blocking_q]")
|
||||
{
|
||||
TEST_CASE("full_queue", "[mpmc_blocking_q]") {
|
||||
size_t q_size = 100;
|
||||
spdlog::details::mpmc_blocking_queue<int> q(q_size);
|
||||
for (int i = 0; i < static_cast<int>(q_size); i++)
|
||||
{
|
||||
q.enqueue(i + 0); // i+0 to force rvalue and avoid tidy warnings on the same time if we std::move(i) instead
|
||||
for (int i = 0; i < static_cast<int>(q_size); i++) {
|
||||
q.enqueue(i + 0); // i+0 to force rvalue and avoid tidy warnings on the same time if we
|
||||
// std::move(i) instead
|
||||
}
|
||||
|
||||
q.enqueue_nowait(123456);
|
||||
REQUIRE(q.overrun_counter() == 1);
|
||||
|
||||
for (int i = 1; i < static_cast<int>(q_size); i++)
|
||||
{
|
||||
for (int i = 1; i < static_cast<int>(q_size); i++) {
|
||||
int item = -1;
|
||||
q.dequeue(item);
|
||||
REQUIRE(item == i);
|
||||
|
@ -5,79 +5,78 @@ using spdlog::memory_buf_t;
|
||||
using spdlog::details::to_string_view;
|
||||
|
||||
// log to str and return it
|
||||
template<typename... Args>
|
||||
static std::string log_to_str(const std::string &msg, const Args &...args)
|
||||
{
|
||||
template <typename... Args>
|
||||
static std::string log_to_str(const std::string &msg, const Args &...args) {
|
||||
std::ostringstream oss;
|
||||
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("pattern_tester", oss_sink);
|
||||
oss_logger.set_level(spdlog::level::info);
|
||||
|
||||
oss_logger.set_formatter(std::unique_ptr<spdlog::formatter>(new spdlog::pattern_formatter(args...)));
|
||||
oss_logger.set_formatter(
|
||||
std::unique_ptr<spdlog::formatter>(new spdlog::pattern_formatter(args...)));
|
||||
|
||||
oss_logger.info(msg);
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
TEST_CASE("custom eol", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("custom eol", "[pattern_formatter]") {
|
||||
std::string msg = "Hello custom eol test";
|
||||
std::string eol = ";)";
|
||||
REQUIRE(log_to_str(msg, "%v", spdlog::pattern_time_type::local, ";)") == msg + eol);
|
||||
}
|
||||
|
||||
TEST_CASE("empty format", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("empty format", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "", spdlog::pattern_time_type::local, "").empty());
|
||||
}
|
||||
|
||||
TEST_CASE("empty format2", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("empty format2", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "", spdlog::pattern_time_type::local, "\n") == "\n");
|
||||
}
|
||||
|
||||
TEST_CASE("level", "[pattern_formatter]")
|
||||
{
|
||||
REQUIRE(log_to_str("Some message", "[%l] %v", spdlog::pattern_time_type::local, "\n") == "[info] Some message\n");
|
||||
TEST_CASE("level", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "[%l] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[info] Some message\n");
|
||||
}
|
||||
|
||||
TEST_CASE("short level", "[pattern_formatter]")
|
||||
{
|
||||
REQUIRE(log_to_str("Some message", "[%L] %v", spdlog::pattern_time_type::local, "\n") == "[I] Some message\n");
|
||||
TEST_CASE("short level", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "[%L] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[I] Some message\n");
|
||||
}
|
||||
|
||||
TEST_CASE("name", "[pattern_formatter]")
|
||||
{
|
||||
REQUIRE(log_to_str("Some message", "[%n] %v", spdlog::pattern_time_type::local, "\n") == "[pattern_tester] Some message\n");
|
||||
TEST_CASE("name", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "[%n] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[pattern_tester] Some message\n");
|
||||
}
|
||||
|
||||
TEST_CASE("date MM/DD/YY ", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("date MM/DD/YY ", "[pattern_formatter]") {
|
||||
auto now_tm = spdlog::details::os::localtime();
|
||||
std::stringstream oss;
|
||||
oss << std::setfill('0') << std::setw(2) << now_tm.tm_mon + 1 << "/" << std::setw(2) << now_tm.tm_mday << "/" << std::setw(2)
|
||||
<< (now_tm.tm_year + 1900) % 1000 << " Some message\n";
|
||||
REQUIRE(log_to_str("Some message", "%D %v", spdlog::pattern_time_type::local, "\n") == oss.str());
|
||||
oss << std::setfill('0') << std::setw(2) << now_tm.tm_mon + 1 << "/" << std::setw(2)
|
||||
<< now_tm.tm_mday << "/" << std::setw(2) << (now_tm.tm_year + 1900) % 1000
|
||||
<< " Some message\n";
|
||||
REQUIRE(log_to_str("Some message", "%D %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
oss.str());
|
||||
}
|
||||
|
||||
TEST_CASE("color range test1", "[pattern_formatter]")
|
||||
{
|
||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%v%$", spdlog::pattern_time_type::local, "\n");
|
||||
TEST_CASE("color range test1", "[pattern_formatter]") {
|
||||
auto formatter = std::make_shared<spdlog::pattern_formatter>(
|
||||
"%^%v%$", spdlog::pattern_time_type::local, "\n");
|
||||
|
||||
memory_buf_t buf;
|
||||
spdlog::fmt_lib::format_to(std::back_inserter(buf), "Hello");
|
||||
memory_buf_t formatted;
|
||||
std::string logger_name = "test";
|
||||
spdlog::details::log_msg msg(logger_name, spdlog::level::info, spdlog::string_view_t(buf.data(), buf.size()));
|
||||
spdlog::details::log_msg msg(logger_name, spdlog::level::info,
|
||||
spdlog::string_view_t(buf.data(), buf.size()));
|
||||
formatter->format(msg, formatted);
|
||||
REQUIRE(msg.color_range_start == 0);
|
||||
REQUIRE(msg.color_range_end == 5);
|
||||
REQUIRE(log_to_str("hello", "%^%v%$", spdlog::pattern_time_type::local, "\n") == "hello\n");
|
||||
}
|
||||
|
||||
TEST_CASE("color range test2", "[pattern_formatter]")
|
||||
{
|
||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^%$", spdlog::pattern_time_type::local, "\n");
|
||||
TEST_CASE("color range test2", "[pattern_formatter]") {
|
||||
auto formatter =
|
||||
std::make_shared<spdlog::pattern_formatter>("%^%$", spdlog::pattern_time_type::local, "\n");
|
||||
std::string logger_name = "test";
|
||||
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "");
|
||||
memory_buf_t formatted;
|
||||
@ -87,8 +86,7 @@ TEST_CASE("color range test2", "[pattern_formatter]")
|
||||
REQUIRE(log_to_str("", "%^%$", spdlog::pattern_time_type::local, "\n") == "\n");
|
||||
}
|
||||
|
||||
TEST_CASE("color range test3", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("color range test3", "[pattern_formatter]") {
|
||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("%^***%$");
|
||||
std::string logger_name = "test";
|
||||
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
|
||||
@ -98,9 +96,9 @@ TEST_CASE("color range test3", "[pattern_formatter]")
|
||||
REQUIRE(msg.color_range_end == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("color range test4", "[pattern_formatter]")
|
||||
{
|
||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("XX%^YYY%$", spdlog::pattern_time_type::local, "\n");
|
||||
TEST_CASE("color range test4", "[pattern_formatter]") {
|
||||
auto formatter = std::make_shared<spdlog::pattern_formatter>(
|
||||
"XX%^YYY%$", spdlog::pattern_time_type::local, "\n");
|
||||
std::string logger_name = "test";
|
||||
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
|
||||
|
||||
@ -108,11 +106,11 @@ TEST_CASE("color range test4", "[pattern_formatter]")
|
||||
formatter->format(msg, formatted);
|
||||
REQUIRE(msg.color_range_start == 2);
|
||||
REQUIRE(msg.color_range_end == 5);
|
||||
REQUIRE(log_to_str("ignored", "XX%^YYY%$", spdlog::pattern_time_type::local, "\n") == "XXYYY\n");
|
||||
REQUIRE(log_to_str("ignored", "XX%^YYY%$", spdlog::pattern_time_type::local, "\n") ==
|
||||
"XXYYY\n");
|
||||
}
|
||||
|
||||
TEST_CASE("color range test5", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("color range test5", "[pattern_formatter]") {
|
||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%^");
|
||||
std::string logger_name = "test";
|
||||
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
|
||||
@ -122,8 +120,7 @@ TEST_CASE("color range test5", "[pattern_formatter]")
|
||||
REQUIRE(msg.color_range_end == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("color range test6", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("color range test6", "[pattern_formatter]") {
|
||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("**%$");
|
||||
std::string logger_name = "test";
|
||||
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "ignored");
|
||||
@ -137,62 +134,70 @@ TEST_CASE("color range test6", "[pattern_formatter]")
|
||||
// Test padding
|
||||
//
|
||||
|
||||
TEST_CASE("level_left_padded", "[pattern_formatter]")
|
||||
{
|
||||
REQUIRE(log_to_str("Some message", "[%8l] %v", spdlog::pattern_time_type::local, "\n") == "[ info] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%8!l] %v", spdlog::pattern_time_type::local, "\n") == "[ info] Some message\n");
|
||||
TEST_CASE("level_left_padded", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "[%8l] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[ info] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%8!l] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[ info] Some message\n");
|
||||
}
|
||||
|
||||
TEST_CASE("level_right_padded", "[pattern_formatter]")
|
||||
{
|
||||
REQUIRE(log_to_str("Some message", "[%-8l] %v", spdlog::pattern_time_type::local, "\n") == "[info ] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%-8!l] %v", spdlog::pattern_time_type::local, "\n") == "[info ] Some message\n");
|
||||
TEST_CASE("level_right_padded", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "[%-8l] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[info ] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%-8!l] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[info ] Some message\n");
|
||||
}
|
||||
|
||||
TEST_CASE("level_center_padded", "[pattern_formatter]")
|
||||
{
|
||||
REQUIRE(log_to_str("Some message", "[%=8l] %v", spdlog::pattern_time_type::local, "\n") == "[ info ] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%=8!l] %v", spdlog::pattern_time_type::local, "\n") == "[ info ] Some message\n");
|
||||
TEST_CASE("level_center_padded", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "[%=8l] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[ info ] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%=8!l] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[ info ] Some message\n");
|
||||
}
|
||||
|
||||
TEST_CASE("short level_left_padded", "[pattern_formatter]")
|
||||
{
|
||||
REQUIRE(log_to_str("Some message", "[%3L] %v", spdlog::pattern_time_type::local, "\n") == "[ I] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%3!L] %v", spdlog::pattern_time_type::local, "\n") == "[ I] Some message\n");
|
||||
TEST_CASE("short level_left_padded", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "[%3L] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[ I] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%3!L] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[ I] Some message\n");
|
||||
}
|
||||
|
||||
TEST_CASE("short level_right_padded", "[pattern_formatter]")
|
||||
{
|
||||
REQUIRE(log_to_str("Some message", "[%-3L] %v", spdlog::pattern_time_type::local, "\n") == "[I ] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%-3!L] %v", spdlog::pattern_time_type::local, "\n") == "[I ] Some message\n");
|
||||
TEST_CASE("short level_right_padded", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "[%-3L] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[I ] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%-3!L] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[I ] Some message\n");
|
||||
}
|
||||
|
||||
TEST_CASE("short level_center_padded", "[pattern_formatter]")
|
||||
{
|
||||
REQUIRE(log_to_str("Some message", "[%=3L] %v", spdlog::pattern_time_type::local, "\n") == "[ I ] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%=3!L] %v", spdlog::pattern_time_type::local, "\n") == "[ I ] Some message\n");
|
||||
TEST_CASE("short level_center_padded", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "[%=3L] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[ I ] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%=3!L] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[ I ] Some message\n");
|
||||
}
|
||||
|
||||
TEST_CASE("left_padded_short", "[pattern_formatter]")
|
||||
{
|
||||
REQUIRE(log_to_str("Some message", "[%3n] %v", spdlog::pattern_time_type::local, "\n") == "[pattern_tester] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%3!n] %v", spdlog::pattern_time_type::local, "\n") == "[pat] Some message\n");
|
||||
TEST_CASE("left_padded_short", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "[%3n] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[pattern_tester] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%3!n] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[pat] Some message\n");
|
||||
}
|
||||
|
||||
TEST_CASE("right_padded_short", "[pattern_formatter]")
|
||||
{
|
||||
REQUIRE(log_to_str("Some message", "[%-3n] %v", spdlog::pattern_time_type::local, "\n") == "[pattern_tester] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%-3!n] %v", spdlog::pattern_time_type::local, "\n") == "[pat] Some message\n");
|
||||
TEST_CASE("right_padded_short", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "[%-3n] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[pattern_tester] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%-3!n] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[pat] Some message\n");
|
||||
}
|
||||
|
||||
TEST_CASE("center_padded_short", "[pattern_formatter]")
|
||||
{
|
||||
REQUIRE(log_to_str("Some message", "[%=3n] %v", spdlog::pattern_time_type::local, "\n") == "[pattern_tester] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%=3!n] %v", spdlog::pattern_time_type::local, "\n") == "[pat] Some message\n");
|
||||
TEST_CASE("center_padded_short", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "[%=3n] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[pattern_tester] Some message\n");
|
||||
REQUIRE(log_to_str("Some message", "[%=3!n] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[pat] Some message\n");
|
||||
}
|
||||
|
||||
TEST_CASE("left_padded_huge", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("left_padded_huge", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "[%-300n] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[pattern_tester ] Some message\n");
|
||||
|
||||
@ -200,8 +205,7 @@ TEST_CASE("left_padded_huge", "[pattern_formatter]")
|
||||
"[pattern_tester ] Some message\n");
|
||||
}
|
||||
|
||||
TEST_CASE("left_padded_max", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("left_padded_max", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "[%-64n] %v", spdlog::pattern_time_type::local, "\n") ==
|
||||
"[pattern_tester ] Some message\n");
|
||||
|
||||
@ -211,8 +215,7 @@ TEST_CASE("left_padded_max", "[pattern_formatter]")
|
||||
|
||||
// Test padding + truncate flag
|
||||
|
||||
TEST_CASE("paddinng_truncate", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("paddinng_truncate", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("123456", "%6!v", spdlog::pattern_time_type::local, "\n") == "123456\n");
|
||||
REQUIRE(log_to_str("123456", "%5!v", spdlog::pattern_time_type::local, "\n") == "12345\n");
|
||||
REQUIRE(log_to_str("123456", "%7!v", spdlog::pattern_time_type::local, "\n") == " 123456\n");
|
||||
@ -228,42 +231,43 @@ TEST_CASE("paddinng_truncate", "[pattern_formatter]")
|
||||
REQUIRE(log_to_str("123456", "%0!v", spdlog::pattern_time_type::local, "\n") == "\n");
|
||||
}
|
||||
|
||||
TEST_CASE("padding_truncate_funcname", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("padding_truncate_funcname", "[pattern_formatter]") {
|
||||
spdlog::sinks::test_sink_st test_sink;
|
||||
|
||||
const char *pattern = "%v [%5!!]";
|
||||
auto formatter = std::unique_ptr<spdlog::formatter>(new spdlog::pattern_formatter(pattern));
|
||||
test_sink.set_formatter(std::move(formatter));
|
||||
|
||||
spdlog::details::log_msg msg1{spdlog::source_loc{"ignored", 1, "func"}, "test_logger", spdlog::level::info, "message"};
|
||||
spdlog::details::log_msg msg1{spdlog::source_loc{"ignored", 1, "func"}, "test_logger",
|
||||
spdlog::level::info, "message"};
|
||||
test_sink.log(msg1);
|
||||
REQUIRE(test_sink.lines()[0] == "message [ func]");
|
||||
|
||||
spdlog::details::log_msg msg2{spdlog::source_loc{"ignored", 1, "function"}, "test_logger", spdlog::level::info, "message"};
|
||||
spdlog::details::log_msg msg2{spdlog::source_loc{"ignored", 1, "function"}, "test_logger",
|
||||
spdlog::level::info, "message"};
|
||||
test_sink.log(msg2);
|
||||
REQUIRE(test_sink.lines()[1] == "message [funct]");
|
||||
}
|
||||
|
||||
TEST_CASE("padding_funcname", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("padding_funcname", "[pattern_formatter]") {
|
||||
spdlog::sinks::test_sink_st test_sink;
|
||||
|
||||
const char *pattern = "%v [%10!]";
|
||||
auto formatter = std::unique_ptr<spdlog::formatter>(new spdlog::pattern_formatter(pattern));
|
||||
test_sink.set_formatter(std::move(formatter));
|
||||
|
||||
spdlog::details::log_msg msg1{spdlog::source_loc{"ignored", 1, "func"}, "test_logger", spdlog::level::info, "message"};
|
||||
spdlog::details::log_msg msg1{spdlog::source_loc{"ignored", 1, "func"}, "test_logger",
|
||||
spdlog::level::info, "message"};
|
||||
test_sink.log(msg1);
|
||||
REQUIRE(test_sink.lines()[0] == "message [ func]");
|
||||
|
||||
spdlog::details::log_msg msg2{spdlog::source_loc{"ignored", 1, "func567890123"}, "test_logger", spdlog::level::info, "message"};
|
||||
spdlog::details::log_msg msg2{spdlog::source_loc{"ignored", 1, "func567890123"}, "test_logger",
|
||||
spdlog::level::info, "message"};
|
||||
test_sink.log(msg2);
|
||||
REQUIRE(test_sink.lines()[1] == "message [func567890123]");
|
||||
}
|
||||
|
||||
TEST_CASE("clone-default-formatter", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("clone-default-formatter", "[pattern_formatter]") {
|
||||
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>();
|
||||
auto formatter_2 = formatter_1->clone();
|
||||
std::string logger_name = "test";
|
||||
@ -277,8 +281,7 @@ TEST_CASE("clone-default-formatter", "[pattern_formatter]")
|
||||
REQUIRE(to_string_view(formatted_1) == to_string_view(formatted_2));
|
||||
}
|
||||
|
||||
TEST_CASE("clone-default-formatter2", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("clone-default-formatter2", "[pattern_formatter]") {
|
||||
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("%+");
|
||||
auto formatter_2 = formatter_1->clone();
|
||||
std::string logger_name = "test";
|
||||
@ -292,8 +295,7 @@ TEST_CASE("clone-default-formatter2", "[pattern_formatter]")
|
||||
REQUIRE(to_string_view(formatted_1) == to_string_view(formatted_2));
|
||||
}
|
||||
|
||||
TEST_CASE("clone-formatter", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("clone-formatter", "[pattern_formatter]") {
|
||||
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("%D %X [%] [%n] %v");
|
||||
auto formatter_2 = formatter_1->clone();
|
||||
std::string logger_name = "test";
|
||||
@ -307,10 +309,10 @@ TEST_CASE("clone-formatter", "[pattern_formatter]")
|
||||
REQUIRE(to_string_view(formatted_1) == to_string_view(formatted_2));
|
||||
}
|
||||
|
||||
TEST_CASE("clone-formatter-2", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("clone-formatter-2", "[pattern_formatter]") {
|
||||
using spdlog::pattern_time_type;
|
||||
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("%D %X [%] [%n] %v", pattern_time_type::utc, "xxxxxx\n");
|
||||
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>(
|
||||
"%D %X [%] [%n] %v", pattern_time_type::utc, "xxxxxx\n");
|
||||
auto formatter_2 = formatter_1->clone();
|
||||
std::string logger_name = "test2";
|
||||
spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message");
|
||||
@ -323,43 +325,35 @@ TEST_CASE("clone-formatter-2", "[pattern_formatter]")
|
||||
REQUIRE(to_string_view(formatted_1) == to_string_view(formatted_2));
|
||||
}
|
||||
|
||||
class custom_test_flag : public spdlog::custom_flag_formatter
|
||||
{
|
||||
class custom_test_flag : public spdlog::custom_flag_formatter {
|
||||
public:
|
||||
explicit custom_test_flag(std::string txt)
|
||||
: some_txt{std::move(txt)}
|
||||
{}
|
||||
: some_txt{std::move(txt)} {}
|
||||
|
||||
void format(const spdlog::details::log_msg &, const std::tm &tm, spdlog::memory_buf_t &dest) override
|
||||
{
|
||||
if (some_txt == "throw_me")
|
||||
{
|
||||
void format(const spdlog::details::log_msg &,
|
||||
const std::tm &tm,
|
||||
spdlog::memory_buf_t &dest) override {
|
||||
if (some_txt == "throw_me") {
|
||||
throw spdlog::spdlog_ex("custom_flag_exception_test");
|
||||
}
|
||||
else if (some_txt == "time")
|
||||
{
|
||||
auto formatted = spdlog::fmt_lib::format("{:d}:{:02d}{:s}", tm.tm_hour % 12, tm.tm_min, tm.tm_hour / 12 ? "PM" : "AM");
|
||||
} else if (some_txt == "time") {
|
||||
auto formatted = spdlog::fmt_lib::format("{:d}:{:02d}{:s}", tm.tm_hour % 12, tm.tm_min,
|
||||
tm.tm_hour / 12 ? "PM" : "AM");
|
||||
dest.append(formatted.data(), formatted.data() + formatted.size());
|
||||
return;
|
||||
}
|
||||
some_txt = std::string(padinfo_.width_, ' ') + some_txt;
|
||||
dest.append(some_txt.data(), some_txt.data() + some_txt.size());
|
||||
}
|
||||
spdlog::details::padding_info get_padding_info()
|
||||
{
|
||||
return padinfo_;
|
||||
}
|
||||
spdlog::details::padding_info get_padding_info() { return padinfo_; }
|
||||
|
||||
std::string some_txt;
|
||||
|
||||
std::unique_ptr<custom_flag_formatter> clone() const override
|
||||
{
|
||||
std::unique_ptr<custom_flag_formatter> clone() const override {
|
||||
return std::make_unique<custom_test_flag>(some_txt);
|
||||
}
|
||||
};
|
||||
// test clone with custom flag formatters
|
||||
TEST_CASE("clone-custom_formatter", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("clone-custom_formatter", "[pattern_formatter]") {
|
||||
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>();
|
||||
formatter_1->add_flag<custom_test_flag>('t', "custom_output").set_pattern("[%n] [%t] %v");
|
||||
auto formatter_2 = formatter_1->clone();
|
||||
@ -371,7 +365,8 @@ TEST_CASE("clone-custom_formatter", "[pattern_formatter]")
|
||||
formatter_1->format(msg, formatted_1);
|
||||
formatter_2->format(msg, formatted_2);
|
||||
|
||||
auto expected = spdlog::fmt_lib::format("[logger-name] [custom_output] some message{}", spdlog::details::os::default_eol);
|
||||
auto expected = spdlog::fmt_lib::format("[logger-name] [custom_output] some message{}",
|
||||
spdlog::details::os::default_eol);
|
||||
|
||||
REQUIRE(to_string_view(formatted_1) == expected);
|
||||
REQUIRE(to_string_view(formatted_2) == expected);
|
||||
@ -387,8 +382,7 @@ static const char *const test_path = "\\a\\b\\c/myfile.cpp";
|
||||
static const char *const test_path = "/a/b//myfile.cpp";
|
||||
#endif
|
||||
|
||||
TEST_CASE("short filename formatter-1", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("short filename formatter-1", "[pattern_formatter]") {
|
||||
spdlog::pattern_formatter formatter("%s", spdlog::pattern_time_type::local, "");
|
||||
memory_buf_t formatted;
|
||||
std::string logger_name = "logger-name";
|
||||
@ -399,8 +393,7 @@ TEST_CASE("short filename formatter-1", "[pattern_formatter]")
|
||||
REQUIRE(to_string_view(formatted) == "myfile.cpp");
|
||||
}
|
||||
|
||||
TEST_CASE("short filename formatter-2", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("short filename formatter-2", "[pattern_formatter]") {
|
||||
spdlog::pattern_formatter formatter("%s:%#", spdlog::pattern_time_type::local, "");
|
||||
memory_buf_t formatted;
|
||||
std::string logger_name = "logger-name";
|
||||
@ -411,8 +404,7 @@ TEST_CASE("short filename formatter-2", "[pattern_formatter]")
|
||||
REQUIRE(to_string_view(formatted) == "myfile.cpp:123");
|
||||
}
|
||||
|
||||
TEST_CASE("short filename formatter-3", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("short filename formatter-3", "[pattern_formatter]") {
|
||||
spdlog::pattern_formatter formatter("%s %v", spdlog::pattern_time_type::local, "");
|
||||
memory_buf_t formatted;
|
||||
std::string logger_name = "logger-name";
|
||||
@ -423,8 +415,7 @@ TEST_CASE("short filename formatter-3", "[pattern_formatter]")
|
||||
REQUIRE(to_string_view(formatted) == " Hello");
|
||||
}
|
||||
|
||||
TEST_CASE("full filename formatter", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("full filename formatter", "[pattern_formatter]") {
|
||||
spdlog::pattern_formatter formatter("%g", spdlog::pattern_time_type::local, "");
|
||||
memory_buf_t formatted;
|
||||
std::string logger_name = "logger-name";
|
||||
@ -435,52 +426,61 @@ TEST_CASE("full filename formatter", "[pattern_formatter]")
|
||||
REQUIRE(to_string_view(formatted) == test_path);
|
||||
}
|
||||
|
||||
TEST_CASE("custom flags", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("custom flags", "[pattern_formatter]") {
|
||||
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
||||
formatter->add_flag<custom_test_flag>('t', "custom1").add_flag<custom_test_flag>('u', "custom2").set_pattern("[%n] [%t] [%u] %v");
|
||||
formatter->add_flag<custom_test_flag>('t', "custom1")
|
||||
.add_flag<custom_test_flag>('u', "custom2")
|
||||
.set_pattern("[%n] [%t] [%u] %v");
|
||||
|
||||
memory_buf_t formatted;
|
||||
|
||||
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message");
|
||||
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
||||
"some message");
|
||||
formatter->format(msg, formatted);
|
||||
auto expected = spdlog::fmt_lib::format("[logger-name] [custom1] [custom2] some message{}", spdlog::details::os::default_eol);
|
||||
auto expected = spdlog::fmt_lib::format("[logger-name] [custom1] [custom2] some message{}",
|
||||
spdlog::details::os::default_eol);
|
||||
|
||||
REQUIRE(to_string_view(formatted) == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("custom flags-padding", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("custom flags-padding", "[pattern_formatter]") {
|
||||
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
||||
formatter->add_flag<custom_test_flag>('t', "custom1").add_flag<custom_test_flag>('u', "custom2").set_pattern("[%n] [%t] [%5u] %v");
|
||||
formatter->add_flag<custom_test_flag>('t', "custom1")
|
||||
.add_flag<custom_test_flag>('u', "custom2")
|
||||
.set_pattern("[%n] [%t] [%5u] %v");
|
||||
|
||||
memory_buf_t formatted;
|
||||
|
||||
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message");
|
||||
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
||||
"some message");
|
||||
formatter->format(msg, formatted);
|
||||
auto expected = spdlog::fmt_lib::format("[logger-name] [custom1] [ custom2] some message{}", spdlog::details::os::default_eol);
|
||||
auto expected = spdlog::fmt_lib::format("[logger-name] [custom1] [ custom2] some message{}",
|
||||
spdlog::details::os::default_eol);
|
||||
|
||||
REQUIRE(to_string_view(formatted) == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("custom flags-exception", "[pattern_formatter]")
|
||||
{
|
||||
TEST_CASE("custom flags-exception", "[pattern_formatter]") {
|
||||
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
||||
formatter->add_flag<custom_test_flag>('t', "throw_me").add_flag<custom_test_flag>('u', "custom2").set_pattern("[%n] [%t] [%u] %v");
|
||||
formatter->add_flag<custom_test_flag>('t', "throw_me")
|
||||
.add_flag<custom_test_flag>('u', "custom2")
|
||||
.set_pattern("[%n] [%t] [%u] %v");
|
||||
|
||||
memory_buf_t formatted;
|
||||
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message");
|
||||
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
||||
"some message");
|
||||
CHECK_THROWS_AS(formatter->format(msg, formatted), spdlog::spdlog_ex);
|
||||
}
|
||||
|
||||
TEST_CASE("override need_localtime", "[pattern_formatter]")
|
||||
{
|
||||
auto formatter = std::make_shared<spdlog::pattern_formatter>(spdlog::pattern_time_type::local, "\n");
|
||||
TEST_CASE("override need_localtime", "[pattern_formatter]") {
|
||||
auto formatter =
|
||||
std::make_shared<spdlog::pattern_formatter>(spdlog::pattern_time_type::local, "\n");
|
||||
formatter->add_flag<custom_test_flag>('t', "time").set_pattern("%t> %v");
|
||||
|
||||
{
|
||||
memory_buf_t formatted;
|
||||
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message");
|
||||
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
||||
"some message");
|
||||
formatter->format(msg, formatted);
|
||||
REQUIRE(to_string_view(formatted) == "0:00AM> some message\n");
|
||||
}
|
||||
@ -490,11 +490,12 @@ TEST_CASE("override need_localtime", "[pattern_formatter]")
|
||||
|
||||
auto now_tm = spdlog::details::os::localtime();
|
||||
std::stringstream oss;
|
||||
oss << (now_tm.tm_hour % 12) << ":" << std::setfill('0') << std::setw(2) << now_tm.tm_min << (now_tm.tm_hour / 12 ? "PM" : "AM")
|
||||
<< "> some message\n";
|
||||
oss << (now_tm.tm_hour % 12) << ":" << std::setfill('0') << std::setw(2) << now_tm.tm_min
|
||||
<< (now_tm.tm_hour / 12 ? "PM" : "AM") << "> some message\n";
|
||||
|
||||
memory_buf_t formatted;
|
||||
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message");
|
||||
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info,
|
||||
"some message");
|
||||
formatter->format(msg, formatted);
|
||||
REQUIRE(to_string_view(formatted) == oss.str());
|
||||
}
|
||||
|
@ -4,32 +4,34 @@ static const char *const tested_logger_name = "null_logger";
|
||||
static const char *const tested_logger_name2 = "null_logger2";
|
||||
|
||||
#ifndef SPDLOG_NO_EXCEPTIONS
|
||||
TEST_CASE("register_drop", "[registry]")
|
||||
{
|
||||
TEST_CASE("register_drop", "[registry]") {
|
||||
spdlog::drop_all();
|
||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
||||
REQUIRE(spdlog::get(tested_logger_name) != nullptr);
|
||||
// Throw if registering existing name
|
||||
REQUIRE_THROWS_AS(spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name), spdlog::spdlog_ex);
|
||||
REQUIRE_THROWS_AS(spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name),
|
||||
spdlog::spdlog_ex);
|
||||
}
|
||||
|
||||
TEST_CASE("explicit register", "[registry]")
|
||||
{
|
||||
TEST_CASE("explicit register", "[registry]") {
|
||||
spdlog::drop_all();
|
||||
auto logger = std::make_shared<spdlog::logger>(tested_logger_name, std::make_shared<spdlog::sinks::null_sink_st>());
|
||||
auto logger = std::make_shared<spdlog::logger>(tested_logger_name,
|
||||
std::make_shared<spdlog::sinks::null_sink_st>());
|
||||
spdlog::register_logger(logger);
|
||||
REQUIRE(spdlog::get(tested_logger_name) != nullptr);
|
||||
// Throw if registering existing name
|
||||
REQUIRE_THROWS_AS(spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name), spdlog::spdlog_ex);
|
||||
REQUIRE_THROWS_AS(spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name),
|
||||
spdlog::spdlog_ex);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE("apply_all", "[registry]")
|
||||
{
|
||||
TEST_CASE("apply_all", "[registry]") {
|
||||
spdlog::drop_all();
|
||||
auto logger = std::make_shared<spdlog::logger>(tested_logger_name, std::make_shared<spdlog::sinks::null_sink_st>());
|
||||
auto logger = std::make_shared<spdlog::logger>(tested_logger_name,
|
||||
std::make_shared<spdlog::sinks::null_sink_st>());
|
||||
spdlog::register_logger(logger);
|
||||
auto logger2 = std::make_shared<spdlog::logger>(tested_logger_name2, std::make_shared<spdlog::sinks::null_sink_st>());
|
||||
auto logger2 = std::make_shared<spdlog::logger>(
|
||||
tested_logger_name2, std::make_shared<spdlog::sinks::null_sink_st>());
|
||||
spdlog::register_logger(logger2);
|
||||
|
||||
int counter = 0;
|
||||
@ -45,24 +47,21 @@ TEST_CASE("apply_all", "[registry]")
|
||||
REQUIRE(counter == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("drop", "[registry]")
|
||||
{
|
||||
TEST_CASE("drop", "[registry]") {
|
||||
spdlog::drop_all();
|
||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
||||
spdlog::drop(tested_logger_name);
|
||||
REQUIRE_FALSE(spdlog::get(tested_logger_name));
|
||||
}
|
||||
|
||||
TEST_CASE("drop-default", "[registry]")
|
||||
{
|
||||
TEST_CASE("drop-default", "[registry]") {
|
||||
spdlog::set_default_logger(spdlog::null_logger_st(tested_logger_name));
|
||||
spdlog::drop(tested_logger_name);
|
||||
REQUIRE_FALSE(spdlog::default_logger());
|
||||
REQUIRE_FALSE(spdlog::get(tested_logger_name));
|
||||
}
|
||||
|
||||
TEST_CASE("drop_all", "[registry]")
|
||||
{
|
||||
TEST_CASE("drop_all", "[registry]") {
|
||||
spdlog::drop_all();
|
||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name2);
|
||||
@ -72,8 +71,7 @@ TEST_CASE("drop_all", "[registry]")
|
||||
REQUIRE_FALSE(spdlog::default_logger());
|
||||
}
|
||||
|
||||
TEST_CASE("drop non existing", "[registry]")
|
||||
{
|
||||
TEST_CASE("drop non existing", "[registry]") {
|
||||
spdlog::drop_all();
|
||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
||||
spdlog::drop("some_name");
|
||||
@ -82,28 +80,26 @@ TEST_CASE("drop non existing", "[registry]")
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("default logger", "[registry]")
|
||||
{
|
||||
TEST_CASE("default logger", "[registry]") {
|
||||
spdlog::drop_all();
|
||||
spdlog::set_default_logger(spdlog::null_logger_st(tested_logger_name));
|
||||
REQUIRE(spdlog::get(tested_logger_name) == spdlog::default_logger());
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("set_default_logger(nullptr)", "[registry]")
|
||||
{
|
||||
TEST_CASE("set_default_logger(nullptr)", "[registry]") {
|
||||
spdlog::set_default_logger(nullptr);
|
||||
REQUIRE_FALSE(spdlog::default_logger());
|
||||
}
|
||||
|
||||
TEST_CASE("disable automatic registration", "[registry]")
|
||||
{
|
||||
TEST_CASE("disable automatic registration", "[registry]") {
|
||||
// set some global parameters
|
||||
spdlog::level level = spdlog::level::warn;
|
||||
spdlog::set_level(level);
|
||||
// but disable automatic registration
|
||||
spdlog::set_automatic_registration(false);
|
||||
auto logger1 = spdlog::create<spdlog::sinks::daily_file_sink_st>(tested_logger_name, SPDLOG_FILENAME_T("filename"), 11, 59);
|
||||
auto logger1 = spdlog::create<spdlog::sinks::daily_file_sink_st>(
|
||||
tested_logger_name, SPDLOG_FILENAME_T("filename"), 11, 59);
|
||||
auto logger2 = spdlog::create_async<spdlog::sinks::stdout_color_sink_mt>(tested_logger_name2);
|
||||
// loggers should not be part of the registry
|
||||
REQUIRE_FALSE(spdlog::get(tested_logger_name));
|
||||
|
@ -1,8 +1,7 @@
|
||||
#include "includes.h"
|
||||
#include "spdlog/sinks/ringbuffer_sink.h"
|
||||
|
||||
TEST_CASE("test_drain", "[ringbuffer_sink]")
|
||||
{
|
||||
TEST_CASE("test_drain", "[ringbuffer_sink]") {
|
||||
const size_t sink_size = 3;
|
||||
auto sink = std::make_shared<spdlog::sinks::ringbuffer_sink_mt>(sink_size);
|
||||
spdlog::logger l("logger", sink);
|
||||
@ -10,16 +9,15 @@ TEST_CASE("test_drain", "[ringbuffer_sink]")
|
||||
|
||||
// log more than the sink size by one and test that the first message is dropped
|
||||
// test 3 times to make sure the ringbuffer is working correctly multiple times
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
for (size_t i = 0; i < sink_size + 1; ++i)
|
||||
{
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (size_t i = 0; i < sink_size + 1; ++i) {
|
||||
l.info("{}", i);
|
||||
}
|
||||
|
||||
int counter = 0;
|
||||
sink->drain([&](std::string_view msg) {
|
||||
REQUIRE(msg == spdlog::fmt_lib::format("*** {}{}", counter + 1, spdlog::details::os::default_eol));
|
||||
REQUIRE(msg == spdlog::fmt_lib::format("*** {}{}", counter + 1,
|
||||
spdlog::details::os::default_eol));
|
||||
counter++;
|
||||
});
|
||||
|
||||
@ -27,18 +25,15 @@ TEST_CASE("test_drain", "[ringbuffer_sink]")
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("test_drain_raw", "[ringbuffer_sink]")
|
||||
{
|
||||
TEST_CASE("test_drain_raw", "[ringbuffer_sink]") {
|
||||
const size_t sink_size = 3;
|
||||
auto sink = std::make_shared<spdlog::sinks::ringbuffer_sink_mt>(sink_size);
|
||||
spdlog::logger l("logger", sink);
|
||||
|
||||
// log more than the sink size by one and test that the first message is dropped
|
||||
// test 3 times to make sure the ringbuffer is working correctly multiple times
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
for (size_t i = 0; i < sink_size + 1; ++i)
|
||||
{
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (size_t i = 0; i < sink_size + 1; ++i) {
|
||||
l.info("{}", i);
|
||||
}
|
||||
|
||||
@ -52,8 +47,7 @@ TEST_CASE("test_drain_raw", "[ringbuffer_sink]")
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("test_empty", "[ringbuffer_sink]")
|
||||
{
|
||||
TEST_CASE("test_empty", "[ringbuffer_sink]") {
|
||||
const size_t sink_size = 3;
|
||||
auto sink = std::make_shared<spdlog::sinks::ringbuffer_sink_mt>(sink_size);
|
||||
spdlog::logger l("logger", sink);
|
||||
@ -63,14 +57,12 @@ TEST_CASE("test_empty", "[ringbuffer_sink]")
|
||||
});
|
||||
}
|
||||
|
||||
TEST_CASE("test_empty_size", "[ringbuffer_sink]")
|
||||
{
|
||||
TEST_CASE("test_empty_size", "[ringbuffer_sink]") {
|
||||
const size_t sink_size = 0;
|
||||
auto sink = std::make_shared<spdlog::sinks::ringbuffer_sink_mt>(sink_size);
|
||||
spdlog::logger l("logger", sink);
|
||||
|
||||
for (size_t i = 0; i < sink_size + 1; ++i)
|
||||
{
|
||||
for (size_t i = 0; i < sink_size + 1; ++i) {
|
||||
l.info("{}", i);
|
||||
}
|
||||
|
||||
|
@ -15,56 +15,46 @@
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
template<class Mutex>
|
||||
class test_sink : public base_sink<Mutex>
|
||||
{
|
||||
template <class Mutex>
|
||||
class test_sink : public base_sink<Mutex> {
|
||||
const size_t lines_to_save = 100;
|
||||
|
||||
public:
|
||||
size_t msg_counter()
|
||||
{
|
||||
size_t msg_counter() {
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
return msg_counter_;
|
||||
}
|
||||
|
||||
size_t flush_counter()
|
||||
{
|
||||
size_t flush_counter() {
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
return flush_counter_;
|
||||
}
|
||||
|
||||
void set_delay(std::chrono::milliseconds delay)
|
||||
{
|
||||
void set_delay(std::chrono::milliseconds delay) {
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
delay_ = delay;
|
||||
}
|
||||
|
||||
// return last output without the eol
|
||||
std::vector<std::string> lines()
|
||||
{
|
||||
std::vector<std::string> lines() {
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
return lines_;
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
void sink_it_(const details::log_msg &msg) override {
|
||||
memory_buf_t formatted;
|
||||
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||
// save the line without the eol
|
||||
auto eol_len = strlen(details::os::default_eol);
|
||||
if (lines_.size() < lines_to_save)
|
||||
{
|
||||
if (lines_.size() < lines_to_save) {
|
||||
lines_.emplace_back(formatted.begin(), formatted.end() - eol_len);
|
||||
}
|
||||
msg_counter_++;
|
||||
std::this_thread::sleep_for(delay_);
|
||||
}
|
||||
|
||||
void flush_() override
|
||||
{
|
||||
flush_counter_++;
|
||||
}
|
||||
void flush_() override { flush_counter_++; }
|
||||
|
||||
size_t msg_counter_{0};
|
||||
size_t flush_counter_{0};
|
||||
|
@ -6,15 +6,15 @@
|
||||
|
||||
using spdlog::details::os::default_eol;
|
||||
|
||||
TEST_CASE("test_source_location", "[source_location]")
|
||||
{
|
||||
TEST_CASE("test_source_location", "[source_location]") {
|
||||
std::ostringstream oss;
|
||||
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_st>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
oss_logger.set_pattern("%s:%# %v");
|
||||
|
||||
oss_logger.info("Hello {}", "source location");
|
||||
REQUIRE(oss.str() == std::string("test_source_location.cpp:16 Hello source location") + default_eol);
|
||||
REQUIRE(oss.str() ==
|
||||
std::string("test_source_location.cpp:16 Hello source location") + default_eol);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,11 +1,11 @@
|
||||
/*
|
||||
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
* This content is released under the MIT License as specified in
|
||||
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
*/
|
||||
#include "includes.h"
|
||||
#include "spdlog/sinks/stdout_sinks.h"
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
TEST_CASE("stdout_st", "[stdout]")
|
||||
{
|
||||
TEST_CASE("stdout_st", "[stdout]") {
|
||||
auto l = spdlog::stdout_logger_st("test");
|
||||
l->set_pattern("%+");
|
||||
l->set_level(spdlog::level::trace);
|
||||
@ -13,8 +13,7 @@ TEST_CASE("stdout_st", "[stdout]")
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("stdout_mt", "[stdout]")
|
||||
{
|
||||
TEST_CASE("stdout_mt", "[stdout]") {
|
||||
auto l = spdlog::stdout_logger_mt("test");
|
||||
l->set_pattern("%+");
|
||||
l->set_level(spdlog::level::debug);
|
||||
@ -22,16 +21,14 @@ TEST_CASE("stdout_mt", "[stdout]")
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("stderr_st", "[stderr]")
|
||||
{
|
||||
TEST_CASE("stderr_st", "[stderr]") {
|
||||
auto l = spdlog::stderr_logger_st("test");
|
||||
l->set_pattern("%+");
|
||||
l->info("Test stderr_st");
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("stderr_mt", "[stderr]")
|
||||
{
|
||||
TEST_CASE("stderr_mt", "[stderr]") {
|
||||
auto l = spdlog::stderr_logger_mt("test");
|
||||
l->set_pattern("%+");
|
||||
l->info("Test stderr_mt");
|
||||
@ -42,16 +39,14 @@ TEST_CASE("stderr_mt", "[stderr]")
|
||||
}
|
||||
|
||||
// color loggers
|
||||
TEST_CASE("stdout_color_st", "[stdout]")
|
||||
{
|
||||
TEST_CASE("stdout_color_st", "[stdout]") {
|
||||
auto l = spdlog::stdout_color_st("test");
|
||||
l->set_pattern("%+");
|
||||
l->info("Test stdout_color_st");
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("stdout_color_mt", "[stdout]")
|
||||
{
|
||||
TEST_CASE("stdout_color_mt", "[stdout]") {
|
||||
auto l = spdlog::stdout_color_mt("test");
|
||||
l->set_pattern("%+");
|
||||
l->set_level(spdlog::level::trace);
|
||||
@ -59,8 +54,7 @@ TEST_CASE("stdout_color_mt", "[stdout]")
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("stderr_color_st", "[stderr]")
|
||||
{
|
||||
TEST_CASE("stderr_color_st", "[stderr]") {
|
||||
auto l = spdlog::stderr_color_st("test");
|
||||
l->set_pattern("%+");
|
||||
l->set_level(spdlog::level::debug);
|
||||
@ -68,8 +62,7 @@ TEST_CASE("stderr_color_st", "[stderr]")
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
TEST_CASE("stderr_color_mt", "[stderr]")
|
||||
{
|
||||
TEST_CASE("stderr_color_mt", "[stderr]") {
|
||||
auto l = spdlog::stderr_color_mt("test");
|
||||
l->set_pattern("%+");
|
||||
l->info("Test stderr_color_mt");
|
||||
|
@ -2,8 +2,7 @@
|
||||
#include "test_sink.h"
|
||||
#include "spdlog/stopwatch.h"
|
||||
|
||||
TEST_CASE("stopwatch1", "[stopwatch]")
|
||||
{
|
||||
TEST_CASE("stopwatch1", "[stopwatch]") {
|
||||
using std::chrono::milliseconds;
|
||||
using clock = std::chrono::steady_clock;
|
||||
milliseconds wait_ms(200);
|
||||
@ -17,8 +16,7 @@ TEST_CASE("stopwatch1", "[stopwatch]")
|
||||
REQUIRE(sw.elapsed() <= diff_ms + tolerance_ms);
|
||||
}
|
||||
|
||||
TEST_CASE("stopwatch2", "[stopwatch]")
|
||||
{
|
||||
TEST_CASE("stopwatch2", "[stopwatch]") {
|
||||
using spdlog::sinks::test_sink_st;
|
||||
using std::chrono::duration_cast;
|
||||
using std::chrono::milliseconds;
|
||||
|
@ -1,8 +1,7 @@
|
||||
#include "includes.h"
|
||||
#include "spdlog/sinks/systemd_sink.h"
|
||||
|
||||
TEST_CASE("systemd", "[all]")
|
||||
{
|
||||
TEST_CASE("systemd", "[all]") {
|
||||
auto systemd_sink = std::make_shared<spdlog::sinks::systemd_sink_st>();
|
||||
spdlog::logger logger("spdlog_systemd_test", systemd_sink);
|
||||
logger.set_level(spdlog::level::trace);
|
||||
|
@ -2,8 +2,7 @@
|
||||
#include "test_sink.h"
|
||||
#include "spdlog/async.h"
|
||||
|
||||
TEST_CASE("time_point1", "[time_point log_msg]")
|
||||
{
|
||||
TEST_CASE("time_point1", "[time_point log_msg]") {
|
||||
std::shared_ptr<spdlog::sinks::test_sink_st> test_sink(new spdlog::sinks::test_sink_st);
|
||||
spdlog::logger logger("test-time_point", test_sink);
|
||||
|
||||
@ -13,8 +12,7 @@ TEST_CASE("time_point1", "[time_point log_msg]")
|
||||
|
||||
// all the following should have the same time
|
||||
test_sink->set_delay(std::chrono::milliseconds(10));
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
for (int i = 0; i < 5; i++) {
|
||||
spdlog::details::log_msg msg{tp, source, "test_logger", spdlog::level::info, "message"};
|
||||
test_sink->log(msg);
|
||||
}
|
||||
@ -23,7 +21,8 @@ TEST_CASE("time_point1", "[time_point log_msg]")
|
||||
logger.log(tp, source, spdlog::level::info, "formatted message");
|
||||
logger.log(tp, source, spdlog::level::info, "formatted message");
|
||||
logger.log(tp, source, spdlog::level::info, "formatted message");
|
||||
logger.log(source, spdlog::level::info, "formatted message"); // last line has different time_point
|
||||
logger.log(source, spdlog::level::info,
|
||||
"formatted message"); // last line has different time_point
|
||||
|
||||
// now the real test... that the times are the same.
|
||||
std::vector<std::string> lines = test_sink->lines();
|
||||
|
@ -1,41 +1,35 @@
|
||||
#include "includes.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
#include <windows.h>
|
||||
#else
|
||||
# include <sys/types.h>
|
||||
# include <dirent.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#endif
|
||||
|
||||
void prepare_logdir()
|
||||
{
|
||||
void prepare_logdir() {
|
||||
spdlog::drop_all();
|
||||
#ifdef _WIN32
|
||||
system("rmdir /S /Q test_logs");
|
||||
#else
|
||||
auto rv = system("rm -rf test_logs");
|
||||
if (rv != 0)
|
||||
{
|
||||
if (rv != 0) {
|
||||
throw std::runtime_error("Failed to rm -rf test_logs");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string file_contents(const std::string &filename)
|
||||
{
|
||||
std::string file_contents(const std::string &filename) {
|
||||
std::ifstream ifs(filename, std::ios_base::binary);
|
||||
if (!ifs)
|
||||
{
|
||||
if (!ifs) {
|
||||
throw std::runtime_error("Failed open file ");
|
||||
}
|
||||
return std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
|
||||
}
|
||||
|
||||
std::size_t count_lines(const std::string &filename)
|
||||
{
|
||||
std::size_t count_lines(const std::string &filename) {
|
||||
std::ifstream ifs(filename);
|
||||
if (!ifs)
|
||||
{
|
||||
if (!ifs) {
|
||||
throw std::runtime_error("Failed open file ");
|
||||
}
|
||||
|
||||
@ -46,23 +40,17 @@ std::size_t count_lines(const std::string &filename)
|
||||
return counter;
|
||||
}
|
||||
|
||||
void require_message_count(const std::string &filename, const std::size_t messages)
|
||||
{
|
||||
if (strlen(spdlog::details::os::default_eol) == 0)
|
||||
{
|
||||
void require_message_count(const std::string &filename, const std::size_t messages) {
|
||||
if (strlen(spdlog::details::os::default_eol) == 0) {
|
||||
REQUIRE(count_lines(filename) == 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
REQUIRE(count_lines(filename) == messages);
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t get_filesize(const std::string &filename)
|
||||
{
|
||||
std::size_t get_filesize(const std::string &filename) {
|
||||
std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
|
||||
if (!ifs)
|
||||
{
|
||||
if (!ifs) {
|
||||
throw std::runtime_error("Failed open file ");
|
||||
}
|
||||
|
||||
@ -70,10 +58,8 @@ std::size_t get_filesize(const std::string &filename)
|
||||
}
|
||||
|
||||
// source: https://stackoverflow.com/a/2072890/192001
|
||||
bool ends_with(std::string const &value, std::string const &ending)
|
||||
{
|
||||
if (ending.size() > value.size())
|
||||
{
|
||||
bool ends_with(std::string const &value, std::string const &ending) {
|
||||
if (ending.size() > value.size()) {
|
||||
return false;
|
||||
}
|
||||
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
|
||||
@ -81,24 +67,20 @@ bool ends_with(std::string const &value, std::string const &ending)
|
||||
|
||||
#ifdef _WIN32
|
||||
// Based on: https://stackoverflow.com/a/37416569/192001
|
||||
std::size_t count_files(const std::string &folder)
|
||||
{
|
||||
std::size_t count_files(const std::string &folder) {
|
||||
size_t counter = 0;
|
||||
WIN32_FIND_DATAA ffd;
|
||||
|
||||
// Start iterating over the files in the folder directory.
|
||||
HANDLE hFind = ::FindFirstFileA((folder + "\\*").c_str(), &ffd);
|
||||
if (hFind != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (hFind != INVALID_HANDLE_VALUE) {
|
||||
do // Managed to locate and create an handle to that folder.
|
||||
{
|
||||
if (ffd.cFileName[0] != '.')
|
||||
counter++;
|
||||
} while (::FindNextFileA(hFind, &ffd) != 0);
|
||||
::FindClose(hFind);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
throw std::runtime_error("Failed open folder " + folder);
|
||||
}
|
||||
|
||||
@ -106,18 +88,15 @@ std::size_t count_files(const std::string &folder)
|
||||
}
|
||||
#else
|
||||
// Based on: https://stackoverflow.com/a/2802255/192001
|
||||
std::size_t count_files(const std::string &folder)
|
||||
{
|
||||
std::size_t count_files(const std::string &folder) {
|
||||
size_t counter = 0;
|
||||
DIR *dp = opendir(folder.c_str());
|
||||
if (dp == nullptr)
|
||||
{
|
||||
if (dp == nullptr) {
|
||||
throw std::runtime_error("Failed open folder " + folder);
|
||||
}
|
||||
|
||||
struct dirent *ep = nullptr;
|
||||
while ((ep = readdir(dp)) != nullptr)
|
||||
{
|
||||
while ((ep = readdir(dp)) != nullptr) {
|
||||
if (ep->d_name[0] != '.')
|
||||
counter++;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user