如何将NSLog转换为文件 [英] How to NSLog into a file

查看:67
本文介绍了如何将NSLog转换为文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将每个 NSLog 写入控制台,但也可以写入文件?我想准备这个而不将 NSLog 替换为 someExternalFunctionForLogging

Is it possible to write every NSLog not only into console, but into a file too? I want to prepare this without replacing NSLog into someExternalFunctionForLogging.

替换所有 NSLog 将是真正的问题。也许有可能从控制台解析数据或捕获消息?

It will be real problem to replace all NSLog. Maybe there is possibility for parsing data from console or catching messages?

推荐答案

选项1:使用ASL



NSLog输出日志到ASL (Apple的syslog版本)和控制台,这意味着当您使用iPhone模拟器时,它已经写入Mac中的文件。如果要读取它,请打开应用程序Console.app,然后在过滤器字段中键入应用程序的名称。要在iPhone设备中执行相同操作,您需要使用ASL API并进行一些编码。

Option 1: Use ASL

NSLog outputs log to ASL (Apple's version of syslog) and console, meaning it is already writing to a file in your Mac when you use the iPhone simulator. If you want to read it open the application Console.app, and type the name of your application in the filter field. To do the same in your iPhone device, you would need to use the ASL API and do some coding.

假设你在模拟器上运行而你不想使用Console.app。您可以使用freopen将错误流重定向到您喜欢的文件:

freopen([path cStringUsingEncoding:NSASCIIStringEncoding],a +,stderr);

请参阅解释和示例项目了解详细信息。

Let's say you are running on the simulator and you don't want to use the Console.app. You can redirect the error stream to a file of your liking using freopen:
freopen([path cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
See this explanation and sample project for details.

或者您可以使用宏来使用自定义函数覆盖NSLog。例如,将此类添加到项目中:

Or you can override NSLog with a custom function using a macro. Example, add this class to your project:

// file Log.h
#define NSLog(args...) _Log(@"DEBUG ", __FILE__,__LINE__,__PRETTY_FUNCTION__,args);
@interface Log : NSObject
void _Log(NSString *prefix, const char *file, int lineNumber, const char *funcName, NSString *format,...);
@end

// file Log.m
#import "Log.h"
@implementation Log
void _Log(NSString *prefix, const char *file, int lineNumber, const char *funcName, NSString *format,...) {
    va_list ap;
    va_start (ap, format);
    format = [format stringByAppendingString:@"\n"];
    NSString *msg = [[NSString alloc] initWithFormat:[NSString stringWithFormat:@"%@",format] arguments:ap];   
    va_end (ap);
    fprintf(stderr,"%s%50s:%3d - %s",[prefix UTF8String], funcName, lineNumber, [msg UTF8String]);
    [msg release];
}
@end

并在项目范围内导入,将以下内容添加到您的< application> -Prefix.pch

And import it project wide adding the following to your <application>-Prefix.pch:

#import "Log.h"

现在每次拨打NSLog都会被你的自定义功能所取代,而无需触摸你的现有代码。但是,上面的功能只是打印到控制台。要添加文件输出,请在_Log上面添加此函数:

Now every call to NSLog will be replaced with your custom function without the need to touch your existing code. However, the function above is only printing to console. To add file output, add this function above _Log:

void append(NSString *msg){
    // get path to Documents/somefile.txt
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"logfile.txt"];
    // create if needed
    if (![[NSFileManager defaultManager] fileExistsAtPath:path]){
        fprintf(stderr,"Creating file at %s",[path UTF8String]);
        [[NSData data] writeToFile:path atomically:YES];
    } 
    // append
    NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:path];
    [handle truncateFileAtOffset:[handle seekToEndOfFile]];
    [handle writeData:[msg dataUsingEncoding:NSUTF8StringEncoding]];
    [handle closeFile];
}

并在_Log函数中将此行添加到fprintf下面:

and add this line below fprintf in the _Log function:

append(msg);

文件编写也适用于您的iPhone设备,但文件将在其中的目录中创建,除非您添加代码将其发送回您的Mac,或者在应用程序内的视图中显示代码,或者使用iTunes添加文档目录,否则您将无法访问。

File writing also works in your iPhone device, but the file will be created in a directory inside it, and you won't be able to access unless you add code to send it back to your mac, or show it on a view inside your app, or use iTunes to add the documents directory.

这篇关于如何将NSLog转换为文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆