ログのエクスポート
File または XLFacility に保存されたログをエクスポートし、生成されたファイル URL をアプリから共有します。
前提条件
- 現在の出力先が
.fileまたは.xlfacilityであること。 .xlfacilityを選択する場合は、XLFacility プラグインがインストールされていること。- 保存済みログが 1 件以上あること。
API Reference
フレームワーク
AIBudsLog.xcframework
宣言
- Swift
- Objective-C
/// 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)?)/// Exports all retained logs to one file.
- (void)exportLogsWithCompletion:(void (^ _Nullable)(NSString * _Nullable, NSDate * _Nullable, NSDate * _Nullable, NSError * _Nullable))completion;
/// Exports retained logs to size-limited files.
- (void)exportLogsWithMaxFileSize:(uint64_t)maxFileSize
completion:(void (^ _Nullable)(NSArray<NSString *> * _Nullable, NSDate * _Nullable, NSDate * _Nullable, NSError * _Nullable))completion;どちらの操作も LogService で定義され、logService から呼び出します。
使用例
1 つのファイルへエクスポート
- Swift
- Objective-C
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)])
}
}[AIBudsLogSDK.logService
exportLogsWithCompletion:^(
NSString *filePath, NSDate *startDate, NSDate *endDate, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error != nil) {
NSLog(@"Export failed: %@", error.localizedDescription);
return;
}
if (filePath == nil) {
NSLog(@"Export completed without a file");
return;
}
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
[self presentShareSheetWithItems:@[ fileURL ]];
});
}];サイズ制限付きファイルへエクスポート
- Swift
- Objective-C
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) }
}uint64_t fiveMegabytes = 5 * 1000000;
[AIBudsLogSDK.logService exportLogsWithMaxFileSize:fiveMegabytes
completion:^(NSArray<NSString *> *paths,
NSDate *startDate,
NSDate *endDate,
NSError *error) {
if (error != nil || paths.count == 0) {
return;
}
NSMutableArray<NSURL *> *URLs = [NSMutableArray array];
for (NSString *path in paths) {
[URLs addObject:[NSURL fileURLWithPath:path]];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self presentShareSheetWithItems:URLs];
});
}];注意事項
maxFileSizeの単位はバイトです。Demo では 1 MB を1_000_000バイトとして変換します。.consoleと.osloggerはエクスポートに対応しません。ログ収集前に永続保存に対応する出力先へ切り替えてください。- パスとエラーが同時に返された場合は失敗として扱い、パス一覧が空の場合はエクスポート成果物なしとして扱います。
UIActivityViewControllerはメインスレッドで表示し、iPad ではポップオーバーの表示元を設定します。