如何将 NSLog 登录到文件中 [英] How to NSLog into a file

查看:27
本文介绍了如何将 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:@"
"];
    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天全站免登陆