如何使用手机获取并导出 Log?

在不连接 Xcode 的情况下,使用手机获取并导出 Log,以便分析定位问题。
发布时间: 2016-04-15 10:00

回答:

1、在 Appdelegate.m 中添加下面代码

- (BOOL)application:(UIApplication *)application

    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


  //重定向 log 到本地问题

  //在 info.plist 中打开 Application supports iTunes file sharing

      if (![[[UIDevice currentDevice] model] isEqualToString:@"iPhone Simulator"]) {

          [self redirectNSlogToDocumentFolder];

      }


    //设置Log级别,开发阶段打印详细log

    [RCIMClient sharedRCIMClient].logLevel = RC_Log_Level_Info;

}


- (void)redirectNSlogToDocumentFolder {

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

                                                       NSUserDomainMask, YES);

  NSString *documentDirectory = [paths objectAtIndex:0];


  NSDate *currentDate = [NSDate date];

  NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];

  [dateformatter setDateFormat:@"MMddHHmmss"];

  NSString *formattedDate = [dateformatter stringFromDate:currentDate];


  NSString *fileName = [NSString stringWithFormat:@"rc%@.log", formattedDate];

  NSString *logFilePath =

      [documentDirectory stringByAppendingPathComponent:fileName];


  freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+",

          stdout);

  freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+",

          stderr);

}


2、运行项目,复现问题,然后从“沙盒/Documents”路径下导出 rc 开头的 log 文件。