|
| 1 | +//===----------------- OSLog.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 contains the new swift APIs for OS log that accept string |
| 14 | +// interpolations. This is a prototype meant for experimentation and testing. |
| 15 | +// Do not use it outside of tests. |
| 16 | + |
| 17 | +@_exported import os |
| 18 | + |
| 19 | +@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) |
| 20 | +public struct Logger { |
| 21 | + internal let logObject: OSLog |
| 22 | + |
| 23 | + /// Create a custom OS log object. |
| 24 | + public init(subsystem: String, category: String) { |
| 25 | + logObject = OSLog(subsystem: subsystem, category: category) |
| 26 | + } |
| 27 | + |
| 28 | + /// Return the default OS log object. |
| 29 | + public init() { |
| 30 | + logObject = OSLog.default |
| 31 | + } |
| 32 | + |
| 33 | + /// Log a string interpolation at a given level. The level is `default` if |
| 34 | + /// it is not specified. |
| 35 | + public func log(level: OSLogType = .default, _ message: OSLogMessage) { |
| 36 | + osLog(logObject, level, message) |
| 37 | + } |
| 38 | + |
| 39 | + // TODO: define overloads for logging at specific levels: debug, info, notice, |
| 40 | + // error, fault based on the Swift forum "logging-levels" discussion. |
| 41 | +} |
| 42 | + |
| 43 | +/// Given an instance of the custom string interpolation type: `OSLogMessage`, |
| 44 | +/// extract the format string, serialize the arguments to a byte buffer, |
| 45 | +/// and pass them to the OS logging system. |
| 46 | +@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) |
| 47 | +internal func osLog( |
| 48 | + _ logObject: OSLog, |
| 49 | + _ logLevel: OSLogType, |
| 50 | + _ message: OSLogMessage |
| 51 | +) { |
| 52 | + guard logObject.isEnabled(type: logLevel) else { return } |
| 53 | + |
| 54 | + let bufferSize = message.bufferSize |
| 55 | + let bufferMemory = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize) |
| 56 | + var builder = OSLogByteBufferBuilder(bufferMemory) |
| 57 | + |
| 58 | + message.serializeArguments(into: &builder) |
| 59 | + |
| 60 | + message.formatString.withCString { cFormatString in |
| 61 | + ___os_log_impl(UnsafeMutableRawPointer(mutating: #dsohandle), |
| 62 | + logObject, |
| 63 | + logLevel, |
| 64 | + cFormatString, |
| 65 | + bufferMemory, |
| 66 | + UInt32(bufferSize)) |
| 67 | + } |
| 68 | + bufferMemory.deallocate() |
| 69 | +} |
0 commit comments