Skip to content

Commit

Permalink
changed way feature flag worked
Browse files Browse the repository at this point in the history
  • Loading branch information
KennyHuRadar committed Dec 11, 2023
1 parent 81e1404 commit 40ecb97
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions RadarSDK/RadarLogBuffer.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

@implementation RadarLogBuffer {
NSMutableArray<RadarLog *> *mutableLogBuffer;
NSMutableArray<RadarLog *> *inMemoryLogBuffer;
BOOL useLogPersistence;
}

Expand All @@ -30,6 +31,7 @@ - (instancetype)init {
RadarFeatureSettings *featureSettings = [RadarSettings featureSettings];
useLogPersistence = featureSettings.useLogPersistence;
mutableLogBuffer = [NSMutableArray<RadarLog *> new];
inMemoryLogBuffer = [NSMutableArray<RadarLog *> new];
if (useLogPersistence) {
NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
self.logFileDir = [documentsDirectory stringByAppendingPathComponent:@"radar_logs"];
Expand All @@ -54,32 +56,31 @@ + (instancetype)sharedInstance {

- (void)write:(RadarLogLevel)level type:(RadarLogType)type message:(NSString *)message {
@synchronized (self) {
NSUInteger logLength = [mutableLogBuffer count];
if (logLength >= MAX_BUFFER_SIZE) {
[self purgeOldestLogs];
}
// add new log to buffer
RadarLog *radarLog = [[RadarLog alloc] initWithLevel:level type:type message:message];
[mutableLogBuffer addObject:radarLog];

if (useLogPersistence) {
RadarLog *radarLog = [[RadarLog alloc] initWithLevel:level type:type message:message];
[mutableLogBuffer addObject:radarLog];
NSUInteger logLength = [mutableLogBuffer count];
[inMemoryLogBuffer addObject:radarLog];
NSUInteger logLength = [inMemoryLogBuffer count];
if (logLength >= MAX_MEMORY_BUFFER_SIZE) {
[self persistLogs];
}
}
else {
NSUInteger logLength = [mutableLogBuffer count];
if (logLength >= MAX_BUFFER_SIZE) {
[self purgeOldestLogs];
}
// add new log to buffer
RadarLog *radarLog = [[RadarLog alloc] initWithLevel:level type:type message:message];
[mutableLogBuffer addObject:radarLog];
}
}
}
}

- (void)persistLogs {
@synchronized (self) {
if (useLogPersistence) {
NSArray *flushableLogs = [mutableLogBuffer copy];
NSArray *flushableLogs = [inMemoryLogBuffer copy];
[self addLogsToBuffer:flushableLogs];
[mutableLogBuffer removeAllObjects];
[inMemoryLogBuffer removeAllObjects];
}
}
}
Expand Down Expand Up @@ -115,12 +116,10 @@ - (void)writeToFileStorage:(NSArray <RadarLog *> *)logs {

- (void)append:(RadarLogLevel)level type:(RadarLogType)type message:(NSString *)message {
@synchronized (self) {
[self write:level type:type message:message];
if (useLogPersistence){
[self writeToFileStorage:@[[[RadarLog alloc] initWithLevel:level type:type message:message]]];
}
else {
[self write:level type:type message:message];
}
}
}

Expand All @@ -147,6 +146,7 @@ - (void)purgeOldestLogs {

- (void)removeLogsFromBuffer:(NSUInteger)numLogs {
@synchronized (self) {
[mutableLogBuffer removeObjectsInRange:NSMakeRange(0, numLogs)];
if (useLogPersistence) {
NSArray<NSString *> *files = [self.fileHandler allFilesInDirectory:self.logFileDir];
numLogs = MIN(numLogs, [files count]);
Expand All @@ -155,14 +155,13 @@ - (void)removeLogsFromBuffer:(NSUInteger)numLogs {
NSString *filePath = [self.logFileDir stringByAppendingPathComponent:file];
[self.fileHandler deleteFileAtPath:filePath];
}
} else {
[mutableLogBuffer removeObjectsInRange:NSMakeRange(0, numLogs)];
}
}
}

- (void)addLogsToBuffer:(NSArray<RadarLog *> *)logs {
@synchronized (self) {
[mutableLogBuffer addObjectsFromArray:logs];
if (useLogPersistence) {
NSArray<NSString *> *files = [self.fileHandler allFilesInDirectory:self.logFileDir];
NSUInteger bufferSize = [files count];
Expand All @@ -176,16 +175,14 @@ - (void)addLogsToBuffer:(NSArray<RadarLog *> *)logs {
[self writeToFileStorage:@[purgeLog]];
}
[self writeToFileStorage:logs];
} else {
[mutableLogBuffer addObjectsFromArray:logs];
}
}
}
}

//for use in testing
-(void)clearBuffer {
@synchronized (self) {
[mutableLogBuffer removeAllObjects];
[inMemoryLogBuffer removeAllObjects];
NSArray<NSString *> *files = [self.fileHandler allFilesInDirectory:self.logFileDir];
if (files) {
for (NSString *file in files) {
Expand Down

0 comments on commit 40ecb97

Please sign in to comment.