|
| 1 | +//===----------------- OSLogIntegerTypes.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 integer expressions into a |
| 14 | +// OSLogMesage. It defines `appendInterpolation` functions for standard integer |
| 15 | +// types. It also defines extensions for serializing integer types into the |
| 16 | +// argument buffer passed to os_log ABIs. |
| 17 | +// |
| 18 | +// The `appendInterpolation` functions defined in this file accept formatting |
| 19 | +// and privacy options along with the interpolated expression as shown below: |
| 20 | +// |
| 21 | +// "\(x, format: .hex, privacy: .private\)" |
| 22 | + |
| 23 | +extension OSLogInterpolation { |
| 24 | + |
| 25 | + /// Define interpolation for expressions of type Int. |
| 26 | + /// - Parameters: |
| 27 | + /// - number: the interpolated expression of type Int, which is autoclosured. |
| 28 | + /// - format: a formatting option available for integer types, defined by the |
| 29 | + /// enum `IntFormat`. |
| 30 | + /// - privacy: a privacy qualifier which is either private or public. |
| 31 | + /// The default is public. |
| 32 | + @_transparent |
| 33 | + @_optimize(none) |
| 34 | + public mutating func appendInterpolation( |
| 35 | + _ number: @autoclosure @escaping () -> Int, |
| 36 | + format: IntFormat = .decimal, |
| 37 | + privacy: Privacy = .public |
| 38 | + ) { |
| 39 | + appendInteger(number, format: format, privacy: privacy) |
| 40 | + } |
| 41 | + |
| 42 | + /// Define interpolation for expressions of type Int32. |
| 43 | + /// - Parameters: |
| 44 | + /// - number: the interpolated expression of type Int32, which is autoclosured. |
| 45 | + /// - format: a formatting option available for integer types, defined by the |
| 46 | + /// enum `IntFormat`. |
| 47 | + /// - privacy: a privacy qualifier which is either private or public. |
| 48 | + /// The default is public. |
| 49 | + @_transparent |
| 50 | + @_optimize(none) |
| 51 | + public mutating func appendInterpolation( |
| 52 | + _ number: @autoclosure @escaping () -> Int32, |
| 53 | + format: IntFormat = .decimal, |
| 54 | + privacy: Privacy = .public |
| 55 | + ) { |
| 56 | + appendInteger(number, format: format, privacy: privacy) |
| 57 | + } |
| 58 | + |
| 59 | + /// Given an integer, create and append a format specifier for the integer to the |
| 60 | + /// format string property. Also, append the integer along with necessary headers |
| 61 | + /// to the OSLogArguments property. |
| 62 | + @_transparent |
| 63 | + @_optimize(none) |
| 64 | + @usableFromInline |
| 65 | + internal mutating func appendInteger<T>( |
| 66 | + _ number: @escaping () -> T, |
| 67 | + format: IntFormat, |
| 68 | + privacy: Privacy |
| 69 | + ) where T: FixedWidthInteger { |
| 70 | + guard argumentCount < maxOSLogArgumentCount else { return } |
| 71 | + |
| 72 | + let isPrivateArgument = isPrivate(privacy) |
| 73 | + formatString += |
| 74 | + getIntegerFormatSpecifier( |
| 75 | + T.self, |
| 76 | + format, |
| 77 | + isPrivateArgument) |
| 78 | + addIntHeaders(isPrivateArgument, sizeForEncoding(T.self)) |
| 79 | + |
| 80 | + arguments.append(number) |
| 81 | + argumentCount += 1 |
| 82 | + } |
| 83 | + |
| 84 | + /// Update preamble and append argument headers based on the parameters of |
| 85 | + /// the interpolation. |
| 86 | + @_transparent |
| 87 | + @_optimize(none) |
| 88 | + @usableFromInline |
| 89 | + internal mutating func addIntHeaders(_ isPrivate: Bool, _ byteCount: Int) { |
| 90 | + // Append argument header. |
| 91 | + let argumentHeader = getArgumentHeader(isPrivate: isPrivate, type: .scalar) |
| 92 | + arguments.append(argumentHeader) |
| 93 | + |
| 94 | + // Append number of bytes needed to serialize the argument. |
| 95 | + arguments.append(UInt8(byteCount)) |
| 96 | + |
| 97 | + // Increment total byte size by the number of bytes needed for this |
| 98 | + // argument, which is the sum of the byte size of the argument and |
| 99 | + // two bytes needed for the headers. |
| 100 | + totalBytesForSerializingArguments += byteCount + 2 |
| 101 | + |
| 102 | + preamble = getUpdatedPreamble(isPrivate: isPrivate) |
| 103 | + } |
| 104 | + |
| 105 | + /// Construct an os_log format specifier from the given parameters. |
| 106 | + /// This function must be constant evaluable and all its arguments |
| 107 | + /// must be known at compile time. |
| 108 | + @inlinable |
| 109 | + @_semantics("oslog.interpolation.getFormatSpecifier") |
| 110 | + @_effects(readonly) |
| 111 | + @_optimize(none) |
| 112 | + internal func getIntegerFormatSpecifier<T>( |
| 113 | + _ integerType: T.Type, |
| 114 | + _ format: IntFormat, |
| 115 | + _ isPrivate: Bool |
| 116 | + ) -> String where T : FixedWidthInteger { |
| 117 | + var formatSpecifier: String = isPrivate ? "%{private}" : "%{public}" |
| 118 | + |
| 119 | + // Add a length modifier to the specifier. |
| 120 | + // TODO: more length modifiers will be added. |
| 121 | + if (integerType.bitWidth == CLongLong.bitWidth) { |
| 122 | + formatSpecifier += "ll" |
| 123 | + } |
| 124 | + |
| 125 | + // TODO: more format specifiers will be added. |
| 126 | + switch (format) { |
| 127 | + case .hex: |
| 128 | + formatSpecifier += "x" |
| 129 | + case .octal: |
| 130 | + formatSpecifier += "o" |
| 131 | + default: |
| 132 | + formatSpecifier += integerType.isSigned ? "d" : "u" |
| 133 | + } |
| 134 | + return formatSpecifier |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +extension OSLogArguments { |
| 139 | + /// Append an (autoclosured) interpolated expression of integer type, passed to |
| 140 | + /// `OSLogMessage.appendInterpolation`, to the array of closures tracked |
| 141 | + /// by this instance. |
| 142 | + @usableFromInline |
| 143 | + internal mutating func append<T>( |
| 144 | + _ value: @escaping () -> T |
| 145 | + ) where T: FixedWidthInteger { |
| 146 | + argumentClosures!.append({ $0.serialize(value()) }) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +/// Return the number of bytes needed for serializing an integer argument as |
| 151 | +/// specified by os_log. This function must be constant evaluable. |
| 152 | +@inlinable |
| 153 | +@_semantics("oslog.integers.sizeForEncoding") |
| 154 | +@_effects(readonly) |
| 155 | +@_optimize(none) |
| 156 | +internal func sizeForEncoding<T>( |
| 157 | + _ type: T.Type |
| 158 | +) -> Int where T : FixedWidthInteger { |
| 159 | + return type.bitWidth &>> logBitsPerByte |
| 160 | +} |
| 161 | + |
| 162 | +extension OSLogByteBufferBuilder { |
| 163 | + /// Serialize an integer at the buffer location pointed to by `position`. |
| 164 | + @usableFromInline |
| 165 | + internal mutating func serialize<T>( |
| 166 | + _ value: T |
| 167 | + ) where T : FixedWidthInteger { |
| 168 | + let byteCount = sizeForEncoding(T.self) |
| 169 | + let dest = UnsafeMutableRawBufferPointer(start: position, count: byteCount) |
| 170 | + withUnsafeBytes(of: value) { dest.copyMemory(from: $0) } |
| 171 | + position += byteCount |
| 172 | + } |
| 173 | +} |
0 commit comments