app扩展输出log到文件并从containing app读取

来源:互联网 发布:利用淘宝客刷单 编辑:程序博客网 时间:2024/06/07 18:54

app扩展,即appex,如today widget,network extension之类。现在有个需求是在开发扩展时,将log输出到文件,在必要的时候获取文件内容进行查看。此处不讨论将log通过网络传输到服务器的情况,也不讨论使用类似CocoaLumberjack这样的第三方库的情况,我们简单的使用NSLog而已。

  • 思路整理:

    • 首先,我们要能取得log文件,此处不讨论通过网络将log文件传输到服务器的情况。如果是普通应用,可以在xcode->devices里面通过Download container的方法,将应用数据下载到电脑上。对于扩展,虽然也有自己的沙盒,但是我并没有找到直接将沙盒数据下载到电脑的方法。所以不考虑将log写到扩展沙盒里面。
    • 所以考虑变通的方法,将log写到扩展和应用都能访问到的位置,然后在应用中读取log并转存到应用的沙盒,这样就可以使用xcode直接取得log。这个都能访问到的位置是基于扩展和应用同属于一个app group,这样可以使用[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:[self sharedGroupIdentifier]];获取到这个container目录。
    • 然后我们就只有考虑怎么将NSLog的内容写到文件里面去了。最方便的方法是使用freopen重定向。
  • 相关代码:

    • 扩展中的代码:在扩展启动时,获取group container路径,构造log文件路径,并重定向stdout, stderr。
    NSURL * containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:[self sharedGroupIdentifier]];    NSString* logFilePath = [[containerURL URLByAppendingPathComponent:@"appex.log"] path];    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
 重定向使用"a+"模式,将log添加到文件末尾,因此多次启动扩展Log是累积的。
  • 应用中的代码:
    NSURL * containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:[self sharedGroupIdentifier]];    NSString* appexLogFilePath = [[containerURL URLByAppendingPathComponent:@"appex.log"] path];    //读取appex的log内容到string    NSString* content = [NSString stringWithContentsOfFile:appexLogFilePath encoding:NSUTF8StringEncoding error:nil];    NSLog(@"appex log的内容是:\n%@",content);    if(content.length==0){        return;    }    //save the log to a file in document folder    //使用当前时间命名doucments中的log文件    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    [dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"];    NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];    NSString *docLogFileName = [NSString stringWithFormat:@"%@.appex.log", strDate];    NSString *docLogFilePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:docLogFileName];    //将content转存到应用沙盒documents中    BOOL result = [content writeToFile:docLogFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];    NSLog(@"save log to:%@ result=%d",docLogFilePath,result);    if(result){        //如果成功将appex的log清空        //clear the appex log file        NSString *ccc=@"";        [ccc writeToFile:appexLogFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];     }
  • 这个方法简单的进行了log的转存,并清除了appex的log,每次调用都生成一个新的log文件放在documents里面。需要注意的是,如果log文件很大,就不能直接读取和保存了,需要打开一个文件流,逐步读取和保存。
原创粉丝点击