SDK logs and debugging
SDK Logs
What are the SDK Logs?
The SDK logs is a zip file containning 2 kinds of file:
- Bracelet logs: file with technical informations coming from the firmware like commands received / sent, connection status, firmware actions
- SDK logs: file with technical informations coming from the SDK like the actions performed by the SDK, the BLE scan / communication
With these files the Cosano team will be able to investiguate any situation.
The log files available are today's and yesterday's only.
We will only be able investiguate a situation in this range
How to implement it
Here is how to add the export of the logs to your application:
First, enable log writing. You can do this directly in the AppDelegate when initializing the app. Refer to the Log.swift file in the sample app for an example.
All logs generated by the SDK and the firmware will be automatically written to files.
Then you can add an option in your app to generate a .zip file and export it (share option)
Full sample code: (available in BraceletStatusView.swift into the sample app)
func shareDebugFolder() {
let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
// Create archive
var filePathArray: [String] = [String]()
var dirs: [String]? = nil
do {
dirs = try FileManager.default.contentsOfDirectory(
atPath: URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("Debug").path)
} catch {
}
(dirs as NSArray?)?.enumerateObjects({ obj, idx, stop in
let filename = obj as? String
let path = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("Debug/\(filename ?? "")").path
filePathArray.append(path)
})
let pathArchive = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("debug.zip").path
let pathDirectory = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("Debug").path
do {
let documentsURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let docs = try FileManager.default.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
let files = docs.filter{ $0.pathExtension == "realm" }
for txtFilePath in files{
//filePathArray.append(txtFilePath.path)
}
SSZipArchive.createZipFile(atPath: pathArchive, withContentsOfDirectory: pathDirectory, keepParentDirectory: true, compressionLevel: 1, password: "cor2771_log", aes: false, progressHandler: nil)
} catch {
}
var activityItems: [AnyHashable] = []
activityItems.append("Logs")
let filePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("debug.zip").path
if FileManager.default.fileExists(atPath: filePath) {
let activityItemsToSend = ["Logs", URL(fileURLWithPath: filePath)] as [Any]
let fileURL = NSURL(fileURLWithPath: filePath)
// Create the Array which includes the files you want to share
var filesToShare = [Any]()
// Add the path of the file to the Array
filesToShare.append(fileURL)
let shareActivity = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)
shareActivity.setValue("Logs", forKey: "subject")
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .normal)
UINavigationBar.appearance().tintColor = UIColor.black
// Re-setting the default color for the bar buttons again on activity's completion
shareActivity.completionWithItemsHandler = { (activityType, completed:Bool, returnedItems:[Any]?, error: Error?) in
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .normal)
UINavigationBar.appearance().tintColor = UIColor.black
}
if var topController = keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
topController.present(shareActivity, animated: true, completion: nil)
}
} else {
let alert = UIAlertController(title: "Warning", message: "This file is not available yet for download. Please retry in few minutes.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
if var topController = keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
topController.present(alert, animated: true, completion: nil)
}
}
}
This will create a .zip file with all the technical information that we would need for the support and show the share feature from iOS to let you send this zip file where you want.
The logs contains a maximum of 5 days of hiytory.
Please send it to us to Matthias Burel's email: mburel@corsano.com