xcode 如何将 nslog 输出重定向到文件而不是控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3184235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect the nslog output to file instead of console
提问by Unicorn
I have cocoa application running on OS X. I have used NSLog for debugging purpose. Now I want to redirect the log statements to file instead of console.
我在 OS X 上运行可可应用程序。我使用 NSLog 进行调试。现在我想将日志语句重定向到文件而不是控制台。
I have used this method but it results logging in Console as well as in file.
我已经使用了这种方法,但它导致在控制台和文件中登录。
- (BOOL)redirectNSLog
{
// Create log file
[@"" writeToFile:@"/NSLog.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
id fileHandle = [NSFileHandle fileHandleForWritingAtPath:@"/NSLog.txt"];
if (!fileHandle) return NSLog(@"Opening log failed"), NO;
[fileHandle retain];
// Redirect stderr
int err = dup2([fileHandle fileDescriptor], STDERR_FILENO);
if (!err) return NSLog(@"Couldn't redirect stderr"), NO;
return YES;
}
Is it possible to not have log statement in console but only in file ??
是否可以在控制台中没有日志语句而只在文件中??
采纳答案by Yuji
NSLog
is made to log into the console. You need to define your own function MyLog
or whatever, and replace all occurrences of NSLog
into MyLog
.
NSLog
登录到控制台。您需要定义自己的函数MyLog
或其他函数,并替换所有出现的NSLog
into MyLog
。
回答by
Step 1: Include following function in AppDelegate:
第 1 步:在 AppDelegate 中包含以下函数:
- (void) redirectConsoleLogToDocumentFolder
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
freopen([logPath fileSystemRepresentation],"a+",stderr);
}
Step 2: Call this function at the start of function applicationDidFinishLaunchingWithOptions...
第 2 步:在函数 applicationDidFinishLaunchingWithOptions 开始时调用此函数...
Thats it, Every NSLog() will now get redirected to this console.log file, which you can find in the documents directory.
就是这样,每个 NSLog() 现在都将被重定向到这个 console.log 文件,您可以在文档目录中找到该文件。
回答by user1227928
Recently i have faced similar requirement and this is how i have done it.
最近我遇到了类似的要求,这就是我所做的。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self redirectConsoleLogToDocumentFolder];
return YES;
}
- (void) redirectConsoleLogToDocumentFolder
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.txt"];
freopen([logPath fileSystemRepresentation],"a+",stderr);
}
And Now if you want to this console to user
现在,如果您想将此控制台用于用户
-(void)displayLog{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.txt"];
NSError *err = nil;
NSString *fileContents = [NSString stringWithContentsOfFile:logPath
encoding:NSUTF8StringEncoding
error:&err];
if (fileContents == nil) {
NSLog(@"Error reading %@: %@", logPath, err);
} else {
self.textView.text = fileContents;
}
}
回答by dbainbridge
You may be interested in CocoaLumberHyman. It is a very flexible logging framework for both Mac OS X and iOS. One logging statement can be sent not only to the console but to a file simultaneously. Plus it is actually faster then NSLog. I use it in a project that has common code for both OS X and iOS.
您可能对CocoaLumberHyman感兴趣。它是适用于 Mac OS X 和 iOS 的非常灵活的日志记录框架。一个日志语句不仅可以发送到控制台,还可以同时发送到一个文件。此外,它实际上比 NSLog 更快。我在一个具有 OS X 和 iOS 通用代码的项目中使用它。