Commit 032035e7 authored by gabime's avatar gabime

format

parent 66b5772b
......@@ -18,22 +18,26 @@
// Create a file logger which creates new files with a specified time step and fixed file size:
//
// std::shared_ptr<logger> step_logger_mt(const std::string &logger_name, const filename_t &filename, unsigned seconds = 60, const
// filename_t &tmp_ext = ".tmp", unsigned max_file_size = std::numeric_limits<unsigned>::max(), bool delete_empty_files = true, const filename_t &file_header = ""); std::shared_ptr<logger>
// step_logger_st(const std::string &logger_name, const filename_t &filename, unsigned seconds = 60, const filename_t &tmp_ext = ".tmp",
// unsigned max_file_size = std::numeric_limits<unsigned>::max());
// filename_t &tmp_ext = ".tmp", unsigned max_file_size = std::numeric_limits<unsigned>::max(), bool delete_empty_files = true, const
// filename_t &file_header = ""); std::shared_ptr<logger> step_logger_st(const std::string &logger_name, const filename_t &filename,
// unsigned seconds = 60, const filename_t &tmp_ext = ".tmp", unsigned max_file_size = std::numeric_limits<unsigned>::max());
// Example for spdlog_impl.h
// Create a file logger that creates new files with a specified increment
// inline std::shared_ptr<spdlog::logger> spdlog::step_logger_mt(
// const std::string &logger_name, const filename_t &filename_fmt, unsigned seconds, const filename_t &tmp_ext, unsigned max_file_size, bool delete_empty_files, const filename_t &file_header)
// const std::string &logger_name, const filename_t &filename_fmt, unsigned seconds, const filename_t &tmp_ext, unsigned max_file_size,
// bool delete_empty_files, const filename_t &file_header)
// {
// return create<spdlog::sinks::step_file_sink_mt>(logger_name, filename_fmt, seconds, tmp_ext, max_file_size, delete_empty_files, file_header);
// return create<spdlog::sinks::step_file_sink_mt>(logger_name, filename_fmt, seconds, tmp_ext, max_file_size, delete_empty_files,
// file_header);
// }
// inline std::shared_ptr<spdlog::logger> spdlog::step_logger_st(
// const std::string &logger_name, const filename_t &filename_fmt, unsigned seconds, const filename_t &tmp_ext, unsigned max_file_size, bool delete_empty_files, const filename_t &file_header)
// const std::string &logger_name, const filename_t &filename_fmt, unsigned seconds, const filename_t &tmp_ext, unsigned max_file_size,
// bool delete_empty_files, const filename_t &file_header)
// {
// return create<spdlog::sinks::step_file_sink_st>(logger_name, filename_fmt, seconds, tmp_ext, max_file_size, delete_empty_files, file_header);
// return create<spdlog::sinks::step_file_sink_st>(logger_name, filename_fmt, seconds, tmp_ext, max_file_size, delete_empty_files,
// file_header);
// }
namespace spdlog {
......@@ -64,7 +68,8 @@ template<class Mutex, class FileNameCalc = default_step_file_name_calculator>
class step_file_sink SPDLOG_FINAL : public base_sink<Mutex>
{
public:
step_file_sink(filename_t base_filename, unsigned step_seconds, filename_t tmp_ext, unsigned max_size, bool delete_empty_files, filename_t file_header)
step_file_sink(filename_t base_filename, unsigned step_seconds, filename_t tmp_ext, unsigned max_size, bool delete_empty_files,
filename_t file_header)
: _base_filename(std::move(base_filename))
, _tmp_ext(std::move(tmp_ext))
, _step_seconds(step_seconds)
......@@ -102,7 +107,8 @@ public:
if (!_current_size)
{
_current_size += _file_header.formatted.size();
if (_current_size) _file_helper.write(_file_header);
if (_current_size)
_file_helper.write(_file_header);
}
}
......@@ -126,9 +132,9 @@ protected:
{
filename_t new_filename;
std::tie(new_filename, std::ignore) = FileNameCalc::calc_filename(_base_filename, _tmp_ext);
bool change_occured = !details::file_helper::file_exists(new_filename);
if (change_occured)
if (change_occured)
{
close_current_file();
......@@ -142,7 +148,8 @@ protected:
if (change_occured)
{
_current_size = _file_header.formatted.size();
if (_current_size) _file_helper.write(_file_header);
if (_current_size)
_file_helper.write(_file_header);
}
}
......@@ -182,7 +189,8 @@ private:
if (details::file_helper::file_exists(_current_filename) && details::os::rename(_current_filename, target) != 0)
{
throw spdlog_ex("step_file_sink: failed renaming " + filename_to_str(_current_filename) + " to " + filename_to_str(target), errno);
throw spdlog_ex(
"step_file_sink: failed renaming " + filename_to_str(_current_filename) + " to " + filename_to_str(target), errno);
}
}
......
This diff is collapsed.
......@@ -21,8 +21,8 @@ inline spdlog::async_logger::async_logger(const std::string &logger_name, const
const async_overflow_policy overflow_policy, const std::function<void()> &worker_warmup_cb,
const std::chrono::milliseconds &flush_interval_ms, const std::function<void()> &worker_teardown_cb)
: logger(logger_name, begin, end)
, _async_log_helper(new details::async_log_helper(
logger_name, _formatter, _sinks, queue_size, _err_handler, overflow_policy, worker_warmup_cb, flush_interval_ms, worker_teardown_cb))
, _async_log_helper(new details::async_log_helper(logger_name, _formatter, _sinks, queue_size, _err_handler, overflow_policy,
worker_warmup_cb, flush_interval_ms, worker_teardown_cb))
{
}
......
......@@ -84,7 +84,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *msg)
log_msg.raw << msg;
_sink_it(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
SPDLOG_CATCH_AND_HANDLE
}
template<typename T>
......@@ -100,7 +100,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const T &msg)
log_msg.raw << msg;
_sink_it(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
SPDLOG_CATCH_AND_HANDLE
}
template<typename Arg1, typename... Args>
......@@ -307,14 +307,14 @@ inline void spdlog::logger::_set_formatter(formatter_ptr msg_formatter)
inline void spdlog::logger::flush()
{
try
{
for (auto &sink : _sinks)
{
sink->flush();
}
}
SPDLOG_CATCH_AND_HANDLE
try
{
for (auto &sink : _sinks)
{
sink->flush();
}
}
SPDLOG_CATCH_AND_HANDLE
}
inline void spdlog::logger::_default_err_handler(const std::string &msg)
......
......@@ -12,10 +12,10 @@ class failing_sink : public spdlog::sinks::sink
throw std::runtime_error("some error happened during log");
}
void flush() override
{
throw std::runtime_error("some error happened during flush");
}
void flush() override
{
throw std::runtime_error("some error happened during flush");
}
};
using namespace std;
TEST_CASE("default_error_handler", "[errors]]")
......@@ -67,10 +67,10 @@ TEST_CASE("default_error_handler2", "[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(); });
REQUIRE_THROWS_AS(logger->flush(), custom_ex);
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->flush(), custom_ex);
}
TEST_CASE("async_error_handler", "[errors]]")
......@@ -115,10 +115,10 @@ TEST_CASE("async_error_handler2", "[errors]]")
throw std::runtime_error("Failed open logs/custom_err2.txt");
ofs << err_msg;
});
logger->info("Hello failure");
logger->info("Hello failure");
spdlog::drop("failed_logger"); // force logger to drain the queue and shutdown
spdlog::set_sync_mode();
logger.reset();
logger.reset();
}
REQUIRE(file_contents("logs/custom_err2.txt") == err_msg);
......
......@@ -70,7 +70,7 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
{
prepare_logdir();
size_t max_size = 10 * 1024;
size_t max_size = 10 * 1024;
std::string basename = "logs/rotating_log.txt";
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 1);
for (int i = 0; i < 10; ++i)
......@@ -90,7 +90,7 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
logger->flush();
REQUIRE(get_filesize(filename) <= max_size);
auto filename1 = "logs/rotating_log.1.txt";
auto filename1 = "logs/rotating_log.1.txt";
REQUIRE(get_filesize(filename1) <= max_size);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment