Skip to content

Commit 70ae578

Browse files
committed
Merge branch 'fix/frizlab/compilation_on_windows' into develop
2 parents 223886c + b238331 commit 70ae578

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
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
}

Tests/JSONLoggerTests/Helpers/FileHandleHelper.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,55 @@ extension FileHandle {
1919
if #available(macOS 10.15, *) {
2020
try close()
2121
} else {
22+
#if !os(Windows)
2223
/* closeFile exists but I’m not sure it never throws an ObjC exception, so I call the C function. */
2324
let ret = system_close(fileDescriptor)
2425
guard ret == 0 else {
2526
throw Errno()
2627
}
28+
#else
29+
fatalError("Unreachable code reached: In else of #available with only Apple platforms checks, but also on Windows.")
30+
#endif
2731
}
2832
}
2933

3034
func jl_readToEnd() throws -> Data? {
3135
if #available(macOS 10.15, tvOS 13.0, iOS 13.0, watchOS 6.0, *) {
3236
#if swift(>=5.2) || !canImport(Darwin)
3337
return try readToEnd()
34-
#else
38+
#elseif !os(Windows)
3539
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 512, alignment: MemoryLayout<UInt8>.alignment)
3640
defer {buffer.deallocate()}
3741

3842
var nread = 0
3943
var ret = Data()
40-
repeat {
41-
nread = system_read(fileDescriptor, buffer.baseAddress, buffer.count)
42-
ret += buffer[0..<nread]
43-
} while nread > 0
44+
while ({ nread = system_read(fileDescriptor, buffer.baseAddress, buffer.count); return nread }()) > 0 {
45+
ret += buffer[0..<Int(nread)]
46+
}
4447
guard nread >= 0 else {
4548
throw Errno()
4649
}
4750
return ret
51+
#else
52+
#error("How can we get here? We can import Darwin, but we are on Windows??")
4853
#endif
4954
} else {
55+
#if !os(Windows)
5056
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 512, alignment: MemoryLayout<UInt8>.alignment)
5157
defer {buffer.deallocate()}
5258

5359
var nread = 0
5460
var ret = Data()
55-
repeat {
56-
nread = system_read(fileDescriptor, buffer.baseAddress, buffer.count)
57-
ret += buffer[0..<nread]
58-
} while nread > 0
61+
while ({ nread = system_read(fileDescriptor, buffer.baseAddress, buffer.count); return nread }()) > 0 {
62+
ret += buffer[0..<Int(nread)]
63+
}
5964
guard nread >= 0 else {
6065
throw Errno()
6166
}
6267
return ret
68+
#else
69+
fatalError("Unreachable code reached: In else of #available with only Apple platforms checks, but also on Windows.")
70+
#endif
6371
}
6472
}
6573

0 commit comments

Comments
 (0)