Option to add or remove debug level log from trace file and NSLog

- each trace statement has a type, possible values debug, error, info, default, fault
- debug level can be used for long dumps, eg value dumps
- this new setting allows to remove or add the debug level from NSLog and trace file
- by default it's not enabled
This commit is contained in:
Johan Degraeve 2020-06-20 19:58:38 +02:00
parent 53ec84da1d
commit 8e5a5c3b8b
5 changed files with 59 additions and 18 deletions

View File

@ -175,6 +175,9 @@ extension UserDefaults {
/// OSLogEnabled enabled or not
case OSLogEnabled = "OSLogEnabled"
/// should debug level logs be added in trace file or not, and also in NSLog
case addDebugLevelLogsInTraceFileAndNSLog = "addDebugLevelLogsInTraceFileAndNSLog"
/// to merge from 3.x to 4.x, can be deleted once 3.x is not used anymore
case cgmTransmitterDeviceAddress = "cgmTransmitterDeviceAddress"
@ -825,6 +828,16 @@ extension UserDefaults {
}
}
/// OSLogEnabled - default false
var addDebugLevelLogsInTraceFileAndNSLog: Bool {
get {
return bool(forKey: Key.addDebugLevelLogsInTraceFileAndNSLog.rawValue)
}
set {
set(newValue, forKey: Key.addDebugLevelLogsInTraceFileAndNSLog.rawValue)
}
}
/// to merge from 3.x to 4.x, can be deleted once 3.x is not used anymore
var cgmTransmitterDeviceAddress: String? {
get {

View File

@ -81,3 +81,4 @@
"volumeTestiOSSoundExplanation" = "A sound is played now with the same volume as will be used for an alarm that would have an alarm type with 'override mute' off - and for missed reading alerts which always use the iOS volume.\n\nPress one of the volume buttons to stop the sound, then change the volume with the volume buttons and try again.";
"volumeTestSoundPlayer" = "Volume test sound player";
"volumeTestiOSSound" = "Volume test iOS sound";
"debugLevel" = "Include debug level";

View File

@ -330,6 +330,10 @@ class Texts_SettingsView {
return NSLocalizedString("sendTraceFile", tableName: filename, bundle: Bundle.main, value: "Send Trace File", comment: "in Settings, row title to send settings")
}()
static let debugLevel: String = {
return NSLocalizedString("debugLevel", tableName: filename, bundle: Bundle.main, value: "Include debug level", comment: "in Settings, to enable debug level in trace file")
}()
static let describeProblem: String = {
return NSLocalizedString("describeProblem", tableName: filename, bundle: Bundle.main, value: "Explain why you need to send the trace file with as much detail as possible. If you have already reported your problem in the Facebook support group 'xDrip4iOS', then mention your facebook name in the e-mail", comment: "Text in pop up shown when user wants to send the trace file")
}()

View File

@ -146,34 +146,39 @@ func trace(_ message: StaticString, log:OSLog, category: String, type: OSLogType
// create timeStamp to use in NSLog and tracefile
let timeStamp = dateFormatNSLog.string(from: Date())
// nslog if enabled
if UserDefaults.standard.NSLogEnabled {
// nslog if enabled and if type = debug, then check also if debug logging is required
if UserDefaults.standard.NSLogEnabled && (type != .debug || (type == .debug && UserDefaults.standard.addDebugLevelLogsInTraceFileAndNSLog)) {
NSLog("%@", ConstantsLog.tracePrefix + " " + timeStamp + " " + category + " " + actualMessage)
}
// write trace to file
do {
let textToWrite = timeStamp + " " + category + " " + actualMessage + "\n"
if let fileHandle = FileHandle(forWritingAtPath: traceFileName.path) {
// write trace to file, only if type is not .debug or type is .debug and addDebugLevelLogsInTraceFileAndNSLog is true
if type != .debug || (type == .debug && UserDefaults.standard.addDebugLevelLogsInTraceFileAndNSLog) {
do {
// file already exists, go to end of file and append text
fileHandle.seekToEndOfFile()
fileHandle.write(textToWrite.data(using: .utf8)!)
} else {
let textToWrite = timeStamp + " " + category + " " + actualMessage + "\n"
// file doesn't exist yet
try textToWrite.write(to: traceFileName, atomically: true, encoding: String.Encoding.utf8)
if let fileHandle = FileHandle(forWritingAtPath: traceFileName.path) {
// file already exists, go to end of file and append text
fileHandle.seekToEndOfFile()
fileHandle.write(textToWrite.data(using: .utf8)!)
} else {
// file doesn't exist yet
try textToWrite.write(to: traceFileName, atomically: true, encoding: String.Encoding.utf8)
}
} catch {
NSLog("%@", ConstantsLog.tracePrefix + " " + dateFormatNSLog.string(from: Date()) + " write trace to file failed")
}
} catch {
NSLog("%@", ConstantsLog.tracePrefix + " " + dateFormatNSLog.string(from: Date()) + " write trace to file failed")
}
// check if tracefile has reached limit size and if yes rotate the files
if traceFileName.fileSize > ConstantsTrace.maximumFileSizeInMB * 1024 * 1024 {

View File

@ -7,6 +7,9 @@ fileprivate enum Setting:Int, CaseIterable {
/// to send trace file
case sendTraceFile = 0
/// should debug level logs be stored in trace file yes or no
case debugLevel = 1
}
class SettingsViewTraceSettingsViewModel: NSObject {
@ -52,6 +55,9 @@ extension SettingsViewTraceSettingsViewModel: SettingsViewModelProtocol {
case .sendTraceFile:
return Texts_SettingsView.sendTraceFile
case .debugLevel:
return Texts_SettingsView.debugLevel
}
}
@ -64,6 +70,9 @@ extension SettingsViewTraceSettingsViewModel: SettingsViewModelProtocol {
case .sendTraceFile:
return .disclosureIndicator
case .debugLevel:
return .none
}
}
@ -76,6 +85,9 @@ extension SettingsViewTraceSettingsViewModel: SettingsViewModelProtocol {
case .sendTraceFile:
return nil
case .debugLevel:
return nil
}
}
@ -88,7 +100,10 @@ extension SettingsViewTraceSettingsViewModel: SettingsViewModelProtocol {
case .sendTraceFile:
return nil
case .debugLevel:
return UISwitch(isOn: UserDefaults.standard.addDebugLevelLogsInTraceFileAndNSLog, action: {(isOn:Bool) in UserDefaults.standard.addDebugLevelLogsInTraceFileAndNSLog = isOn})
}
}
@ -138,6 +153,9 @@ extension SettingsViewTraceSettingsViewModel: SettingsViewModelProtocol {
}
case .debugLevel:
return .nothing
}
}