fixed issue #809

This commit is contained in:
Valera Cogut 2018-07-09 17:17:32 +03:00
parent d8437ac286
commit 20724f7ecc
6 changed files with 85 additions and 13 deletions

View file

@ -3,16 +3,19 @@
#include <fc/filesystem.hpp>
#include <fc/io/iostream.hpp>
#include <fstream>
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<const char>& buffer, size_t len, size_t offset);
void put( char c );

View file

@ -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();
}
}

View file

@ -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);

View file

@ -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);
}

View file

@ -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 )

72
tests/logging_tests.cpp Normal file
View file

@ -0,0 +1,72 @@
#include <boost/test/unit_test.hpp>
#include <boost/chrono.hpp>
#include <boost/thread/thread.hpp>
#include <fc/thread/thread.hpp>
#include <fc/reflect/reflect.hpp>
#include <fc/reflect/variant.hpp>
#include <fc/log/file_appender.hpp>
#include <fc/log/logger.hpp>
#include <fc/log/logger_config.hpp>
#include <fc/variant.hpp>
#include <fc/time.hpp>
#include <fc/io/json.hpp>
#include <fc/io/fstream.hpp>
#include <thread>
#include <iostream>
#include <fstream>
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()