Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log file of current hour gets overwritten by default #809 #56

Merged
merged 2 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/fc/io/fstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
#include <fc/shared_ptr.hpp>
#include <fc/filesystem.hpp>
#include <fc/io/iostream.hpp>
#include <fstream>

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 );
Expand Down
2 changes: 1 addition & 1 deletion src/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/io/fstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/fstream.hpp>

using namespace std;

namespace fc {
class ofstream::impl : public fc::retainable {
public:
Expand All @@ -23,13 +25,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);
Expand Down
8 changes: 2 additions & 6 deletions src/log/file_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)" );
}
}

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

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 72 additions & 0 deletions tests/logging_tests.cpp
Original file line number Diff line number Diff line change
@@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this could be simplified if we added a member to the file_appender that is the current filename.

@abitmore what do you think? Is such a change advisable? Is it worth it? Would it be considered "scope creep"?

Advantage: Easier testing, elimination of duplicate code.
Disadvantage: It could encourage someone to open the file and add their own information in the log.
Disadvantage: This test would have to cast the fc::appender::ptr to a file_appender to get the value.
Disadvantage: By the time the test gets the filename, it could be stale due to the multithreaded nature of this appender. Although the code above suffers from the same problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't have an idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I do this change @jmjatlanta ? guys what do you think ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disadvantage: By the time the test gets the filename, it could be stale due to the multithreaded nature of this appender. Although the code above suffers from the same problem. - Why current test code has the same problem ? Doesn't this test code run in it's own thread to check how appender works ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so should I change this snippet of code or not ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmjatlanta should I change that code or not ? is this issue finished or not yet ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think changing the code is worth it, as the problem will exist regardless. There is a very slim chance that the test will fail, but I doubt anyone will run into it. I will review everything again. If the chance of failure is there, perhaps we should document it as a comment within the test.


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