Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds log delegate #102

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions TelnyxRTC/Telnyx/Models/TxConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -67,6 +70,7 @@ public struct TxConfig {
self.ringBackTone = ringBackTone
self.ringtone = ringtone
Logger.log.verboseLevel = logLevel
Logger.log.delegate = logDelegate
}

// MARK: - Methods
Expand Down
16 changes: 11 additions & 5 deletions TelnyxRTC/Telnyx/Utils/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class Timestamp {
}
}

public protocol LoggerDelegate: AnyObject {
func captureLogMessage(_ message: String)
}

enum VertoDirection: Int {
case inbound = 0
case outbound
Expand All @@ -67,38 +71,40 @@ 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() {}


/// Prints information messages if `verboseLevel` is set to `.all` or `.info`
/// - 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))
}
}

/// Prints Error messages if `verboseLevel` is set to `.all` or `.error`
/// - 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))
}
}

/// Prints Warning messages if `verboseLevel` is set to `.all` or `.warning`
/// - 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))
}
}

/// Prints Success messages if `verboseLevel` is set to `.all` or `.success`
/// - 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))
}
}

Expand All @@ -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))
}
}

Expand Down