-
Notifications
You must be signed in to change notification settings - Fork 3
/
CBFLog.m
204 lines (165 loc) · 5.53 KB
/
CBFLog.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* CBFLog is a singleton logging object for more powerful logging in your cocoa projects
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* CBFLog.m
*
* Created by Camron Flanders on 2.23.09
* Copyright 2009 camronflanders. All rights reserved.
*
*/
#import "CBFLog.h"
@interface NSString (CBFStringAdditions)
- (NSString *)handleWithMaxLength:(int)theLength;
@end
@implementation NSString (CBFStringAdditions)
- (NSString *)handleWithMaxLength:(int)theLength {
if([self length] <= theLength) {
return [self stringByPaddingToLength:theLength withString:@" " startingAtIndex:0];
} else {
NSRange range;
int maxLength = theLength - 3; // 3 is the length of the separator
range.length = [self length] - maxLength;
range.location = floor(maxLength/2);
return [self stringByReplacingCharactersInRange:range withString:@"..."];
}
}
@end
@implementation CBFLog
#pragma mark -
#pragma mark SINGLETON STUFF
static CBFLog *sharedDebug = nil;
static NSArray *severityLevels = nil;
static NSString *outputLevelStringFormat = @"[%@] ";
static NSString *lineNumberStringFormat = @"%d ";
static NSString *functionNameStringFormat = @"%s | ";
NSString *filePathString;
NSString *levelString;
NSString *lineNumString;
NSString *functionNameString;
NSString *dateString;
static NSString *blankString = @"";
+ (CBFLog *) sharedDebug
{
return sharedDebug;
}
+ (void)initialize
{
if(!sharedDebug)
sharedDebug = [[self alloc] init];
if(severityLevels == nil)
severityLevels = [[NSArray alloc] initWithObjects: @"CRITICAL",
@" ERROR ",
@"WARNING ",
@" NOTIFY ",
@" INFO ",
@" DEBUG ", nil];
}
+ (id) allocWithZone:(NSZone *) zone
{
if (sharedDebug)
{
//The caller expects to receive a new object, so implicitly retain it to balance out the caller's eventual release message.
return [sharedDebug retain];
}
else
{
//When not already set, +initialize is our caller—it's creating the shared instance. Let this go through.
return [super allocWithZone:zone];
}
return nil;
}
- (id) init {
if (!hasInited)
{
if ((self = [super init]))
{
hasInited = YES;
}
}
return self;
}
#pragma mark -
#pragma mark debug methods
- (void)log:(int)severity overrideGlobal:(BOOL)override fileName:(const char *)file lineNumber:(int)line functionName:(const char *)funcNameString input:(NSString *)message, ...
{
// check to see if we have disabled debugging globally and return unless
// we want to override the global setting for this statment.
if(!GLOBAL_SOFT_ENABLE && !override) return;
// if we are below our threshold, return
if(severity > LOG_LEVEL) return;
va_list argList;
va_start(argList, message);
NSString *messageString = [[[NSString alloc] initWithFormat:message
arguments:argList] autorelease];
va_end(argList);
NSString *logString;
if(!BARE_OUTPUT)
{
if(LOG_SEVERITY)
{
int outputLevel = (severity) ? severity : 0;
levelString = [NSString stringWithString:[severityLevels objectAtIndex:outputLevel]];
levelString = [NSString stringWithFormat:outputLevelStringFormat, levelString];
} else
levelString = blankString;
if(LOG_PATH)
{
filePathString = [[[NSString alloc] initWithBytes:file
length:strlen(file)
encoding:NSUTF8StringEncoding] autorelease];
if(!LOG_FULL_PATH)
filePathString = [filePathString lastPathComponent];
} else
filePathString = blankString;
filePathString = [filePathString handleWithMaxLength:FILE_NAME_LENGTH];
if(LOG_LINE_NUM)
lineNumString = [NSString stringWithFormat:lineNumberStringFormat, line];
else
lineNumString = blankString;
lineNumString = [lineNumString stringByPaddingToLength:LINE_NUM_PADDING withString:@" " startingAtIndex:0];
if(LOG_FUNC_NAME)
functionNameString = [NSString stringWithFormat:functionNameStringFormat, funcNameString];
else
functionNameString = blankString;
functionNameString = [functionNameString handleWithMaxLength:FUNC_NAME_LENGTH];
logString = [[[NSString alloc] initWithFormat:@"%@%@%@%@%@", levelString,
filePathString,
lineNumString,
functionNameString,
messageString] autorelease];
} else
logString = [[[NSString alloc] initWithString:messageString] autorelease];
if(USE_NSLOG)
NSLog(@"%@", logString);
else
printf("%s\n", [logString UTF8String]);
}
- (void)blankLine
{
if(USE_NSLOG)
NSLog(@"\n");
else
printf("\n");
}
@end