-
Notifications
You must be signed in to change notification settings - Fork 25
Description
Problem
Using TimedRollingFileTree we regularly – but not consistently – face this error:
[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: PathAccessException: Creation failed, path = '/var/mobile/Containers/Data/Application/7B8B2748-2BC0-4CA6-810B-88E170F2633B' (OS Error: Operation not permitted, errno = 1)
#0 _Directory.createSync (dart:io/directory_impl.dart:136:7)
#1 _Directory.createSync (dart:io/directory_impl.dart:131:16)
#2 _Directory.createSync (dart:io/directory_impl.dart:131:16)
#3 FimberFileTree._flushBuffer (package:fimber_io/src/file_log.dart:69:21)
#4 new FimberFileTree.<anonymous closure> (package:fimber_io/src/file_log.dart:47:13)
#5 _RootZone.runUnaryGuarded (dart:async/zone.dart:891:10)
#6 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:381:11)
#7 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:312:7)
The result is that the log file cannot be created and logs are lost.
Analysis
LogFileNameFormatter, which is used by TimedRollingFileTree, is the culprit. It's responsible for formatting/replacing the date/time part of the filename. However, if any other part of the log file path matches any of the date time tokens (e.g. 'DD' for the day part), it will be replaced as well. This is especially common and uncontrollable in the application identifier.
Example
Consider an app installation with the identifier 7B8BDD48-2BC0-4CA6-810B-88E170F2633B. The filenameFormat would be something like '/var/mobile/Containers/Data/Application/7B8BDD48-2BC0-4CA6-810B-88E170F2633B/Documents/logs/log_YYYYMMDD_HHmmSS.txt'.
The statement .replaceAll(_dayToken, dateTime.day.toString().padLeft(2, '0')) in would then replace the DD in "7B8BDD48-2BC0-4CA6-810B-88E170F2633B" with 27 (on today's date), resulting in an illegal application identifier ("7B8B2748-2BC0-4CA6-810B-88E170F2633B") and the above PathAccessException.
Solution
The prefix and postfix should probably not be part of the filenameFormat and appended after replacing all the date/time tokens in the format function.