로그 내보내기
File 또는 XLFacility에 저장된 로그를 내보내고, 앱에서 생성된 파일 URL을 공유합니다.
사전 요구 사항
- 현재 출력 대상이
.file또는.xlfacility여야 합니다. .xlfacility을 선택했다면 XLFacility 플러그인이 설치되어 있어야 합니다.- 저장된 로그가 하나 이상 있어야 합니다.
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에서 호출합니다.
사용 예제
단일 파일로 내보내기
- 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의 단위는 byte입니다. Demo에서는 1 MB를1_000_000byte로 계산합니다..console과.oslogger는 내보내기를 지원하지 않습니다. 로그를 수집하기 전에 영구 저장 출력 대상으로 변경하세요.- 경로와 오류가 함께 반환되면 실패로 처리하고, 경로 목록이 비어 있으면 내보낸 파일이 없는 것으로 처리하세요.
UIActivityViewController는 메인 스레드에서 표시하고, iPad에서는 popover의 source를 설정하세요.