81 lines
2.9 KiB
Swift
81 lines
2.9 KiB
Swift
//
|
||
// 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
|
||
)
|
||
}
|
||
}
|
||
}
|