Skip to content

Commit 575e247

Browse files
committed
Merge branch 'fix/frizlab/possible_objc_exception' into develop
2 parents dc5daf5 + 36b1082 commit 575e247

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

Sources/CLTLogger.swift

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Tests/CLTLoggerTests/CLTLoggerTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ final class CLTLoggerTests : XCTestCase {
6868
var logger = Logger(label: "my logger")
6969
logger.logLevel = .trace
7070

71-
CLTLogger.write(Data("\n".utf8), to: .standardError)
71+
CLTLogger.writeLog(Data("\n".utf8), to: .standardError)
7272

7373
logger.trace( "trace: Example of a log at this level. Isn’t it amazing?")
7474
logger.debug( "debug: Example of a log at this level. Isn’t it amazing?")
@@ -78,11 +78,11 @@ final class CLTLoggerTests : XCTestCase {
7878
logger.error( "error: Example of a log at this level. Isn’t it amazing?")
7979
logger.critical("critical: Example of a log at this level. Isn’t it amazing?")
8080

81-
CLTLogger.write(Data("\n".utf8), to: .standardError)
81+
CLTLogger.writeLog(Data("\n".utf8), to: .standardError)
8282

8383
logger.info("An informational message with metadata.", metadata: ["component": "LoggerTester", "array-value": .array(["1", "2", "3"]), "dictionary-value": .dictionary(["key1": "value1", "key2": "value2", "key3": "value3"])])
8484

85-
CLTLogger.write(Data("\n".utf8), to: .standardError)
85+
CLTLogger.writeLog(Data("\n".utf8), to: .standardError)
8686
}
8787

8888
func testVisual3() {

0 commit comments

Comments
 (0)