This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSOLogger.m
477 lines (380 loc) · 14.1 KB
/
SOLogger.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
SOLogger
Copyright Standard Orbit Software, LLC. All rights reserved.
License at the bottom of the file.
*/
#import "SOLogger.h"
#import <asl.h>
#ifndef __has_feature
#define __has_feature(x) 0
#endif
#define NO_ARC !__has_feature(objc_arc)
#if SOLOGGER_DEBUG
static inline void LOG_ASLCLIENT(const char *where, aslclient ASLClient);
#endif
static NSString * const SOLoggerWillDieNotification = @"SOLoggerWillDie";
static NSString * const SOLoggerDidChangeSeverityFilterNotification = @"SOLoggerDidChangeSeverityFilter";
static NSString * const SOLoggerDidAddDescriptorNotification = @"SOLoggerDidAddDescriptor";
static NSString * const SOLoggerDidRemoveDescriptorNotification = @"SOLoggerDidRemoveDescriptor";
uint32_t SOLoggerDefaultASLOptions = ASL_OPT_NO_DELAY | ASL_OPT_STDERR | ASL_OPT_NO_REMOTE;
@class SOLogger;
@interface _SOASLClient : NSObject
{
@private
aslclient _aslclientRef;
}
- (id) initWithLogger:(SOLogger *)logger facility:(NSString *)facility options:(uint32_t)options;
- (aslclient) aslclientRef;
@end
@implementation _SOASLClient
- (id) initWithLogger:(SOLogger *)logger facility:(NSString *)facility options:(uint32_t)options
{
self = [super init];
if (!self) return nil;
if (logger == nil)
{
#if NO_ARC
[self release];
#endif
return nil;
}
//const char *normalizedFacility = (facility) ? [facility UTF8String] : "com.apple.console";
_aslclientRef = asl_open (NULL /*ident*/, [facility UTF8String] , options);
/* Register for logger notifications */
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeSeverityFilter:) name:SOLoggerDidChangeSeverityFilterNotification object:logger];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didAddDescriptor:) name:SOLoggerDidAddDescriptorNotification object:logger];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRemoveDescriptor:) name:SOLoggerDidRemoveDescriptorNotification object:logger];
return self;
}
- (id) init
{
return [self initWithLogger:nil facility:nil options:0];
}
- (void) dealloc
{
/* Done with all notifications */
[[NSNotificationCenter defaultCenter] removeObserver:self];
// Ensure that the ASL connection is closed.
if (_aslclientRef)
{
asl_close (_aslclientRef);
_aslclientRef = NULL;
}
#if SOLOGGER_DEBUG
NSThread *currentThread = [NSThread currentThread];
char *death_rattle = NULL;
asprintf(&death_rattle, "Deallocating ASLClient %p on %s thread %p\n", self, ([currentThread isMainThread] ? "main" : "background"), currentThread);
fprintf(stderr, "%s", death_rattle);
fflush(stderr);
if (death_rattle) free(death_rattle);
#endif
#if NO_ARC
[super dealloc];
#endif
}
- (aslclient) aslclientRef
{
aslclient asl = NULL;
@synchronized(self)
{
asl = _aslclientRef;
}
return asl;
}
- (void) didChangeSeverityFilter:(NSNotification *)note
{
NSNumber *newMask = [[note userInfo] objectForKey:@"severityFilterMask"];
if (newMask == nil) return;
@synchronized(self)
{
asl_set_filter(_aslclientRef, [newMask intValue]);
}
#if SOLOGGER_DEBUG
LOG_ASLCLIENT(__PRETTY_FUNCTION__, _aslclientRef);
#endif
}
- (void) didAddDescriptor:(NSNotification *)note
{
NSNumber *newDescriptor = [[note userInfo] objectForKey:@"descriptor"];
if (newDescriptor == nil) return;
@synchronized(self)
{
int err = asl_add_log_file(_aslclientRef, [newDescriptor intValue]);
if (err != 0)
{
NSLog (@"asl_add_log_file() failed to add %d", [newDescriptor intValue]);
}
}
#if SOLOGGER_DEBUG
LOG_ASLCLIENT(__PRETTY_FUNCTION__, _aslclientRef);
#endif
}
- (void) didRemoveDescriptor:(NSNotification *)note
{
NSNumber *newDescriptor = [[note userInfo] objectForKey:@"descriptor"];
if (newDescriptor == nil) return;
@synchronized(self)
{
int err = asl_remove_log_file(_aslclientRef, [newDescriptor intValue]);
if (err != 0)
{
NSLog (@"asl_remove_log_file() failed to remove %d", [newDescriptor intValue]);
}
}
#if SOLOGGER_DEBUG
LOG_ASLCLIENT(__PRETTY_FUNCTION__, _aslclientRef);
#endif
}
@end
#pragma mark -
@implementation SOLogger
@synthesize facility = _facility;
@synthesize options = _ASLOptions;
@synthesize severityFilterMask = _severityFilterMask;
#pragma mark -
#pragma mark Creation
- (id) initWithFacility:(NSString *)facility options:(uint32_t)options;
{
self = [super init];
if ( !self ) return nil;
_facility = [facility copy];
_ASLOptions = options;
_extraLoggingDescriptors = [[NSMutableSet alloc] init];
_ASLClientCache = [[NSCache alloc] init];
/* If this is a debug build, set the default filtering to include everything.
Otherwise, default to logging all messages from PANIC up to NOTICE level, excluding INFO and DEBUG level messages.
*/
#if DEBUG
_severityFilterMask = ASL_FILTER_MASK_UPTO (ASL_LEVEL_DEBUG);
#else
_severityFilterMask = ASL_FILTER_MASK_UPTO (ASL_LEVEL_NOTICE);
#endif
return self;
}
- (id) init
{
return [self initWithFacility:nil options:SOLoggerDefaultASLOptions];
}
- (void) dealloc;
{
/* Broadcast to dependent ASL clients that we're going away */
[[NSNotificationCenter defaultCenter] postNotificationName:SOLoggerWillDieNotification object:self];
#if NO_ARC
[_ASLClientCache release];
[_facility release];
[_extraLoggingDescriptors release];
#endif
_ASLClientCache = nil;
_facility = nil;
_extraLoggingDescriptors = nil;
#if SOLOGGER_DEBUG
NSThread *currentThread = [NSThread currentThread];
NSLog(@"Deallocating %@ on %@ thread %p", self, ([currentThread isMainThread] ? @"main" : @"background"), currentThread);
#endif
#if NO_ARC
[super dealloc];
#endif
}
/*
Search the current thread's threadDictionary for a cached ASL client reference. Create and configure one if not found.
*/
- (aslclient) aslclientRef
{
NSString *threadKey = [NSString stringWithFormat:@"Thread%p", [NSThread currentThread]];
_SOASLClient *ASLClient = [_ASLClientCache objectForKey:threadKey];
/* If no ASL connection cached for this thread, create and configure one. */
if (ASLClient == nil)
{
#if SOLOGGER_DEBUG
NSLog (@"%s creating new ASL client for thread %p for logger %@", __PRETTY_FUNCTION__, [NSThread currentThread], self);
#endif
/* Create a new ASL client cover object */
ASLClient = [[_SOASLClient alloc] initWithLogger:self facility:[self facility] options:[self options]];
if (ASLClient)
{
/* Cache the new ASL client
*/
[_ASLClientCache setObject:ASLClient forKey:threadKey];
#if NO_ARC
[ASLClient release];
#endif
/* Configure the connection's filter mask.
*/
asl_set_filter ([ASLClient aslclientRef], [self severityFilterMask]);
// Pass on the set of external logging descriptors
for (NSNumber *descriptor in [self additionalDescriptors])
{
int err = asl_add_log_file ([ASLClient aslclientRef], [descriptor intValue]);
if (err != 0)
{
NSLog (@"%@: asl_add_log_file() failed to add external logging descriptor: %d", self, [descriptor intValue]);
}
}
}
}
aslclient aslclientRef = (ASLClient == nil) ? NULL : [ASLClient aslclientRef];
return aslclientRef;
}
#pragma mark -
#pragma mark Logging
- (void) debug: (NSString *)format, ...;
{
va_list arglist;
va_start (arglist, format);
[self logWithLevel:ASL_LEVEL_DEBUG format:format arguments:arglist];
va_end (arglist);
}
- (void) info: (NSString *)format, ...;
{
va_list arglist;
va_start (arglist, format);
[self logWithLevel:ASL_LEVEL_INFO format:format arguments:arglist];
va_end (arglist);
}
- (void) notice: (NSString *)format, ...;
{
va_list arglist;
va_start (arglist, format);
[self logWithLevel:ASL_LEVEL_NOTICE format:format arguments:arglist];
va_end (arglist);
}
- (void) warning: (NSString *)format, ...;
{
va_list arglist;
va_start (arglist, format);
[self logWithLevel:ASL_LEVEL_WARNING format:format arguments:arglist];
va_end (arglist);
}
- (void) error: (NSString *)format, ...;
{
va_list arglist;
va_start (arglist, format);
[self logWithLevel:ASL_LEVEL_ERR format:format arguments:arglist];
va_end (arglist);
}
- (void) alert: (NSString *)format, ...;
{
va_list arglist;
va_start (arglist, format);
[self logWithLevel:ASL_LEVEL_ALERT format:format arguments:arglist];
va_end (arglist);
}
- (void) critical: (NSString *)format, ...;
{
va_list arglist;
va_start (arglist, format);
[self logWithLevel:ASL_LEVEL_CRIT format:format arguments:arglist];
va_end (arglist);
}
- (void) panic: (NSString *)format, ...;
{
va_list arglist;
va_start (arglist, format);
[self logWithLevel:ASL_LEVEL_EMERG format:format arguments:arglist];
va_end (arglist);
}
#pragma mark -
#pragma mark Logging Primitives
- (void) logWithLevel:(int)aslLevel format:(NSString *)format arguments:(va_list)arguments
{
if (format == nil) return;
aslclient asl = [self aslclientRef];
if (asl == NULL)
{
NSLog (@"%s failed to get ASL client reference for this thread.", __PRETTY_FUNCTION__);
}
else
{
aslmsg msg = asl_new (ASL_TYPE_MSG);
asl_set (msg, ASL_KEY_FACILITY, [[self facility] UTF8String]);
/* asl_log() does not handle the %@ format specifier, so process the format and arguments into an NSString first. */
NSString *text = [[NSString alloc] initWithFormat:format arguments:arguments];
/* Log the text as UTF-8 string */
int err = asl_log (asl, msg, aslLevel, "%s", [text UTF8String]);
if (err != 0)
{
NSLog (@"asl_log() failed to write the message: %@", text);
}
#if NO_ARC
[text release];
#endif
text = nil;
// Cleanup
asl_free (msg);
}
}
#pragma mark -
#pragma mark Additional Logging Descriptors
- (NSSet *) additionalDescriptors
{
NSSet *immutable = nil;
@synchronized (_extraLoggingDescriptors)
{
immutable = [NSSet setWithSet: _extraLoggingDescriptors];
}
return immutable;
}
- (void) addDescriptor:(int)fd
{
@synchronized (_extraLoggingDescriptors)
{
NSNumber *descriptor = [NSNumber numberWithInt:fd];
/* Add the new descriptor to the current thread's ASL connection. */
aslclient asl = [self aslclientRef];
if (asl)
{
/* Cache the descriptor in our private list */
[_extraLoggingDescriptors addObject:descriptor];
/* Broadcast the logger change to associated ASL clients. */
[[NSNotificationCenter defaultCenter] postNotificationName:SOLoggerDidAddDescriptorNotification object:self userInfo:[NSDictionary dictionaryWithObject:descriptor forKey:@"descriptor"]];
}
}
}
- (void) removeDescriptor:(int)fd
{
@synchronized (_extraLoggingDescriptors)
{
/* Remove the descriptor from our private list, regardless of how things went with ASL. */
NSNumber *descriptor = [NSNumber numberWithInt:fd];
[_extraLoggingDescriptors removeObject:descriptor];
/* Broadcast the logger change to associated ASL clients. */
[[NSNotificationCenter defaultCenter] postNotificationName:SOLoggerDidRemoveDescriptorNotification object:self userInfo:[NSDictionary dictionaryWithObject:descriptor forKey:@"descriptor"]];
}
}
#pragma mark -
#pragma mark Accessors
- (int) severityFilterMask
{
int mask = 0;
@synchronized (self)
{
mask = _severityFilterMask;
}
return mask;
}
- (void) setSeverityFilterMask:(int)newMask
{
@synchronized (self)
{
_severityFilterMask = newMask;
[[NSNotificationCenter defaultCenter] postNotificationName:SOLoggerDidChangeSeverityFilterNotification object:self userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:newMask] forKey:@"severityFilterMask"]];
}
}
@end
#if SOLOGGER_DEBUG
static inline void LOG_ASLCLIENT(const char *where, aslclient ASLClient)
{
NSThread *currentThread = [NSThread currentThread];
NSLog (@"%s aslclient %p on %@ thread %p", where, ASLClient, ([currentThread isEqual:[NSThread mainThread]] ? @"main" : @"background"), currentThread);
}
#endif
#pragma mark -
#pragma mark License
/*
Copyright (c) 2009-2012, Standard Orbit Software, LLC. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the Standard Orbit Software, LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/