Reputation: 823
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);
Upvotes: 1
Views: 1375
Reputation: 463
Please have a look this may help :
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 :
#import <Foundation/Foundation.h>
@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"
#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;
}
}
After the implementation of USTLogger Class we just need to call these method when we need nslog in file
#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
This will write whole nslog output in a file and it is stored in application document directory, we can get that nslog file by enabling file sharing for application.
source code be downloaded from below link :
Upvotes: 2
Reputation: 5543
You might want to take a look at Lumberjack, which is a comprehensive logging library.
Upvotes: 2
Reputation: 46533
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.
Upvotes: 1