Skip to content

Commit 6317b17

Browse files
Merge pull request #23437 from ravikandhadai/loggingAPI-PR
[os_log][stdlib-private] Add tests for checking the correctness of buffer and format string construction in the new OS log APIs.
2 parents 1131fe1 + 2f2afd0 commit 6317b17

File tree

2 files changed

+386
-1
lines changed

2 files changed

+386
-1
lines changed

stdlib/private/OSLog/OSLog.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,28 @@ internal func osLog(
6767
}
6868
bufferMemory.deallocate()
6969
}
70+
71+
/// A test helper that constructs a byte buffer and a format string from an
72+
/// instance of `OSLogMessage` using the same logic as the function `osLog`,
73+
/// and applies a given `assertion` to the constructed format string and
74+
/// byte buffer. This function should be used only in tests.
75+
/// - Parameters:
76+
/// - message: An instance of `OSLogMessage` created from string interpolation
77+
/// - assertion: A closure that takes a format string and a pointer to a
78+
/// byte buffer and asserts a condition.
79+
public // @testable
80+
func _checkFormatStringAndBuffer(
81+
_ message: OSLogMessage,
82+
with assertion: (String, UnsafeBufferPointer<UInt8>) -> Void
83+
) {
84+
let bufferSize = message.bufferSize
85+
let bufferMemory = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize)
86+
var builder = OSLogByteBufferBuilder(bufferMemory)
87+
message.serializeArguments(into: &builder)
88+
89+
assertion(
90+
message.formatString,
91+
UnsafeBufferPointer(start: UnsafePointer(bufferMemory), count: bufferSize))
92+
93+
bufferMemory.deallocate()
94+
}

0 commit comments

Comments
 (0)