Skip to content

Commit 853c8eb

Browse files
committed
Add OSLog wrappers and deprecation warnings
# Conflicts: # PrettyLogger.xcodeproj/project.pbxproj # Sources/PrettyLoggerPrivacy.swift
1 parent 467f62e commit 853c8eb

4 files changed

Lines changed: 179 additions & 0 deletions

File tree

Sources/Global.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,68 @@
11
import Foundation
22

3+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
4+
public func fatal(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
5+
PrettyLogger.shared.fatal(message, category: category, privacy: privacy)
6+
}
7+
8+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
9+
public func error(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
10+
PrettyLogger.shared.error(message, category: category, privacy: privacy)
11+
}
12+
13+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
14+
public func warning(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
15+
PrettyLogger.shared.warning(message, category: category, privacy: privacy)
16+
}
17+
18+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
19+
public func info(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
20+
PrettyLogger.shared.info(message, category: category, privacy: privacy)
21+
}
22+
23+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
24+
public func debug(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
25+
PrettyLogger.shared.debug(message, category: category, privacy: privacy)
26+
}
27+
28+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
29+
public func trace(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
30+
PrettyLogger.shared.trace(message, category: category, privacy: privacy)
31+
}
32+
33+
// MARK: - Deprecations
34+
35+
@available(*, deprecated, renamed: "fatal", message: "Use `fatal` to use the unified OS logger")
336
@discardableResult
437
public func logFatal(_ items: Any..., separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
538
return PrettyLogger.shared.logFatal(items, separator: separator, terminator: terminator, file: file, line: line, column: column, function: function)
639
}
740

41+
@available(*, deprecated, renamed: "error", message: "Use `error` to use the unified OS logger")
842
@discardableResult
943
public func logError(_ items: Any..., separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
1044
return PrettyLogger.shared.logError(items, separator: separator, terminator: terminator, file: file, line: line, column: column, function: function)
1145
}
1246

47+
@available(*, deprecated, renamed: "warning", message: "Use `warning` to use the unified OS logger")
1348
@discardableResult
1449
public func logWarning(_ items: Any..., separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
1550
return PrettyLogger.shared.logWarning(items, separator: separator, terminator: terminator, file: file, line: line, column: column, function: function)
1651
}
1752

53+
@available(*, deprecated, renamed: "info", message: "Use `info` to use the unified OS logger")
1854
@discardableResult
1955
public func logInfo(_ items: Any..., separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
2056
return PrettyLogger.shared.logInfo(items, separator: separator, terminator: terminator, file: file, line: line, column: column, function: function)
2157
}
2258

59+
@available(*, deprecated, renamed: "debug", message: "Use `debug` to use the unified OS logger")
2360
@discardableResult
2461
public func logDebug(_ items: Any..., separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
2562
return PrettyLogger.shared.logDebug(items, separator: separator, terminator: terminator, file: file, line: line, column: column, function: function)
2663
}
2764

65+
@available(*, deprecated, renamed: "trace", message: "Use `trace` to use the unified OS logger")
2866
@discardableResult
2967
public func logTrace(_ items: Any..., separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
3068
return PrettyLogger.shared.logTrace(items, separator: separator, terminator: terminator, file: file, line: line, column: column, function: function)

Sources/PrettyLogger.swift

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import Combine
3+
import OSLog
34

45
public class PrettyLogger {
56
public static let shared = PrettyLogger()
@@ -22,41 +23,149 @@ public class PrettyLogger {
2223
return log(.fatal, items: items, separator: separator, terminator: terminator, file: file, line: line, column: column, function: function)
2324
}
2425

26+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
27+
func fatal(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
28+
let logger = if let category {
29+
Logger.custom(category)
30+
} else {
31+
Logger.default
32+
}
33+
34+
switch privacy {
35+
case .auto:
36+
logger.fault("\(message)")
37+
case .public:
38+
logger.fault("\(message, privacy: .public)")
39+
case .private:
40+
logger.fault("\(message, privacy: .private)")
41+
}
42+
}
43+
2544
internal func logError(_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
2645
if level < .error {
2746
return nil
2847
}
2948
return log(.error, items: items, separator: separator, terminator: terminator, file: file, line: line, column: column, function: function)
3049
}
3150

51+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
52+
func error(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
53+
let logger = if let category {
54+
Logger.custom(category)
55+
} else {
56+
Logger.default
57+
}
58+
59+
switch privacy {
60+
case .auto:
61+
logger.error("\(message)")
62+
case .public:
63+
logger.error("\(message, privacy: .public)")
64+
case .private:
65+
logger.error("\(message, privacy: .private)")
66+
}
67+
}
68+
3269
internal func logWarning(_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
3370
if level < .warn {
3471
return nil
3572
}
3673
return log(.warn, items: items, separator: separator, terminator: terminator, file: file, line: line, column: column, function: function)
3774
}
3875

76+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
77+
func warning(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
78+
let logger = if let category {
79+
Logger.custom(category)
80+
} else {
81+
Logger.default
82+
}
83+
84+
switch privacy {
85+
case .auto:
86+
logger.warning("\(message)")
87+
case .public:
88+
logger.warning("\(message, privacy: .public)")
89+
case .private:
90+
logger.warning("\(message, privacy: .private)")
91+
}
92+
}
93+
3994
internal func logInfo(_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
4095
if level < .info {
4196
return nil
4297
}
4398
return log(.info, items: items, separator: separator, terminator: terminator, file: file, line: line, column: column, function: function)
4499
}
45100

101+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
102+
func info(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
103+
let logger = if let category {
104+
Logger.custom(category)
105+
} else {
106+
Logger.default
107+
}
108+
109+
switch privacy {
110+
case .auto:
111+
logger.info("\(message)")
112+
case .public:
113+
logger.info("\(message, privacy: .public)")
114+
case .private:
115+
logger.info("\(message, privacy: .private)")
116+
}
117+
}
118+
46119
internal func logDebug(_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
47120
if level < .debug {
48121
return nil
49122
}
50123
return log(.debug, items: items, separator: separator, terminator: terminator, file: file, line: line, column: column, function: function)
51124
}
52125

126+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
127+
func debug(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
128+
let logger = if let category {
129+
Logger.custom(category)
130+
} else {
131+
Logger.default
132+
}
133+
134+
switch privacy {
135+
case .auto:
136+
logger.debug("\(message)")
137+
case .public:
138+
logger.debug("\(message, privacy: .public)")
139+
case .private:
140+
logger.debug("\(message, privacy: .private)")
141+
}
142+
}
143+
53144
internal func logTrace(_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
54145
if level < .trace {
55146
return nil
56147
}
57148
return log(.trace, items: items, separator: separator, terminator: terminator, file: file, line: line, column: column, function: function)
58149
}
59150

151+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
152+
func trace(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
153+
let logger = if let category {
154+
Logger.custom(category)
155+
} else {
156+
Logger.default
157+
}
158+
159+
switch privacy {
160+
case .auto:
161+
logger.trace("\(message)")
162+
case .public:
163+
logger.trace("\(message, privacy: .public)")
164+
case .private:
165+
logger.trace("\(message, privacy: .private)")
166+
}
167+
}
168+
60169
private func log(_ logLevel: PrettyLoggerLevel, items: [Any], separator: String?, terminator: String?, file: String, line: Int, column: Int, function: String, date: Date = Date()) -> String? {
61170
let separator = separator ?? self.separator
62171
let terminator = terminator ?? self.terminator
@@ -94,3 +203,13 @@ public class PrettyLogger {
94203
return "\(stringDate)\(level) \(message) \(stringLocation)"
95204
}
96205
}
206+
207+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
208+
extension Logger {
209+
private static let subsytem = Bundle.main.bundleIdentifier!
210+
static let `default` = Logger(subsystem: subsytem, category: "default")
211+
212+
static func custom(_ category: String) -> Self {
213+
.init(subsystem: subsytem, category: category)
214+
}
215+
}

Sources/PrettyLoggerLevel.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import OSLog
23

34
public enum PrettyLoggerLevel: Int, Comparable, CaseIterable {
45
case disable = 0

Sources/PrettyLoggerPrivacy.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import OSLog
2+
3+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
4+
public enum PrettyLoggerPrivacy {
5+
case auto
6+
case `public`
7+
case `private`
8+
9+
var osLogPrivacy: OSLogPrivacy {
10+
switch self {
11+
case .auto:
12+
return .auto
13+
14+
case .public:
15+
return .public
16+
17+
case .private:
18+
return .private
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)