@@ -17,137 +17,154 @@ import SWBUtil
17
17
18
18
import struct SWBLLBuild. Diagnostic
19
19
import Foundation
20
+ import Synchronization
20
21
21
22
fileprivate class LoggingDelegate : BuildSystemDelegate {
22
- let queue = SWBQueue ( label: " LLBuildTests.LoggingDelegate.queue " , qos: UserDefaults . defaultRequestQoS)
23
23
var buildSystem : BuildSystem ? = nil
24
24
let fs : ( any SWBLLBuild . FileSystem ) ?
25
25
let fsProxy : any FSProxy
26
- var log : [ String ] = [ ]
27
- var errors : [ String ] = [ ]
26
+ private struct Output : Sendable {
27
+ var log : [ String ] = [ ]
28
+ var errors : [ String ] = [ ]
29
+ }
30
+ private let output = SWBMutex < Output > ( . init( ) )
28
31
let ignoreStatusChanges : Bool
29
32
33
+ var log : [ String ] {
34
+ output. withLock { $0. log }
35
+ }
36
+
37
+ var errors : [ String ] {
38
+ output. withLock { $0. errors }
39
+ }
40
+
30
41
init ( fs: any FSProxy , ignoreStatusChanges: Bool = false ) {
31
42
self . fs = SWBLLBuild . FileSystemImpl ( fs)
32
43
self . fsProxy = fs
33
44
self . ignoreStatusChanges = ignoreStatusChanges
34
45
}
35
46
47
+ func append( log: String ) {
48
+ output. withLock { output in
49
+ output. log. append ( log)
50
+ }
51
+ }
52
+
36
53
func lookupTool( _ name: String ) -> ( any Tool ) ? {
37
54
return nil
38
55
}
39
56
40
57
func hadCommandFailure( ) {
41
- queue . blocking_sync {
42
- self . log. append ( " had-command-failure " )
58
+ output . withLock { output in
59
+ output . log. append ( " had-command-failure " )
43
60
}
44
61
buildSystem? . cancel ( )
45
62
}
46
63
47
64
func handleDiagnostic( _ diagnostic: Diagnostic ) {
48
- queue . blocking_sync {
49
- self . log. append ( " \( diagnostic. kind) : \( diagnostic. message) " )
50
- self . errors. append ( " \( diagnostic. kind) : \( diagnostic. message) " )
65
+ output . withLock { output in
66
+ output . log. append ( " \( diagnostic. kind) : \( diagnostic. message) " )
67
+ output . errors. append ( " \( diagnostic. kind) : \( diagnostic. message) " )
51
68
}
52
69
}
53
70
54
71
func commandStatusChanged( _ command: Command , kind: CommandStatusKind ) {
55
72
if !ignoreStatusChanges {
56
- queue . blocking_sync {
57
- self . log. append ( " command-status-changed: \( command. name) , to: \( kind) " )
73
+ output . withLock { output in
74
+ output . log. append ( " command-status-changed: \( command. name) , to: \( kind) " )
58
75
}
59
76
}
60
77
}
61
78
func commandPreparing( _ command: Command ) {
62
- queue . blocking_sync {
63
- self . log. append ( " command-preparing: \( command. name) " )
79
+ output . withLock { output in
80
+ output . log. append ( " command-preparing: \( command. name) " )
64
81
}
65
82
}
66
83
func commandStarted( _ command: Command ) {
67
- queue . blocking_sync {
68
- self . log. append ( " command-started: \( command. name) " )
84
+ output . withLock { output in
85
+ output . log. append ( " command-started: \( command. name) " )
69
86
}
70
87
}
71
88
func shouldCommandStart( _ command: Command ) -> Bool {
72
- queue . blocking_sync {
73
- self . log. append ( " should-command-start: \( command. name) " )
89
+ output . withLock { output in
90
+ output . log. append ( " should-command-start: \( command. name) " )
74
91
}
75
92
return true
76
93
}
77
94
func commandFinished( _ command: Command , result: CommandResult ) {
78
- queue . blocking_sync {
79
- self . log. append ( " command-finished: \( command. name) " )
95
+ output . withLock { output in
96
+ output . log. append ( " command-finished: \( command. name) " )
80
97
}
81
98
}
82
99
func commandFoundDiscoveredDependency( _ command: Command , path: String , kind: DiscoveredDependencyKind ) {
83
- queue . blocking_sync {
84
- self . log. append ( " command-found-discovered-dependency: \( path) \( kind) " )
100
+ output . withLock { output in
101
+ output . log. append ( " command-found-discovered-dependency: \( path) \( kind) " )
85
102
}
86
103
}
87
104
func commandHadError( _ command: Command , message: String ) {
88
- queue . blocking_sync {
89
- self . log. append ( " command-had-error: \( command. name) : \( message) " )
90
- self . errors. append ( " command-had-error: \( command. name) : \( message) " )
105
+ output . withLock { output in
106
+ output . log. append ( " command-had-error: \( command. name) : \( message) " )
107
+ output . errors. append ( " command-had-error: \( command. name) : \( message) " )
91
108
}
92
109
}
93
110
func commandHadNote( _ command: Command , message: String ) {
94
- queue . blocking_sync {
95
- self . log. append ( " command-had-note: \( command. name) : \( message) " )
111
+ output . withLock { output in
112
+ output . log. append ( " command-had-note: \( command. name) : \( message) " )
96
113
}
97
114
}
98
115
func commandHadWarning( _ command: Command , message: String ) {
99
- queue . blocking_sync {
100
- self . log. append ( " command-had-warning: \( command. name) : \( message) " )
116
+ output . withLock { output in
117
+ output . log. append ( " command-had-warning: \( command. name) : \( message) " )
101
118
}
102
119
}
103
120
func commandProcessStarted( _ command: Command , process: ProcessHandle ) {
104
- queue . blocking_sync {
105
- self . log. append ( " command-process-started: \( command. name) -- \( command. description) " )
121
+ output . withLock { output in
122
+ output . log. append ( " command-process-started: \( command. name) -- \( command. description) " )
106
123
}
107
124
}
108
125
func commandProcessHadError( _ command: Command , process: ProcessHandle , message: String ) {
109
- queue . blocking_sync {
110
- self . log. append ( " command-process-error: \( message) " )
126
+ output . withLock { output in
127
+ output . log. append ( " command-process-error: \( message) " )
111
128
}
112
129
}
113
130
func commandProcessHadOutput( _ command: Command , process: ProcessHandle , data: [ UInt8 ] ) {
114
- queue . blocking_sync {
115
- self . log. append ( " command-process-output: \( command. name) : \( ByteString ( data) . bytes. asReadableString ( ) . debugDescription) " )
131
+ output . withLock { output in
132
+ output . log. append ( " command-process-output: \( command. name) : \( ByteString ( data) . bytes. asReadableString ( ) . debugDescription) " )
116
133
}
117
134
}
118
135
func commandProcessFinished( _ command: Command , process: ProcessHandle , result: CommandExtendedResult ) {
119
- queue . blocking_sync {
120
- self . log. append ( " command-process-finished: \( command. name) " )
136
+ output . withLock { output in
137
+ output . log. append ( " command-process-finished: \( command. name) " )
121
138
}
122
139
}
123
140
func determinedRuleNeedsToRun( _ rule: BuildKey , reason: RuleRunReason , inputRule: BuildKey ? ) {
124
- queue . blocking_sync {
125
- self . log. append ( " determined-rule-needs-to-run: \( rule. description) " )
141
+ output . withLock { output in
142
+ output . log. append ( " determined-rule-needs-to-run: \( rule. description) " )
126
143
}
127
144
}
128
145
func cycleDetected( rules: [ BuildKey ] ) {
129
- queue . blocking_sync {
130
- self . log. append ( " cycle-detected: \( rules. map { $0. key } ) " )
146
+ output . withLock { output in
147
+ output . log. append ( " cycle-detected: \( rules. map { $0. key } ) " )
131
148
}
132
149
}
133
150
func commandCannotBuildOutputDueToMissingInputs( _ command: Command , output: BuildKey , inputs: [ BuildKey ] ) {
134
- queue . blocking_sync {
151
+ self . output . withLock { outputBox in
135
152
let msg = " commandCannotBuildOutputDueToMissingInputs: \( command. name) \( output. key) \( inputs. map { $0. key } ) "
136
- self . log. append ( msg)
137
- self . errors. append ( msg)
153
+ outputBox . log. append ( msg)
154
+ outputBox . errors. append ( msg)
138
155
}
139
156
}
140
157
func cannotBuildNodeDueToMultipleProducers( output: BuildKey , commands: [ Command ] ) {
141
- queue . blocking_sync {
158
+ self . output . withLock { outputBox in
142
159
let msg = " cannotBuildNodeDueToMultipleProducers: \( output. key) \( commands. map { $0. name } ) "
143
- self . log. append ( msg)
144
- self . errors. append ( msg)
160
+ outputBox . log. append ( msg)
161
+ outputBox . errors. append ( msg)
145
162
}
146
163
}
147
164
148
165
func shouldResolveCycle( rules: [ BuildKey ] , candidate: BuildKey , action: CycleAction ) -> Bool {
149
- queue . blocking_sync {
150
- self . log. append ( " should-resolve-cycle: \( rules. map { $0. key } ) " )
166
+ output . withLock { output in
167
+ output . log. append ( " should-resolve-cycle: \( rules. map { $0. key } ) " )
151
168
}
152
169
return false ;
153
170
}
@@ -521,7 +538,7 @@ fileprivate class LoggingDelegate: BuildSystemDelegate {
521
538
}
522
539
523
540
func execute( _ command: Command , _ commandInterface: BuildSystemCommandInterface ) -> Bool {
524
- delegate. log . append ( " write-command: execute " )
541
+ delegate. append ( log : " write-command: execute " )
525
542
do {
526
543
try delegate. fsProxy. write ( Static . outputPath, contents: [ ] )
527
544
return true
0 commit comments