From 021a9782b26ea7d02f447f206f5c3ad2a99354c9 Mon Sep 17 00:00:00 2001 From: Stanislau Malinouski Date: Fri, 29 Sep 2023 11:46:10 +0200 Subject: [PATCH] Adds log delegate --- TelnyxRTC/Telnyx/Models/TxConfig.swift | 8 ++++++-- TelnyxRTC/Telnyx/Utils/Logger.swift | 16 +++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/TelnyxRTC/Telnyx/Models/TxConfig.swift b/TelnyxRTC/Telnyx/Models/TxConfig.swift index 668ee9b3..bdfba17e 100644 --- a/TelnyxRTC/Telnyx/Models/TxConfig.swift +++ b/TelnyxRTC/Telnyx/Models/TxConfig.swift @@ -34,7 +34,8 @@ public struct TxConfig { pushDeviceToken: String? = nil, ringtone: String? = nil, ringBackTone: String? = nil, - logLevel: LogLevel = .none) { + logLevel: LogLevel = .none, + logDelegate: LoggerDelegate? = nil) { self.sipUser = sipUser self.password = password if let pushToken = pushDeviceToken { @@ -44,6 +45,7 @@ public struct TxConfig { self.ringBackTone = ringBackTone self.ringtone = ringtone Logger.log.verboseLevel = logLevel + Logger.log.delegate = logDelegate } /// Constructor of the Telnyx SDK configuration: Login using a token. @@ -58,7 +60,8 @@ public struct TxConfig { pushDeviceToken: String? = nil, ringtone: String? = nil, ringBackTone: String? = nil, - logLevel: LogLevel = .none) { + logLevel: LogLevel = .none, + logDelegate: LoggerDelegate? = nil) { self.token = token if let pushToken = pushDeviceToken { //Create a notification configuration if there's an available a device push notification token @@ -67,6 +70,7 @@ public struct TxConfig { self.ringBackTone = ringBackTone self.ringtone = ringtone Logger.log.verboseLevel = logLevel + Logger.log.delegate = logDelegate } // MARK: - Methods diff --git a/TelnyxRTC/Telnyx/Utils/Logger.swift b/TelnyxRTC/Telnyx/Utils/Logger.swift index d0302494..5f6fc025 100644 --- a/TelnyxRTC/Telnyx/Utils/Logger.swift +++ b/TelnyxRTC/Telnyx/Utils/Logger.swift @@ -45,6 +45,10 @@ class Timestamp { } } +public protocol LoggerDelegate: AnyObject { + func captureLogMessage(_ message: String) +} + enum VertoDirection: Int { case inbound = 0 case outbound @@ -67,6 +71,8 @@ class Logger { private var infoGlyph: String = "\u{1F535}" // Glyph for messages of level .Info private var timeStamp:Timestamp = Timestamp() + public weak var delegate: LoggerDelegate? + private init() {} @@ -74,7 +80,7 @@ class Logger { /// - Parameter message: message to be printed public func i(message: String) { if verboseLevel == .all || verboseLevel == .info { - print("TxClient : \(timeStamp.printTimestamp())" + buildMessage(level: .info, message: message)) + delegate?.captureLogMessage("TxClient : \(timeStamp.printTimestamp())" + buildMessage(level: .info, message: message)) } } @@ -82,7 +88,7 @@ class Logger { /// - Parameter message: message to be printed public func e(message: String) { if verboseLevel == .all || verboseLevel == .error { - print("TxClient : \(timeStamp.printTimestamp())" + buildMessage(level: .error, message: message)) + delegate?.captureLogMessage("TxClient : \(timeStamp.printTimestamp())" + buildMessage(level: .error, message: message)) } } @@ -90,7 +96,7 @@ class Logger { /// - Parameter message: message to be printed public func w(message: String) { if verboseLevel == .all || verboseLevel == .warning { - print("TxClient : \(timeStamp.printTimestamp())" + buildMessage(level: .warning, message: message)) + delegate?.captureLogMessage("TxClient : \(timeStamp.printTimestamp())" + buildMessage(level: .warning, message: message)) } } @@ -98,7 +104,7 @@ class Logger { /// - Parameter message: message to be printed public func s(message: String) { if verboseLevel == .all || verboseLevel == .success { - print( "TxClient : \(timeStamp.printTimestamp())" + buildMessage(level: .success, message: message)) + delegate?.captureLogMessage( "TxClient : \(timeStamp.printTimestamp())" + buildMessage(level: .success, message: message)) } } @@ -108,7 +114,7 @@ class Logger { /// - direction: direction of the message. Inbound-outbound public func verto(message: String, direction: VertoDirection) { if verboseLevel == .all || verboseLevel == .verto { - print("TxClient : \(timeStamp.printTimestamp())" + buildMessage(level: .verto, message: message, direction: direction)) + delegate?.captureLogMessage("TxClient : \(timeStamp.printTimestamp())" + buildMessage(level: .verto, message: message, direction: direction)) } }