Skip to main content

Export Logs

Export retained file or XLFacility logs and share the generated file URLs from your application.

Prerequisites

  • The active destination is .file or .xlfacility.
  • The XLFacility plugin is installed when .xlfacility is selected.
  • At least one retained log is available.

API Reference

Framework

AIBudsLog.xcframework

Declaration

Swift
/// Exports all retained logs to one file.
/// - Parameter completion: Called with the exported path, optional beginning
///   and ending timestamps, and an error.
func exportLogs(_ completion: ((
                    _ filePath: String?,
                    _ logStartFrom: Date?,
                    _ logEndAt: Date?,
                    _ error: NSError?
                ) -> Void)?)

/// Exports retained logs to size-limited files.
/// - Parameters:
///   - maxFileSize: Maximum size of each exported file, in bytes.
///   - completion: Called with exported paths, optional beginning and ending
///     timestamps, and an error.
func exportLogs(_ maxFileSize: UInt64,
                   completion: ((
                       _ filePaths: [String]?,
                       _ logStartFrom: Date?,
                       _ logEndAt: Date?,
                       _ error: NSError?
                   ) -> Void)?)

Both operations are defined by LogService and invoked on logService.

Usage Examples

Export One File

Swift
AIBudsLogSDK.logService.exportLogs { filePath, startDate, endDate, error in
    DispatchQueue.main.async {
        if let error {
            print("Export failed: \(error.localizedDescription)")
            return
        }

        guard let filePath else {
            print("Export completed without a file")
            return
        }

        print("Log range: \(String(describing: startDate)) — \(String(describing: endDate))")
        presentShareSheet(items: [URL(fileURLWithPath: filePath)])
    }
}

Export Size-Limited Files

Swift
let fiveMegabytes: UInt64 = 5 * 1_000_000
AIBudsLogSDK.logService.exportLogs(fiveMegabytes) { paths, _, _, error in
    guard error == nil, let paths, !paths.isEmpty else { return }
    let URLs = paths.map(URL.init(fileURLWithPath:))
    DispatchQueue.main.async { presentShareSheet(items: URLs) }
}

Notes

  • maxFileSize is expressed in bytes. The Demo converts decimal megabytes using 1_000_000 bytes per MB.
  • Export is unsupported for .console and .oslogger; switch to a persistent destination before collecting logs.
  • Treat an error as failure even when paths are returned, and treat an empty path collection as no export artifact.
  • Present UIActivityViewController on the main thread and configure its popover source on iPad.