当前位置: 首页 > 工具软件 > Log4Cocoa > 使用案例 >

cocoa app 将NSLog 及crash 日志 输出到桌面

左康安
2023-12-01

- (void) redirectConsoleLogToDocumentFolder{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH.mm.ss.SSS"];
    NSString *timeString = [dateFormat stringFromDate:now];
    
    NSString *fileName = [NSString stringWithFormat:@"%@ %@", timeString, @".log"];
    
    NSString *logPath = [documentsDirectory stringByAppendingPathComponent:fileName];
    freopen([logPath fileSystemRepresentation],"a+",stderr);
}

- (void)applicationWillFinishLaunching:(NSNotification *)notification{
    [self redirectConsoleLogToDocumentFolder];
}
 类似资料: