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

Pwm1234/rotate on open #958

Merged
merged 7 commits into from
Jan 24, 2019
Merged

Pwm1234/rotate on open #958

merged 7 commits into from
Jan 24, 2019

Conversation

pwm1234
Copy link

@pwm1234 pwm1234 commented Jan 11, 2019

add argument to rotating file sink for rotate_on_open

This change adds logic to the rotating_file_sink constructor so the caller can control whether or not the log file is rotated at initialization. This change provides new functionality when the optional argument rotate_on_open is true the log file will be rotated first, so the current log begins in a new file. An old log file will be lost only if the maximum number has already been reached.

This is a refresh of PR #823.

@pwm1234
Copy link
Author

pwm1234 commented Jan 23, 2019

Is this pull request acceptable to be merged into the main v1.x branch?

@gabime
Copy link
Owner

gabime commented Jan 24, 2019

There was a review comment that i think is improtant to fix

@pwm1234
Copy link
Author

pwm1234 commented Jan 24, 2019

I am sorry, but I am not sure what review comment needs to be fixed. Can give me a link?

@@ -31,12 +31,14 @@ template<typename Mutex>
class rotating_file_sink final : public base_sink<Mutex>
{
public:
rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files)
rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open=false)
: base_filename_(std::move(base_filename))
, max_size_(max_size)
, max_files_(max_files)
{
file_helper_.open(calc_filename(base_filename_, 0));
Copy link
Owner

Choose a reason for hiding this comment

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

can be optimized. why open and then immediately close in rotate?

@pwm1234
Copy link
Author

pwm1234 commented Jan 24, 2019

I tried to address your comment. The optimization did introduce a little more complexity, but I think the changes are reasonable. I also updated to the latest v1.x commit. Are there other changes you want in this PR?

@gabime
Copy link
Owner

gabime commented Jan 24, 2019

The optimization did introduce a little more complexity,

I was thinking about something simpler (without modifying rotate_()):

   rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open=false)
        : base_filename_(std::move(base_filename))
        , max_size_(max_size)
        , max_files_(max_files)
    {
        if (rotate_on_open)
        {
            rotate_();
            current_size_ = 0;  
        }
        else
        {
           file_helper_.open(calc_filename(base_filename_, 0));
           current_size_ = file_helper_.size(); // expensive. called only once
        }
    }

@pwm1234
Copy link
Author

pwm1234 commented Jan 24, 2019

But the modification to rotate() is required because file_helper_.reopen(true) is called in rotate without first opening the file_helper_; this throws an exception. I only understand two choices either: 1) leave out the optimization or 2) introduce the complexity so we can avoid the call to reopen when open has not yet been called.

@gabime
Copy link
Owner

gabime commented Jan 24, 2019

Good point. Let's leave out the optimization then. Maybe later we'll find a way to optimize without complicating the rotate_() too much.

@pwm1234
Copy link
Author

pwm1234 commented Jan 24, 2019

I agree that the minor optimization is not worth the complexity. I made a commit that removes the undesirable complexity with rotate_(). This final(?) change avoids fixes an error by not rotating an empty file. Please let me know if you have any other changes for this PR. If not, I look forward to having this PR be part of spdlog. Thank you for your help and for making spdlog available.

@gabime gabime merged commit 2463fe9 into gabime:v1.x Jan 24, 2019
@gabime
Copy link
Owner

gabime commented Jan 24, 2019

Merged. Thanks @pwm1234

bachittle pushed a commit to bachittle/spdlog that referenced this pull request Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants