Last active
September 26, 2019 14:25
-
-
Save Pranit-Harekar/db25eafbb94cd616ab6af63eab6df621 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private let datadogQueue: DispatchQueue = DispatchQueue(label: "foo.datadog") | |
class Logger { | |
fileprivate var buffer = [NSDictionary]() | |
fileprivate var backgroundTaskIdentifier: UIBackgroundTaskIdentifier = UIBackgroundTaskIdentifier.invalid | |
fileprivate var observer: NSObjectProtocol? | |
// ... | |
init() { | |
// ... | |
// This ensures when the user quits the app or puts in background we send the logs to server | |
observer = NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "UIApplicationWillResignActiveNotification"), object: nil, queue: nil, using: { | |
[unowned self] note in | |
let tmpbuffer = self.buffer | |
self.buffer = [NSDictionary]() | |
self.backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "saveDatadogLogRecords", | |
expirationHandler: { | |
self.endBackgroundTask() | |
}) | |
self.sendLogsInBuffer(buffer: tmpbuffer) | |
}) | |
} | |
deinit { | |
if let observer = observer { | |
NotificationCenter.default.removeObserver(observer) | |
} | |
} | |
func log<T>( _ message: @autoclosure () -> T, level: LogLevel, filename: String, line: Int) { | |
// logic to convert given parameters to a dictionary | |
addLogToBuffer(dict) | |
} | |
private func addLogToBuffer(_ dict: NSDictionary) { | |
datadogQueue.async { | |
self.buffer.append(dict) | |
if self.buffer.count > DatadogConfig.maxEntriesInBuffer { // DatadogConfig.maxEntriesInBuffer = 25 entries | |
let tmpbuffer = self.buffer | |
self.buffer = [NSDictionary]() | |
self.sendLogsInBuffer(buffer: tmpbuffer) | |
} | |
} | |
} | |
private func sendLogsInBuffer(buffer: [NSDictionary]) { | |
// logic to send logs to server | |
} | |
private func endBackgroundTask() { | |
if self.backgroundTaskIdentifier != UIBackgroundTaskIdentifier.invalid { | |
let value = UIBackgroundTaskIdentifier(rawValue: self.backgroundTaskIdentifier.rawValue) | |
UIApplication.shared.endBackgroundTask(value) | |
self.backgroundTaskIdentifier = UIBackgroundTaskIdentifier.invalid | |
print("Ending background task") | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Crashed: slimlogger.datadog | |
0 libobjc.A.dylib 0x18339ebe8 objc_retain + 8 | |
1 libswiftCore.dylib 0x1b1aa6c78 swift::metadataimpl::ValueWitnesses<swift::metadataimpl::ObjCRetainableBox>::initializeWithCopy(swift::OpaqueValue*, swift::OpaqueValue*, swift::TargetMetadata<swift::InProcess> const*) + 24 | |
2 libswiftCore.dylib 0x1b1a9af50 swift_arrayInitWithCopy + 120 | |
3 SpringboardRetail 0x10045f814 specialized _ArrayBuffer._copyContents(subRange:initializing:) (<compiler-generated>) | |
4 SpringboardRetail 0x10045eeb0 specialized _ArrayBufferProtocol._arrayOutOfPlaceUpdate(_:_:_:_:) (<compiler-generated>) | |
5 SpringboardRetail 0x10045ecb0 specialized Array._copyToNewBuffer(oldCount:) (<compiler-generated>) | |
6 SpringboardRetail 0x10045d16c closure #1 in SlimDatadogDestination.addLogToBuffer(_:) (<compiler-generated>) | |
7 SpringboardRetail 0x1004607b0 thunk for @escaping @callee_guaranteed () -> () (<compiler-generated>) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment