@@ -175,11 +175,11 @@ public struct CLTLogger : LogHandler {
175175 /* We compute the data to print outside of the lock. */
176176 let data = Self . format ( message: message. description, flatMetadata: effectiveFlatMetadata, multilineMode: multilineMode, constants: constants)
177177
178- Self . write ( data, to: outputFileHandle)
178+ Self . writeLog ( data, to: outputFileHandle)
179179 }
180180
181181 /** Writes to the given file descriptor like the logger would. */
182- public static func write ( _ data: Data , to fh: FileHandle ) {
182+ public static func writeLog ( _ data: Data , to fh: FileHandle ) {
183183 /* We lock, because the writeAll function might split the write in more than 1 write
184184 * (if the write system call only writes a part of the data).
185185 * If another part of the program writes to the file descriptor, we might get interleaved data,
@@ -189,16 +189,42 @@ public struct CLTLogger : LogHandler {
189189 /* Is the write retried on interrupt?
190190 * We’ll assume yes, but we don’t and can’t know for sure
191191 * until FileHandle has been migrated to the open-source Foundation. */
192- if #available( macOS 10 . 15 . 4 , iOS 13 . 4 , tvOS 13 . 4 , watchOS 6 . 2 , * ) {
193- #if swift(>=5.2) || (!os(macOS) && !os(iOS) && !os(tvOS) && !os(watchOS) )
192+ if #available( macOS 10 . 15 . 4 , tvOS 13 . 4 , iOS 13 . 4 , watchOS 6 . 2 , * ) {
193+ #if swift(>=5.2) || !canImport(Darwin )
194194 _ = try ? fh. write ( contentsOf: data)
195195#else
196- /* Note: This throws an actual objc exception if it fails. */
197- fh. write ( data)
196+ /* Let’s write “manullay” (FileHandle’s write(_:) method throws an ObjC exception in case of an error).
197+ * This code is copied below. */
198+ data. withUnsafeBytes { bytes in
199+ guard !bytes. isEmpty else {
200+ return
201+ }
202+ var written : Int = 0
203+ repeat {
204+ written += write (
205+ fh. fileDescriptor,
206+ bytes. baseAddress!. advanced ( by: written) ,
207+ bytes. count - written
208+ )
209+ } while written < bytes. count && ( errno == EINTR || errno == EAGAIN)
210+ }
198211#endif
199212 } else {
200- /* Note: This throws an actual objc exception if it fails. */
201- fh. write ( data)
213+ /* Let’s write “manullay” (FileHandle’s write(_:) method throws an ObjC exception in case of an error).
214+ * This is a copy of the code just above. */
215+ data. withUnsafeBytes { bytes in
216+ guard !bytes. isEmpty else {
217+ return
218+ }
219+ var written : Int = 0
220+ repeat {
221+ written += write (
222+ fh. fileDescriptor,
223+ bytes. baseAddress!. advanced ( by: written) ,
224+ bytes. count - written
225+ )
226+ } while written < bytes. count && ( errno == EINTR || errno == EAGAIN)
227+ }
202228 }
203229 }
204230 }
0 commit comments