I would like that the XCode output (from NSLog() or whatever) is automatically written into a file in the application's folder. In other words, I just want to see the whole content of the XCode area debug into a text file.
I saw some code snippets but each of them avoids the display into XCode (that is unacceptable). I just want to duplicate the debug text to a file... Is there a way to do that ?
EDIT 1 (after trying the Clement's answer) :
I get a linker error :
EDIT 2 (how I have implemented the Clement's answer) :
I created a new class called LogHelper :
LogHelper.h :
#define NSLog(args...) _Log(@"DEBUG ", __FILE__,__LINE__,__PRETTY_FUNCTION__,args);
@interface MyCustomLog : NSObject
void _Log(NSString *prefix, const char *file, int lineNumber, const char *funcName, NSString *format,...);
@end
LogHelper.m :
#import "LogHelper.h"
@implementation MyCustomLog : NSObject
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:@"myCustomLog.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];
}
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]);
append(msg);
}
@end
And I added #import "LogHelper.h"
in myApplication-Prefix.pch
.
EDIT 3 (I found a solution) :
I set an environment variable called "runningFromXCode" (follow this link to see how to do it) to keep the debug text showing into the console when the app is running from XCode ;
I wrote this piece of code into the
application:didFinishLaunchingWithOptions:
method (source) :if (getenv("runningFromXCode") == NULL){ NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [allPaths objectAtIndex:0]; NSString *pathForLog = [documentsDirectory stringByAppendingPathComponent:@"log.txt"]; freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr); }