You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/SessionUtilitiesKit/LibSession/LibSession.swift

102 lines
3.4 KiB
Swift

// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved.
//
// stringlint:disable
import Foundation
import SessionUtil
// MARK: - LibSession
public enum LibSession {
private static let logLevels: [LogCategory: LOG_LEVEL] = [
.config: LOG_LEVEL_INFO,
.network: LOG_LEVEL_INFO,
.manual: LOG_LEVEL_INFO,
]
public static var version: String { String(cString: LIBSESSION_UTIL_VERSION_STR) }
}
// MARK: - Logging
extension LibSession {
public static func addLogger() {
/// Set the default log level first (unless specified we only care about semi-dangerous logs)
session_logger_set_level_default(LOG_LEVEL_WARN)
/// Then set any explicit category log levels we have
logLevels.forEach { cat, level in
guard let cCat: [CChar] = cat.rawValue.cString(using: .utf8) else { return }
session_logger_set_level(cCat, level)
}
/// Finally register the actual logger callback
session_add_logger_full({ msgPtr, msgLen, catPtr, catLen, lvl in
guard let msg: String = String(pointer: msgPtr, length: msgLen, encoding: .utf8) else { return }
/// Logs from libSession come through in the format:
/// `[yyyy-MM-dd hh:mm:ss] [+{lifetime}s] [{cat}:{lvl}|log.hpp:{line}] {message}`
/// We want to remove the extra data because it doesn't help the logs
let processedMessage: String = {
let logParts: [String] = msg.components(separatedBy: "] ")
guard logParts.count == 4 else { return msg.trimmingCharacters(in: .whitespacesAndNewlines) }
let message: String = String(logParts[3]).trimmingCharacters(in: .whitespacesAndNewlines)
return "\(logParts[1])] \(message)"
}()
Log.custom(Log.Level(lvl), [LogCategory(catPtr, catLen).logCat], processedMessage)
})
}
public static func clearLoggers() {
session_clear_loggers()
}
// MARK: - Internal
fileprivate enum LogCategory: String {
case libSession
case config
case network
case quic
case manual
var logCat: Log.Category {
switch self {
case .libSession: return "libSession"
case .config: return "libSession:config"
case .network: return "libSession:network"
case .quic: return "libSession:quic"
case .manual: return "libSession:manual"
}
}
init(_ catPtr: UnsafePointer<CChar>?, _ catLen: Int) {
switch String(pointer: catPtr, length: catLen, encoding: .utf8).map({ LogCategory(rawValue: $0) }) {
case .some(let cat): self = cat
case .none: self = .libSession
}
}
}
}
// MARK: - Convenience
fileprivate extension Log.Level {
init(_ level: LOG_LEVEL) {
switch level {
case LOG_LEVEL_TRACE: self = .verbose
case LOG_LEVEL_DEBUG: self = .debug
case LOG_LEVEL_INFO: self = .info
case LOG_LEVEL_WARN: self = .warn
case LOG_LEVEL_ERROR: self = .error
case LOG_LEVEL_CRITICAL: self = .critical
default: self = .off
}
}
}