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

Enable reopening log files #595

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bhjortsberg
Copy link

@bhjortsberg bhjortsberg commented Dec 8, 2017

Once a Logger is configured the application keeps the file open. This is due to that LogStreamReferenceMap is keeping a reference to the FileStream object and unregistering the Logger will never close the file.

Switching to weak_ptr in LogStreamReferenceMap i.e. not holding a reference makes unregistering logger close the file if no other reference to FileStream object exist.

The use case is for logrotation where logrotation removes the logfile and sends a SIGHUP to the application. This fix makes it possible to reconfigure the logger to reopen the log file. I added a sample to illustrate the use case.

This is a

  • Breaking change
  • New feature
  • Bugfix

I have

@bhjortsberg
Copy link
Author

Apparently unit test fails, I haven't looked at them yet. Any comment on this change, does it break the architecture? Or is it just the tests that need to be updated?

@bhjortsberg
Copy link
Author

Tests passes when I run them locally with my latest update.

Travis CI build seem to have some problem with cmake when building googletest.

LogStreamReferenceMap was keeping a reference to FileStream
which kept the file open even though Logger was unregistred.
@BrainStone
Copy link

I would love to see this added, as without using the linux logrotate utility is impossible, as that relies on the program being able to close and reopen the log file.

if (!needNewStream && filestreamIter->second.lock() == nullptr) {
// Filestream is expired and exists in map
needNewStream = true;
m_logStreamsReference->erase(filestreamIter);

Choose a reason for hiding this comment

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

There may still be some remaining elements in map that can't be removed

Choose a reason for hiding this comment

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

I don't get that can't be removed.
This erase only one expired file stream.
If it is not expired, why remove?

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.

None yet

4 participants