ZcashLightClientKit/ZcashLightClientKitTests/utils/SampleLogger.swift

81 lines
2.9 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// SampleLogger.swift
// ZcashLightClientSample
//
// Created by Francisco Gindre on 3/9/20.
// Copyright © 2020 Electric Coin Company. All rights reserved.
//
import Foundation
import ZcashLightClientKit
import os
// swiftlint:disable force_unwrapping print_function_usage
class SampleLogger: ZcashLightClientKit.Logger {
enum LogLevel: Int {
case debug
case error
case warning
case event
case info
}
enum LoggerType {
case osLog
case printerLog
}
var level: LogLevel
var loggerType: LoggerType
init(logLevel: LogLevel, type: LoggerType = .osLog) {
self.level = logLevel
self.loggerType = type
}
private static let subsystem = Bundle.main.bundleIdentifier!
static let oslog = OSLog(subsystem: subsystem, category: "logs")
func debug(_ message: String, file: StaticString = #file, function: StaticString = #function, line: Int = #line) {
guard level.rawValue == LogLevel.debug.rawValue else { return }
log(level: "DEBUG 🐞", message: message, file: file, function: function, line: line)
}
func error(_ message: String, file: StaticString = #file, function: StaticString = #function, line: Int = #line) {
guard level.rawValue <= LogLevel.error.rawValue else { return }
log(level: "ERROR 💥", message: message, file: file, function: function, line: line)
}
func warn(_ message: String, file: StaticString = #file, function: StaticString = #function, line: Int = #line) {
guard level.rawValue <= LogLevel.warning.rawValue else { return }
log(level: "WARNING ⚠️", message: message, file: file, function: function, line: line)
}
func event(_ message: String, file: StaticString = #file, function: StaticString = #function, line: Int = #line) {
guard level.rawValue <= LogLevel.event.rawValue else { return }
log(level: "EVENT ⏱", message: message, file: file, function: function, line: line)
}
func info(_ message: String, file: StaticString = #file, function: StaticString = #function, line: Int = #line) {
guard level.rawValue <= LogLevel.info.rawValue else { return }
log(level: "INFO ", message: message, file: file, function: function, line: line)
}
private func log(level: String, message: String, file: StaticString = #file, function: StaticString = #function, line: Int = #line) {
let fileName = (String(describing: file) as NSString).lastPathComponent
switch loggerType {
case .printerLog:
print("[\(level)] \(fileName) - \(function) - line: \(line) -> \(message)")
default:
os_log(
"[%{public}@] %{public}@ - %{public}@ - Line: %{public}d -> %{public}@",
level,
fileName,
String(describing: function),
line,
message
)
}
}
}