mirror of https://github.com/oxen-io/session-ios
WIP
parent
595080aba2
commit
d2161545d6
@ -1,25 +0,0 @@
|
|||||||
//
|
|
||||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
|
||||||
//
|
|
||||||
|
|
||||||
#import <CocoaLumberjack/DDFileLogger.h>
|
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
|
||||||
|
|
||||||
@interface DebugLogger : NSObject
|
|
||||||
|
|
||||||
+ (instancetype)sharedLogger;
|
|
||||||
|
|
||||||
- (void)enableFileLogging;
|
|
||||||
|
|
||||||
- (void)disableFileLogging;
|
|
||||||
|
|
||||||
- (void)enableTTYLogging;
|
|
||||||
|
|
||||||
- (void)wipeLogs;
|
|
||||||
|
|
||||||
- (NSArray<NSString *> *)allLogFilePaths;
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
|
@ -1,138 +0,0 @@
|
|||||||
//
|
|
||||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
|
||||||
//
|
|
||||||
|
|
||||||
#import "DebugLogger.h"
|
|
||||||
#import "OWSScrubbingLogFormatter.h"
|
|
||||||
|
|
||||||
#import <SessionUtilitiesKit/AppContext.h>
|
|
||||||
#import <SessionUtilitiesKit/OWSFileSystem.h>
|
|
||||||
|
|
||||||
#pragma mark Logging - Production logging wants us to write some logs to a file in case we need it for debugging.
|
|
||||||
#import <CocoaLumberjack/DDTTYLogger.h>
|
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
|
||||||
|
|
||||||
const NSUInteger kMaxDebugLogFileSize = 1024 * 1024 * 3;
|
|
||||||
|
|
||||||
@interface DebugLogger ()
|
|
||||||
|
|
||||||
@property (nonatomic, nullable) DDFileLogger *fileLogger;
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
#pragma mark -
|
|
||||||
|
|
||||||
@implementation DebugLogger
|
|
||||||
|
|
||||||
+ (instancetype)sharedLogger
|
|
||||||
{
|
|
||||||
static DebugLogger *sharedManager = nil;
|
|
||||||
static dispatch_once_t onceToken;
|
|
||||||
dispatch_once(&onceToken, ^{
|
|
||||||
sharedManager = [self new];
|
|
||||||
});
|
|
||||||
return sharedManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
+ (NSString *)mainAppLogsDirPath
|
|
||||||
{
|
|
||||||
NSString *dirPath = [[OWSFileSystem cachesDirectoryPath] stringByAppendingPathComponent:@"Logs"];
|
|
||||||
[OWSFileSystem ensureDirectoryExists:dirPath];
|
|
||||||
return dirPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
+ (NSString *)shareExtensionLogsDirPath
|
|
||||||
{
|
|
||||||
NSString *dirPath =
|
|
||||||
[[OWSFileSystem appSharedDataDirectoryPath] stringByAppendingPathComponent:@"ShareExtensionLogs"];
|
|
||||||
[OWSFileSystem ensureDirectoryExists: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.
|
|
||||||
id<DDLogFileManager> 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).
|
|
||||||
self.fileLogger.logFileManager.maximumNumberOfLogFiles = 3;
|
|
||||||
self.fileLogger.maximumFileSize = kMaxDebugLogFileSize;
|
|
||||||
self.fileLogger.logFormatter = [OWSScrubbingLogFormatter new];
|
|
||||||
|
|
||||||
[DDLog addLogger:self.fileLogger];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)disableFileLogging
|
|
||||||
{
|
|
||||||
[DDLog removeLogger:self.fileLogger];
|
|
||||||
self.fileLogger = nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)enableTTYLogging
|
|
||||||
{
|
|
||||||
[DDLog addLogger:DDTTYLogger.sharedInstance];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSArray<NSString *> *)allLogFilePaths
|
|
||||||
{
|
|
||||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
||||||
NSMutableSet<NSString *> *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) {
|
|
||||||
OWSFailDebug(@"Failed to find log files: %@", 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<NSString *> *logPaths = logPathSet.allObjects;
|
|
||||||
return [logPaths sortedArrayUsingSelector:@selector((compare:))];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)wipeLogs
|
|
||||||
{
|
|
||||||
NSArray<NSString *> *logFilePaths = self.allLogFilePaths;
|
|
||||||
|
|
||||||
BOOL reenableLogging = (self.fileLogger ? YES : NO);
|
|
||||||
if (reenableLogging) {
|
|
||||||
[self disableFileLogging];
|
|
||||||
}
|
|
||||||
|
|
||||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
||||||
NSError *error;
|
|
||||||
for (NSString *logFilePath in logFilePaths) {
|
|
||||||
BOOL success = [fileManager removeItemAtPath:logFilePath error:&error];
|
|
||||||
if (!success || error) {
|
|
||||||
OWSFailDebug(@"Failed to delete log file: %@", error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (reenableLogging) {
|
|
||||||
[self enableFileLogging];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
|
@ -1,11 +0,0 @@
|
|||||||
//
|
|
||||||
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
||||||
//
|
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
|
||||||
|
|
||||||
@interface OWSScrubbingLogFormatter : DDLogFileFormatterDefault
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
|
@ -1,91 +0,0 @@
|
|||||||
//
|
|
||||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
|
||||||
//
|
|
||||||
|
|
||||||
#import "OWSScrubbingLogFormatter.h"
|
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
|
||||||
|
|
||||||
@implementation OWSScrubbingLogFormatter
|
|
||||||
|
|
||||||
- (NSRegularExpression *)phoneRegex
|
|
||||||
{
|
|
||||||
static NSRegularExpression *regex = nil;
|
|
||||||
static dispatch_once_t onceToken;
|
|
||||||
dispatch_once(&onceToken, ^{
|
|
||||||
NSError *error;
|
|
||||||
regex = [NSRegularExpression regularExpressionWithPattern:@"\\+\\d{7,12}(\\d{3})"
|
|
||||||
options:NSRegularExpressionCaseInsensitive
|
|
||||||
error:&error];
|
|
||||||
if (error || !regex) {
|
|
||||||
OWSFail(@"could not compile regular expression: %@", error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return regex;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSRegularExpression *)dataRegex
|
|
||||||
{
|
|
||||||
static NSRegularExpression *regex = nil;
|
|
||||||
static dispatch_once_t onceToken;
|
|
||||||
dispatch_once(&onceToken, ^{
|
|
||||||
NSError *error;
|
|
||||||
regex = [NSRegularExpression regularExpressionWithPattern:@"<([\\da-f]{2})[\\da-f]{6}( [\\da-f]{8})*>"
|
|
||||||
options:NSRegularExpressionCaseInsensitive
|
|
||||||
error:&error];
|
|
||||||
if (error || !regex) {
|
|
||||||
OWSFail(@"could not compile regular expression: %@", error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return regex;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSRegularExpression *)ipV4AddressRegex
|
|
||||||
{
|
|
||||||
static NSRegularExpression *regex = nil;
|
|
||||||
static dispatch_once_t onceToken;
|
|
||||||
dispatch_once(&onceToken, ^{
|
|
||||||
// NOTE: The group matches the last quad of the IPv4 address.
|
|
||||||
NSError *error;
|
|
||||||
regex = [NSRegularExpression regularExpressionWithPattern:@"\\d+\\.\\d+\\.\\d+\\.(\\d+)"
|
|
||||||
options:NSRegularExpressionCaseInsensitive
|
|
||||||
error:&error];
|
|
||||||
if (error || !regex) {
|
|
||||||
OWSFail(@"could not compile regular expression: %@", error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return regex;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSString *__nullable)formatLogMessage:(DDLogMessage *)logMessage
|
|
||||||
{
|
|
||||||
NSString *logString = [super formatLogMessage:logMessage];
|
|
||||||
|
|
||||||
NSRegularExpression *phoneRegex = self.phoneRegex;
|
|
||||||
logString = [phoneRegex stringByReplacingMatchesInString:logString
|
|
||||||
options:0
|
|
||||||
range:NSMakeRange(0, [logString length])
|
|
||||||
withTemplate:@"[ REDACTED_PHONE_NUMBER:xxx$1 ]"];
|
|
||||||
|
|
||||||
|
|
||||||
// We capture only the first two characters of the hex string for logging.
|
|
||||||
// example log line: "Called someFunction with nsData: <01234567 89abcdef>"
|
|
||||||
// scrubbed output: "Called someFunction with nsData: [ REDACTED_DATA:01 ]"
|
|
||||||
NSRegularExpression *dataRegex = self.dataRegex;
|
|
||||||
logString = [dataRegex stringByReplacingMatchesInString:logString
|
|
||||||
options:0
|
|
||||||
range:NSMakeRange(0, [logString length])
|
|
||||||
withTemplate:@"[ REDACTED_DATA:$1... ]"];
|
|
||||||
|
|
||||||
NSRegularExpression *ipV4AddressRegex = self.ipV4AddressRegex;
|
|
||||||
logString = [ipV4AddressRegex stringByReplacingMatchesInString:logString
|
|
||||||
options:0
|
|
||||||
range:NSMakeRange(0, [logString length])
|
|
||||||
withTemplate:@"[ REDACTED_IPV4_ADDRESS:...$1 ]"];
|
|
||||||
|
|
||||||
return logString;
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
|
Loading…
Reference in New Issue