Skip to content

Commit 86494e0

Browse files
committed
Fix main target compilation on Windows
1 parent 223886c commit 86494e0

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

Sources/JSONLogger.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,19 +236,17 @@ public struct JSONLogger : LogHandler {
236236
let interLogData: Data
237237
if Self.isFirstLog {interLogData = Data(); Self.isFirstLog = false}
238238
else {interLogData = lineSeparator}
239-
/* Is there a better idea than silently drop the message in case of fail? */
240-
/* Is the write retried on interrupt?
241-
* We’ll assume yes, but we don’t and can’t know for sure
242-
* until FileHandle has been migrated to the open-source Foundation. */
243239
let data = interLogData + lineDataNoSeparator
240+
let fh = outputFileHandle
241+
244242
/* Is there a better idea than silently drop the message in case of fail? */
245243
/* Is the write retried on interrupt?
246244
* We’ll assume yes, but we don’t and can’t know for sure
247245
* until FileHandle has been migrated to the open-source Foundation. */
248246
if #available(macOS 10.15.4, tvOS 13.4, iOS 13.4, watchOS 6.2, *) {
249247
#if swift(>=5.2) || !canImport(Darwin)
250-
_ = try? outputFileHandle.write(contentsOf: data)
251-
#else
248+
_ = try? fh.write(contentsOf: data)
249+
#elseif !os(Windows)
252250
/* Let’s write “manually” (FileHandle’s write(_:) method throws an ObjC exception in case of an error).
253251
* This code is copied below. */
254252
data.withUnsafeBytes{ bytes in
@@ -257,15 +255,20 @@ public struct JSONLogger : LogHandler {
257255
}
258256
var written: Int = 0
259257
repeat {
258+
/* Note: Windows version takes an UInt32 for the number of bytes to write and returns an Int32.
259+
* It does not matter, fh.fileDescriptor is not accessible on Windows anyways… */
260260
written += write(
261-
outputFileHandle.fileDescriptor,
261+
fh.fileDescriptor,
262262
bytes.baseAddress!.advanced(by: written),
263263
bytes.count - written
264264
)
265265
} while written < bytes.count && (errno == EINTR || errno == EAGAIN)
266266
}
267+
#else
268+
#error("How can we get here? We can import Darwin, but we are on Windows??")
267269
#endif
268270
} else {
271+
#if !os(Windows)
269272
/* Let’s write “manually” (FileHandle’s write(_:) method throws an ObjC exception in case of an error).
270273
* This is a copy of the code just above. */
271274
data.withUnsafeBytes{ bytes in
@@ -274,13 +277,18 @@ public struct JSONLogger : LogHandler {
274277
}
275278
var written: Int = 0
276279
repeat {
280+
/* Note: Windows version takes an UInt32 for the number of bytes to write and returns an Int32.
281+
* It does not matter, fh.fileDescriptor is not accessible on Windows anyways… */
277282
written += write(
278-
outputFileHandle.fileDescriptor,
283+
fh.fileDescriptor,
279284
bytes.baseAddress!.advanced(by: written),
280285
bytes.count - written
281286
)
282287
} while written < bytes.count && (errno == EINTR || errno == EAGAIN)
283288
}
289+
#else
290+
fatalError("Unreachable code reached: In else of #available with only Apple platforms checks, but also on Windows.")
291+
#endif
284292
}
285293
}
286294
}

0 commit comments

Comments
 (0)