星空网 > 软件开发 > 操作系统

【代码笔记】将log日志保存到文件

代码:

【代码笔记】将log日志保存到文件images/loading.gif' data-original="http://common.cnblogs.com/images/copycode.gif" />
#import "AppDelegate.h"#import "RootViewController.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  // Override point for customization after application launch.    RootViewController *rootVC=[[RootViewController alloc]init];  UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:rootVC];  self.window.rootViewController=nav;      //将log日志保存到文件  #ifdef BH_DEBUG     [self redirectNSLogToDocumentFolder];  #else      #endif    self.window.backgroundColor = [UIColor whiteColor];  [self.window makeKeyAndVisible];  return YES;}- (void)redirectNSLogToDocumentFolder{  //如果已经连接Xcode调试则不输出到文件  if(isatty(STDOUT_FILENO)) {    return;  }    UIDevice *device = [UIDevice currentDevice];  //在模拟器不保存到文件中  if([[device model] hasSuffix:@"Simulator"]){    return;  }    //将NSlog打印信息保存到Document目录下的Log文件夹下  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  NSString *logDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Log"];    NSFileManager *fileManager = [NSFileManager defaultManager];  BOOL fileExists = [fileManager fileExistsAtPath:logDirectory];  if (!fileExists) {    [fileManager createDirectoryAtPath:logDirectory withIntermediateDirectories:YES attributes:nil error:nil];  }    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];  [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];  //每次启动后都保存一个新的日志文件中  NSString *dateStr = [formatter stringFromDate:[NSDate date]];  NSString *logFilePath = [logDirectory stringByAppendingFormat:@"/%@.log",dateStr];    //将log输入到文件  freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);  freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);    //未捕获的Objective-C异常日志  NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);}void UncaughtExceptionHandler(NSException* exception){  NSString* name = [ exception name ];  NSString* reason = [ exception reason ];  NSArray* symbols = [ exception callStackSymbols ];  //异常发生时的调用栈  NSMutableString* strSymbols = [ [ NSMutableString alloc ] init ];  //将调用栈拼成输出日志的字符串  for ( NSString* item in symbols )  {    [strSymbols appendString: item ];    [strSymbols appendString: @"\r\n" ];  }    //将crash日志保存到Document目录下的Log文件夹下  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  NSString *logDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Log"];    NSFileManager *fileManager = [NSFileManager defaultManager];  if (![fileManager fileExistsAtPath:logDirectory]) {    [fileManager createDirectoryAtPath:logDirectory withIntermediateDirectories:YES attributes:nil error:nil];  }    NSString *logFilePath = [logDirectory stringByAppendingPathComponent:@"UncaughtException.log"];  NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];  [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];  NSString *dateStr = [formatter stringFromDate:[NSDate date]];    NSString *crashString = [NSString stringWithFormat:@"<- %@ ->[ Uncaught Exception ]\r\nName: %@, Reason: %@\r\n[ Fe Symbols Start ]\r\n%@[ Fe Symbols End ]\r\n\r\n", dateStr, name, reason, strSymbols];    //把错误日志写到文件中  if (![fileManager fileExistsAtPath:logFilePath]) {    [crashString writeToFile:logFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];  }else{    NSFileHandle *outFile = [NSFileHandle fileHandleForWritingAtPath:logFilePath];    [outFile seekToEndOfFile];    [outFile writeData:[crashString dataUsingEncoding:NSUTF8StringEncoding]];    [outFile closeFile];  }      //把错误日志发送到邮箱  //  NSString *urlStr = [NSString stringWithFormat:@"mailto://test@163.com?subject=bug报告&body=感谢您的配合!<br><br><br>错误详情:<br>%@",crashString ];  //  NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];  //  [[UIApplication sharedApplication] openURL:url];}
【代码笔记】将log日志保存到文件

 




原标题:【代码笔记】将log日志保存到文件

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

境外账户的钱怎么转到国内:https://www.goluckyvip.com/tag/72625.html
代国外网络账户开户:https://www.goluckyvip.com/tag/72629.html
货换标:https://www.goluckyvip.com/tag/7263.html
海外账户开户方法:https://www.goluckyvip.com/tag/72630.html
中辉国际期货美原油开户:https://www.goluckyvip.com/tag/72631.html
如何 海外 开户:https://www.goluckyvip.com/tag/72635.html
零售晚报:携程发布318成绩单 近800万商家GMV增长101% :https://www.kjdsnews.com/a/1836405.html
19点聊电商:百度云:大模型应用元年 企业如何先“卷”一步? :https://www.kjdsnews.com/a/1836406.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流