2013-03-27 131 views
0

我正在使用代码将所有NSLog写入文本文件。我如何选择我只需写入文件的NSlog?如何将某些NSLog输出重定向到文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"MOVbandlog.txt"]; 
//freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr); 
freopen([logPath fileSystemRepresentation],"a+",stderr); 

回答

1

您可以创建两个版本的NSLog,因为仅使用NSLog的MYLog将显示在控制台上。第二个将调用NSLog并写入文件的MYLogFile。

所以基本上你需要制作两个宏或方法。

+0

我能得到一个例子吗?这是我的一个NSlog NSLog(@“\ n ********* data from device ******** \ n%@”,[data description]); – TWcode 2013-03-27 01:07:43

+0

做一些调整[这里](http://stackoverflow.com/questions/9619708/nslog-to-both-console-and-file) – 2013-03-27 01:20:36

+0

这行不行:fprintf(myFileDescriptor,“%s \ n”,[ formattedMessage UTF8String]);有关myfileDescriptor的信息 – TWcode 2013-03-27 01:40:23

2

请看看这可能会帮助:

重定向当设备没有连接到系统的NSLog输出到文件中scenerios有用的,我们需要的控制台日志跟踪了一些issues.In为了做到NSObject中的这一点,我们添加了一个singelton类USTLogger子类:用于USTLogger.h文件

源代码如下:

#import <Foundation/Foundation.h> 

@interface USTLogger : NSObject 
{ 
BOOL stdErrRedirected; 
} 
@property (nonatomic, assign) BOOL stdErrRedirected; 

-(void) writeNSLogToFile; 

+ (USTLogger *) sharedInstance; 

为USTLogger.m文件源代码如下:

#import "USTLogger.h" 
#import <unistd.h> 

@implementation USTLogger 

@synthesize stdErrRedirected; 

static int savedStdErr = 0; 
static USTLogger *sharedInstance; 

+ (USTLogger *) sharedInstance { 
static dispatch_once_t once; 
static id sharedInstance; 
dispatch_once(&once, ^{ 
sharedInstance = [[self alloc] init]; 
}); 
return sharedInstance; 
} 

-(id)init { 
return self; 
} 

- (void) writeNSLogToFile 
{ 

if (!stdErrRedirected) 
{ 
stdErrRedirected = YES; 
savedStdErr = dup(STDERR_FILENO); 

NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *logPath = [cachesDirectory stringByAppendingPathComponent:@"nslog.log"]; 
freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr); 
} 
} 

- (void)restoreStdErr 
{ 
if (stdErrRedirected) 
{ 
stdErrRedirected = NO; 
fflush(stderr); 

dup2(savedStdErr, STDERR_FILENO); 
close(savedStdErr); 
savedStdErr = 0; 
} 
} 

后USTLogger类的实现,我们只需要调用这些方法时,我们需要在文件

#import "AppDelegate.h" 
#import "USTLogger.h" 

@interface AppDelegate() 

@end 

@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
// Override point for customization after application launch. 

[[USTLogger sharedInstance] writeNSLogToFile]; 

NSLog(@"didFinishLaunchingWithOptions"); 

return YES; 
} 
@end 

这将写完整的NSLog输出文件并将其保存在应用程序文件目录的NSLog,我们可以通过为应用程序启用文件共享来获得该nslog文件。

源代码从下面的链接下载:

https://shankertiwari3.wordpress.com/2015/06/09/redirect-the-nslog-output-to-file-instead-of-console/

+0

虽然此链接可能会回答问题,但最好在此处包含答案的重要部分并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 – 2015-06-15 10:39:41

+0

感谢Jon Stirling,我编辑过相同的内容。 – 2015-06-15 11:26:41

相关问题