Skip to content

Commit

Permalink
Adding the ability to specify a custom logsDirectory to DDLogFileMana…
Browse files Browse the repository at this point in the history
…gerDefault.
  • Loading branch information
robbiehanson committed Apr 11, 2011
1 parent aa20e28 commit cc71a77
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
5 changes: 5 additions & 0 deletions Lumberjack/DDFileLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
// Default log file manager.
//
// All log files are placed inside the logsDirectory.
// If a specific logsDirectory isn't specified, the default directory is used.
// On Mac, this is in ~/Library/Application Support/<Application Name>/Logs.
// On iPhone, this is in ~/Documents/Logs.
//
Expand All @@ -93,8 +94,12 @@
@interface DDLogFileManagerDefault : NSObject <DDLogFileManager>
{
NSUInteger maximumNumberOfLogFiles;
NSString *_logsDirectory;
}

- (id)init;
- (id)initWithLogsDirectory:(NSString *)logsDirectory;

@end

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
42 changes: 28 additions & 14 deletions Lumberjack/DDFileLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
@interface DDLogFileManagerDefault (PrivateAPI)

- (void)deleteOldLogFiles;
- (NSString *)defaultLogsDirectory;

@end

Expand Down Expand Up @@ -50,25 +51,35 @@ @implementation DDLogFileManagerDefault

@synthesize maximumNumberOfLogFiles;


- (id)init
{
return [self initWithLogsDirectory:nil];
}

- (id)initWithLogsDirectory:(NSString *)aLogsDirectory
{
if ((self = [super init]))
{
maximumNumberOfLogFiles = DEFAULT_LOG_MAX_NUM_LOG_FILES;

if (aLogsDirectory)
_logsDirectory = [aLogsDirectory copy];
else
_logsDirectory = [[self defaultLogsDirectory] copy];

NSKeyValueObservingOptions kvoOptions = NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew;

[self addObserver:self forKeyPath:@"maximumNumberOfLogFiles" options:kvoOptions context:nil];

NSLogVerbose(@"DDFileLogManagerDefault: logsDir:\n%@", [self logsDirectory]);
NSLogVerbose(@"DDFileLogManagerDefault: logsDirectory:\n%@", [self logsDirectory]);
NSLogVerbose(@"DDFileLogManagerDefault: sortedLogFileNames:\n%@", [self sortedLogFileNames]);
}
return self;
}

- (void)dealloc
{
[_logsDirectory release];
[super dealloc];
}

Expand Down Expand Up @@ -98,15 +109,13 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
{
#if GCD_MAYBE_AVAILABLE

dispatch_block_t block = ^{
dispatch_async([DDLog loggingQueue], ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[self deleteOldLogFiles];

[pool drain];
};

dispatch_async([DDLog loggingQueue], block);
});

#endif
}
Expand Down Expand Up @@ -187,10 +196,10 @@ - (void)deleteOldLogFiles
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
* Returns the path to the logs directory.
* Returns the path to the default logs directory.
* If the logs directory doesn't exist, this method automatically creates it.
**/
- (NSString *)logsDirectory
- (NSString *)defaultLogsDirectory
{
#if TARGET_OS_IPHONE
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
Expand All @@ -204,19 +213,25 @@ - (NSString *)logsDirectory
NSString *baseDir = [basePath stringByAppendingPathComponent:appName];
#endif

NSString *logsDir = [baseDir stringByAppendingPathComponent:@"Logs"];
return [baseDir stringByAppendingPathComponent:@"Logs"];
}

- (NSString *)logsDirectory
{
// We could do this check once, during initalization, and not bother again.
// But this way the code continues to work if the directory gets deleted while the code is running.

if(![[NSFileManager defaultManager] fileExistsAtPath:logsDir])
if (![[NSFileManager defaultManager] fileExistsAtPath:_logsDirectory])
{
NSError *err = nil;
if(![[NSFileManager defaultManager] createDirectoryAtPath:logsDir
withIntermediateDirectories:YES attributes:nil error:&err])
if (![[NSFileManager defaultManager] createDirectoryAtPath:_logsDirectory
withIntermediateDirectories:YES attributes:nil error:&err])
{
NSLogError(@"DDFileLogManagerDefault: Error creating logsDirectory: %@", err);
}
}

return logsDir;
return _logsDirectory;
}

- (BOOL)isLogFile:(NSString *)fileName
Expand Down Expand Up @@ -253,7 +268,6 @@ - (BOOL)isLogFile:(NSString *)fileName
- (NSArray *)unsortedLogFilePaths
{
NSString *logsDirectory = [self logsDirectory];

NSArray *fileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:logsDirectory error:nil];

NSMutableArray *unsortedLogFilePaths = [NSMutableArray arrayWithCapacity:[fileNames count]];
Expand Down

0 comments on commit cc71a77

Please sign in to comment.