简体   繁体   中英

boost::log with multi-process

I have asked a question about how to use boost::log in multi-process How to use Boost::log not to rewrite the log file?

That answer can solve most part of the problem. But in some rare case, when one process is writing log and another process is starting to write log,

13548:Tue Nov 30 17:33:41 2021
12592:Tue Nov 30 17:33:41 2021
13548:Tue Nov 30 17:33:41 2021
12592:Tue Nov 30 17:33:4572:Tue Nov 30 17:33:41 2021
17196:Tue Nov 30 17:33:41 2021
8572:Tue Nov 30 17:33:41 2021
17196:Tue Nov 30 17:33:41 2021
8572:Tue Nov 30 17:33:41 2021

The fourth line 17:33:4(1 2021) the rear part was erased with (8)572:Tue Nov which was written by the other process.

How do I prevent this?

Boost.Log does not synchronize multiple processes writing to the same file. You must have a single process that writes logs to the file, while other processes passing their log records to the writer. There are multiple ways to achieve this. For example, you can pass your logs to a syslog service or writing your own log writer process. In the latter case, you can use inter-process queue to pass log messages between processes.

Alternatively, you can write your own sink backend that will perform synchronization when writing to a shared file. However, I suspect the performance will be lower than having a single log writer process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM