From fd8df5b8202b02be8e10c9055da4e99ea87460a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=BDubom=C3=ADr=20Carik?= Date: Tue, 28 Feb 2017 00:59:23 +0100 Subject: [PATCH] Don't hardcode '.txt.' log file name suffix (resolve #333) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ľubomír Carik --- example/example.cpp | 4 +-- include/spdlog/details/spdlog_impl.h | 8 ++--- include/spdlog/sinks/file_sinks.h | 47 +++++++++++++--------------- include/spdlog/spdlog.h | 2 +- tests/file_log.cpp | 24 +++++++------- 5 files changed, 40 insertions(+), 45 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 89a3d077..4a9ffc6f 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -39,7 +39,7 @@ int main(int, char*[]) // Create basic file logger (not rotated) - auto my_logger = spd::basic_logger_mt("basic_logger", "logs/basic.txt"); + auto my_logger = spd::basic_logger_mt("basic_logger", "logs/basic"); my_logger->info("Some log message"); // Create a file rotating logger with 5mb size max and 3 rotated files @@ -106,7 +106,7 @@ void async_example() { size_t q_size = 4096; //queue size must be power of 2 spdlog::set_async_mode(q_size); - auto async_file = spd::daily_logger_st("async_file_logger", "logs/async_log.txt"); + auto async_file = spd::daily_logger_st("async_file_logger", "logs/async_log"); for (int i = 0; i < 100; ++i) async_file->info("Async message #{}", i); diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index a8011db4..79d3ac45 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -61,23 +61,23 @@ inline std::shared_ptr spdlog::basic_logger_st(const std::string // Create multi/single threaded rotating file logger inline std::shared_ptr spdlog::rotating_logger_mt(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files) { - return create(logger_name, filename, SPDLOG_FILENAME_T("txt"), max_file_size, max_files); + return create(logger_name, filename, max_file_size, max_files); } inline std::shared_ptr spdlog::rotating_logger_st(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files) { - return create(logger_name, filename, SPDLOG_FILENAME_T("txt"), max_file_size, max_files); + return create(logger_name, filename, max_file_size, max_files); } // Create file logger which creates new file at midnight): inline std::shared_ptr spdlog::daily_logger_mt(const std::string& logger_name, const filename_t& filename, int hour, int minute) { - return create(logger_name, filename, SPDLOG_FILENAME_T("txt"), hour, minute); + return create(logger_name, filename, hour, minute); } inline std::shared_ptr spdlog::daily_logger_st(const std::string& logger_name, const filename_t& filename, int hour, int minute) { - return create(logger_name, filename, SPDLOG_FILENAME_T("txt"), hour, minute); + return create(logger_name, filename, hour, minute); } diff --git a/include/spdlog/sinks/file_sinks.h b/include/spdlog/sinks/file_sinks.h index d40499b6..721a96df 100644 --- a/include/spdlog/sinks/file_sinks.h +++ b/include/spdlog/sinks/file_sinks.h @@ -64,16 +64,15 @@ template class rotating_file_sink : public base_sink < Mutex > { public: - rotating_file_sink(const filename_t &base_filename, const filename_t &extension, - std::size_t max_size, std::size_t max_files ) : + rotating_file_sink(const filename_t &base_filename, + std::size_t max_size, std::size_t max_files) : _base_filename(base_filename), - _extension(extension), _max_size(max_size), _max_files(max_files), _current_size(0), _file_helper() { - _file_helper.open(calc_filename(_base_filename, 0, _extension)); + _file_helper.open(calc_filename(_base_filename, 0)); _current_size = _file_helper.size(); //expensive. called only once } @@ -95,21 +94,21 @@ protected: } private: - static filename_t calc_filename(const filename_t& filename, std::size_t index, const filename_t& extension) + static filename_t calc_filename(const filename_t& filename, std::size_t index) { std::conditional::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w; if (index) - w.write(SPDLOG_FILENAME_T("{}.{}.{}"), filename, index, extension); + w.write(SPDLOG_FILENAME_T("{}.{}"), filename, index); else - w.write(SPDLOG_FILENAME_T("{}.{}"), filename, extension); + w.write(SPDLOG_FILENAME_T("{}"), filename); return w.str(); } // Rotate files: - // log.txt -> log.1.txt - // log.1.txt -> log2.txt - // log.2.txt -> log3.txt - // log.3.txt -> delete + // log.txt -> log.txt.1 + // log.txt.1 -> log.txt.2 + // log.txt.2 -> log.txt.3 + // lo3.txt.3 -> delete void _rotate() { @@ -117,8 +116,8 @@ private: _file_helper.close(); for (auto i = _max_files; i > 0; --i) { - filename_t src = calc_filename(_base_filename, i - 1, _extension); - filename_t target = calc_filename(_base_filename, i, _extension); + filename_t src = calc_filename(_base_filename, i - 1); + filename_t target = calc_filename(_base_filename, i); if (details::file_helper::file_exists(target)) { @@ -135,7 +134,6 @@ private: _file_helper.reopen(true); } filename_t _base_filename; - filename_t _extension; std::size_t _max_size; std::size_t _max_files; std::size_t _current_size; @@ -150,27 +148,27 @@ typedef rotating_file_sinkrotating_file_sink_st; */ struct default_daily_file_name_calculator { - // Create filename for the form basename.YYYY-MM-DD_hh-mm.extension - static filename_t calc_filename(const filename_t& basename, const filename_t& extension) + // Create filename for the form basename.YYYY-MM-DD_hh-mm + static filename_t calc_filename(const filename_t& basename) { std::tm tm = spdlog::details::os::localtime(); std::conditional::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w; - w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}.{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, extension); + w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min); return w.str(); } }; /* - * Generator of daily log file names in format basename.YYYY-MM-DD.extension + * Generator of daily log file names in format basename.YYYY-MM-DD */ struct dateonly_daily_file_name_calculator { - // Create filename for the form basename.YYYY-MM-DD.extension - static filename_t calc_filename(const filename_t& basename, const filename_t& extension) + // Create filename for the form basename.YYYY-MM-DD + static filename_t calc_filename(const filename_t& basename) { std::tm tm = spdlog::details::os::localtime(); std::conditional::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w; - w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}.{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, extension); + w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); return w.str(); } }; @@ -185,17 +183,15 @@ public: //create daily file sink which rotates on given time daily_file_sink( const filename_t& base_filename, - const filename_t& extension, int rotation_hour, int rotation_minute) : _base_filename(base_filename), - _extension(extension), _rotation_h(rotation_hour), _rotation_m(rotation_minute) { if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59) throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor"); _rotation_tp = _next_rotation_tp(); - _file_helper.open(FileNameCalc::calc_filename(_base_filename, _extension)); + _file_helper.open(FileNameCalc::calc_filename(_base_filename)); } void flush() override @@ -208,7 +204,7 @@ protected: { if (std::chrono::system_clock::now() >= _rotation_tp) { - _file_helper.open(FileNameCalc::calc_filename(_base_filename, _extension)); + _file_helper.open(FileNameCalc::calc_filename(_base_filename)); _rotation_tp = _next_rotation_tp(); } _file_helper.write(msg); @@ -231,7 +227,6 @@ private: } filename_t _base_filename; - filename_t _extension; int _rotation_h; int _rotation_m; std::chrono::system_clock::time_point _rotation_tp; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 6b93a7a1..a505b003 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -124,7 +124,7 @@ std::shared_ptr create(const std::string& logger_name, const It& sinks_b // Create and register a logger with templated sink type // Example: -// spdlog::create("mylog", "dailylog_filename", "txt"); +// spdlog::create("mylog", "dailylog_filename"); template std::shared_ptr create(const std::string& logger_name, Args...); diff --git a/tests/file_log.cpp b/tests/file_log.cpp index 6fa32fa2..45f6e8c1 100644 --- a/tests/file_log.cpp +++ b/tests/file_log.cpp @@ -7,7 +7,7 @@ TEST_CASE("simple_file_logger", "[simple_logger]]") { prepare_logdir(); - std::string filename = "logs/simple_log.txt"; + std::string filename = "logs/simple_log"; auto logger = spdlog::create("logger", filename); logger->set_pattern("%v"); @@ -24,7 +24,7 @@ TEST_CASE("simple_file_logger", "[simple_logger]]") TEST_CASE("flush_on", "[flush_on]]") { prepare_logdir(); - std::string filename = "logs/simple_log.txt"; + std::string filename = "logs/simple_log"; auto logger = spdlog::create("logger", filename); logger->set_pattern("%v"); @@ -50,7 +50,7 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]") logger->info("Test message {}", i); logger->flush(); - auto filename = basename + ".txt"; + auto filename = basename; REQUIRE(count_lines(filename) == 10); } @@ -64,14 +64,14 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]") logger->info("Test message {}", i); logger->flush(); - auto filename = basename + ".txt"; + auto filename = basename; REQUIRE(count_lines(filename) == 10); for (int i = 0; i < 1000; i++) logger->info("Test message {}", i); logger->flush(); REQUIRE(get_filesize(filename) <= 1024); - auto filename1 = basename + ".1.txt"; + auto filename1 = basename + ".1"; REQUIRE(get_filesize(filename1) <= 1024); } @@ -83,7 +83,7 @@ TEST_CASE("daily_logger", "[daily_logger]]") std::string basename = "logs/daily_log"; std::tm tm = spdlog::details::os::localtime(); fmt::MemoryWriter w; - w.write("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}.txt", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min); + w.write("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min); auto logger = spdlog::daily_logger_mt("logger", basename, 0, 0); logger->flush_on(spdlog::level::info); @@ -106,9 +106,9 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]") std::string basename = "logs/daily_dateonly"; std::tm tm = spdlog::details::os::localtime(); fmt::MemoryWriter w; - w.write("{}_{:04d}-{:02d}-{:02d}.txt", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); + w.write("{}_{:04d}-{:02d}-{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); - auto logger = spdlog::create("logger", basename, "txt", 0, 0); + auto logger = spdlog::create("logger", basename, 0, 0); for (int i = 0; i < 10; ++i) logger->info("Test message {}", i); logger->flush(); @@ -118,11 +118,11 @@ 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, const spdlog::filename_t& extension) + static spdlog::filename_t calc_filename(const spdlog::filename_t& basename) { std::tm tm = spdlog::details::os::localtime(); fmt::MemoryWriter w; - w.write("{}{:04d}{:02d}{:02d}.{}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, extension); + w.write("{}{:04d}{:02d}{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); return w.str(); } }; @@ -138,9 +138,9 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]") std::string basename = "logs/daily_dateonly"; std::tm tm = spdlog::details::os::localtime(); fmt::MemoryWriter w; - w.write("{}{:04d}{:02d}{:02d}.txt", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); + w.write("{}{:04d}{:02d}{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); - auto logger = spdlog::create("logger", basename, "txt", 0, 0); + auto logger = spdlog::create("logger", basename, 0, 0); for (int i = 0; i < 10; ++i) logger->info("Test message {}", i);