Improve log cleanup on launch

This commit is contained in:
Michelle Linington 2022-04-14 16:23:21 -07:00
parent 3c7f6db203
commit c19fa8b950
5 changed files with 43 additions and 2 deletions

View File

@ -1267,7 +1267,7 @@ static void uncaughtExceptionHandler(NSException *exception)
[SignalApp.shared ensureRootViewController:launchStartedAt];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{ [DebugLogger.shared removeObsoleteDebugLogs]; });
^{ [DebugLogger.shared postLaunchLogCleanup]; });
}
- (void)registrationStateDidChange

View File

@ -22,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)wipeLogs;
- (void)removeObsoleteDebugLogs;
- (void)postLaunchLogCleanup;
- (NSArray<NSString *> *)allLogFilePaths;

View File

@ -9,6 +9,7 @@
#import <CocoaLumberjack/DDTTYLogger.h>
#import <SignalCoreKit/NSDate+OWS.h>
#import <SignalServiceKit/AppContext.h>
#import <SignalServiceKit/AppVersion.h>
#import <SignalServiceKit/OWSFileSystem.h>
#import <SignalServiceKit/SignalServiceKit-Swift.h>
#import <SignalServiceKit/TestAppContext.h>
@ -229,6 +230,21 @@ const NSUInteger kMaxDebugLogFileSize = 1024 * 1024 * 3;
}
}
static NSString *const kLastCleanupVersionKey = @"kLastCleanupVersionKey";
static NSString *const kFirstValidLogVersion = @"5.35.1.2";
- (void)postLaunchLogCleanup
{
NSString *lastCleanupVersion = [CurrentAppContext().appUserDefaults stringForKey:kLastCleanupVersionKey] ?: @"0";
if ([AppVersion compareAppVersion:lastCleanupVersion with:kFirstValidLogVersion] == NSOrderedAscending) {
[self wipeLogs];
OWSLogWarn(@"Wiped logs. (%@ < %@)", lastCleanupVersion, kFirstValidLogVersion);
}
[self removeObsoleteDebugLogs];
[CurrentAppContext().appUserDefaults setObject:AppVersion.shared.currentAppVersion4 forKey:kLastCleanupVersionKey];
}
@end
#pragma mark -

View File

@ -55,6 +55,8 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)isFirstLaunch;
+ (NSComparisonResult)compareAppVersion:(NSString *)lhs with:(NSString *)rhs;
@end
NS_ASSUME_NONNULL_END

View File

@ -215,6 +215,29 @@ NSString *const kNSUserDefaults_LastCompletedLaunchAppVersion_NSE
return self.firstAppVersion != nil;
}
+ (NSComparisonResult)compareAppVersion:(NSString *)lhs with:(NSString *)rhs
{
// It might be nice to have a first-class version struct that's comparable, but it's not important right now.
NSArray<NSString *> *lhsComponents = [lhs componentsSeparatedByString:@"."];
NSArray<NSString *> *rhsComponents = [rhs componentsSeparatedByString:@"."];
NSUInteger largestIdx = MAX(lhsComponents.count, rhsComponents.count);
for (NSInteger idx = 0; idx < largestIdx; idx++) {
// If we run off the end of an array, we'll assume zero for the component segment
NSString *lhsComponentString = (idx < lhsComponents.count) ? lhsComponents[idx] : nil;
NSString *rhsComponentString = (idx < rhsComponents.count) ? rhsComponents[idx] : nil;
NSInteger lhsComponent = [lhsComponentString integerValue];
NSInteger rhsComponent = [rhsComponentString integerValue];
if (lhsComponent != rhsComponent) {
return (lhsComponent < rhsComponent) ? NSOrderedAscending : NSOrderedDescending;
}
}
// If we get here, the versions are effectively equal
return NSOrderedSame;
}
@end
NS_ASSUME_NONNULL_END