|
| 1 | +//===----------------- OSLogNSObjectType.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 NSObject into a OSLogMesage. |
| 14 | +// It defines `appendInterpolation` function for NSObject type. It also defines |
| 15 | +// extensions for generating an os_log format string for NSObjects (using the |
| 16 | +// format specifier %@) and for serializing NSObject into the argument buffer |
| 17 | +// passed to os_log ABIs. |
| 18 | +// |
| 19 | +// The `appendInterpolation` function defined in this file accept formatting |
| 20 | +// and privacy options along with the interpolated expression as shown below: |
| 21 | +// |
| 22 | +// "\(x, privacy: .public\)" |
| 23 | +import ObjectiveC |
| 24 | + |
| 25 | +extension OSLogInterpolation { |
| 26 | + |
| 27 | + /// Define interpolation for expressions of type NSObject. |
| 28 | + /// - Parameters: |
| 29 | + /// - argumentObject: the interpolated expression of type NSObject, which is autoclosured. |
| 30 | + /// - privacy: a privacy qualifier which is either private or public. Default is private. |
| 31 | + /// TODO: create a specifier to denote auto-inferred privacy level and make it default. |
| 32 | + @_semantics("constant_evaluable") |
| 33 | + @inlinable |
| 34 | + @_optimize(none) |
| 35 | + public mutating func appendInterpolation( |
| 36 | + _ argumentObject: @autoclosure @escaping () -> NSObject, |
| 37 | + privacy: Privacy = .private |
| 38 | + ) { |
| 39 | + guard argumentCount < maxOSLogArgumentCount else { return } |
| 40 | + |
| 41 | + let isPrivateArgument = isPrivate(privacy) |
| 42 | + formatString += getNSObjectFormatSpecifier(isPrivateArgument) |
| 43 | + addNSObjectHeaders(isPrivateArgument) |
| 44 | + |
| 45 | + arguments.append(argumentObject) |
| 46 | + argumentCount += 1 |
| 47 | + } |
| 48 | + |
| 49 | + /// Update preamble and append argument headers based on the parameters of |
| 50 | + /// the interpolation. |
| 51 | + @_semantics("constant_evaluable") |
| 52 | + @inlinable |
| 53 | + @_optimize(none) |
| 54 | + internal mutating func addNSObjectHeaders(_ isPrivate: Bool) { |
| 55 | + // Append argument header. |
| 56 | + let header = getArgumentHeader(isPrivate: isPrivate, type: .object) |
| 57 | + arguments.append(header) |
| 58 | + |
| 59 | + // Append number of bytes needed to serialize the argument. |
| 60 | + let byteCount = pointerSizeInBytes() |
| 61 | + arguments.append(UInt8(byteCount)) |
| 62 | + |
| 63 | + // Increment total byte size by the number of bytes needed for this |
| 64 | + // argument, which is the sum of the byte size of the argument and |
| 65 | + // two bytes needed for the headers. |
| 66 | + totalBytesForSerializingArguments += byteCount + 2 |
| 67 | + |
| 68 | + preamble = getUpdatedPreamble(isPrivate: isPrivate, isScalar: false) |
| 69 | + } |
| 70 | + |
| 71 | + /// Construct an os_log format specifier from the given parameters. |
| 72 | + /// This function must be constant evaluable and all its arguments |
| 73 | + /// must be known at compile time. |
| 74 | + @inlinable |
| 75 | + @_semantics("constant_evaluable") |
| 76 | + @_effects(readonly) |
| 77 | + @_optimize(none) |
| 78 | + internal func getNSObjectFormatSpecifier(_ isPrivate: Bool) -> String { |
| 79 | + // TODO: create a specifier to denote auto-inferred privacy. |
| 80 | + return isPrivate ? "%{private}@" : "%{public}@" |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +extension OSLogArguments { |
| 85 | + /// Append an (autoclosured) interpolated expression of type NSObject, passed to |
| 86 | + /// `OSLogMessage.appendInterpolation`, to the array of closures tracked |
| 87 | + /// by this instance. |
| 88 | + @_semantics("constant_evaluable") |
| 89 | + @inlinable |
| 90 | + @_optimize(none) |
| 91 | + internal mutating func append(_ value: @escaping () -> NSObject) { |
| 92 | + argumentClosures.append({ (position, _) in |
| 93 | + serialize(value(), at: &position) |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/// Serialize an NSObject pointer at the buffer location pointed by |
| 99 | +/// `bufferPosition`. |
| 100 | +@inlinable |
| 101 | +@_alwaysEmitIntoClient |
| 102 | +@inline(__always) |
| 103 | +internal func serialize( |
| 104 | + _ object: NSObject, |
| 105 | + at bufferPosition: inout ByteBufferPointer |
| 106 | +) { |
| 107 | + let byteCount = pointerSizeInBytes(); |
| 108 | + let dest = |
| 109 | + UnsafeMutableRawBufferPointer(start: bufferPosition, count: byteCount) |
| 110 | + // Get the address of this NSObject as an UnsafeRawPointer. |
| 111 | + let objectAddress = Unmanaged.passUnretained(object).toOpaque() |
| 112 | + // Copy the address into the destination buffer. Note that the input NSObject |
| 113 | + // is an interpolated expression and is guaranteed to be alive until the |
| 114 | + // os_log ABI call is completed by the implementation. Therefore, passing |
| 115 | + // this address to the os_log ABI is safe. |
| 116 | + withUnsafeBytes(of: objectAddress) { dest.copyMemory(from: $0) } |
| 117 | + bufferPosition += byteCount |
| 118 | +} |
0 commit comments