From bf21d0c0ede464a055f17d7d2061d3ac512fcca5 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 1 Dec 2017 12:20:09 -0500 Subject: [PATCH] Enable logging in SAE; rework log file management to include multiple log directories. --- Signal.xcodeproj/project.pbxproj | 16 +-- Signal/src/AppDelegate.m | 10 +- Signal/src/Profiles/OWSProfileManager.m | 17 +--- .../AdvancedSettingsTableViewController.m | 6 +- .../ConversationViewController.m | 7 +- .../ViewControllers/DebugUI/DebugUIMessages.m | 16 +-- ...WS102MoveLoggingPreferenceToUserDefaults.m | 2 +- Signal/src/util/Pastelog.m | 26 ++--- SignalMessaging/utils/DebugLogger.h | 6 +- SignalMessaging/utils/DebugLogger.m | 99 ++++++++++++------- .../utils}/OWSPreferences.h | 4 +- .../utils}/OWSPreferences.m | 9 +- .../Messages/Attachments/TSAttachmentStream.m | 17 +--- .../src/Storage/TSStorageManager.m | 20 +--- SignalServiceKit/src/Util/MIMETypeUtil.m | 12 +-- SignalServiceKit/src/Util/OWSFileSystem.h | 5 + SignalServiceKit/src/Util/OWSFileSystem.m | 29 ++++++ .../ShareViewController.swift | 37 ++----- .../SignalShareExtension-Bridging-Header.h | 1 + 19 files changed, 159 insertions(+), 180 deletions(-) rename {Signal/src/environment => SignalMessaging/utils}/OWSPreferences.h (97%) rename {Signal/src/environment => SignalMessaging/utils}/OWSPreferences.m (98%) diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index d34df5ebf..6aae4041e 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -46,6 +46,8 @@ 3461284B1FD0B94000532771 /* SAELoadViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3461284A1FD0B93F00532771 /* SAELoadViewController.swift */; }; 346129321FD1A26100532771 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 346129051FD1A25B00532771 /* Localizable.strings */; }; 346129341FD1A88700532771 /* OWSSwiftUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346129331FD1A88700532771 /* OWSSwiftUtils.swift */; }; + 346129391FD1B47300532771 /* OWSPreferences.h in Headers */ = {isa = PBXBuildFile; fileRef = 346129371FD1B47200532771 /* OWSPreferences.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3461293A1FD1B47300532771 /* OWSPreferences.m in Sources */ = {isa = PBXBuildFile; fileRef = 346129381FD1B47200532771 /* OWSPreferences.m */; }; 346B66311F4E29B200E5122F /* CropScaleImageViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346B66301F4E29B200E5122F /* CropScaleImageViewController.swift */; }; 3471B1DA1EB7C63600F6AEC8 /* NewNonContactConversationViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3471B1D91EB7C63600F6AEC8 /* NewNonContactConversationViewController.m */; }; 3472229F1EB22FFE00E53955 /* AddToGroupViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3472229E1EB22FFE00E53955 /* AddToGroupViewController.m */; }; @@ -299,7 +301,6 @@ 76EB054018170B33006006FC /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB03C318170B33006006FC /* AppDelegate.m */; }; 76EB057A18170B33006006FC /* OWSContactsManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB040918170B33006006FC /* OWSContactsManager.m */; }; 76EB058218170B33006006FC /* Environment.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB041318170B33006006FC /* Environment.m */; }; - 76EB058818170B33006006FC /* OWSPreferences.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB041918170B33006006FC /* OWSPreferences.m */; }; 76EB058A18170B33006006FC /* Release.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB041B18170B33006006FC /* Release.m */; }; 76EB063A18170B33006006FC /* FunctionalUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB04EB18170B33006006FC /* FunctionalUtil.m */; }; 76EB063C18170B33006006FC /* NumberUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB04ED18170B33006006FC /* NumberUtil.m */; }; @@ -354,7 +355,6 @@ B660F7171C29988E00687D6E /* OWSContactsManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB040918170B33006006FC /* OWSContactsManager.m */; }; B660F7181C29988E00687D6E /* CryptoTools.m in Sources */ = {isa = PBXBuildFile; fileRef = 7038632418F70C0700D4A43F /* CryptoTools.m */; }; B660F71B1C29988E00687D6E /* Environment.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB041318170B33006006FC /* Environment.m */; }; - B660F71F1C29988E00687D6E /* OWSPreferences.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB041918170B33006006FC /* OWSPreferences.m */; }; B660F7201C29988E00687D6E /* Release.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB041B18170B33006006FC /* Release.m */; }; B660F7211C29988E00687D6E /* SignalKeyingStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = B6B1013B196D213F007E3930 /* SignalKeyingStorage.m */; }; B660F7221C29988E00687D6E /* VersionMigrations.m in Sources */ = {isa = PBXBuildFile; fileRef = B60C16641988999D00E97A6C /* VersionMigrations.m */; }; @@ -573,6 +573,8 @@ 346129301FD1A26000532771 /* tr_TR */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = tr_TR; path = Signal/translations/tr_TR.lproj/Localizable.strings; sourceTree = SOURCE_ROOT; }; 346129311FD1A26100532771 /* es */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = es; path = Signal/translations/es.lproj/Localizable.strings; sourceTree = SOURCE_ROOT; }; 346129331FD1A88700532771 /* OWSSwiftUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OWSSwiftUtils.swift; sourceTree = ""; }; + 346129371FD1B47200532771 /* OWSPreferences.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OWSPreferences.h; sourceTree = ""; }; + 346129381FD1B47200532771 /* OWSPreferences.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OWSPreferences.m; sourceTree = ""; }; 346B66301F4E29B200E5122F /* CropScaleImageViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CropScaleImageViewController.swift; sourceTree = ""; }; 3471B1D81EB7C63600F6AEC8 /* NewNonContactConversationViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NewNonContactConversationViewController.h; sourceTree = ""; }; 3471B1D91EB7C63600F6AEC8 /* NewNonContactConversationViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NewNonContactConversationViewController.m; sourceTree = ""; }; @@ -887,8 +889,6 @@ 76EB040918170B33006006FC /* OWSContactsManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OWSContactsManager.m; sourceTree = ""; }; 76EB041218170B33006006FC /* Environment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Environment.h; sourceTree = ""; }; 76EB041318170B33006006FC /* Environment.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Environment.m; sourceTree = ""; }; - 76EB041818170B33006006FC /* OWSPreferences.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OWSPreferences.h; sourceTree = ""; }; - 76EB041918170B33006006FC /* OWSPreferences.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OWSPreferences.m; sourceTree = ""; }; 76EB041A18170B33006006FC /* Release.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Release.h; sourceTree = ""; }; 76EB041B18170B33006006FC /* Release.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Release.m; sourceTree = ""; }; 76EB04EA18170B33006006FC /* FunctionalUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FunctionalUtil.h; sourceTree = ""; }; @@ -1145,6 +1145,8 @@ 34480B4A1FD0A7A200BC14EF /* OWSLogger.h */, 34480B4B1FD0A7A300BC14EF /* OWSLogger.m */, 34480B481FD0A60200BC14EF /* OWSMath.h */, + 346129371FD1B47200532771 /* OWSPreferences.h */, + 346129381FD1B47200532771 /* OWSPreferences.m */, 34480B4F1FD0A7A300BC14EF /* OWSScrubbingLogFormatter.h */, 34480B511FD0A7A400BC14EF /* OWSScrubbingLogFormatter.m */, 346129331FD1A88700532771 /* OWSSwiftUtils.swift */, @@ -1658,8 +1660,6 @@ 45666F731D9BFDB9008FE134 /* Migrations */, B6258B311C29E2E60014138E /* NotificationsManager.h */, B6258B321C29E2E60014138E /* NotificationsManager.m */, - 76EB041818170B33006006FC /* OWSPreferences.h */, - 76EB041918170B33006006FC /* OWSPreferences.m */, 76EB041A18170B33006006FC /* Release.h */, 76EB041B18170B33006006FC /* Release.m */, B6B1013A196D213F007E3930 /* SignalKeyingStorage.h */, @@ -2052,6 +2052,7 @@ 34480B611FD0A98800BC14EF /* UIColor+OWS.h in Headers */, 453518961FC63DBF00210559 /* SignalMessaging.h in Headers */, 34480B631FD0A98800BC14EF /* UIView+OWS.h in Headers */, + 346129391FD1B47300532771 /* OWSPreferences.h in Headers */, 34480B5B1FD0A7E300BC14EF /* SignalMessaging-Prefix.pch in Headers */, 34480B681FD0AA9400BC14EF /* UIFont+OWS.h in Headers */, ); @@ -2684,6 +2685,7 @@ 34480B621FD0A98800BC14EF /* UIColor+OWS.m in Sources */, 34480B531FD0A7A400BC14EF /* OWSLogger.m in Sources */, 34480B641FD0A98800BC14EF /* UIView+OWS.m in Sources */, + 3461293A1FD1B47300532771 /* OWSPreferences.m in Sources */, 34480B671FD0AA9400BC14EF /* UIFont+OWS.m in Sources */, 346129341FD1A88700532771 /* OWSSwiftUtils.swift in Sources */, 34480B591FD0A7A400BC14EF /* OWSScrubbingLogFormatter.m in Sources */, @@ -2882,7 +2884,6 @@ 34B3F8941E8DF1710035BE1A /* HomeViewController.m in Sources */, 34E8BF381EE9E2FD00F5F4CA /* FingerprintViewScanController.m in Sources */, 346B66311F4E29B200E5122F /* CropScaleImageViewController.swift in Sources */, - 76EB058818170B33006006FC /* OWSPreferences.m in Sources */, 45E5A6991F61E6DE001E4A8A /* MarqueeLabel.swift in Sources */, 34D1F0B01F867BFC0066283D /* OWSSystemMessageCell.m in Sources */, 45A663C51F92EC760027B59E /* GroupTableViewCell.swift in Sources */, @@ -2913,7 +2914,6 @@ B660F7171C29988E00687D6E /* OWSContactsManager.m in Sources */, B660F7181C29988E00687D6E /* CryptoTools.m in Sources */, B660F71B1C29988E00687D6E /* Environment.m in Sources */, - B660F71F1C29988E00687D6E /* OWSPreferences.m in Sources */, 451DA3CA1F148A9F008E2423 /* DeviceSleepManager.swift in Sources */, B660F7201C29988E00687D6E /* Release.m in Sources */, 45BB93391E688E14001E3939 /* UIDevice+featureSupport.swift in Sources */, diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 03bbebfb6..eb0e2cafd 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -94,16 +94,16 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; // This should be the first thing we do. SetCurrentAppContext([MainAppContext new]); - BOOL loggingIsEnabled; + BOOL isLoggingEnabled; #ifdef DEBUG // Specified at Product -> Scheme -> Edit Scheme -> Test -> Arguments -> Environment to avoid things like // the phone directory being looked up during tests. - loggingIsEnabled = TRUE; + isLoggingEnabled = TRUE; [DebugLogger.sharedLogger enableTTYLogging]; #elif RELEASE - loggingIsEnabled = OWSPreferences.loggingIsEnabled; + isLoggingEnabled = OWSPreferences.isLoggingEnabled; #endif - if (loggingIsEnabled) { + if (isLoggingEnabled) { [DebugLogger.sharedLogger enableFileLogging]; } @@ -117,7 +117,7 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; // ensureIsReadyForAppExtensions may have changed the state of the logging // preference (due to [NSUserDefaults migrateToSharedUserDefaults]), so honor // that change if necessary. - if (loggingIsEnabled && !OWSPreferences.loggingIsEnabled) { + if (isLoggingEnabled && !OWSPreferences.isLoggingEnabled) { [DebugLogger.sharedLogger disableFileLogging]; } #endif diff --git a/Signal/src/Profiles/OWSProfileManager.m b/Signal/src/Profiles/OWSProfileManager.m index 8c05020f7..2973aa0fe 100644 --- a/Signal/src/Profiles/OWSProfileManager.m +++ b/Signal/src/Profiles/OWSProfileManager.m @@ -1408,22 +1408,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; dispatch_once(&onceToken, ^{ profileAvatarsDirPath = OWSProfileManager.sharedDataProfileAvatarsDirPath; - BOOL isDirectory; - BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:profileAvatarsDirPath isDirectory:&isDirectory]; - if (exists) { - OWSAssert(isDirectory); - - DDLogInfo(@"Profile avatars directory already exists"); - } else { - NSError *error = nil; - [[NSFileManager defaultManager] createDirectoryAtPath:profileAvatarsDirPath - withIntermediateDirectories:YES - attributes:nil - error:&error]; - if (error) { - DDLogError(@"Failed to create profile avatars directory: %@", error); - } - } + [OWSFileSystem ensureDirectoryExists:profileAvatarsDirPath]; [OWSFileSystem protectFolderAtPath:profileAvatarsDirPath]; }); diff --git a/Signal/src/ViewControllers/AdvancedSettingsTableViewController.m b/Signal/src/ViewControllers/AdvancedSettingsTableViewController.m index 11897ce2f..c95c184e8 100644 --- a/Signal/src/ViewControllers/AdvancedSettingsTableViewController.m +++ b/Signal/src/ViewControllers/AdvancedSettingsTableViewController.m @@ -90,12 +90,12 @@ NS_ASSUME_NONNULL_BEGIN OWSTableSection *loggingSection = [OWSTableSection new]; loggingSection.headerTitle = NSLocalizedString(@"LOGGING_SECTION", nil); [loggingSection addItem:[OWSTableItem switchItemWithText:NSLocalizedString(@"SETTINGS_ADVANCED_DEBUGLOG", @"") - isOn:[OWSPreferences loggingIsEnabled] + isOn:[OWSPreferences isLoggingEnabled] target:weakSelf selector:@selector(didToggleEnableLogSwitch:)]]; - if ([OWSPreferences loggingIsEnabled]) { + if ([OWSPreferences isLoggingEnabled]) { [loggingSection addItem:[OWSTableItem actionItemWithText:NSLocalizedString(@"SETTINGS_ADVANCED_SUBMIT_DEBUGLOG", @"") actionBlock:^{ @@ -263,7 +263,7 @@ NS_ASSUME_NONNULL_BEGIN [[DebugLogger sharedLogger] enableFileLogging]; } - [OWSPreferences setLoggingEnabled:sender.isOn]; + [OWSPreferences setIsLoggingEnabled:sender.isOn]; [self updateTableContents]; } diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 379ff523e..da87dbd6f 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -2700,12 +2700,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { { NSString *temporaryDirectory = NSTemporaryDirectory(); NSString *videoDirPath = [temporaryDirectory stringByAppendingPathComponent:@"videos"]; - if (![[NSFileManager defaultManager] fileExistsAtPath:videoDirPath]) { - [[NSFileManager defaultManager] createDirectoryAtPath:videoDirPath - withIntermediateDirectories:YES - attributes:nil - error:nil]; - } + [OWSFileSystem ensureDirectoryExists:videoDirPath]; return [NSURL fileURLWithPath:videoDirPath]; } diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m index c8799987a..d4cedee0e 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m @@ -8,9 +8,9 @@ #import "OWSTableViewController.h" #import "Signal-Swift.h" #import "ThreadUtil.h" -#import #import #import +#import #import #import #import @@ -337,19 +337,7 @@ NS_ASSUME_NONNULL_BEGIN [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; NSString *randomFilesDirectoryPath = [[documentDirectoryURL path] stringByAppendingPathComponent:@"cached_random_files"]; - NSError *error; - if (![fileManager fileExistsAtPath:randomFilesDirectoryPath]) { - [fileManager createDirectoryAtPath:randomFilesDirectoryPath - withIntermediateDirectories:YES - attributes:nil - error:&error]; - OWSAssert(!error); - if (error) { - DDLogError(@"Error creating directory: %@", error); - failure(); - return; - } - } + [OWSFileSystem ensureDirectoryExists:randomFilesDirectoryPath]; NSString *filePath = [randomFilesDirectoryPath stringByAppendingPathComponent:filename]; if ([fileManager fileExistsAtPath:filePath]) { success(filePath); diff --git a/Signal/src/environment/Migrations/OWS102MoveLoggingPreferenceToUserDefaults.m b/Signal/src/environment/Migrations/OWS102MoveLoggingPreferenceToUserDefaults.m index 84e12fdd1..f1fa858a2 100644 --- a/Signal/src/environment/Migrations/OWS102MoveLoggingPreferenceToUserDefaults.m +++ b/Signal/src/environment/Migrations/OWS102MoveLoggingPreferenceToUserDefaults.m @@ -27,7 +27,7 @@ static NSString *const OWS102MoveLoggingPreferenceToUserDefaultsMigrationId = @" if (existingValue) { DDLogInfo(@"%@ assigning existing value: %@", self.logTag, existingValue); - [OWSPreferences setLoggingEnabled:[existingValue boolValue]]; + [OWSPreferences setIsLoggingEnabled:[existingValue boolValue]]; if (![existingValue boolValue]) { DDLogInfo(@"%@ Disabling file logger after one-time log settings migration.", self.logTag); diff --git a/Signal/src/util/Pastelog.m b/Signal/src/util/Pastelog.m index c5bbb53cf..741f3b0de 100644 --- a/Signal/src/util/Pastelog.m +++ b/Signal/src/util/Pastelog.m @@ -3,7 +3,8 @@ // #import "Pastelog.h" -#include +#import "DebugLogger.h" +#import @interface Pastelog () @@ -59,18 +60,21 @@ otherButtonTitles:nil]; [[self sharedManager].loadingAlertView show]; - NSArray *fileNames = fileLogger.logFileManager.sortedLogFileNames; - NSArray *filePaths = fileLogger.logFileManager.sortedLogFilePaths; + NSArray *logFilePaths = DebugLogger.sharedLogger.allLogFilePaths; - NSMutableDictionary *gistFiles = [@{} mutableCopy]; + NSMutableDictionary *gistFiles = [NSMutableDictionary new]; - NSError *error; - - for (unsigned int i = 0; i < [filePaths count]; i++) { - [gistFiles setObject:@{@"content":[NSString stringWithContentsOfFile:[filePaths objectAtIndex:i] encoding:NSUTF8StringEncoding error:&error]} forKey:[fileNames objectAtIndex:i]]; - } - - if (error) { + for (NSString *logFilePath in logFilePaths) { + NSError *error; + NSString *logContents = + [NSString stringWithContentsOfFile:logFilePath encoding:NSUTF8StringEncoding error:&error]; + if (error) { + OWSFail(@"%@ Error loading log file contents: %@", self.logTag, error); + continue; + } + gistFiles[logFilePath.lastPathComponent] = @{ + @"content" : logContents, + }; } NSDictionary *gistDict = @{@"description":[self gistDescription], @"files":gistFiles}; diff --git a/SignalMessaging/utils/DebugLogger.h b/SignalMessaging/utils/DebugLogger.h index 113803726..5c61751f8 100644 --- a/SignalMessaging/utils/DebugLogger.h +++ b/SignalMessaging/utils/DebugLogger.h @@ -6,6 +6,8 @@ @interface DebugLogger : NSObject +@property (nonatomic) DDFileLogger *fileLogger; + + (instancetype)sharedLogger; - (void)enableFileLogging; @@ -16,8 +18,6 @@ - (void)wipeLogs; -- (NSString *)logsDirectory; - -@property (nonatomic) DDFileLogger *fileLogger; +- (NSArray *)allLogFilePaths; @end diff --git a/SignalMessaging/utils/DebugLogger.m b/SignalMessaging/utils/DebugLogger.m index 83e44f917..01c900ec8 100644 --- a/SignalMessaging/utils/DebugLogger.m +++ b/SignalMessaging/utils/DebugLogger.m @@ -4,15 +4,13 @@ #import "DebugLogger.h" #import "OWSScrubbingLogFormatter.h" +#import #import +#import #pragma mark Logging - Production logging wants us to write some logs to a file in case we need it for debugging. #import -@interface DebugLogger () - -@end - @implementation DebugLogger + (instancetype)sharedLogger @@ -25,11 +23,38 @@ return sharedManager; } ++ (NSString *)mainAppLogsDirPath +{ + NSString *dirPath = [[OWSFileSystem cachesDirectoryPath] stringByAppendingPathComponent:@"Logs"]; + [OWSFileSystem ensureDirectoryExists:dirPath]; + [OWSFileSystem protectFolderAtPath:dirPath]; + return dirPath; +} + ++ (NSString *)shareExtensionLogsDirPath +{ + NSString *dirPath = + [[OWSFileSystem appSharedDataDirectoryPath] stringByAppendingPathComponent:@"ShareExtensionLogs"]; + [OWSFileSystem ensureDirectoryExists:dirPath]; + [OWSFileSystem protectFolderAtPath:dirPath]; + return dirPath; +} + +- (NSString *)logsDirPath +{ + // This assumes that the only app extension is the share app extension. + return (CurrentAppContext().isMainApp ? DebugLogger.mainAppLogsDirPath : DebugLogger.shareExtensionLogsDirPath); +} - (void)enableFileLogging { + NSString *logsDirPath = [self logsDirPath]; + // Logging to file, because it's in the Cache folder, they are not uploaded in iTunes/iCloud backups. - self.fileLogger = [DDFileLogger new]; + id logFileManager = + [[DDLogFileManagerDefault alloc] initWithLogsDirectory:logsDirPath defaultFileProtectionLevel:@""]; + self.fileLogger = [[DDFileLogger alloc] initWithLogFileManager:logFileManager]; + // 24 hour rolling. self.fileLogger.rollingFrequency = kDayInterval; // Keep last 3 days of logs - or last 3 logs (if logs rollover due to max file size). @@ -52,22 +77,46 @@ [DDLog addLogger:DDTTYLogger.sharedInstance]; } +- (NSArray *)allLogFilePaths +{ + NSFileManager *fileManager = [NSFileManager defaultManager]; + NSMutableSet *logPathSet = [NSMutableSet new]; + for (NSString *logDirPath in @[ + DebugLogger.mainAppLogsDirPath, + DebugLogger.shareExtensionLogsDirPath, + ]) { + NSError *error; + for (NSString *filename in [fileManager contentsOfDirectoryAtPath:logDirPath error:&error]) { + NSString *logPath = [logDirPath stringByAppendingPathComponent:filename]; + [logPathSet addObject:logPath]; + } + if (error) { + OWSFail(@"%@ Failed to find log files: %@", self.logTag, error); + } + } + // To be extra conservative, also add all logs from log file manager. + // This should be redundant with the logic above. + [logPathSet addObjectsFromArray:self.fileLogger.logFileManager.unsortedLogFilePaths]; + NSArray *logPaths = logPathSet.allObjects; + return [logPaths sortedArrayUsingSelector:@selector(compare:)]; +} + - (void)wipeLogs { - BOOL reenableLogging = (self.fileLogger ? YES : NO); - NSError *error; - NSArray *logsPath = self.fileLogger.logFileManager.unsortedLogFilePaths; + NSArray *logFilePaths = self.allLogFilePaths; + BOOL reenableLogging = (self.fileLogger ? YES : NO); if (reenableLogging) { [self disableFileLogging]; } - for (NSUInteger i = 0; i < logsPath.count; i++) { - [[NSFileManager defaultManager] removeItemAtPath:[logsPath objectAtIndex:i] error:&error]; - } - - if (error) { - DDLogError(@"Logs couldn't be removed. %@", error.description); + NSFileManager *fileManager = [NSFileManager defaultManager]; + NSError *error; + for (NSString *logFilePath in logFilePaths) { + BOOL success = [fileManager removeItemAtPath:logFilePath error:&error]; + if (!success || error) { + OWSFail(@"%@ Failed to delete log file: %@", self.logTag, error); + } } if (reenableLogging) { @@ -75,26 +124,4 @@ } } -- (NSString *)logsDirectory -{ - NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); - NSString *baseDir = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; - NSString *logsDirectory = [baseDir stringByAppendingPathComponent:@"Logs"]; - - if (![[NSFileManager defaultManager] fileExistsAtPath:logsDirectory]) { - NSError *error; - - [[NSFileManager defaultManager] createDirectoryAtPath:logsDirectory - withIntermediateDirectories:YES - attributes:nil - error:nil]; - - if (error) { - DDLogError(@"Log folder couldn't be created. %@", error.description); - } - } - - return logsDirectory; -} - @end diff --git a/Signal/src/environment/OWSPreferences.h b/SignalMessaging/utils/OWSPreferences.h similarity index 97% rename from Signal/src/environment/OWSPreferences.h rename to SignalMessaging/utils/OWSPreferences.h index 6a0cb1882..b49728860 100644 --- a/Signal/src/environment/OWSPreferences.h +++ b/SignalMessaging/utils/OWSPreferences.h @@ -36,8 +36,8 @@ extern NSString *const OWSPreferencesKeyEnableDebugLog; - (BOOL)getHasArchivedAMessage; - (void)setHasArchivedAMessage:(BOOL)enabled; -+ (BOOL)loggingIsEnabled; -+ (void)setLoggingEnabled:(BOOL)flag; ++ (BOOL)isLoggingEnabled; ++ (void)setIsLoggingEnabled:(BOOL)flag; - (BOOL)screenSecurityIsEnabled; - (void)setScreenSecurity:(BOOL)flag; diff --git a/Signal/src/environment/OWSPreferences.m b/SignalMessaging/utils/OWSPreferences.m similarity index 98% rename from Signal/src/environment/OWSPreferences.m rename to SignalMessaging/utils/OWSPreferences.m index 8548715f8..db83da31b 100644 --- a/Signal/src/environment/OWSPreferences.m +++ b/SignalMessaging/utils/OWSPreferences.m @@ -3,8 +3,8 @@ // #import "OWSPreferences.h" -#import "TSStorageHeaders.h" #import +#import NS_ASSUME_NONNULL_BEGIN @@ -41,7 +41,8 @@ NSString *const OWSPreferencesKey_IsReadyForAppExtensions = @"isReadyForAppExten #pragma mark - Helpers -- (void)clear { +- (void)clear +{ [NSUserDefaults removeAll]; } @@ -108,7 +109,7 @@ NSString *const OWSPreferencesKey_IsReadyForAppExtensions = @"isReadyForAppExten } } -+ (BOOL)loggingIsEnabled ++ (BOOL)isLoggingEnabled { NSNumber *preference = [NSUserDefaults.appUserDefaults objectForKey:OWSPreferencesKeyEnableDebugLog]; @@ -119,7 +120,7 @@ NSString *const OWSPreferencesKey_IsReadyForAppExtensions = @"isReadyForAppExten } } -+ (void)setLoggingEnabled:(BOOL)flag ++ (void)setIsLoggingEnabled:(BOOL)flag { // Logging preferences are stored in UserDefaults instead of the database, so that we can (optionally) start // logging before the database is initialized. This is important because sometimes there are problems *with* the diff --git a/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m b/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m index 390f6b9d0..27912cfb7 100644 --- a/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m +++ b/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m @@ -204,22 +204,7 @@ NS_ASSUME_NONNULL_BEGIN dispatch_once(&onceToken, ^{ attachmentsFolder = TSAttachmentStream.sharedDataAttachmentsDirPath; - BOOL isDirectory; - BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:attachmentsFolder isDirectory:&isDirectory]; - if (exists) { - OWSAssert(isDirectory); - - DDLogInfo(@"Attachments directory already exists"); - } else { - NSError *error = nil; - [[NSFileManager defaultManager] createDirectoryAtPath:attachmentsFolder - withIntermediateDirectories:YES - attributes:nil - error:&error]; - if (error) { - DDLogError(@"Failed to create attachments directory: %@", error); - } - } + [OWSFileSystem ensureDirectoryExists:attachmentsFolder]; [OWSFileSystem protectFolderAtPath:attachmentsFolder]; }); diff --git a/SignalServiceKit/src/Storage/TSStorageManager.m b/SignalServiceKit/src/Storage/TSStorageManager.m index b1877cb59..207973206 100644 --- a/SignalServiceKit/src/Storage/TSStorageManager.m +++ b/SignalServiceKit/src/Storage/TSStorageManager.m @@ -388,23 +388,9 @@ void setDatabaseInitialized() { NSString *databaseDirPath = [[OWSFileSystem appSharedDataDirectoryPath] stringByAppendingPathComponent:@"database"]; - NSFileManager *fileManager = [NSFileManager defaultManager]; - if (![fileManager fileExistsAtPath:databaseDirPath]) { - NSError *_Nullable error; - BOOL success = [fileManager createDirectoryAtPath:databaseDirPath - withIntermediateDirectories:NO - attributes:nil - error:&error]; - if (!success || error) { - NSString *errorDescription = - [NSString stringWithFormat:@"%@ Could not create new database directory: %@, error: %@", - self.logTag, - databaseDirPath, - error]; - OWSFail(@"%@", errorDescription); - [NSException raise:TSStorageManagerExceptionName_CouldNotCreateDatabaseDirectory - format:@"%@", errorDescription]; - } + if (![OWSFileSystem ensureDirectoryExists:databaseDirPath]) { + [NSException raise:TSStorageManagerExceptionName_CouldNotCreateDatabaseDirectory + format:@"Could not create new database directory"]; } return databaseDirPath; } diff --git a/SignalServiceKit/src/Util/MIMETypeUtil.m b/SignalServiceKit/src/Util/MIMETypeUtil.m index 8ea2effe8..ef09171cc 100644 --- a/SignalServiceKit/src/Util/MIMETypeUtil.m +++ b/SignalServiceKit/src/Util/MIMETypeUtil.m @@ -3,6 +3,7 @@ // #import "MIMETypeUtil.h" +#import "OWSFileSystem.h" #if TARGET_OS_IPHONE #import @@ -321,15 +322,8 @@ NSString *const kSyncMessageFileExtension = @"bin"; NSString *attachmentFolderPath = [folder stringByAppendingPathComponent:uniqueId]; NSError *error = nil; BOOL attachmentFolderPathExists = [[NSFileManager defaultManager] fileExistsAtPath:attachmentFolderPath]; - if (!attachmentFolderPathExists) { - [[NSFileManager defaultManager] createDirectoryAtPath:attachmentFolderPath - withIntermediateDirectories:YES - attributes:nil - error:&error]; - if (error) { - OWSFail(@"Failed to create attachment directory: %@", error); - return nil; - } + if (![OWSFileSystem ensureDirectoryExists:attachmentFolderPath]) { + return nil; } return [attachmentFolderPath stringByAppendingPathComponent:[NSString diff --git a/SignalServiceKit/src/Util/OWSFileSystem.h b/SignalServiceKit/src/Util/OWSFileSystem.h index b9abd6a5d..9538aecf3 100644 --- a/SignalServiceKit/src/Util/OWSFileSystem.h +++ b/SignalServiceKit/src/Util/OWSFileSystem.h @@ -14,10 +14,15 @@ NS_ASSUME_NONNULL_BEGIN + (NSString *)appSharedDataDirectoryPath; ++ (NSString *)cachesDirectoryPath; + + (void)moveAppFilePath:(NSString *)oldFilePath sharedDataFilePath:(NSString *)newFilePath exceptionName:(NSString *)exceptionName; +// Returns NO IFF the directory does not exist and could not be created. ++ (BOOL)ensureDirectoryExists:(NSString *)dirPath; + @end NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Util/OWSFileSystem.m b/SignalServiceKit/src/Util/OWSFileSystem.m index 8746b1443..9c633b648 100644 --- a/SignalServiceKit/src/Util/OWSFileSystem.m +++ b/SignalServiceKit/src/Util/OWSFileSystem.m @@ -44,6 +44,13 @@ NS_ASSUME_NONNULL_BEGIN return [groupContainerDirectoryURL path]; } ++ (NSString *)cachesDirectoryPath +{ + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); + OWSAssert(paths.count >= 1); + return paths[0]; +} + + (void)moveAppFilePath:(NSString *)oldFilePath sharedDataFilePath:(NSString *)newFilePath exceptionName:(NSString *)exceptionName @@ -82,6 +89,28 @@ NS_ASSUME_NONNULL_BEGIN fabs([startDate timeIntervalSinceNow])); } ++ (BOOL)ensureDirectoryExists:(NSString *)dirPath +{ + BOOL isDirectory; + BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:dirPath isDirectory:&isDirectory]; + if (exists) { + OWSAssert(isDirectory); + + return YES; + } else { + NSError *error = nil; + [[NSFileManager defaultManager] createDirectoryAtPath:dirPath + withIntermediateDirectories:YES + attributes:nil + error:&error]; + if (error) { + OWSFail(@"%@ Failed to create directory: %@, error: %@", self.logTag, dirPath, error); + return NO; + } + return YES; + } +} + @end NS_ASSUME_NONNULL_END diff --git a/SignalShareExtension/ShareViewController.swift b/SignalShareExtension/ShareViewController.swift index 103166fe3..047ff75f0 100644 --- a/SignalShareExtension/ShareViewController.swift +++ b/SignalShareExtension/ShareViewController.swift @@ -16,37 +16,19 @@ class ShareViewController: UINavigationController, SAELoadViewDelegate { // This should be the first thing we do. SetCurrentAppContext(ShareAppExtensionContext()) - // DebugLogger.shared().enableTTYLogging() - // if _isDebugAssertConfiguration() { - // DebugLogger.shared().enableFileLogging() - // } else { - // // TODO: Consult OWSPreferences.loggingIsEnabled. - // DebugLogger.shared().enableFileLogging() - // } + DebugLogger.shared().enableTTYLogging() + if _isDebugAssertConfiguration() { + DebugLogger.shared().enableFileLogging() + } else if (OWSPreferences.isLoggingEnabled()) { + // TODO: Consult OWSPreferences.isLoggingEnabled. + DebugLogger.shared().enableFileLogging() + } _ = AppVersion() - //DDLogWarn(@"%@ application: didFinishLaunchingWithOptions.", self.logTag); - // - //// We need to do this _after_ we set up logging but _before_ we do - //// anything else. - //[self ensureIsReadyForAppExtensions]; - // - //#if RELEASE - // // ensureIsReadyForAppExtensions may have changed the state of the logging - // // preference (due to [NSUserDefaults migrateToSharedUserDefaults]), so honor - // // that change if necessary. - //if (loggingIsEnabled && !OWSPreferences.loggingIsEnabled) { - // [DebugLogger.sharedLogger disableFileLogging]; - //} - //#endif - // - //[AppVersion instance]; - // //[self startupLogging]; Logger.debug("\(self.logTag()) \(#function)") - print("\(self.logTag()) \(#function) \(self.view.frame)") let loadViewController = SAELoadViewController(delegate:self) self.pushViewController(loadViewController, animated: false) @@ -63,22 +45,19 @@ class ShareViewController: UINavigationController, SAELoadViewDelegate { //let proofOfSharedStorage = TSAccountManager.localNumber() let proofOfSharedStorage = "TODO" - print("shared framework: \(proofOfSharedFramework) \n sharedStorage: \(proofOfSharedStorage) \n proof of ssk: \(proofOfSSK)") + Logger.debug("shared framework: \(proofOfSharedFramework) \n sharedStorage: \(proofOfSharedStorage) \n proof of ssk: \(proofOfSSK)") Logger.debug("\(self.logTag()) \(#function)") - print("\(self.logTag()) \(#function) \(self.view.frame)") } override func viewWillAppear(_ animated: Bool) { Logger.debug("\(self.logTag()) \(#function)") - print("\(self.logTag()) \(#function) \(self.view.frame)") super.viewWillAppear(animated) } override func viewDidAppear(_ animated: Bool) { Logger.debug("\(self.logTag()) \(#function)") - print("\(self.logTag()) \(#function) \(self.view.frame)") super.viewDidAppear(animated) } diff --git a/SignalShareExtension/SignalShareExtension-Bridging-Header.h b/SignalShareExtension/SignalShareExtension-Bridging-Header.h index 8da866ec8..07a0c7ed6 100644 --- a/SignalShareExtension/SignalShareExtension-Bridging-Header.h +++ b/SignalShareExtension/SignalShareExtension-Bridging-Header.h @@ -9,6 +9,7 @@ #import "UIFont+OWS.h" #import "UIView+OWS.h" #import +#import #import #import #import