-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLogger.m
More file actions
74 lines (61 loc) · 1.82 KB
/
Copy pathFileLogger.m
File metadata and controls
74 lines (61 loc) · 1.82 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// FileLogger.m
// WordPress
//
// Created by Jorge Bernal on 2/23/11.
// Copyright 2011 WordPress. All rights reserved.
//
#import "FileLogger.h"
NSString *FileLoggerPath() {
static NSString *filePath;
if (filePath == nil) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePath = [[documentsDirectory stringByAppendingPathComponent:@"wordpress.log"] retain];
}
return filePath;
}
@implementation FileLogger
- (void)dealloc {
[logFile dealloc]; logFile = nil;
[super dealloc];
}
- (id) init {
self = [super init];
if (self) {
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:FileLoggerPath()])
[fileManager createFileAtPath:FileLoggerPath()
contents:nil
attributes:nil];
logFile = [[NSFileHandle fileHandleForWritingAtPath:FileLoggerPath()] retain];
[logFile seekToEndOfFile];
}
return self;
}
- (void)flush {
[logFile synchronizeFile];
}
- (void)log:(NSString *)message {
[logFile writeData:[[NSString stringWithFormat:@"%@ %@\n", [NSDate date], message] dataUsingEncoding:NSUTF8StringEncoding]];
}
- (void)reset {
[logFile truncateFileAtOffset:0];
}
+ (void)log:(NSString *)format, ... {
va_list ap;
va_start(ap, format);
NSString *message = [[NSString alloc] initWithFormat:format arguments:ap];
#if !FILELOGGER_ONLY_NSLOG_ON_DEBUG || defined(DEBUG)
NSLog(@"# %@", message); // The # symbol indicates that the message will be logged to file, useful when looking at the console
#endif
[[FileLogger sharedInstance] log:message];
[message release];
va_end(ap);
}
+ (FileLogger *)sharedInstance {
static FileLogger *instance = nil;
if (instance == nil) instance = [[FileLogger alloc] init];
return instance;
}
@end