DaveWoodCom / XCGLogger

A debug log framework for use in Swift projects. Allows you to log details to the console (and optionally a file), just like you would have with NSLog() or print(), but with additional information, such as the date, function name, filename and line number.
MIT License
3.93k stars 477 forks source link

AutoRotatingFileDestination Crash some times #278

Open Zeropointer11 opened 5 years ago

Zeropointer11 commented 5 years ago

Hi i use the XCGLogger Version 6.1.0 github "DaveWoodCom/XCGLogger" "6.1.0" and get sometimes the File Rotataion Crash at syncronizeFile() may be you will add a Lock on the File Handler to fix it?

private func closeFile() { logFileHandle?.synchronizeFile() logFileHandle?.closeFile() logFileHandle = nil }

Here is the Crash Report

` Application Specific Information: Terminating app due to uncaught exception 'NSFileHandleOperationException', reason: ' -[NSConcreteFileHandle synchronizeFile]: No such file or directory'

Last Exception Backtrace: 0 CoreFoundation 0x00000001948c4ec4 __exceptionPreprocess + 228 1 libobjc.A.dylib 0x0000000193a95a40 objc_exception_throw + 52 2 CoreFoundation 0x00000001947cb594 +[NSException raise:format:] + 112 3 Foundation 0x0000000195318610 NSFileHandleRaiseOperationException + 156 4 XCGLogger 0x000000010d25a38c XCGLogger.FileDestination.rotateFile(to: Any, closure: (Swift.Bool) -> ()?) -> Swift.Bool (FileDestination.swift:137) 5 XCGLogger 0x000000010d249478 XCGLogger.AutoRotatingFileDestination.rotateFile() -> () (AutoRotatingFileDestination.swift:237) 6 XCGLogger 0x000000010d249768 XCGLogger.AutoRotatingFileDestination.write(message: Swift.String) -> () (AutoRotatingFileDestination.swift:283) 7 XCGLogger 0x000000010d23902c closure #1 () -> () in XCGLogger.BaseQueuedDestination.output(logDetails: XCGLogger.LogDetails, message: Swift.String) -> () (BaseQueuedDestination.swift:41) 8 XCGLogger 0x000000010d238bcc XCGLogger.BaseQueuedDestination.output(logDetails: XCGLogger.LogDetails, message: Swift.String) -> () (BaseQueuedDestination.swift:48) 9 XCGLogger 0x000000010d23ea8c XCGLogger.BaseDestination.process(logDetails: XCGLogger.LogDetails) -> () (BaseDestination.swift:124) 10 XCGLogger 0x000000010d2400c4 protocol witness for XCGLogger.DestinationProtocol.process(logDetails: XCGLogger.LogDetails) -> () in conformance XCGLogger.BaseDestination : XCGLogger.DestinationProtocol in XCGLogger (:0) 11 XCGLogger 0x000000010d25dff8 XCGLogger.XCGLogger.logln(: XCGLogger.XCGLogger.Level, functionName: Swift.String, fileName: Swift.String, lineNumber: Swift.Int, userInfo: [Swift.String : Any], closure: () -> Any?) -> () (XCGLogger.swift:335) 12 XCGLogger 0x000000010d25dc7c XCGLogger.XCGLogger.logln(: XCGLogger.XCGLogger.Level, functionName: Swift.StaticString, fileName: Swift.StaticString, lineNumber: Swift.Int, userInfo: [Swift.String : Any], closure: () -> Any?) -> () (XCGLogger.swift:313) 13 XCGLogger 0x000000010d25f9d0 merged XCGLogger.XCGLogger.verbose(: @autoclosure () -> Any?, functionName: Swift.StaticString, fileName: Swift.StaticString, lineNumber: Swift.Int, userInfo: [Swift.String : Any]) -> () + 144 14 XCGLogger 0x000000010d25f2ac XCGLogger.XCGLogger.warning(_: @autoclosure () -> Any?, functionName: Swift.StaticString, fileName: Swift.StaticString, lineNumber: Swift.Int, userInfo: [Swift.String : Any]) -> () + 28 15 Ticketing-Offline 0x00000001031135b4 generic specialization of closure #3 (Alamofire.DefaultDownloadResponse) -> () in closure #1 (RxSwift.AnyObserver) -> RxSwift.Disposable in TicketingOffline.TSAPIService.download(: Swift.String, destinationPath: Swift.String, method: Alamofire.HTTPMethod, parameters: [Swift.String : Any]?, headers: [Swift.String : Swift.String]?) -> RxSwift.Observable (TSAPIService.swift:390) `

ehmjaysee commented 5 years ago

Hi, are you configuring your log to use background processing? i.e. fileDestination.logQueue = XCGLogger.logQueue

If yes then you may be hitting the same issue I reported here: https://github.com/DaveWoodCom/XCGLogger/issues/260

When you enable background processing a crash can occur if there are any messages waiting in the log message queue when the rotation happens. The rotation code will close the old file and open a new file. Later on the background thread runs and tries to write log messages to the old file which was just closed by the rotation logic. Writing to a stale file ID causes the crash.

Zeropointer11 commented 5 years ago

Hi, ehmjaysee yes i am using the logQueue for processing it in Background. But i have add it to fix the AutoRotate crashes. For a smal amount of time it looks like the crash was fixed but it was not. When i did not set the logQueue it Crashs on every file Rotation.