Search code examples
objective-cxcodenslog

How to redirect certain NSLog output to a file


I'm using the code to write all NSLogs to a text file. How can I select the NSlogs that I only need to write to file?

  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);

Solution

  • You can create two version of NSLog, as MYLog using only NSLog, will show on console. Second one MYLogFile that will call NSLog and write to file as well.

    So basically you need to make two macros or methods.