Skip to content

Commit b4febe2

Browse files
authored
Add print logger (#829)
To support VSCode/Cursor envs or where OSLog is broken
1 parent 845aee2 commit b4febe2

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

Sources/LiveKit/LiveKit.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@
1515
*/
1616

1717
import Foundation
18-
import OSLog
1918
internal import LiveKitWebRTC
2019

21-
// Lazily initialized to the first logger
22-
let sharedLogger = LiveKitSDK.state.logger
23-
2420
/// The open source platform for real-time communication.
2521
///
2622
/// See [LiveKit's Online Docs](https://docs.livekit.io/) for more information.
@@ -80,3 +76,6 @@ public class LiveKitSDK: NSObject, Loggable {
8076
DeviceManager.prepare()
8177
}
8278
}
79+
80+
// Lazily initialized to the first logger
81+
let sharedLogger = LiveKitSDK.state.logger

Sources/LiveKit/Support/Logger.swift

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,45 @@ public struct DisabledLogger: Logger {
6666
) {}
6767
}
6868

69+
/// A simple `print` logger suitable for debugging in terminal environments outside Xcode
70+
public struct PrintLogger: Logger {
71+
private let minLevel: LogLevel
72+
private let colors: Bool
73+
74+
public init(minLevel: LogLevel = .info, colors: Bool = true) {
75+
self.minLevel = minLevel
76+
self.colors = colors
77+
}
78+
79+
public func log(
80+
_ message: @autoclosure () -> CustomStringConvertible,
81+
_ level: LogLevel,
82+
source _: @autoclosure () -> String?,
83+
file _: StaticString,
84+
type: Any.Type,
85+
function: StaticString,
86+
line _: UInt,
87+
metaData _: ScopedMetadataContainer
88+
) {
89+
guard level >= minLevel else { return }
90+
print("[\(colorCode(level))\(level)\(resetCode)] \(type).\(function) \(message())")
91+
}
92+
93+
private func colorCode(_ level: LogLevel) -> String {
94+
guard colors else { return "" }
95+
switch level {
96+
case .debug: return "\u{001B}[36m"
97+
case .info: return "\u{001B}[94m"
98+
case .warning: return "\u{001B}[33m"
99+
case .error: return "\u{001B}[31m"
100+
}
101+
}
102+
103+
private var resetCode: String {
104+
colors ? "\u{001B}[0m" : ""
105+
}
106+
}
107+
69108
/// A logger that logs to OSLog
70109
/// - Parameter minLevel: The minimum level to log
71110
/// - Parameter rtc: Whether to log WebRTC output
@@ -172,7 +211,7 @@ extension Loggable {
172211

173212
@objc
174213
@frozen
175-
public enum LogLevel: Int, Sendable, Comparable {
214+
public enum LogLevel: Int, Sendable, Comparable, CustomStringConvertible {
176215
case debug
177216
case info
178217
case warning
@@ -201,6 +240,15 @@ public enum LogLevel: Int, Sendable, Comparable {
201240
public static func < (lhs: LogLevel, rhs: LogLevel) -> Bool {
202241
lhs.rawValue < rhs.rawValue
203242
}
243+
244+
public var description: String {
245+
switch self {
246+
case .debug: "Debug"
247+
case .info: "Info"
248+
case .warning: "Warning"
249+
case .error: "Error"
250+
}
251+
}
204252
}
205253

206254
extension LKRTCLoggingSeverity {

0 commit comments

Comments
 (0)