mirror of
https://github.com/bitwarden/ios.git
synced 2025-12-12 07:43:01 -06:00
63 lines
1.5 KiB
Swift
63 lines
1.5 KiB
Swift
import Foundation
|
|
|
|
/// Wraps Swift.print() within DEBUG
|
|
///
|
|
/// - Note: *print()* might cause [security
|
|
/// vulnerabilities](https://codifiedsecurity.com/mobile-app-security-testing-checklist-ios/)
|
|
///
|
|
/// - Parameter object: The object which is to be logged
|
|
///
|
|
func print(_ object: Any) {
|
|
#if DEBUG
|
|
Swift.print(object)
|
|
#endif
|
|
}
|
|
|
|
class Log {
|
|
static let shared = Log()
|
|
|
|
private init() {}
|
|
|
|
private static var isLoggingEnabled: Bool {
|
|
#if DEBUG
|
|
return true
|
|
#else
|
|
return false
|
|
#endif
|
|
}
|
|
|
|
static var dateFormat = "yyyy-MM-dd hh:mm:ssSSS"
|
|
static var dateFormatter: DateFormatter {
|
|
let formatter = DateFormatter()
|
|
formatter.dateFormat = dateFormat
|
|
formatter.locale = Locale.current
|
|
formatter.timeZone = TimeZone.current
|
|
return formatter
|
|
}
|
|
|
|
class func e(
|
|
_ object: Any,
|
|
filename: String = #file,
|
|
line: Int = #line,
|
|
column: Int = #column,
|
|
funcName: String = #function,
|
|
) {
|
|
if isLoggingEnabled {
|
|
print(
|
|
"\(Date().toString()) Error [\(sourceFileName(filePath: filename))]:\(line) \(column) \(funcName) -> \(object)",
|
|
)
|
|
}
|
|
}
|
|
|
|
private class func sourceFileName(filePath: String) -> String {
|
|
let components = filePath.components(separatedBy: "/")
|
|
return components.isEmpty ? "" : components.last!
|
|
}
|
|
}
|
|
|
|
extension Date {
|
|
func toString() -> String {
|
|
Log.dateFormatter.string(from: self as Date)
|
|
}
|
|
}
|