@@ -38,8 +38,6 @@ public final class PreviewAction: AsyncAction {
3838 static var allowConcurrentPreviews = false
3939
4040 private let printHTMLTemplatePath : Bool
41-
42- var logHandle = LogHandle . standardOutput
4341
4442 let port : Int
4543
@@ -89,17 +87,17 @@ public final class PreviewAction: AsyncAction {
8987 ///
9088 /// - Parameter logHandle: The file handle that the convert and preview actions will print debug messages to.
9189 public func perform( logHandle: inout LogHandle ) async throws -> ActionResult {
92- self . logHandle = logHandle
90+ self . logHandle. sync { $0 = logHandle }
9391
9492 if let rootURL = convertAction. rootURL {
95- print ( " Input: \( rootURL. path) " , to : & self . logHandle )
93+ print ( " Input: \( rootURL. path) " )
9694 }
9795 // TODO: This never did output human readable string; rdar://74324255
9896 // print("Input: \(convertAction.documentationCoverageOptions)", to: &self.logHandle)
9997
10098 // In case a developer is using a custom template log its path.
10199 if printHTMLTemplatePath, let htmlTemplateDirectory = convertAction. htmlTemplateDirectory {
102- print ( " Template: \( htmlTemplateDirectory. path) " , to : & self . logHandle )
100+ print ( " Template: \( htmlTemplateDirectory. path) " )
103101 }
104102
105103 let previewResult = try await preview ( )
@@ -124,15 +122,16 @@ public final class PreviewAction: AsyncAction {
124122 let previewResult : ActionResult
125123 // Preview the output and monitor the source bundle for changes.
126124 do {
127- print ( String ( repeating: " = " , count: 40 ) , to : & logHandle )
125+ print ( String ( repeating: " = " , count: 40 ) )
128126 if let previewURL = URL ( string: " http://localhost: \( port) " ) {
129- print ( " Starting Local Preview Server " , to : & logHandle )
127+ print ( " Starting Local Preview Server " )
130128 printPreviewAddresses ( base: previewURL)
131- print ( String ( repeating: " = " , count: 40 ) , to : & logHandle )
129+ print ( String ( repeating: " = " , count: 40 ) )
132130 }
133131
134132 let to : PreviewServer . Bind = bindServerToSocketPath. map { . socket( path: $0) } ?? . localhost( port: port)
135- servers [ serverIdentifier] = try PreviewServer ( contentURL: convertAction. targetDirectory, bindTo: to, logHandle: & logHandle)
133+ var logHandleCopy = logHandle. sync { $0 }
134+ servers [ serverIdentifier] = try PreviewServer ( contentURL: convertAction. targetDirectory, bindTo: to, logHandle: & logHandleCopy)
136135
137136 // When the user stops docc - stop the preview server first before exiting.
138137 trapSignals ( )
@@ -159,7 +158,8 @@ public final class PreviewAction: AsyncAction {
159158
160159 func convert( ) async throws -> ActionResult {
161160 convertAction = try createConvertAction ( )
162- let ( result, context) = try await convertAction. perform ( logHandle: & logHandle)
161+ var logHandleCopy = logHandle. sync { $0 }
162+ let ( result, context) = try await convertAction. perform ( logHandle: & logHandleCopy)
163163
164164 previewPaths = try context. previewPaths ( )
165165 return result
@@ -168,15 +168,23 @@ public final class PreviewAction: AsyncAction {
168168 private func printPreviewAddresses( base: URL ) {
169169 // If the preview paths are empty, just print the base.
170170 let firstPath = previewPaths. first ?? " "
171- print ( " \t Address: \( base. appendingPathComponent ( firstPath) . absoluteString) " , to : & logHandle )
171+ print ( " \t Address: \( base. appendingPathComponent ( firstPath) . absoluteString) " )
172172
173173 let spacing = String ( repeating: " " , count: " Address: " . count)
174174 for previewPath in previewPaths. dropFirst ( ) {
175- print ( " \t \( spacing) \( base. appendingPathComponent ( previewPath) . absoluteString) " , to : & logHandle )
175+ print ( " \t \( spacing) \( base. appendingPathComponent ( previewPath) . absoluteString) " )
176176 }
177177 }
178178
179- var monitoredConvertTask : Task < Void , Never > ?
179+ private var logHandle : Synchronized < LogHandle > = . init( . none)
180+
181+ fileprivate func print( _ string: String , terminator: String = " \n " ) {
182+ logHandle. sync { logHandle in
183+ Swift . print ( string, terminator: terminator, to: & logHandle)
184+ }
185+ }
186+
187+ fileprivate var monitoredConvertTask : Task < Void , Never > ?
180188}
181189
182190// Monitoring a source folder: Asynchronous output reading and file system events are supported only on macOS.
@@ -192,38 +200,27 @@ extension PreviewAction {
192200 }
193201
194202 monitor = try DirectoryMonitor ( root: rootURL) { _, _ in
195- print ( " Source bundle was modified, converting... " , terminator: " " , to : & self . logHandle )
203+ self . print ( " Source bundle was modified, converting... " , terminator: " " )
196204 self . monitoredConvertTask? . cancel ( )
197205 self . monitoredConvertTask = Task {
198- defer {
199- // Reload the directory contents and start to monitor for changes.
200- do {
201- try monitor. restart ( )
202- } catch {
203- // The file watching system API has thrown, stop watching.
204- print ( " Watching for changes has failed. To continue preview with watching restart docc. " , to: & self . logHandle)
205- print ( error. localizedDescription, to: & self . logHandle)
206- }
207- }
208-
209206 do {
210207 let result = try await self . convert ( )
211208 if result. didEncounterError {
212209 throw ErrorsEncountered ( )
213210 }
214- print ( " Done. " , to : & self . logHandle )
211+ self . print ( " Done. " )
215212 } catch DocumentationContext . ContextError . registrationDisabled {
216213 // The context cancelled loading the bundles and threw to yield execution early.
217- print ( " \n Conversion cancelled... " , to : & self . logHandle )
214+ self . print ( " \n Conversion cancelled... " )
218215 } catch is CancellationError {
219- print ( " \n Conversion cancelled... " , to : & self . logHandle )
216+ self . print ( " \n Conversion cancelled... " )
220217 } catch {
221- print ( " \n \( error. localizedDescription) \n Compilation failed " , to : & self . logHandle )
218+ self . print ( " \n \( error. localizedDescription) \n Compilation failed " )
222219 }
223220 }
224221 }
225222 try monitor. start ( )
226- print ( " Monitoring \( rootURL. path) for changes... " , to : & self . logHandle )
223+ self . print ( " Monitoring \( rootURL. path) for changes... " )
227224 }
228225}
229226#endif // !os(Linux) && !os(Android)
0 commit comments