Skip to content

Commit e871fb2

Browse files
committed
Improve OSLog context and fix tests
1 parent d762e3f commit e871fb2

8 files changed

Lines changed: 290 additions & 101 deletions

Sources/Global.swift

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,58 @@ import Foundation
22

33
// MARK: - Primary OSLog-based API
44

5-
public func logFatal(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
6-
PrettyLogger.shared.logFatal(message, category: category, privacy: privacy)
5+
public func logFatal(
6+
_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto,
7+
file: String = #file, line: Int = #line, column: Int = #column, function: String = #function
8+
) {
9+
PrettyLogger.shared.logFatal(
10+
message, category: category, privacy: privacy, file: file, line: line, column: column,
11+
function: function)
712
}
813

9-
public func logError(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
10-
PrettyLogger.shared.logError(message, category: category, privacy: privacy)
14+
public func logError(
15+
_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto,
16+
file: String = #file, line: Int = #line, column: Int = #column, function: String = #function
17+
) {
18+
PrettyLogger.shared.logError(
19+
message, category: category, privacy: privacy, file: file, line: line, column: column,
20+
function: function)
1121
}
1222

13-
public func logWarning(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
14-
PrettyLogger.shared.logWarning(message, category: category, privacy: privacy)
23+
public func logWarning(
24+
_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto,
25+
file: String = #file, line: Int = #line, column: Int = #column, function: String = #function
26+
) {
27+
PrettyLogger.shared.logWarning(
28+
message, category: category, privacy: privacy, file: file, line: line, column: column,
29+
function: function)
1530
}
1631

17-
public func logInfo(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
18-
PrettyLogger.shared.logInfo(message, category: category, privacy: privacy)
32+
public func logInfo(
33+
_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto,
34+
file: String = #file, line: Int = #line, column: Int = #column, function: String = #function
35+
) {
36+
PrettyLogger.shared.logInfo(
37+
message, category: category, privacy: privacy, file: file, line: line, column: column,
38+
function: function)
1939
}
2040

21-
public func logDebug(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
22-
PrettyLogger.shared.logDebug(message, category: category, privacy: privacy)
41+
public func logDebug(
42+
_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto,
43+
file: String = #file, line: Int = #line, column: Int = #column, function: String = #function
44+
) {
45+
PrettyLogger.shared.logDebug(
46+
message, category: category, privacy: privacy, file: file, line: line, column: column,
47+
function: function)
2348
}
2449

25-
public func logTrace(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
26-
PrettyLogger.shared.logTrace(message, category: category, privacy: privacy)
50+
public func logTrace(
51+
_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto,
52+
file: String = #file, line: Int = #line, column: Int = #column, function: String = #function
53+
) {
54+
PrettyLogger.shared.logTrace(
55+
message, category: category, privacy: privacy, file: file, line: line, column: column,
56+
function: function)
2757
}
2858

2959
// MARK: - Legacy print-based API (deprecated)

Sources/PrettyLogger.swift

Lines changed: 152 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,88 +18,139 @@ public class PrettyLogger {
1818

1919
// MARK: - Primary OSLog-based API
2020

21-
public func logFatal(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
21+
internal func logFatal(
22+
_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto,
23+
file: String, line: Int, column: Int, function: String
24+
) {
2225
// Check level filtering
2326
if level < .fatal {
2427
return
2528
}
2629

27-
let logger = createLogger(for: category)
28-
logWithPrivacy(logger: logger, level: .fault, message: message, privacy: privacy)
29-
30-
// Send to output stream
31-
let output = PrettyLoggerOutput(level: .fatal, message: message)
32-
self.output.send(output)
30+
logWithPrivacy(
31+
logger: createLogger(for: category),
32+
level: .fault,
33+
prettyLoggerLevel: .fatal,
34+
message: message,
35+
privacy: privacy,
36+
file: file,
37+
line: line,
38+
column: column,
39+
function: function
40+
)
3341
}
3442

35-
public func logError(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
43+
internal func logError(
44+
_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto,
45+
file: String, line: Int, column: Int, function: String
46+
) {
3647
if level < .error {
3748
return
3849
}
3950

40-
let logger = createLogger(for: category)
41-
logWithPrivacy(logger: logger, level: .error, message: message, privacy: privacy)
42-
43-
// Send to output stream
44-
let output = PrettyLoggerOutput(level: .error, message: message)
45-
self.output.send(output)
51+
logWithPrivacy(
52+
logger: createLogger(for: category),
53+
level: .error,
54+
prettyLoggerLevel: .error,
55+
message: message,
56+
privacy: privacy,
57+
file: file,
58+
line: line,
59+
column: column,
60+
function: function
61+
)
4662
}
4763

48-
public func logWarning(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
64+
internal func logWarning(
65+
_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto,
66+
file: String, line: Int, column: Int, function: String
67+
) {
4968
if level < .warn {
5069
return
5170
}
5271

53-
let logger = createLogger(for: category)
54-
logWithPrivacy(logger: logger, level: .default, message: message, privacy: privacy)
55-
56-
// Send to output stream
57-
let output = PrettyLoggerOutput(level: .warn, message: message)
58-
self.output.send(output)
72+
logWithPrivacy(
73+
logger: createLogger(for: category),
74+
level: .default,
75+
prettyLoggerLevel: .warn,
76+
message: message,
77+
privacy: privacy,
78+
file: file,
79+
line: line,
80+
column: column,
81+
function: function
82+
)
5983
}
6084

61-
public func logInfo(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
85+
internal func logInfo(
86+
_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto,
87+
file: String, line: Int, column: Int, function: String
88+
) {
6289
if level < .info {
6390
return
6491
}
6592

66-
let logger = createLogger(for: category)
67-
logWithPrivacy(logger: logger, level: .info, message: message, privacy: privacy)
68-
69-
// Send to output stream
70-
let output = PrettyLoggerOutput(level: .info, message: message)
71-
self.output.send(output)
93+
logWithPrivacy(
94+
logger: createLogger(for: category),
95+
level: .info,
96+
prettyLoggerLevel: .info,
97+
message: message,
98+
privacy: privacy,
99+
file: file,
100+
line: line,
101+
column: column,
102+
function: function
103+
)
72104
}
73105

74-
public func logDebug(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
106+
internal func logDebug(
107+
_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto,
108+
file: String, line: Int, column: Int, function: String
109+
) {
75110
if level < .debug {
76111
return
77112
}
78113

79-
let logger = createLogger(for: category)
80-
logWithPrivacy(logger: logger, level: .debug, message: message, privacy: privacy)
81-
82-
// Send to output stream
83-
let output = PrettyLoggerOutput(level: .debug, message: message)
84-
self.output.send(output)
114+
logWithPrivacy(
115+
logger: createLogger(for: category),
116+
level: .debug,
117+
prettyLoggerLevel: .debug,
118+
message: message,
119+
privacy: privacy,
120+
file: file,
121+
line: line,
122+
column: column,
123+
function: function
124+
)
85125
}
86126

87-
public func logTrace(_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto) {
127+
internal func logTrace(
128+
_ message: String, category: String? = nil, privacy: PrettyLoggerPrivacy = .auto,
129+
file: String, line: Int, column: Int, function: String
130+
) {
88131
if level < .trace {
89132
return
90133
}
91134

92-
let logger = createLogger(for: category)
93-
logWithPrivacy(logger: logger, level: .debug, message: message, privacy: privacy)
94-
95-
// Send to output stream
96-
let output = PrettyLoggerOutput(level: .trace, message: message)
97-
self.output.send(output)
135+
logWithPrivacy(
136+
logger: createLogger(for: category),
137+
level: .default,
138+
prettyLoggerLevel: .trace,
139+
message: message,
140+
privacy: privacy,
141+
file: file,
142+
line: line,
143+
column: column,
144+
function: function
145+
)
98146
}
99147

100148
// MARK: - Legacy print-based API (internal)
101149

102-
internal func logFatalLegacy(_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
150+
internal func logFatalLegacy(
151+
_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String,
152+
line: Int, column: Int, function: String
153+
) -> String? {
103154
if level < .fatal {
104155
return nil
105156
}
@@ -108,7 +159,10 @@ public class PrettyLogger {
108159
line: line, column: column, function: function)
109160
}
110161

111-
internal func logErrorLegacy(_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
162+
internal func logErrorLegacy(
163+
_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String,
164+
line: Int, column: Int, function: String
165+
) -> String? {
112166
if level < .error {
113167
return nil
114168
}
@@ -117,7 +171,10 @@ public class PrettyLogger {
117171
line: line, column: column, function: function)
118172
}
119173

120-
internal func logWarningLegacy(_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
174+
internal func logWarningLegacy(
175+
_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String,
176+
line: Int, column: Int, function: String
177+
) -> String? {
121178
if level < .warn {
122179
return nil
123180
}
@@ -126,7 +183,10 @@ public class PrettyLogger {
126183
line: line, column: column, function: function)
127184
}
128185

129-
internal func logInfoLegacy(_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
186+
internal func logInfoLegacy(
187+
_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String,
188+
line: Int, column: Int, function: String
189+
) -> String? {
130190
if level < .info {
131191
return nil
132192
}
@@ -135,7 +195,10 @@ public class PrettyLogger {
135195
line: line, column: column, function: function)
136196
}
137197

138-
internal func logDebugLegacy(_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
198+
internal func logDebugLegacy(
199+
_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String,
200+
line: Int, column: Int, function: String
201+
) -> String? {
139202
if level < .debug {
140203
return nil
141204
}
@@ -144,7 +207,10 @@ public class PrettyLogger {
144207
line: line, column: column, function: function)
145208
}
146209

147-
internal func logTraceLegacy(_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String = #file, line: Int = #line, column: Int = #column, function: String = #function) -> String? {
210+
internal func logTraceLegacy(
211+
_ items: [Any], separator: String? = nil, terminator: String? = nil, file: String,
212+
line: Int, column: Int, function: String
213+
) -> String? {
148214
if level < .trace {
149215
return nil
150216
}
@@ -162,7 +228,8 @@ public class PrettyLogger {
162228
}
163229

164230
private func logWithPrivacy(
165-
logger: Logger, level: OSLogType, message: String, privacy: PrettyLoggerPrivacy
231+
logger: Logger, level: OSLogType, prettyLoggerLevel: PrettyLoggerLevel, message: String,
232+
privacy: PrettyLoggerPrivacy, file: String, line: Int, column: Int, function: String
166233
) {
167234
switch privacy {
168235
case .auto:
@@ -172,6 +239,42 @@ public class PrettyLogger {
172239
case .private:
173240
logger.log(level: level, "\(message, privacy: .private)")
174241
}
242+
243+
sendOutput(
244+
prettyLoggerLevel,
245+
message: message,
246+
file: file,
247+
line: line,
248+
column: column,
249+
function: function
250+
)
251+
}
252+
253+
private func sendOutput(
254+
_ logLevel: PrettyLoggerLevel, message: String, file: String, line: Int, column: Int,
255+
function: String, date: Date = Date()
256+
) {
257+
let stringToPrint = stringForCurrentStyle(
258+
logLevel: logLevel,
259+
message: message,
260+
terminator: terminator,
261+
file: file,
262+
line: line,
263+
column: column,
264+
function: function,
265+
date: date
266+
)
267+
268+
output.send(
269+
PrettyLoggerOutput(
270+
level: logLevel,
271+
message: message,
272+
file: (file as NSString).lastPathComponent,
273+
line: line,
274+
column: column,
275+
formatted: stringToPrint
276+
)
277+
)
175278
}
176279

177280
private func log(

0 commit comments

Comments
 (0)