Skip to content

Commit 076195f

Browse files
maksymmalyhinCorrob
authored andcommitted
GULLogger - count errors and warnigns (#2601)
1 parent ae1251e commit 076195f

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

GoogleUtilities/Example/Tests/Logger/GULLoggerTest.m

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
extern BOOL getGULLoggerDebugMode(void);
3434

35+
extern NSUserDefaults *getGULLoggerUsetDefaults(void);
36+
3537
static NSString *const kMessageCode = @"I-COR000001";
3638

3739
@interface GULLoggerTest : XCTestCase
@@ -153,6 +155,85 @@ - (void)testGULLoggerLevelValues {
153155
XCTAssertEqual(GULLoggerLevelDebug, ASL_LEVEL_DEBUG);
154156
}
155157

158+
- (void)testGetErrorWarningNumberBeforeLogDontCrash {
159+
GULResetLogger();
160+
161+
XCTestExpectation *getErrorCountExpectation =
162+
[self expectationWithDescription:@"getErrorCountExpectation"];
163+
XCTestExpectation *getWarningsCountExpectation =
164+
[self expectationWithDescription:@"getWarningsCountExpectation"];
165+
166+
GULNumberOfErrorsLogged(^(NSInteger count) {
167+
[getErrorCountExpectation fulfill];
168+
});
169+
170+
GULNumberOfWarningsLogged(^(NSInteger count) {
171+
[getWarningsCountExpectation fulfill];
172+
});
173+
174+
[self waitForExpectations:@[ getErrorCountExpectation, getWarningsCountExpectation ]
175+
timeout:0.5
176+
enforceOrder:YES];
177+
}
178+
179+
- (void)testErrorNumberIncrement {
180+
[getGULLoggerUsetDefaults() setInteger:10 forKey:kGULLoggerErrorCountKey];
181+
182+
GULLogError(@"my service", NO, kMessageCode, @"Message.");
183+
184+
XCTestExpectation *getErrorCountExpectation =
185+
[self expectationWithDescription:@"getErrorCountExpectation"];
186+
187+
GULNumberOfErrorsLogged(^(NSInteger count) {
188+
[getErrorCountExpectation fulfill];
189+
XCTAssertEqual(count, 11);
190+
});
191+
192+
[self waitForExpectationsWithTimeout:0.5 handler:NULL];
193+
}
194+
195+
- (void)testWarningNumberIncrement {
196+
[getGULLoggerUsetDefaults() setInteger:5 forKey:kGULLoggerWarningCountKey];
197+
198+
GULLogWarning(@"my service", NO, kMessageCode, @"Message.");
199+
200+
XCTestExpectation *getWarningsCountExpectation =
201+
[self expectationWithDescription:@"getWarningsCountExpectation"];
202+
203+
GULNumberOfWarningsLogged(^(NSInteger count) {
204+
[getWarningsCountExpectation fulfill];
205+
XCTAssertEqual(count, 6);
206+
});
207+
208+
[self waitForExpectationsWithTimeout:0.5 handler:NULL];
209+
}
210+
211+
- (void)testResetIssuesCount {
212+
[getGULLoggerUsetDefaults() setInteger:3 forKey:kGULLoggerErrorCountKey];
213+
[getGULLoggerUsetDefaults() setInteger:4 forKey:kGULLoggerWarningCountKey];
214+
215+
GULResetNumberOfIssuesLogged();
216+
217+
XCTestExpectation *getErrorCountExpectation =
218+
[self expectationWithDescription:@"getErrorCountExpectation"];
219+
XCTestExpectation *getWarningsCountExpectation =
220+
[self expectationWithDescription:@"getWarningsCountExpectation"];
221+
222+
GULNumberOfErrorsLogged(^(NSInteger count) {
223+
[getErrorCountExpectation fulfill];
224+
XCTAssertEqual(count, 0);
225+
});
226+
227+
GULNumberOfWarningsLogged(^(NSInteger count) {
228+
[getWarningsCountExpectation fulfill];
229+
XCTAssertEqual(count, 0);
230+
});
231+
232+
[self waitForExpectations:@[ getErrorCountExpectation, getWarningsCountExpectation ]
233+
timeout:0.5
234+
enforceOrder:YES];
235+
}
236+
156237
// Helper functions.
157238
- (BOOL)logExists {
158239
[self drainGULClientQueue];

GoogleUtilities/Logger/GULLogger.m

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#import <GoogleUtilities/GULAppEnvironmentUtil.h>
2020
#import "Public/GULLoggerLevel.h"
2121

22+
NSString *const kGULLoggerErrorCountKey = @"kGULLoggerErrorCountKey";
23+
NSString *const kGULLoggerWarningCountKey = @"kGULLoggerWarningCountKey";
24+
2225
/// ASL client facility name used by GULLogger.
2326
const char *kGULLoggerASLClientFacilityName = "com.google.utilities.logger";
2427

@@ -43,6 +46,8 @@
4346
static NSRegularExpression *sMessageCodeRegex;
4447
#endif
4548

49+
void GULIncrementLogCountForLevel(GULLoggerLevel level);
50+
4651
void GULLoggerInitializeASL(void) {
4752
dispatch_once(&sGULLoggerOnceToken, ^{
4853
NSInteger majorOSVersion = [[GULAppEnvironmentUtil systemVersion] integerValue];
@@ -149,6 +154,10 @@ void GULLogBasic(GULLoggerLevel level,
149154
NSString *message,
150155
va_list args_ptr) {
151156
GULLoggerInitializeASL();
157+
158+
// Keep count of how many errors and warnings are triggered.
159+
GULIncrementLogCountForLevel(level);
160+
152161
if (!(level <= sGULLoggerMaximumLevel || sGULLoggerDebugMode || forceLog)) {
153162
return;
154163
}
@@ -194,6 +203,70 @@ void GULLogBasic(GULLoggerLevel level,
194203

195204
#undef GUL_MAKE_LOGGER
196205

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+
197270
#pragma mark - GULLoggerWrapper
198271

199272
@implementation GULLoggerWrapper

GoogleUtilities/Logger/Private/GULLogger.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ NS_ASSUME_NONNULL_BEGIN
2525
*/
2626
typedef NSString *const GULLoggerService;
2727

28+
/**
29+
* The key used to store the logger's error count.
30+
*/
31+
extern NSString *const kGULLoggerErrorCountKey;
32+
33+
/**
34+
* The key used to store the logger's warning count.
35+
*/
36+
extern NSString *const kGULLoggerWarningCountKey;
37+
2838
#ifdef __cplusplus
2939
extern "C" {
3040
#endif // __cplusplus
@@ -129,6 +139,21 @@ extern void GULLogDebug(GULLoggerService service,
129139
NSString *message,
130140
...) NS_FORMAT_FUNCTION(4, 5);
131141

142+
/**
143+
* Retrieve the number of errors that have been logged since the stat was last reset.
144+
*/
145+
extern void GULNumberOfErrorsLogged(void (^completion)(NSInteger));
146+
147+
/**
148+
* Retrieve the number of warnings that have been logged since the stat was last reset.
149+
*/
150+
extern void GULNumberOfWarningsLogged(void (^completion)(NSInteger));
151+
152+
/**
153+
* Reset number of errors and warnings that have been logged to 0.
154+
*/
155+
extern void GULResetNumberOfIssuesLogged(void);
156+
132157
#ifdef __cplusplus
133158
} // extern "C"
134159
#endif // __cplusplus

0 commit comments

Comments
 (0)