Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pmbaty | 1 | #import "DDASLLogger.h" |
2 | |||
3 | #import <libkern/OSAtomic.h> |
||
4 | |||
5 | /** |
||
6 | * Welcome to Cocoa Lumberjack! |
||
7 | * |
||
8 | * The project page has a wealth of documentation if you have any questions. |
||
9 | * https://github.com/CocoaLumberjack/CocoaLumberjack |
||
10 | * |
||
11 | * If you're new to the project you may wish to read the "Getting Started" wiki. |
||
12 | * https://github.com/CocoaLumberjack/CocoaLumberjack/wiki/GettingStarted |
||
13 | **/ |
||
14 | |||
15 | #if ! __has_feature(objc_arc) |
||
16 | #warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). |
||
17 | #endif |
||
18 | |||
19 | |||
20 | @implementation DDASLLogger |
||
21 | |||
22 | static DDASLLogger *sharedInstance; |
||
23 | |||
24 | /** |
||
25 | * The runtime sends initialize to each class in a program exactly one time just before the class, |
||
26 | * or any class that inherits from it, is sent its first message from within the program. (Thus the |
||
27 | * method may never be invoked if the class is not used.) The runtime sends the initialize message to |
||
28 | * classes in a thread-safe manner. Superclasses receive this message before their subclasses. |
||
29 | * |
||
30 | * This method may also be called directly (assumably by accident), hence the safety mechanism. |
||
31 | **/ |
||
32 | + (void)initialize |
||
33 | { |
||
34 | static BOOL initialized = NO; |
||
35 | if (!initialized) |
||
36 | { |
||
37 | initialized = YES; |
||
38 | |||
39 | sharedInstance = [[[self class] alloc] init]; |
||
40 | } |
||
41 | } |
||
42 | |||
43 | + (instancetype)sharedInstance |
||
44 | { |
||
45 | return sharedInstance; |
||
46 | } |
||
47 | |||
48 | - (id)init |
||
49 | { |
||
50 | if (sharedInstance != nil) |
||
51 | { |
||
52 | return nil; |
||
53 | } |
||
54 | |||
55 | if ((self = [super init])) |
||
56 | { |
||
57 | // A default asl client is provided for the main thread, |
||
58 | // but background threads need to create their own client. |
||
59 | |||
60 | client = asl_open(NULL, "com.apple.console", 0); |
||
61 | } |
||
62 | return self; |
||
63 | } |
||
64 | |||
65 | - (void)logMessage:(DDLogMessage *)logMessage |
||
66 | { |
||
67 | NSString *logMsg = logMessage->logMsg; |
||
68 | |||
69 | if (formatter) |
||
70 | { |
||
71 | logMsg = [formatter formatLogMessage:logMessage]; |
||
72 | } |
||
73 | |||
74 | if (logMsg) |
||
75 | { |
||
76 | const char *msg = [logMsg UTF8String]; |
||
77 | |||
78 | int aslLogLevel; |
||
79 | switch (logMessage->logFlag) |
||
80 | { |
||
81 | // Note: By default ASL will filter anything above level 5 (Notice). |
||
82 | // So our mappings shouldn't go above that level. |
||
83 | |||
84 | case LOG_FLAG_ERROR : aslLogLevel = ASL_LEVEL_ALERT; break; |
||
85 | case LOG_FLAG_WARN : aslLogLevel = ASL_LEVEL_CRIT; break; |
||
86 | case LOG_FLAG_INFO : aslLogLevel = ASL_LEVEL_ERR; break; |
||
87 | case LOG_FLAG_DEBUG : aslLogLevel = ASL_LEVEL_WARNING; break; |
||
88 | default : aslLogLevel = ASL_LEVEL_NOTICE; break; |
||
89 | } |
||
90 | |||
91 | asl_log(client, NULL, aslLogLevel, "%s", msg); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | - (NSString *)loggerName |
||
96 | { |
||
97 | return @"cocoa.lumberjack.aslLogger"; |
||
98 | } |
||
99 | |||
100 | @end |