Redirect the nslog output to file instead of console


Redirecting the nslog output to a file is useful in scenerios when device is not connected to system and we need the console logs to track down some issues.

In order to do this we added one singelton class USTLogger subclass of NSObject :

Source code for USTLogger.h file are following :


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

-(void) writeNSLogToFile;

+ (USTLogger *) sharedInstance;

Source code for USTLogger.m file are following :

#import "USTLogger.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
//[self restoreStdErr];

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]…

View original post 113 more words