LogManager.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import CocoaLumberjackSwift
  22. /// Note: Add Log level for Swift in Build Settings - Preprocessor Macros for AdHoc and AppStore:
  23. /// DD_LOG_LEVEL=0b0100011 (Error, Warning and Notice)
  24. /// Swift method for logging Notice Log level
  25. @inlinable
  26. public func DDLogNotice(_ message: @autoclosure () -> String,
  27. level: DDLogLevel = DDDefaultLogLevel,
  28. context: Int = 0,
  29. file: StaticString = #file,
  30. function: StaticString = #function,
  31. line: UInt = #line,
  32. tag: Any? = nil,
  33. asynchronous async: Bool = asyncLoggingEnabled,
  34. ddlog: DDLog = .sharedInstance) {
  35. _DDLogMessage(message(), level: level, flag: DDLogFlag(rawValue: DDLogFlag.RawValue(DDLogFlagNotice)) , context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
  36. }
  37. @objc public class LogManager: NSObject {
  38. private static var isDebug: Bool = false
  39. @objc public static let debugLogFile: URL? = FileUtility.appDataDirectory?.appendingPathComponent("validation_log.txt")
  40. /// Log levels definition for Swift. Includes new Notice Log level at the end, to not break the standard Log levels like in <CocoaLumberjack/DDLog.h>
  41. public enum DDLogLevelCustom: UInt {
  42. case err = 0b0000001
  43. case warn = 0b0000010
  44. case info = 0b0000100
  45. case verbose = 0b0001000
  46. case debug = 0b0010000
  47. case notice = 0b0100000
  48. }
  49. @objc public static func initializeGlobalLogger(debug: Bool) {
  50. isDebug = debug
  51. if isDebug {
  52. DDTTYLogger.sharedInstance?.logFormatter = LogFormatterCustom()
  53. DDLog.add(DDTTYLogger.sharedInstance as! DDLogger, with: LogManager.logLevel())
  54. }
  55. // Add Debug Logger is enabled by user
  56. if let validationLogging = UserSettings.shared()?.validationLogging,
  57. validationLogging {
  58. addFileLogger(debugLogFile)
  59. }
  60. else {
  61. removeFileLogger(debugLogFile)
  62. }
  63. }
  64. @objc public static func addFileLogger(_ logFile: URL?) {
  65. guard let logFile = logFile else {
  66. return
  67. }
  68. if let existingLogger = findFileLogger(logFile),
  69. existingLogger.count == 0 {
  70. let fileLogger: FileLoggerCustom = FileLoggerCustom(logFile: logFile)
  71. DDLog.add(fileLogger, with: logLevel())
  72. }
  73. }
  74. @objc public static func removeFileLogger(_ logFile: URL?) {
  75. guard let logFile = logFile else {
  76. return
  77. }
  78. if let existingLoggers = findFileLogger(logFile),
  79. existingLoggers.count > 0 {
  80. for logger in existingLoggers {
  81. DDLog.remove(logger)
  82. }
  83. }
  84. }
  85. @objc public static func deleteLogFile(_ logFile: URL?) {
  86. FileUtility.delete(fileUrl: logFile)
  87. }
  88. @objc public static func logFileSize(_ logFile: URL?) -> Int64 {
  89. guard let logFile = logFile else {
  90. return 0
  91. }
  92. return FileUtility.fileSizeInBytes(fileUrl: logFile) ?? 0
  93. }
  94. /**
  95. Get Log Level depence on debug environment or not.
  96. - Returns: Log Level
  97. */
  98. private static func logLevel() -> DDLogLevel {
  99. // Default log level is Error, Warning and Notice
  100. var ddLogLevel: DDLogLevel = DDLogLevel(rawValue: DDLogLevelCustom.err.rawValue | DDLogLevelCustom.warn.rawValue | DDLogLevelCustom.notice.rawValue)!
  101. if isDebug {
  102. ddLogLevel = .all
  103. }
  104. return ddLogLevel
  105. }
  106. /**
  107. Looking for existing file logger (FileLoggerCustom).
  108. - Parameters:
  109. - logFile: Log file path
  110. - Returns: Array of file loggers
  111. */
  112. private static func findFileLogger(_ logFile: URL?) -> [DDLogger]? {
  113. guard let logFile = logFile else {
  114. return nil
  115. }
  116. var fileLoggers: [DDLogger] = []
  117. for logger in DDLog.allLoggers {
  118. if let fileLogger = logger as? FileLoggerCustom,
  119. fileLogger.logFile == logFile {
  120. fileLoggers.append(logger)
  121. }
  122. }
  123. return fileLoggers
  124. }
  125. }