-
Notifications
You must be signed in to change notification settings - Fork 2
/
DebugLog.m
39 lines (32 loc) · 1.28 KB
/
DebugLog.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// DebugLog.m
//
// Created by Jeremy Olmsted-Thompson on 12/22/11.
// Copyright (c) 2011 JOT. All rights reserved.
//
#import "DebugLog.h"
#define DefaultLogFileName @"DebugLog.txt"
@implementation DebugLog
-(id)initWithLogFilePath:(NSString*)path {
if ((self = [super init])) {
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
}
logFileHandle = [[NSFileHandle fileHandleForUpdatingAtPath:path] retain];
[logFileHandle seekToEndOfFile];
}
return self;
}
-(void)log:(NSString*)logString {
NSLog(@"%@", logString);
[logFileHandle writeData:[[NSString stringWithFormat:@"%@: %@\n", [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterLongStyle], logString] dataUsingEncoding:NSUTF8StringEncoding]];
[logFileHandle synchronizeFile];
}
+(DebugLog*)defaultLog {
static DebugLog *defaultLog = nil;
if (!defaultLog) {
defaultLog = [[DebugLog alloc] initWithLogFilePath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:DefaultLogFileName]];
}
return defaultLog;
}
@end