Skip to content

Commit b598293

Browse files
feature: output to external log files on Combine extension
1 parent d7ccda0 commit b598293

File tree

3 files changed

+1878
-1328
lines changed

3 files changed

+1878
-1328
lines changed

Sources/Public/CombineExtension.swift

+56-13
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
format: CombineOperatorOption.Format = .multiline,
5151
to: Output
5252
) -> Publishers.HandleEvents<Self> {
53+
// Note:
5354
// Use local function for capture arguments.
54-
func _print(_ value: Any, type: CombineOperatorOption.Event, terminator: String = "\n") {
55-
guard when.contains(type) else { return }
5655

56+
func _out<Output: TextOutputStream>(_ value: String, terminator: String = "\n", to: Output) {
5757
let message = prefix.isEmpty
5858
? "\(value)"
5959
: "\(prefix): \(value)"
@@ -62,19 +62,62 @@
6262
Swift.print(message, terminator: terminator, to: &out)
6363
}
6464

65+
func _print(_ value: String, type: CombineOperatorOption.Event) {
66+
guard when.contains(type) else { return }
67+
68+
// Console
69+
_out(value, to: to)
70+
71+
// Log files
72+
#if targetEnvironment(simulator) || os(macOS)
73+
// Do not output to log when specifed `Output`.
74+
if to is StandardOutput {
75+
_out(value, to: Pretty.plainLogStream)
76+
_out(value, to: Pretty.coloredLogStream)
77+
}
78+
#endif
79+
}
80+
6581
func _prettyPrint(value: Any, label: String, type: CombineOperatorOption.Event) {
66-
var s: String = ""
67-
switch format {
68-
case .singleline:
69-
Swift.print("receive \(label): ", terminator: "", to: &s)
70-
Pretty.print(value, option: option, to: &s)
71-
_print(s, type: type, terminator: "")
72-
73-
case .multiline:
74-
Swift.print("receive \(label):", to: &s)
75-
Pretty.prettyPrint(value, option: option, to: &s)
76-
_print(s, type: type, terminator: "")
82+
guard when.contains(type) else { return }
83+
84+
var plain: String = ""
85+
86+
// Console
87+
do {
88+
switch format {
89+
case .singleline:
90+
Swift.print("receive \(label): ", terminator: "", to: &plain)
91+
Pretty.print(value, option: option, colored: false, to: &plain)
92+
93+
case .multiline:
94+
Swift.print("receive \(label):", to: &plain)
95+
Pretty.prettyPrint(value, option: option, to: &plain)
96+
}
97+
98+
_out(plain, terminator: "", to: to)
7799
}
100+
101+
// Log files
102+
#if targetEnvironment(simulator) || os(macOS)
103+
// Do not output to log when specifed `Output`.
104+
if to is StandardOutput {
105+
var colored: String = ""
106+
107+
switch format {
108+
case .singleline:
109+
Swift.print("receive \(label): ", terminator: "", to: &colored)
110+
Pretty.print(value, option: option, colored: true, to: &colored)
111+
112+
case .multiline:
113+
Swift.print("receive \(label):", to: &colored)
114+
Pretty.prettyPrint(value, option: option, colored: true, to: &colored)
115+
}
116+
117+
_out(plain, terminator: "", to: Pretty.plainLogStream)
118+
_out(colored, terminator: "", to: Pretty.coloredLogStream)
119+
}
120+
#endif
78121
}
79122

80123
var option = Pretty.sharedOption

Sources/Public/Pretty.swift

+7-5
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212

1313
#if canImport(Foundation)
1414
import Foundation
15-
16-
private var plainLogStream = LogOutputStream(url: URL(fileURLWithPath: "/tmp/SwiftPrettyPrint/output.log"))
17-
private var coloredLogStream = LogOutputStream(url: URL(fileURLWithPath: "/tmp/SwiftPrettyPrint/output-colored.log"))
1815
#endif
1916

2017
public class Pretty {
2118
/// Global format option
2219
public static var sharedOption: Option = .init()
2320

21+
static var plainLogStream = LogOutputStream(url: URL(fileURLWithPath: "/tmp/SwiftPrettyPrint/output.log"))
22+
static var coloredLogStream = LogOutputStream(url: URL(fileURLWithPath: "/tmp/SwiftPrettyPrint/output-colored.log"))
23+
2424
private init() {}
2525
}
2626

@@ -54,9 +54,10 @@ extension Pretty {
5454
_ targets: Any...,
5555
separator: String = " ",
5656
option: Option = Pretty.sharedOption,
57+
colored: Bool = false,
5758
to output: inout Target
5859
) {
59-
let plain = _print(label: label, targets, separator: separator, option: option, colored: false)
60+
let plain = _print(label: label, targets, separator: separator, option: option, colored: colored)
6061
Swift.print(plain, to: &output)
6162
}
6263

@@ -87,9 +88,10 @@ extension Pretty {
8788
_ targets: Any...,
8889
separator: String = "\n",
8990
option: Option = Pretty.sharedOption,
91+
colored: Bool = false,
9092
to output: inout Target
9193
) {
92-
let plain = _prettyPrint(label: label, targets, separator: separator, option: option, colored: false)
94+
let plain = _prettyPrint(label: label, targets, separator: separator, option: option, colored: colored)
9395
Swift.print(plain, to: &output)
9496
}
9597

0 commit comments

Comments
 (0)