|
19 | 19 | #import <GoogleUtilities/GULAppEnvironmentUtil.h> |
20 | 20 | #import "Public/GULLoggerLevel.h" |
21 | 21 |
|
| 22 | +NSString *const kGULLoggerErrorCountKey = @"kGULLoggerErrorCountKey"; |
| 23 | +NSString *const kGULLoggerWarningCountKey = @"kGULLoggerWarningCountKey"; |
| 24 | + |
22 | 25 | /// ASL client facility name used by GULLogger. |
23 | 26 | const char *kGULLoggerASLClientFacilityName = "com.google.utilities.logger"; |
24 | 27 |
|
|
43 | 46 | static NSRegularExpression *sMessageCodeRegex; |
44 | 47 | #endif |
45 | 48 |
|
| 49 | +void GULIncrementLogCountForLevel(GULLoggerLevel level); |
| 50 | + |
46 | 51 | void GULLoggerInitializeASL(void) { |
47 | 52 | dispatch_once(&sGULLoggerOnceToken, ^{ |
48 | 53 | NSInteger majorOSVersion = [[GULAppEnvironmentUtil systemVersion] integerValue]; |
@@ -149,6 +154,10 @@ void GULLogBasic(GULLoggerLevel level, |
149 | 154 | NSString *message, |
150 | 155 | va_list args_ptr) { |
151 | 156 | GULLoggerInitializeASL(); |
| 157 | + |
| 158 | + // Keep count of how many errors and warnings are triggered. |
| 159 | + GULIncrementLogCountForLevel(level); |
| 160 | + |
152 | 161 | if (!(level <= sGULLoggerMaximumLevel || sGULLoggerDebugMode || forceLog)) { |
153 | 162 | return; |
154 | 163 | } |
@@ -194,6 +203,70 @@ void GULLogBasic(GULLoggerLevel level, |
194 | 203 |
|
195 | 204 | #undef GUL_MAKE_LOGGER |
196 | 205 |
|
| 206 | +#pragma mark - Number of errors and warnings |
| 207 | + |
| 208 | +NSUserDefaults *getGULLoggerUsetDefaults(void) { |
| 209 | + static NSUserDefaults *_userDefaults = nil; |
| 210 | + static dispatch_once_t onceToken; |
| 211 | + dispatch_once(&onceToken, ^{ |
| 212 | + _userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"GoogleUtilities.Logger.GULLogger"]; |
| 213 | + }); |
| 214 | + |
| 215 | + return _userDefaults; |
| 216 | +} |
| 217 | + |
| 218 | +dispatch_queue_t getGULLoggerCounterQueue(void) { |
| 219 | + static dispatch_queue_t queue; |
| 220 | + static dispatch_once_t onceToken; |
| 221 | + dispatch_once(&onceToken, ^{ |
| 222 | + queue = dispatch_queue_create("GoogleUtilities.GULLogger.counterQueue", DISPATCH_QUEUE_SERIAL); |
| 223 | + dispatch_set_target_queue(queue, |
| 224 | + dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)); |
| 225 | + }); |
| 226 | + |
| 227 | + return queue; |
| 228 | +} |
| 229 | + |
| 230 | +void GULAsyncGetUserDefaultsIntegerForKey(NSString *key, void (^completion)(NSInteger)) { |
| 231 | + dispatch_async(getGULLoggerCounterQueue(), ^{ |
| 232 | + NSInteger count = [getGULLoggerUsetDefaults() integerForKey:key]; |
| 233 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 234 | + completion(count); |
| 235 | + }); |
| 236 | + }); |
| 237 | +} |
| 238 | + |
| 239 | +void GULNumberOfErrorsLogged(void (^completion)(NSInteger)) { |
| 240 | + GULAsyncGetUserDefaultsIntegerForKey(kGULLoggerErrorCountKey, completion); |
| 241 | +} |
| 242 | + |
| 243 | +void GULNumberOfWarningsLogged(void (^completion)(NSInteger)) { |
| 244 | + GULAsyncGetUserDefaultsIntegerForKey(kGULLoggerWarningCountKey, completion); |
| 245 | +} |
| 246 | + |
| 247 | +void GULResetNumberOfIssuesLogged(void) { |
| 248 | + dispatch_async(getGULLoggerCounterQueue(), ^{ |
| 249 | + [getGULLoggerUsetDefaults() setInteger:0 forKey:kGULLoggerErrorCountKey]; |
| 250 | + [getGULLoggerUsetDefaults() setInteger:0 forKey:kGULLoggerWarningCountKey]; |
| 251 | + }); |
| 252 | +} |
| 253 | + |
| 254 | +void GULIncrementUserDefaultsIntegerForKey(NSString *key) { |
| 255 | + NSUserDefaults *defaults = getGULLoggerUsetDefaults(); |
| 256 | + NSInteger errorCount = [defaults integerForKey:key]; |
| 257 | + [defaults setInteger:errorCount + 1 forKey:key]; |
| 258 | +} |
| 259 | + |
| 260 | +void GULIncrementLogCountForLevel(GULLoggerLevel level) { |
| 261 | + dispatch_async(getGULLoggerCounterQueue(), ^{ |
| 262 | + if (level == GULLoggerLevelError) { |
| 263 | + GULIncrementUserDefaultsIntegerForKey(kGULLoggerErrorCountKey); |
| 264 | + } else if (level == GULLoggerLevelWarning) { |
| 265 | + GULIncrementUserDefaultsIntegerForKey(kGULLoggerWarningCountKey); |
| 266 | + } |
| 267 | + }); |
| 268 | +} |
| 269 | + |
197 | 270 | #pragma mark - GULLoggerWrapper |
198 | 271 |
|
199 | 272 | @implementation GULLoggerWrapper |
|
0 commit comments