mac自帶的控制臺(tái),在收集程序log方面非常有用础锐,在開(kāi)發(fā)過(guò)程中嗓节,利用好mac的控制臺(tái)非常有意義。
面向github開(kāi)源項(xiàng)目皆警,我完善編寫(xiě)了如下log類赦政,用以日常開(kāi)發(fā)。
import Foundation
import os
/// subsystem for current project.
/// - setup it when start a new project.
private let subsystem = "com.aby.htmlsize"
/// Custom log system.
public class Log {
public struct Output: OptionSet {
public let rawValue: Int
public init(rawValue: Int) {
self.rawValue = rawValue
}
public static let info = Output(rawValue: 1 << 0)
public static let debug = Output(rawValue: 1 << 1)
public static let warning = Output(rawValue: 1 << 2)
public static let error = Output(rawValue: 1 << 3)
public static let all: Output = [.info, .debug, .warning, .error]
}
/// setup log level and when does the log work.
public static var output: Output = [.debug, .warning, .error]
@available(iOS 10.0, *)
/// level info
static let infoLog = OSLog(subsystem: subsystem, category: "INFO")
/// info level
///
/// - Parameters:
/// - string: log info description
/// - fileName: file name
/// - methodName: method name
/// - lineNumber: line number
public static func info(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) {
#if DEBUG
let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "")
let log = "\(file):line \(lineNumber) method:\(methodName):\(string)"
if output.contains(.info) {
if #available(iOS 10.0, *) {
os_log("%@", log: infoLog, type: .info, log)
} else {
print("<INFO>: %@", log)
}
}
#endif
}
@available(iOS 10.0, *)
/// level debug
static let debugLog = OSLog(subsystem: subsystem, category: "DEBUG")
/// debug level
///
/// - Parameters:
/// - string: log info description
/// - fileName: file name
/// - methodName: method name
/// - lineNumber: line number
public static func debug(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) {
#if DEBUG
let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "")
let log = "\(file):line \(lineNumber) method:\(methodName):\(string)"
if output.contains(.debug) {
if #available(iOS 10.0, *) {
os_log("%@", log: debugLog, type: .debug, log)
} else {
print("<DEBUG>: %@", log)
}
}
#endif
}
@available(iOS 10.0, *)
/// level warning
static let warningLog = OSLog(subsystem: subsystem, category: "WARNING")
public static func warning(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) {
if output.contains(.warning) {
let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "")
let log = "\(file):line \(lineNumber) method:\(methodName):\(string)"
if #available(iOS 10.0, *) {
os_log("%@", log: warningLog, type: .fault, log)
} else {
print("<WARNING>: %@", string)
}
}
}
@available(iOS 10.0, *)
/// level error
static let errorLog = OSLog(subsystem: subsystem, category: "ERROR")
public static func error(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) {
if output.contains(.error) {
let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "")
let log = "\(file):line \(lineNumber) method:\(methodName):\n\(string)"
if #available(iOS 10.0, *) {
os_log("%@", log: errorLog, type: .error, log)
} else {
print("<ERROR>: %@", string)
}
}
}
/// just output in console.
///
/// - Parameters:
/// - message: output message.
/// - fileName: file name.
/// - methodName: method name.
/// - lineNumber: line number.
static func out<N>(message: N, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line){
#if DEBUG
let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "")
print("\(file):\(lineNumber) \(methodName):\(message)");
#endif
}
}
log分為4個(gè)級(jí)別耀怜,均可在macOS的日志臺(tái)記錄調(diào)試恢着。output為了滿足單純的print console。
在程序開(kāi)始啟動(dòng)時(shí)财破,可以通過(guò)
// 設(shè)置日志輸出級(jí)別
Log.output = [.error, .warning]
通過(guò)
Log.debug("完成加載, 當(dāng)前ScrollView的長(zhǎng)度為:\(webView.scrollView.contentSize.height)")
Log.error("完成加載掰派,當(dāng)前ScrollView的長(zhǎng)度為:\(webView.scrollView.contentSize.height)")
Log.warning("警告,程序有問(wèn)題")
如上方式來(lái)調(diào)試程序左痢。