From 20724f7eccdf68aa755a6cad65c7f0781cd87849 Mon Sep 17 00:00:00 2001 From: Valera Cogut Date: Mon, 9 Jul 2018 17:17:32 +0300 Subject: [PATCH] fixed issue #809 --- include/fc/io/fstream.hpp | 9 +++-- src/filesystem.cpp | 2 +- src/io/fstream.cpp | 6 ++-- src/log/file_appender.cpp | 8 ++--- tests/CMakeLists.txt | 1 + tests/logging_tests.cpp | 72 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 tests/logging_tests.cpp diff --git a/include/fc/io/fstream.hpp b/include/fc/io/fstream.hpp index 8344ce9..481c051 100644 --- a/include/fc/io/fstream.hpp +++ b/include/fc/io/fstream.hpp @@ -3,16 +3,19 @@ #include #include +#include + +using namespace std; + namespace fc { class path; class ofstream : virtual public ostream { public: - enum mode { out, binary }; ofstream(); - ofstream( const fc::path& file, int m = binary ); + ofstream( const fc::path& file, std::ios_base::openmode m = std::ios_base::out | std::ios_base::binary ); ~ofstream(); - void open( const fc::path& file, int m = binary ); + void open( const fc::path& file, std::ios_base::openmode m = std::ios_base::out | std::ios_base::binary ); size_t writesome( const char* buf, size_t len ); size_t writesome(const std::shared_ptr& buffer, size_t len, size_t offset); void put( char c ); diff --git a/src/filesystem.cpp b/src/filesystem.cpp index fc200bf..eb3d685 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -364,7 +364,7 @@ namespace fc { } if (create) { - fc::ofstream ofs(*_path, fc::ofstream::out | fc::ofstream::binary); + fc::ofstream ofs(*_path, std::ios_base::out | std::ios_base::binary); ofs.close(); } } diff --git a/src/io/fstream.cpp b/src/io/fstream.cpp index a40bbeb..031026b 100644 --- a/src/io/fstream.cpp +++ b/src/io/fstream.cpp @@ -23,13 +23,13 @@ namespace fc { ofstream::ofstream() :my( new impl() ){} - ofstream::ofstream( const fc::path& file, int m ) + ofstream::ofstream( const fc::path& file, std::ios_base::openmode m ) :my( new impl() ) { this->open( file, m ); } ofstream::~ofstream(){} - void ofstream::open( const fc::path& file, int m ) { + void ofstream::open( const fc::path& file, std::ios_base::openmode m ) { const boost::filesystem::path& bfp = file; - my->ofs.open( bfp, std::ios::binary ); + my->ofs.open( bfp, std::ios_base::out | std::ios_base::binary | m ); } size_t ofstream::writesome( const char* buf, size_t len ) { my->ofs.write(buf,len); diff --git a/src/log/file_appender.cpp b/src/log/file_appender.cpp index 49ae357..8e5c44c 100644 --- a/src/log/file_appender.cpp +++ b/src/log/file_appender.cpp @@ -42,7 +42,7 @@ namespace fc { - _rotation_task = async( [this]() { rotate_files( true ); }, "rotate_files(1)" ); + _rotation_task = fc::async( [this]() { rotate_files( true ); }, "rotate_files(1)" ); } } @@ -83,11 +83,7 @@ namespace fc { out.close(); } remove_all(link_filename); // on windows, you can't delete the link while the underlying file is opened for writing - if( fc::exists( log_filename ) ) - out.open( log_filename, std::ios_base::out | std::ios_base::app ); - else - out.open( log_filename, std::ios_base::out | std::ios_base::app); - + out.open( log_filename, std::ios_base::out | std::ios_base::app ); create_hard_link(log_filename, link_filename); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d000013..153067e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -59,5 +59,6 @@ add_executable( all_tests all_tests.cpp time_test.cpp utf8_test.cpp variant_test.cpp + logging_tests.cpp ) target_link_libraries( all_tests fc ) diff --git a/tests/logging_tests.cpp b/tests/logging_tests.cpp new file mode 100644 index 0000000..659c80d --- /dev/null +++ b/tests/logging_tests.cpp @@ -0,0 +1,72 @@ +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +BOOST_AUTO_TEST_SUITE(logging_tests) + +BOOST_AUTO_TEST_CASE(log_reboot) +{ + BOOST_TEST_MESSAGE("Setting up logger"); + fc::file_appender::config conf; + conf.filename = "/tmp/my.log"; + conf.format = "${timestamp} ${thread_name} ${context} ${file}:${line} ${method} ${level}] ${message}"; + conf.flush = true; + conf.rotate = true; + conf.rotation_interval = fc::seconds(5); // rotate every 5 seconds + conf.rotation_limit = fc::seconds(20); // Don't keep files older than 20 seconds + conf.max_object_depth = 200; + + fc::appender::ptr fa = fc::appender::create("file", "file", fc::variant(conf, 200)); + + fc::path prev_log_filename = ""; + + BOOST_TEST_MESSAGE("Starting Loop"); + for(int i = 0; i < conf.rotation_limit.to_seconds(); i++) + { + fc::log_context ctx(fc::log_level::all, "my_file.cpp", i, "my_method()"); + fc::log_message my_log_message( ctx, "${message}", {"message","This is a test"} ); + fa->log(my_log_message); + + fc::time_point now = fc::time_point::now(); + int64_t interval_seconds = conf.rotation_interval.to_seconds(); + int64_t file_number = now.sec_since_epoch() / interval_seconds; + fc::time_point_sec start_time = fc::time_point_sec( (uint32_t)(file_number * interval_seconds) ); + fc::string timestamp_string = start_time.to_non_delimited_iso_string(); + fc::path link_filename = conf.filename; + fc::path log_filename = link_filename.parent_path() / (link_filename.filename().string() + "." + timestamp_string); + + if (prev_log_filename != log_filename) { + if (i > conf.rotation_interval.to_seconds()) { + std::string rez; + fc::read_file_contents(prev_log_filename, rez); + std::size_t found = rez.find("my_file.cpp:" + std::to_string(i - 1)); + BOOST_CHECK(found != std::string::npos); + + fc::read_file_contents(log_filename, rez); + found = rez.find("my_file.cpp:" + std::to_string(i)); + BOOST_CHECK(found != std::string::npos); + } + prev_log_filename = log_filename; + } + + fc::usleep(fc::seconds(1)); + } + BOOST_TEST_MESSAGE("Loop complete"); +} + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file