Skip to content

Instantly share code, notes, and snippets.

@yukin01
Created February 6, 2019 08:22
Show Gist options
  • Save yukin01/408af53b5cc60cc1e9e002bfc74444ac to your computer and use it in GitHub Desktop.
Save yukin01/408af53b5cc60cc1e9e002bfc74444ac to your computer and use it in GitHub Desktop.
RxSwift の Debug に寄せたロガー(メインスレッド判定もできる)
enum Logger {
static let dateFormatter: DateFormatter = {
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
return df
}()
static func log(_ item: String, file: String = #file, line: UInt = #line, function: String = #function) {
printLikeRxDebug(item, file, line, function)
}
static func isMainThread(file: String = #file, line: UInt = #line, function: String = #function) {
printLikeRxDebug("Main Thread: \(Thread.isMainThread)", file, line, function)
}
private static func printLikeRxDebug(_ item: String, _ file: String, _ line: UInt, _ function: String) {
let trimmedFile: String
if let lastIndex = file.lastIndex(of: "/") {
trimmedFile = String(file[file.index(after: lastIndex) ..< file.endIndex])
} else {
trimmedFile = file
}
print("\(Logger.dateFormatter.string(from: Date())): \(trimmedFile):\(line) (\(function)) -> \(item)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment