|
50 | 50 | format: CombineOperatorOption.Format = .multiline,
|
51 | 51 | to: Output
|
52 | 52 | ) -> Publishers.HandleEvents<Self> {
|
| 53 | + // Note: |
53 | 54 | // Use local function for capture arguments.
|
54 |
| - func _print(_ value: Any, type: CombineOperatorOption.Event, terminator: String = "\n") { |
55 |
| - guard when.contains(type) else { return } |
56 | 55 |
|
| 56 | + func _out<Output: TextOutputStream>(_ value: String, terminator: String = "\n", to: Output) { |
57 | 57 | let message = prefix.isEmpty
|
58 | 58 | ? "\(value)"
|
59 | 59 | : "\(prefix): \(value)"
|
|
62 | 62 | Swift.print(message, terminator: terminator, to: &out)
|
63 | 63 | }
|
64 | 64 |
|
| 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 | + |
65 | 81 | 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) |
77 | 99 | }
|
| 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 |
78 | 121 | }
|
79 | 122 |
|
80 | 123 | var option = Pretty.sharedOption
|
|
0 commit comments