|
| 1 | +//===----------------- OSLogStringTypes.swift -----------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +// This file defines extensions for interpolating strings into a OSLogMesage. |
| 14 | +// It defines `appendInterpolation` function for String type. It also defines |
| 15 | +// extensions for serializing strings into the argument buffer passed to |
| 16 | +// os_log ABIs. Note that os_log requires passing a stable pointer to an |
| 17 | +// interpolated string. The SPI: `_convertConstStringToUTF8PointerArgument` |
| 18 | +// is used to construct a stable pointer to a (dynamic) string. |
| 19 | +// |
| 20 | +// The `appendInterpolation` function defined in this file accept formatting |
| 21 | +// and privacy options along with the interpolated expression as shown below: |
| 22 | +// |
| 23 | +// "\(x, privacy: .public\)" |
| 24 | +// |
| 25 | +// TODO: support formatting options such as left and right padding |
| 26 | +// (e.g. %10s, %-10s). |
| 27 | + |
| 28 | +extension OSLogInterpolation { |
| 29 | + |
| 30 | + /// Define interpolation for expressions of type String. |
| 31 | + /// - Parameters: |
| 32 | + /// - argumentString: the interpolated expression of type String, which is autoclosured. |
| 33 | + /// - privacy: a privacy qualifier which is either private or public. Default is private. |
| 34 | + /// TODO: create a specifier to denote auto-inferred privacy level and make it default. |
| 35 | + @_transparent |
| 36 | + @_optimize(none) |
| 37 | + public mutating func appendInterpolation( |
| 38 | + _ argumentString: @autoclosure @escaping () -> String, |
| 39 | + privacy: Privacy = .private |
| 40 | + ) { |
| 41 | + guard argumentCount < maxOSLogArgumentCount else { return } |
| 42 | + |
| 43 | + let isPrivateArgument = isPrivate(privacy) |
| 44 | + formatString += getStringFormatSpecifier(isPrivateArgument) |
| 45 | + addStringHeaders(isPrivateArgument) |
| 46 | + |
| 47 | + arguments.append(argumentString) |
| 48 | + argumentCount += 1 |
| 49 | + } |
| 50 | + |
| 51 | + /// Update preamble and append argument headers based on the parameters of |
| 52 | + /// the interpolation. |
| 53 | + @_transparent |
| 54 | + @_optimize(none) |
| 55 | + @usableFromInline |
| 56 | + internal mutating func addStringHeaders(_ isPrivate: Bool) { |
| 57 | + // Append argument header. |
| 58 | + let header = getArgumentHeader(isPrivate: isPrivate, type: .string) |
| 59 | + arguments.append(header) |
| 60 | + |
| 61 | + // Append number of bytes needed to serialize the argument. |
| 62 | + let byteCount = sizeForEncoding() |
| 63 | + arguments.append(UInt8(byteCount)) |
| 64 | + |
| 65 | + // Increment total byte size by the number of bytes needed for this |
| 66 | + // argument, which is the sum of the byte size of the argument and |
| 67 | + // two bytes needed for the headers. |
| 68 | + totalBytesForSerializingArguments += byteCount + 2 |
| 69 | + |
| 70 | + preamble = getUpdatedPreamble(isPrivate: isPrivate, isScalar: false) |
| 71 | + } |
| 72 | + |
| 73 | + /// Construct an os_log format specifier from the given parameters. |
| 74 | + /// This function must be constant evaluable and all its arguments |
| 75 | + /// must be known at compile time. |
| 76 | + @inlinable |
| 77 | + @_semantics("oslog.interpolation.getFormatSpecifier") |
| 78 | + @_effects(readonly) |
| 79 | + @_optimize(none) |
| 80 | + internal func getStringFormatSpecifier(_ isPrivate: Bool) -> String { |
| 81 | + // TODO: create a specifier to denote auto-inferred privacy. |
| 82 | + return isPrivate ? "%{private}s" : "%{public}s" |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +extension OSLogArguments { |
| 87 | + /// Append an (autoclosured) interpolated expression of String type, passed to |
| 88 | + /// `OSLogMessage.appendInterpolation`, to the array of closures tracked |
| 89 | + /// by this instance. |
| 90 | + @usableFromInline |
| 91 | + internal mutating func append(_ value: @escaping () -> String) { |
| 92 | + argumentClosures!.append({ $0.serialize(value()) }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/// Return the byte size of a pointer as strings are passed to the C os_log ABIs by |
| 97 | +/// a stable pointer to its UTF8 bytes. Since pointers do not have a public |
| 98 | +/// bitWidth property, and since MemoryLayout is not supported by the constant |
| 99 | +/// evaluator, this function returns the byte size of Int, which must equal the |
| 100 | +/// word length of the target architecture and hence the pointer size. |
| 101 | +/// This function must be constant evaluable. |
| 102 | +@inlinable |
| 103 | +@_optimize(none) |
| 104 | +@_effects(readonly) |
| 105 | +@_semantics("oslog.string.sizeForEncoding") |
| 106 | +internal func sizeForEncoding() -> Int { |
| 107 | + return Int.bitWidth &>> logBitsPerByte |
| 108 | +} |
| 109 | + |
| 110 | +extension OSLogByteBufferBuilder { |
| 111 | + /// Serialize a string at the buffer location pointed to by `position`. |
| 112 | + /// Record any auxiliary storage created for getting a stable pointer to the |
| 113 | + /// parameter string in the `self.auxiliaryStorage` property, so that the |
| 114 | + /// storage is alive for the lifetime of `self`. |
| 115 | + @usableFromInline |
| 116 | + internal mutating func serialize(_ stringValue: String) { |
| 117 | + let (optionalStorage, bytePointer): (AnyObject?, UnsafeRawPointer) = |
| 118 | + _convertConstStringToUTF8PointerArgument( |
| 119 | + stringValue) |
| 120 | + |
| 121 | + if let storage = optionalStorage { |
| 122 | + auxiliaryStorage.append(storage) |
| 123 | + } |
| 124 | + |
| 125 | + let byteCount = sizeForEncoding() |
| 126 | + let dest = UnsafeMutableRawBufferPointer(start: position, count: byteCount) |
| 127 | + withUnsafeBytes(of: bytePointer) { dest.copyMemory(from: $0) } |
| 128 | + position += byteCount |
| 129 | + } |
| 130 | +} |
0 commit comments