2020-03-09 13:25:27 -07:00
|
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
|
|
2020-08-10 15:19:59 -07:00
|
|
|
|
class SampleLogger: ZcashLightClientKit.Logger {
|
2020-03-09 13:25:27 -07:00
|
|
|
|
enum LogLevel: Int {
|
|
|
|
|
case debug
|
|
|
|
|
case error
|
|
|
|
|
case warning
|
|
|
|
|
case event
|
|
|
|
|
case info
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-15 06:52:53 -07:00
|
|
|
|
enum LoggerType {
|
|
|
|
|
case osLog
|
|
|
|
|
case printerLog
|
|
|
|
|
}
|
2021-09-17 06:49:58 -07:00
|
|
|
|
|
|
|
|
|
static let oslog = OSLog(subsystem: subsystem, category: "logs")
|
|
|
|
|
// swiftlint:disable:next force_unwrapping
|
|
|
|
|
private static let subsystem = Bundle.main.bundleIdentifier!
|
2021-03-15 06:52:53 -07:00
|
|
|
|
|
2020-03-09 13:25:27 -07:00
|
|
|
|
var level: LogLevel
|
2021-03-15 06:52:53 -07:00
|
|
|
|
var loggerType: LoggerType
|
|
|
|
|
|
|
|
|
|
init(logLevel: LogLevel, type: LoggerType = .osLog) {
|
2020-03-09 13:25:27 -07:00
|
|
|
|
self.level = logLevel
|
2021-03-15 06:52:53 -07:00
|
|
|
|
self.loggerType = type
|
2020-03-09 13:25:27 -07:00
|
|
|
|
}
|
2021-09-17 06:49:58 -07:00
|
|
|
|
|
|
|
|
|
func debug(
|
|
|
|
|
_ message: String,
|
|
|
|
|
file: StaticString = #file,
|
|
|
|
|
function: StaticString = #function,
|
|
|
|
|
line: Int = #line
|
|
|
|
|
) {
|
2020-03-09 13:25:27 -07:00
|
|
|
|
guard level.rawValue == LogLevel.debug.rawValue else { return }
|
|
|
|
|
log(level: "DEBUG 🐞", message: message, file: file, function: function, line: line)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 06:49:58 -07:00
|
|
|
|
func error(
|
|
|
|
|
_ message: String,
|
|
|
|
|
file: StaticString = #file,
|
|
|
|
|
function: StaticString = #function,
|
|
|
|
|
line: Int = #line
|
|
|
|
|
) {
|
2020-03-09 13:25:27 -07:00
|
|
|
|
guard level.rawValue <= LogLevel.error.rawValue else { return }
|
|
|
|
|
log(level: "ERROR 💥", message: message, file: file, function: function, line: line)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 06:49:58 -07:00
|
|
|
|
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)
|
2020-03-09 13:25:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 06:49:58 -07:00
|
|
|
|
func event(
|
|
|
|
|
_ message: String,
|
|
|
|
|
file: StaticString = #file,
|
|
|
|
|
function: StaticString = #function,
|
|
|
|
|
line: Int = #line
|
|
|
|
|
) {
|
2020-03-09 13:25:27 -07:00
|
|
|
|
guard level.rawValue <= LogLevel.event.rawValue else { return }
|
|
|
|
|
log(level: "EVENT ⏱", message: message, file: file, function: function, line: line)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 06:49:58 -07:00
|
|
|
|
func info(
|
|
|
|
|
_ message: String,
|
|
|
|
|
file: StaticString = #file,
|
|
|
|
|
function: StaticString = #function,
|
|
|
|
|
line: Int = #line
|
|
|
|
|
) {
|
2020-03-09 13:25:27 -07:00
|
|
|
|
guard level.rawValue <= LogLevel.info.rawValue else { return }
|
|
|
|
|
log(level: "INFO ℹ️", message: message, file: file, function: function, line: line)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 06:49:58 -07:00
|
|
|
|
private func log(
|
|
|
|
|
level: String,
|
|
|
|
|
message: String,
|
|
|
|
|
file: StaticString = #file,
|
|
|
|
|
function: StaticString = #function,
|
|
|
|
|
line: Int = #line
|
|
|
|
|
) {
|
2021-03-15 06:52:53 -07:00
|
|
|
|
let fileName = (String(describing: file) as NSString).lastPathComponent
|
|
|
|
|
switch loggerType {
|
|
|
|
|
case .printerLog:
|
2021-09-17 06:49:58 -07:00
|
|
|
|
// swiftlint:disable print_function_usage
|
2021-03-15 06:52:53 -07:00
|
|
|
|
print("[\(level)] \(fileName) - \(function) - line: \(line) -> \(message)")
|
2021-09-17 06:49:58 -07:00
|
|
|
|
|
2021-03-15 06:52:53 -07:00
|
|
|
|
default:
|
2021-09-17 06:49:58 -07:00
|
|
|
|
os_log(
|
|
|
|
|
"[%{public}@] %{public}@ - %{public}@ - Line: %{public}d -> %{public}@",
|
|
|
|
|
level,
|
|
|
|
|
fileName,
|
|
|
|
|
String(describing: function),
|
|
|
|
|
line,
|
|
|
|
|
message
|
|
|
|
|
)
|
2021-03-15 06:52:53 -07:00
|
|
|
|
}
|
2020-03-09 13:25:27 -07:00
|
|
|
|
}
|
|
|
|
|
}
|