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

Can't append file using text_file_backend #69

Closed
ercdude opened this issue Jan 5, 2019 · 9 comments
Closed

Can't append file using text_file_backend #69

ercdude opened this issue Jan 5, 2019 · 9 comments

Comments

@ercdude
Copy link

ercdude commented Jan 5, 2019

Hi! I was trying to use the boost log to write into a file and append to it when the application restarts (and not to create a new rotating file) with a basic text_file_backend like the following, but it seems that it doesnt work - which means that nothing changes when boostlog_keywords::open_mode = std::ios_base::app is added.

  typedef boostlog_sinks::synchronous_sink< boostlog_sinks::text_file_backend > file_sink;
  boost::shared_ptr< file_sink > sink(new file_sink(
      boostlog_keywords::auto_flush = true,
      boostlog_keywords::auto_flush = true,
      boostlog_keywords::file_name = "file-%m%d%Y_%H%M_%2N.log",
      boostlog_keywords::rotation_size = 5 * 1024 * 1024,
      boostlog_keywords::format = "[%TimeStamp%][%ThreadID%] *%Severity%* %Message%",
      boostlog_keywords::open_mode = std::ios_base::app
  ));

The full code is here.

@ercdude ercdude changed the title Can't append file using Can't append file using text_file_backend Jan 5, 2019
@Lastique
Copy link
Member

Lastique commented Jan 5, 2019

Appending will only work when the generated file name matches an existing file. This means that file names with timestamps will not work well with appending.

@Lastique Lastique closed this as completed Jan 5, 2019
@ercdude
Copy link
Author

ercdude commented Jan 5, 2019

even if it's in the same time? In the example, if I open the application twice in the same minute, two file logs (file-mmddYY_HHMM_00.log and file-mmddYY_HHMM_01.log) will be created and the file name is the same.

Or you mean that if using time (any, even if it's just the year) the append is ignored?

@Lastique
Copy link
Member

Lastique commented Jan 5, 2019

even if it's in the same time?

It's the file names what matters. In your example, the timestamps do match, but the counter value doesn't.

The counter value doesn't match because you call scan_for_files, which by default updates the starting counter value the sink uses to generate the file name. That call is intended to be used with a file collector, so that the older rotated log files are tracked. You're not using one.

@ercdude
Copy link
Author

ercdude commented Jan 5, 2019

but if I remove the scan_for_files, new file still will be created. But instead of xxx00.log it will be xxx.log000

@ercdude
Copy link
Author

ercdude commented Jan 5, 2019

oooh, okay... I have to remove the file collector. So it's impossible to append and collect at the same time, is that it?

@Lastique
Copy link
Member

Lastique commented Jan 5, 2019

Yes, given that the file name is generated only once, there's no practical way to ensure appending happens while file collecting is enabled. Either appending won't happen (because of differing file names) or there will be name clashes when the file is collected. I'm working on a solution, though, so hopefully this problem will be resolved.

@ercdude
Copy link
Author

ercdude commented Jan 5, 2019

Okay, thanks!

Maybe a disclaimer here would be nice. :) I don't know if/how I can upload a documentation ""fix"", otherwise I would add

@Lastique
Copy link
Member

Lastique commented Jan 5, 2019

If you're interested, you can try if e823f88 helps. With it, you can modify your initialization code like this:

typedef boostlog_sinks::synchronous_sink< boostlog_sinks::text_file_backend > file_sink;
boost::shared_ptr< file_sink > sink(new file_sink(
      boostlog_keywords::auto_flush = true,
      boostlog_keywords::file_name = "file.log",
      boostlog_keywords::target_file_name = "file-%m%d%Y_%H%M_%2N.log",
      boostlog_keywords::rotation_size = 5 * 1024 * 1024,
      boostlog_keywords::format = "[%TimeStamp%][%ThreadID%] *%Severity%* %Message%",
      boostlog_keywords::open_mode = std::ios_base::app
));

@ercdude
Copy link
Author

ercdude commented Jan 5, 2019

Nice, I'll check it out asap. Thanks!

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

No branches or pull requests

2 participants