Search code examples
c++boostboost-logboost-logging

exception of boostlog when date changed to next day


I use boost log by this config.

[Sinks.2]
Filter="%Severity% >= 2"
Destination=TextFile
AutoFlush=true   
Format="[%TimeStamp%] [%ThreadID%] <%Severity%> %Message%"
Asynchronous=false                                        

Target="logs"                                              
FileName="logs/quo.%Y%m%dT%H%M%S.%a.%5N.log.detail"
RotationTimePoint="00:00:00"                               
RotationSize=104857600                                     
MinFreeSpace=4294967296                                    
MaxSize=4294967296                                         
ScanForFiles=All

when date change to next day. my program crash by exception: terminate called after throwing an instance of

'boost::filesystem::filesystem_error'
  what():  boost::filesystem::last_write_time: No such file or directory: "/root/work/hy-trade/bin/debug/logs/quo.20181027T173106.Sat.00000.log.detail"

I check my disk space, find the free space less than MinFreeSpace in config and the file quo.20181027T173106.Sat.00000.log.detail not exists.

how to avoid this exception?

version of boost is 1.67

thank you


Solution

  • It looks like someone had already deleted the log file before it was rotated. It may have been an external process, or Boost.Log.

    With Boost.Log, this can happen if you have multiple file sinks that write log files into the same directory, which is also used as the target directory for the rotated files (i.e. the FileName parameter includes the path specified in the Target parameter, and there are multiple sinks that use that path). The problem is because, according to ScanForFiles=All, the library scans the target directory for any files but does not update the file counter to be used for creating new files. This means that if the file "quo.20181027T173106.Sat.00000.log.detail" was present in that directory when your process started then it would be considered as an old file, even if upon starting your process would be still writing new logs to that file. Then, when a file rotation happens and storage limits are exceeded (e.g. if MinFreeSpace is not satisfied), that file may be deleted. The rotation has to happen on another sink that still stores files into the same "logs" directory.

    To solve the problem you can do one of the following:

    • Use ScanForFiles=Matching in your settings so that the file counter is updated after scanning. This will make sure that new log files have unique names and don't get deleted prematurely.
    • Write log files to a different directory from your target storage. I.e. specify FileName so that it doesn't point to the same directory as Target.

    Also, you may want to add exception handling to avoid crashing in case of errors (which may still happen for whatever reason on filesystem operations). See here and here for more info (also, follow the links in those sections).