|
8 | 8 |
|
9 | 9 | #import "AILogger.h"
|
10 | 10 |
|
11 |
| -static AILogger *defaultLogger; |
| 11 | +static NSString * const kLogTag = @"AdjustIo"; |
| 12 | + |
| 13 | +static AILogLevel staticLogLevel = AILogLevelInfo; |
12 | 14 |
|
13 | 15 |
|
14 | 16 | #pragma mark -
|
15 | 17 | @implementation AILogger
|
16 | 18 |
|
17 |
| -+ (void)setLogTag:(NSString *)logTag { |
18 |
| - AILogger.getDefaultLogger.logTag = logTag; |
19 |
| -} |
20 |
| - |
21 | 19 | + (void)setLogLevel:(AILogLevel)logLevel {
|
22 |
| - AILogger.getDefaultLogger.logLevel = logLevel; |
23 |
| -} |
24 |
| - |
25 |
| -+ (AILogger *)loggerWithTag:(NSString *)logTag { |
26 |
| - return [[AILogger alloc] initWithTag:logTag]; |
27 |
| -} |
28 |
| - |
29 |
| -- (id)initWithTag:(NSString *)tag { |
30 |
| - self = [super init]; |
31 |
| - if (self == nil) return nil; |
32 |
| - |
33 |
| - self.logTag = tag; |
34 |
| - self.logLevel = AILogLevelInfo; |
35 |
| - |
36 |
| - return self; |
37 |
| -} |
38 |
| - |
39 |
| -- (void)verbose:(NSString *)format, ... { |
40 |
| - if (self.logLevel > AILogLevelVerbose) return; |
41 |
| - va_list parameters; va_start(parameters, format); |
42 |
| - [self logLevel:@"v" format:format parameters:parameters]; |
43 |
| -} |
44 |
| - |
45 |
| -- (void)debug:(NSString *)format, ... { |
46 |
| - if (self.logLevel > AILogLevelDebug) return; |
47 |
| - va_list parameters; va_start(parameters, format); |
48 |
| - [self logLevel:@"d" format:format parameters:parameters]; |
49 |
| -} |
50 |
| - |
51 |
| -- (void)info:(NSString *)format, ... { |
52 |
| - if (self.logLevel > AILogLevelInfo) return; |
53 |
| - va_list parameters; va_start(parameters, format); |
54 |
| - [self logLevel:@"i" format:format parameters:parameters]; |
55 |
| -} |
56 |
| - |
57 |
| -- (void)warn:(NSString *)format, ... { |
58 |
| - if (self.logLevel > AILogLevelWarn) return; |
59 |
| - va_list parameters; va_start(parameters, format); |
60 |
| - [self logLevel:@"w" format:format parameters:parameters]; |
61 |
| -} |
62 |
| - |
63 |
| -- (void)error:(NSString *)format, ... { |
64 |
| - if (self.logLevel > AILogLevelError) return; |
65 |
| - va_list parameters; va_start(parameters, format); |
66 |
| - [self logLevel:@"e" format:format parameters:parameters]; |
67 |
| -} |
68 |
| - |
69 |
| -- (void)assert: (NSString *)format, ... { |
70 |
| - if (self.logLevel > AILogLevelAssert) return; |
71 |
| - va_list parameters; va_start(parameters, format); |
72 |
| - [self logLevel:@"a" format:format parameters:parameters]; |
| 20 | + staticLogLevel = logLevel; |
73 | 21 | }
|
74 | 22 |
|
75 | 23 | + (void)verbose:(NSString *)format, ... {
|
76 |
| - if (AILogger.getDefaultLogger.logLevel > AILogLevelVerbose) return; |
| 24 | + if (staticLogLevel > AILogLevelVerbose) return; |
77 | 25 | va_list parameters; va_start(parameters, format);
|
78 |
| - [AILogger.getDefaultLogger logLevel:@"v" format:format parameters:parameters]; |
| 26 | + [self logLevel:@"v" format:format parameters:parameters]; |
79 | 27 | }
|
80 | 28 |
|
81 | 29 | + (void)debug:(NSString *)format, ... {
|
82 |
| - if (AILogger.getDefaultLogger.logLevel > AILogLevelDebug) return; |
| 30 | + if (staticLogLevel > AILogLevelDebug) return; |
83 | 31 | va_list parameters; va_start(parameters, format);
|
84 |
| - [AILogger.getDefaultLogger logLevel:@"d" format:format parameters:parameters]; |
| 32 | + [self logLevel:@"d" format:format parameters:parameters]; |
85 | 33 | }
|
86 | 34 |
|
87 | 35 | + (void)info:(NSString *)format, ... {
|
88 |
| - if (AILogger.getDefaultLogger.logLevel > AILogLevelInfo) return; |
| 36 | + if (staticLogLevel > AILogLevelInfo) return; |
89 | 37 | va_list parameters; va_start(parameters, format);
|
90 |
| - [AILogger.getDefaultLogger logLevel:@"i" format:format parameters:parameters]; |
| 38 | + [self logLevel:@"i" format:format parameters:parameters]; |
91 | 39 | }
|
92 | 40 |
|
93 | 41 | + (void)warn:(NSString *)format, ... {
|
94 |
| - if (AILogger.getDefaultLogger.logLevel > AILogLevelWarn) return; |
| 42 | + if (staticLogLevel > AILogLevelWarn) return; |
95 | 43 | va_list parameters; va_start(parameters, format);
|
96 |
| - [AILogger.getDefaultLogger logLevel:@"w" format:format parameters:parameters]; |
| 44 | + [self logLevel:@"w" format:format parameters:parameters]; |
97 | 45 | }
|
98 | 46 |
|
99 | 47 | + (void)error:(NSString *)format, ... {
|
100 |
| - if (AILogger.getDefaultLogger.logLevel > AILogLevelError) return; |
| 48 | + if (staticLogLevel > AILogLevelError) return; |
101 | 49 | va_list parameters; va_start(parameters, format);
|
102 |
| - [AILogger.getDefaultLogger logLevel:@"e" format:format parameters:parameters]; |
| 50 | + [self logLevel:@"e" format:format parameters:parameters]; |
103 | 51 | }
|
104 | 52 |
|
105 | 53 | + (void)assert:(NSString *)format, ... {
|
106 |
| - if (AILogger.getDefaultLogger.logLevel > AILogLevelAssert) return; |
| 54 | + if (staticLogLevel > AILogLevelAssert) return; |
107 | 55 | va_list parameters; va_start(parameters, format);
|
108 |
| - [AILogger.getDefaultLogger logLevel:@"e" format:format parameters:parameters]; |
| 56 | + [self logLevel:@"a" format:format parameters:parameters]; |
109 | 57 | }
|
110 | 58 |
|
111 |
| -- (void)logLevel:(NSString *)logLevel format:(NSString *)format parameters:(va_list) parameters { |
| 59 | +// private implementation |
| 60 | ++ (void)logLevel:(NSString *)logLevel format:(NSString *)format parameters:(va_list)parameters { |
112 | 61 | NSString *string = [[NSString alloc] initWithFormat:format arguments:parameters];
|
113 | 62 | va_end(parameters);
|
114 | 63 |
|
115 | 64 | NSArray *lines = [string componentsSeparatedByString:@"\n"];
|
116 | 65 | for (NSString *line in lines) {
|
117 |
| - NSLog(@"\t[%@]%@: %@", self.logTag, logLevel, line); |
118 |
| - } |
119 |
| -} |
120 |
| - |
121 |
| -#pragma mark private |
122 |
| -+ (AILogger *)getDefaultLogger { |
123 |
| - if (defaultLogger == nil) { |
124 |
| - defaultLogger = [AILogger loggerWithTag:@"AdjustIo"]; |
| 66 | + NSLog(@"\t[%@]%@: %@", kLogTag, logLevel, line); |
125 | 67 | }
|
126 |
| - return defaultLogger; |
127 | 68 | }
|
128 | 69 |
|
129 | 70 | @end
|
0 commit comments