メインコンテンツまでスキップ

ログのエクスポート

File または XLFacility に保存されたログをエクスポートし、生成されたファイル URL をアプリから共有します。

前提条件

  • 現在の出力先が .file または .xlfacility であること。
  • .xlfacility を選択する場合は、XLFacility プラグインがインストールされていること。
  • 保存済みログが 1 件以上あること。

API Reference

フレームワーク

AIBudsLog.xcframework

宣言

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)?)

どちらの操作も LogService で定義され、logService から呼び出します。

使用例

1 つのファイルへエクスポート

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)])
    }
}

サイズ制限付きファイルへエクスポート

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) }
}

注意事項

  • maxFileSize の単位はバイトです。Demo では 1 MB を 1_000_000 バイトとして変換します。
  • .console.oslogger はエクスポートに対応しません。ログ収集前に永続保存に対応する出力先へ切り替えてください。
  • パスとエラーが同時に返された場合は失敗として扱い、パス一覧が空の場合はエクスポート成果物なしとして扱います。
  • UIActivityViewController はメインスレッドで表示し、iPad ではポップオーバーの表示元を設定します。