mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-28 17:19:34 +08:00
clang-format
This commit is contained in:
@@ -1,22 +1,18 @@
|
||||
/*
|
||||
* 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<iostream>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
|
||||
|
||||
class failing_sink: public spdlog::sinks::sink
|
||||
class failing_sink : public spdlog::sinks::sink
|
||||
{
|
||||
void log(const spdlog::details::log_msg& msg) override
|
||||
void log(const spdlog::details::log_msg &msg) override
|
||||
{
|
||||
throw std::runtime_error("some error happened during log");
|
||||
}
|
||||
|
||||
void flush() override
|
||||
{}
|
||||
void flush() override {}
|
||||
};
|
||||
|
||||
TEST_CASE("default_error_handler", "[errors]]")
|
||||
@@ -39,21 +35,16 @@ TEST_CASE("default_error_handler", "[errors]]")
|
||||
REQUIRE(count_lines(filename) == 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
struct custom_ex
|
||||
{};
|
||||
{
|
||||
};
|
||||
TEST_CASE("custom_error_handler", "[errors]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
std::string filename = "logs/simple_log.txt";
|
||||
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
|
||||
logger->flush_on(spdlog::level::info);
|
||||
logger->set_error_handler([=](const std::string& msg)
|
||||
{
|
||||
throw custom_ex();
|
||||
});
|
||||
logger->set_error_handler([=](const std::string &msg) { throw custom_ex(); });
|
||||
logger->info("Good message #1");
|
||||
#if !defined(SPDLOG_FMT_PRINTF)
|
||||
REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
|
||||
@@ -68,10 +59,7 @@ TEST_CASE("default_error_handler2", "[errors]]")
|
||||
{
|
||||
|
||||
auto logger = spdlog::create<failing_sink>("failed_logger");
|
||||
logger->set_error_handler([=](const std::string& msg)
|
||||
{
|
||||
throw custom_ex();
|
||||
});
|
||||
logger->set_error_handler([=](const std::string &msg) { throw custom_ex(); });
|
||||
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
|
||||
}
|
||||
|
||||
@@ -83,10 +71,10 @@ TEST_CASE("async_error_handler", "[errors]]")
|
||||
std::string filename = "logs/simple_async_log.txt";
|
||||
{
|
||||
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
|
||||
logger->set_error_handler([=](const std::string& msg)
|
||||
{
|
||||
logger->set_error_handler([=](const std::string &msg) {
|
||||
std::ofstream ofs("logs/custom_err.txt");
|
||||
if (!ofs) throw std::runtime_error("Failed open logs/custom_err.txt");
|
||||
if (!ofs)
|
||||
throw std::runtime_error("Failed open logs/custom_err.txt");
|
||||
ofs << err_msg;
|
||||
});
|
||||
logger->info("Good message #1");
|
||||
@@ -96,7 +84,7 @@ TEST_CASE("async_error_handler", "[errors]]")
|
||||
logger->info("Bad format msg %s %s", "xxx");
|
||||
#endif
|
||||
logger->info("Good message #2");
|
||||
spdlog::drop("logger"); //force logger to drain the queue and shutdown
|
||||
spdlog::drop("logger"); // force logger to drain the queue and shutdown
|
||||
spdlog::set_sync_mode();
|
||||
}
|
||||
REQUIRE(count_lines(filename) == 2);
|
||||
@@ -111,14 +99,14 @@ TEST_CASE("async_error_handler2", "[errors]]")
|
||||
spdlog::set_async_mode(128);
|
||||
{
|
||||
auto logger = spdlog::create<failing_sink>("failed_logger");
|
||||
logger->set_error_handler([=](const std::string& msg)
|
||||
{
|
||||
logger->set_error_handler([=](const std::string &msg) {
|
||||
std::ofstream ofs("logs/custom_err2.txt");
|
||||
if (!ofs) throw std::runtime_error("Failed open logs/custom_err2.txt");
|
||||
if (!ofs)
|
||||
throw std::runtime_error("Failed open logs/custom_err2.txt");
|
||||
ofs << err_msg;
|
||||
});
|
||||
logger->info("Hello failure");
|
||||
spdlog::drop("failed_logger"); //force logger to drain the queue and shutdown
|
||||
spdlog::drop("failed_logger"); // force logger to drain the queue and shutdown
|
||||
spdlog::set_sync_mode();
|
||||
}
|
||||
|
||||
|
@@ -1,10 +1,10 @@
|
||||
/*
|
||||
* 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::log_msg;
|
||||
using spdlog::details::file_helper;
|
||||
using spdlog::details::log_msg;
|
||||
|
||||
static const std::string target_filename = "logs/file_helper_test.txt";
|
||||
|
||||
@@ -70,7 +70,7 @@ TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]]")
|
||||
REQUIRE(helper.size() == expected_size);
|
||||
}
|
||||
|
||||
static void test_split_ext(const char* fname, const char* expect_base, const char* expect_ext)
|
||||
static void test_split_ext(const char *fname, const char *expect_base, const char *expect_ext)
|
||||
{
|
||||
spdlog::filename_t filename(fname);
|
||||
spdlog::filename_t expected_base(expect_base);
|
||||
|
@@ -3,7 +3,6 @@
|
||||
*/
|
||||
#include "includes.h"
|
||||
|
||||
|
||||
TEST_CASE("simple_file_logger", "[simple_logger]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
@@ -24,7 +23,6 @@ TEST_CASE("simple_file_logger", "[simple_logger]]")
|
||||
REQUIRE(count_lines(filename) == 2);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("flush_on", "[flush_on]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
@@ -69,7 +67,6 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
@@ -96,11 +93,10 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
||||
REQUIRE(get_filesize(filename1) <= 1024);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("daily_logger", "[daily_logger]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
//calculate filename (time based)
|
||||
// calculate filename (time based)
|
||||
std::string basename = "logs/daily_log";
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
fmt::MemoryWriter w;
|
||||
@@ -121,15 +117,12 @@ TEST_CASE("daily_logger", "[daily_logger]]")
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]")
|
||||
{
|
||||
using sink_type = spdlog::sinks::daily_file_sink<
|
||||
std::mutex,
|
||||
spdlog::sinks::dateonly_daily_file_name_calculator>;
|
||||
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::dateonly_daily_file_name_calculator>;
|
||||
|
||||
prepare_logdir();
|
||||
//calculate filename (time based)
|
||||
// calculate filename (time based)
|
||||
std::string basename = "logs/daily_dateonly";
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
fmt::MemoryWriter w;
|
||||
@@ -151,7 +144,7 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]")
|
||||
|
||||
struct custom_daily_file_name_calculator
|
||||
{
|
||||
static spdlog::filename_t calc_filename(const spdlog::filename_t& basename)
|
||||
static spdlog::filename_t calc_filename(const spdlog::filename_t &basename)
|
||||
{
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
fmt::MemoryWriter w;
|
||||
@@ -162,12 +155,10 @@ struct custom_daily_file_name_calculator
|
||||
|
||||
TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
|
||||
{
|
||||
using sink_type = spdlog::sinks::daily_file_sink<
|
||||
std::mutex,
|
||||
custom_daily_file_name_calculator>;
|
||||
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, custom_daily_file_name_calculator>;
|
||||
|
||||
prepare_logdir();
|
||||
//calculate filename (time based)
|
||||
// calculate filename (time based)
|
||||
std::string basename = "logs/daily_dateonly";
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
fmt::MemoryWriter w;
|
||||
@@ -188,7 +179,6 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
|
||||
REQUIRE(count_lines(filename) == 10);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* File name calculations
|
||||
*/
|
||||
@@ -211,12 +201,8 @@ TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]]")
|
||||
REQUIRE(filename == "rotated.txt");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// regex supported only from gcc 4.9 and above
|
||||
#if defined (_MSC_VER) || !(__GNUC__ <= 4 && __GNUC_MINOR__ < 9)
|
||||
#if defined(_MSC_VER) || !(__GNUC__ <= 4 && __GNUC_MINOR__ < 9)
|
||||
#include <regex>
|
||||
TEST_CASE("daily_file_sink::default_daily_file_name_calculator1", "[daily_file_sink]]")
|
||||
{
|
||||
|
@@ -1,18 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <ostream>
|
||||
#include <chrono>
|
||||
#include <exception>
|
||||
#include "catch.hpp"
|
||||
#include "utils.h"
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <exception>
|
||||
#include <fstream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
#define SPDLOG_TRACE_ON
|
||||
#define SPDLOG_DEBUG_ON
|
||||
|
||||
#include "../include/spdlog/spdlog.h"
|
||||
#include "../include/spdlog/sinks/null_sink.h"
|
||||
#include "../include/spdlog/sinks/ostream_sink.h"
|
||||
|
||||
#include "../include/spdlog/spdlog.h"
|
||||
|
@@ -7,23 +7,24 @@ 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 registring existing name
|
||||
REQUIRE(spdlog::get(tested_logger_name) != nullptr);
|
||||
// Throw if registring existing name
|
||||
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>());
|
||||
spdlog::register_logger(logger);
|
||||
REQUIRE(spdlog::get(tested_logger_name) != nullptr);
|
||||
//Throw if registring existing name
|
||||
// Throw if registring existing name
|
||||
REQUIRE_THROWS_AS(spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name), spdlog::spdlog_ex);
|
||||
}
|
||||
|
||||
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>());
|
||||
@@ -32,26 +33,20 @@ TEST_CASE("apply_all" "[registry]")
|
||||
spdlog::register_logger(logger2);
|
||||
|
||||
int counter = 0;
|
||||
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l)
|
||||
{
|
||||
counter++;
|
||||
});
|
||||
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l) { counter++; });
|
||||
REQUIRE(counter == 2);
|
||||
|
||||
counter = 0;
|
||||
spdlog::drop(tested_logger_name2);
|
||||
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l)
|
||||
{
|
||||
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l) {
|
||||
REQUIRE(l->name() == tested_logger_name);
|
||||
counter++;
|
||||
}
|
||||
);
|
||||
});
|
||||
REQUIRE(counter == 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST_CASE("drop" "[registry]")
|
||||
TEST_CASE("drop"
|
||||
"[registry]")
|
||||
{
|
||||
spdlog::drop_all();
|
||||
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
|
||||
@@ -59,7 +54,8 @@ TEST_CASE("drop" "[registry]")
|
||||
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);
|
||||
@@ -69,8 +65,8 @@ TEST_CASE("drop_all" "[registry]")
|
||||
REQUIRE_FALSE(spdlog::get(tested_logger_name));
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
@@ -79,6 +75,3 @@ TEST_CASE("drop non existing" "[registry]")
|
||||
REQUIRE(spdlog::get(tested_logger_name));
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@@ -1,6 +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"
|
||||
|
||||
@@ -14,7 +14,7 @@ TEST_CASE("debug and trace w/o format string", "[macros]]")
|
||||
logger->set_level(spdlog::level::trace);
|
||||
|
||||
SPDLOG_TRACE(logger, "Test message 1");
|
||||
//SPDLOG_DEBUG(logger, "Test message 2");
|
||||
// SPDLOG_DEBUG(logger, "Test message 2");
|
||||
SPDLOG_DEBUG(logger, "Test message 2");
|
||||
logger->flush();
|
||||
|
||||
@@ -22,7 +22,6 @@ TEST_CASE("debug and trace w/o format string", "[macros]]")
|
||||
REQUIRE(count_lines(filename) == 2);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("debug and trace with format strings", "[macros]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
@@ -34,11 +33,11 @@ TEST_CASE("debug and trace with format strings", "[macros]]")
|
||||
|
||||
#if !defined(SPDLOG_FMT_PRINTF)
|
||||
SPDLOG_TRACE(logger, "Test message {}", 1);
|
||||
//SPDLOG_DEBUG(logger, "Test message 2");
|
||||
// SPDLOG_DEBUG(logger, "Test message 2");
|
||||
SPDLOG_DEBUG(logger, "Test message {}", 222);
|
||||
#else
|
||||
SPDLOG_TRACE(logger, "Test message %d", 1);
|
||||
//SPDLOG_DEBUG(logger, "Test message 2");
|
||||
// SPDLOG_DEBUG(logger, "Test message 2");
|
||||
SPDLOG_DEBUG(logger, "Test message %d", 222);
|
||||
#endif
|
||||
|
||||
@@ -47,4 +46,3 @@ TEST_CASE("debug and trace with format strings", "[macros]]")
|
||||
REQUIRE(ends_with(file_contents(filename), "Test message 222\n"));
|
||||
REQUIRE(count_lines(filename) == 2);
|
||||
}
|
||||
|
||||
|
@@ -1,13 +1,14 @@
|
||||
#include "includes.h"
|
||||
|
||||
// log to str and return it
|
||||
static std::string log_to_str(const std::string& msg, const std::shared_ptr<spdlog::formatter>& formatter = nullptr)
|
||||
static std::string log_to_str(const std::string &msg, const std::shared_ptr<spdlog::formatter> &formatter = nullptr)
|
||||
{
|
||||
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);
|
||||
if (formatter) oss_logger.set_formatter(formatter);
|
||||
if (formatter)
|
||||
oss_logger.set_formatter(formatter);
|
||||
oss_logger.info(msg);
|
||||
return oss.str();
|
||||
}
|
||||
@@ -56,6 +57,7 @@ TEST_CASE("date MM/DD/YY ", "[pattern_formatter]")
|
||||
auto formatter = std::make_shared<spdlog::pattern_formatter>("%D %v", spdlog::pattern_time_type::local, "\n");
|
||||
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";
|
||||
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", formatter) == oss.str());
|
||||
}
|
||||
|
@@ -1,6 +1,5 @@
|
||||
#include "includes.h"
|
||||
|
||||
|
||||
void prepare_logdir()
|
||||
{
|
||||
spdlog::drop_all();
|
||||
@@ -14,18 +13,15 @@ void prepare_logdir()
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
std::string file_contents(const std::string& filename)
|
||||
std::string file_contents(const std::string &filename)
|
||||
{
|
||||
std::ifstream ifs(filename);
|
||||
if (!ifs)
|
||||
throw std::runtime_error("Failed open file ");
|
||||
return std::string((std::istreambuf_iterator<char>(ifs)),
|
||||
(std::istreambuf_iterator<char>()));
|
||||
|
||||
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)
|
||||
@@ -33,12 +29,12 @@ std::size_t count_lines(const std::string& filename)
|
||||
|
||||
std::string line;
|
||||
size_t counter = 0;
|
||||
while(std::getline(ifs, line))
|
||||
while (std::getline(ifs, line))
|
||||
counter++;
|
||||
return counter;
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -47,10 +43,10 @@ std::size_t get_filesize(const std::string& filename)
|
||||
return static_cast<std::size_t>(ifs.tellg());
|
||||
}
|
||||
|
||||
|
||||
// source: https://stackoverflow.com/a/2072890/192001
|
||||
bool ends_with(std::string const & value, std::string const & ending)
|
||||
bool ends_with(std::string const &value, std::string const &ending)
|
||||
{
|
||||
if (ending.size() > value.size()) return false;
|
||||
if (ending.size() > value.size())
|
||||
return false;
|
||||
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
|
||||
}
|
||||
|
@@ -1,16 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include<cstddef>
|
||||
|
||||
std::size_t count_lines(const std::string& filename);
|
||||
std::size_t count_lines(const std::string &filename);
|
||||
|
||||
void prepare_logdir();
|
||||
|
||||
std::string file_contents(const std::string& filename);
|
||||
std::string file_contents(const std::string &filename);
|
||||
|
||||
std::size_t count_lines(const std::string& filename);
|
||||
std::size_t count_lines(const std::string &filename);
|
||||
|
||||
std::size_t get_filesize(const std::string& filename);
|
||||
std::size_t get_filesize(const std::string &filename);
|
||||
|
||||
bool ends_with(std::string const & value, std::string const & ending);
|
||||
bool ends_with(std::string const &value, std::string const &ending);
|
Reference in New Issue
Block a user