-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds ability to provide a OSLog instance via the Config.logger …
…property
- Loading branch information
1 parent
23560ce
commit c10ec29
Showing
2 changed files
with
61 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,52 @@ | ||
import Foundation | ||
|
||
#if !os(Linux) && !os(Windows) | ||
#if canImport(os) | ||
import os.log | ||
#endif | ||
|
||
class Logs { | ||
enum Level { | ||
case debug, info, warn, error | ||
protocol InternalLogging { | ||
func log(_ level: Level, _ staticMsg: StaticString) | ||
func log(_ level: Level, _ staticMsg: StaticString, _ arg: CVarArg) | ||
func log(_ level: Level, _ staticMsg: StaticString, _ arg1: CVarArg, _ arg2: CVarArg) | ||
} | ||
|
||
#if !os(Linux) && !os(Windows) | ||
private static let osLogTypes = [ Level.debug: OSLogType.debug, | ||
Level.info: OSLogType.info, | ||
Level.warn: OSLogType.default, | ||
Level.error: OSLogType.error] | ||
var osLogType: OSLogType { Level.osLogTypes[self]! } | ||
#endif | ||
} | ||
enum Level { | ||
case debug, info, warn, error | ||
|
||
#if !os(Linux) && !os(Windows) | ||
private let logger: OSLog = OSLog(subsystem: "com.launchdarkly.swift-eventsource", category: "LDEventSource") | ||
#if canImport(os) | ||
private static let osLogTypes = [ Level.debug: OSLogType.debug, | ||
Level.info: OSLogType.info, | ||
Level.warn: OSLogType.default, | ||
Level.error: OSLogType.error] | ||
var osLogType: OSLogType { Level.osLogTypes[self]! } | ||
#endif | ||
} | ||
|
||
#if canImport(os) | ||
class OSLogAdapter: InternalLogging { | ||
|
||
private let osLog: OSLog | ||
|
||
init(osLog: OSLog) { | ||
self.osLog = osLog | ||
} | ||
|
||
func log(_ level: Level, _ staticMsg: StaticString) { | ||
os_log(staticMsg, log: logger, type: level.osLogType) | ||
os_log(staticMsg, log: self.osLog, type: level.osLogType) | ||
} | ||
|
||
func log(_ level: Level, _ staticMsg: StaticString, _ arg: CVarArg) { | ||
os_log(staticMsg, log: logger, type: level.osLogType, arg) | ||
os_log(staticMsg, log: self.osLog, type: level.osLogType, arg) | ||
} | ||
|
||
func log(_ level: Level, _ staticMsg: StaticString, _ arg1: CVarArg, _ arg2: CVarArg) { | ||
os_log(staticMsg, log: logger, type: level.osLogType, arg1, arg2) | ||
os_log(staticMsg, log: self.osLog, type: level.osLogType, arg1, arg2) | ||
} | ||
#else | ||
// We use Any over CVarArg here, because on Linux prior to Swift 5.4 String does not conform to CVarArg | ||
func log(_ level: Level, _ staticMsg: StaticString, _ args: Any...) { } | ||
} | ||
#endif | ||
|
||
class NoOpLogging: InternalLogging { | ||
func log(_ level: Level, _ staticMsg: StaticString) {} | ||
func log(_ level: Level, _ staticMsg: StaticString, _ arg: CVarArg) {} | ||
func log(_ level: Level, _ staticMsg: StaticString, _ arg1: CVarArg, _ arg2: CVarArg) {} | ||
} |