forked from steemit/fc
-
Notifications
You must be signed in to change notification settings - Fork 90
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.