@@ -27,7 +27,9 @@ public class ObservabilitySystem {
2727 description: " top scope " ,
2828 parent: . none,
2929 metadata: . none,
30- diagnosticsHandler: handlerProvider. diagnosticsHandler
30+ diagnosticsHandler: handlerProvider. diagnosticsHandler,
31+ outputHandler: handlerProvider. outputHandler,
32+ progressHandler: handlerProvider. progressHandler
3133 )
3234 }
3335
@@ -36,8 +38,10 @@ public class ObservabilitySystem {
3638 self . init ( SingleDiagnosticsHandler ( handler) )
3739 }
3840
39- private struct SingleDiagnosticsHandler : ObservabilityHandlerProvider , DiagnosticsHandler {
40- var diagnosticsHandler : DiagnosticsHandler { self }
41+ private struct SingleDiagnosticsHandler : ObservabilityHandlerProvider , ScopeDiagnosticsHandler , ScopeOutputHandler , ScopeProgressHandler {
42+ var diagnosticsHandler : ScopeDiagnosticsHandler { self }
43+ var outputHandler : ScopeOutputHandler { self }
44+ var progressHandler : ScopeProgressHandler { self }
4145
4246 let underlying : ( ObservabilityScope , Diagnostic ) -> Void
4347
@@ -48,32 +52,49 @@ public class ObservabilitySystem {
4852 func handleDiagnostic( scope: ObservabilityScope , diagnostic: Diagnostic ) {
4953 self . underlying ( scope, diagnostic)
5054 }
55+
56+ #warning("FIXME: should this do something?")
57+ func handleOutput( scope: ObservabilityScope , output: String ) {
58+ // NOOP
59+ }
60+
61+ func handleProgress( scope: ObservabilityScope , step: Int64 , total: Int64 , unit: String ? , description: String ? ) {
62+ // NOOP
63+ }
5164 }
5265}
5366
5467public protocol ObservabilityHandlerProvider {
55- var diagnosticsHandler : DiagnosticsHandler { get }
68+ var diagnosticsHandler : ScopeDiagnosticsHandler { get }
69+ var progressHandler : ScopeProgressHandler { get }
70+ var outputHandler : ScopeOutputHandler { get }
5671}
5772
5873// MARK: - ObservabilityScope
5974
60- public final class ObservabilityScope : DiagnosticsEmitterProtocol , CustomStringConvertible {
75+ public final class ObservabilityScope : DiagnosticsEmitterProtocol , OutputEmitterProtocol , ProgressEmitterProtocol , CustomStringConvertible {
6176 public let description : String
6277 private let parent : ObservabilityScope ?
6378 private let metadata : ObservabilityMetadata ?
6479
6580 private var diagnosticsHandler : DiagnosticsHanderWrapper
81+ private var outputHandler : ScopeOutputHandler
82+ private var progressHandler : ScopeProgressHandler
6683
6784 fileprivate init (
6885 description: String ,
6986 parent: ObservabilityScope ? ,
7087 metadata: ObservabilityMetadata ? ,
71- diagnosticsHandler: DiagnosticsHandler
88+ diagnosticsHandler: ScopeDiagnosticsHandler ,
89+ outputHandler: ScopeOutputHandler ,
90+ progressHandler: ScopeProgressHandler
7291 ) {
7392 self . description = description
7493 self . parent = parent
7594 self . metadata = metadata
7695 self . diagnosticsHandler = DiagnosticsHanderWrapper ( diagnosticsHandler)
96+ self . outputHandler = outputHandler
97+ self . progressHandler = progressHandler
7798 }
7899
79100 public func makeChildScope( description: String , metadata: ObservabilityMetadata ? = . none) -> Self {
@@ -82,7 +103,9 @@ public final class ObservabilityScope: DiagnosticsEmitterProtocol, CustomStringC
82103 description: description,
83104 parent: self ,
84105 metadata: mergedMetadata,
85- diagnosticsHandler: self . diagnosticsHandler
106+ diagnosticsHandler: self . diagnosticsHandler,
107+ outputHandler: self . outputHandler,
108+ progressHandler: self . progressHandler
86109 )
87110 }
88111
@@ -123,11 +146,19 @@ public final class ObservabilityScope: DiagnosticsEmitterProtocol, CustomStringC
123146 self . diagnosticsHandler. handleDiagnostic ( scope: self , diagnostic: diagnostic)
124147 }
125148
126- private struct DiagnosticsHanderWrapper : DiagnosticsHandler {
127- private let underlying : DiagnosticsHandler
149+ public func emit( output: String ) {
150+ self . outputHandler. handleOutput ( scope: self , output: output)
151+ }
152+
153+ public func emit( step: Int64 , total: Int64 , unit: String ? , description: String ? ) {
154+ self . progressHandler. handleProgress ( scope: self , step: step, total: total, unit: unit, description: description)
155+ }
156+
157+ private struct DiagnosticsHanderWrapper : ScopeDiagnosticsHandler {
158+ private let underlying : ScopeDiagnosticsHandler
128159 private var _errorsReported = ThreadSafeBox < Bool > ( false )
129160
130- init ( _ underlying: DiagnosticsHandler ) {
161+ init ( _ underlying: ScopeDiagnosticsHandler ) {
131162 self . underlying = underlying
132163 }
133164
@@ -146,7 +177,7 @@ public final class ObservabilityScope: DiagnosticsEmitterProtocol, CustomStringC
146177
147178// MARK: - Diagnostics
148179
149- public protocol DiagnosticsHandler {
180+ public protocol ScopeDiagnosticsHandler {
150181 func handleDiagnostic( scope: ObservabilityScope , diagnostic: Diagnostic )
151182}
152183
@@ -567,14 +598,25 @@ extension ObservabilitySystem {
567598 self . init ( DiagnosticsEngineAdapter ( diagnosticEngine: diagnosticEngine) )
568599 }
569600
570- private struct DiagnosticsEngineAdapter : ObservabilityHandlerProvider , DiagnosticsHandler {
601+ private struct DiagnosticsEngineAdapter : ObservabilityHandlerProvider , ScopeDiagnosticsHandler , ScopeOutputHandler , ScopeProgressHandler {
571602 let diagnosticEngine : DiagnosticsEngine
572603
573- var diagnosticsHandler : DiagnosticsHandler { self }
604+ var diagnosticsHandler : ScopeDiagnosticsHandler { self }
605+ var outputHandler : ScopeOutputHandler { self }
606+ var progressHandler : ScopeProgressHandler { self }
574607
575608 func handleDiagnostic( scope: ObservabilityScope , diagnostic: Diagnostic ) {
576609 diagnosticEngine. emit ( . init( diagnostic) )
577610 }
611+
612+ #warning("FIXME: do something here?")
613+ func handleOutput( scope: ObservabilityScope , output: String ) {
614+ // NOOP
615+ }
616+
617+ func handleProgress( scope: ObservabilityScope , step: Int64 , total: Int64 , unit: String ? , description: String ? ) {
618+ // NOOP
619+ }
578620 }
579621}
580622
@@ -663,3 +705,43 @@ extension ObservabilityMetadata {
663705 }
664706 }
665707}
708+
709+ // MARK: - Progress
710+
711+ public protocol ScopeProgressHandler {
712+ func handleProgress( scope: ObservabilityScope , step: Int64 , total: Int64 , unit: String ? , description: String ? )
713+ }
714+
715+ public protocol ProgressEmitterProtocol {
716+ func emit( step: Int64 , total: Int64 , unit: String ? , description: String ? )
717+ }
718+
719+ extension ProgressEmitterProtocol {
720+ public func emit( step: Int , total: Int , unit: String ? , description: String ? ) {
721+ self . emit ( step: Int64 ( step) , total: Int64 ( total) , unit: unit, description: description)
722+ }
723+ }
724+
725+ // MARK: - User output
726+
727+ public protocol ScopeOutputHandler {
728+ func handleOutput( scope: ObservabilityScope , output: String )
729+ }
730+
731+ public protocol OutputEmitterProtocol {
732+ func emit( output: String )
733+ }
734+
735+ extension OutputEmitterProtocol {
736+ public func emit( output bytes: [ UInt8 ] ) {
737+ if let output = String ( bytes: bytes, encoding: . utf8) {
738+ self . emit ( output: output)
739+ }
740+ }
741+
742+ public func emit( output data: Data ) {
743+ if let output = String ( data: data, encoding: . utf8) {
744+ self . emit ( output: output)
745+ }
746+ }
747+ }
0 commit comments