This repository has been archived by the owner on Apr 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
STDebugLogFile.m
58 lines (47 loc) · 1.52 KB
/
STDebugLogFile.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// STDebugLogFile.m
//
// Created by Tom Whipple on 1/29/12.
// Copyright (c) 2012 Smartovation Technologies, LLC. All rights reserved.
//
#import "STDebugLogFile.h"
#import "STDebug.h"
@implementation STDebugLogFile
static NSString* logFilePath = nil;
+(void)setLogFile:(NSString*) filename {
NSString* directoryPath = NSTemporaryDirectory();
if (!directoryPath) directoryPath = @"/tmp";
logFilePath = [[directoryPath stringByAppendingPathComponent:filename] retain];
}
+(NSString*) logPath {
return [[logFilePath copy] autorelease];
}
#ifdef DEBUG
static BOOL loggingEnabled = YES;
#else
static BOOL loggingEnabled = NO;
#endif
+(void) setLoggingEnabled:(BOOL) shouldEnable {
loggingEnabled = shouldEnable;
}
+(BOOL) isLoggingEnabled {
return loggingEnabled;
}
+(void)logToFile:(NSString*) message {
if (logFilePath) {
NSString* timestampedMessage = [NSString stringWithFormat:@"%@\t%@\n",[NSDate date], message];
NSData* messageData = [timestampedMessage dataUsingEncoding:NSUTF8StringEncoding];
if (![[NSFileManager defaultManager] fileExistsAtPath:logFilePath]) {
[[NSFileManager defaultManager] createFileAtPath:logFilePath contents:messageData attributes:nil];
}
else {
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:logFilePath];
[output seekToEndOfFile];
[output writeData:messageData];
[output closeFile];
}
}
// log all messages to the console, in addition to the error log
NSLog(@"%@",message); // format to silance warning
}
@end