Skip to content

Commit 592bca7

Browse files
committedMar 14, 2019
[oslog] [stdlib-private] Add a prototype of the new os_log swift APIs
that accept string interpolations. The prototypes are added to stdlib/private directory and will be used only in tests and in experimental code.
1 parent 9fae178 commit 592bca7

File tree

6 files changed

+523
-2
lines changed

6 files changed

+523
-2
lines changed
 

‎apinotes/os.apinotes

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ Enumerators:
3030
SwiftPrivate: true
3131
Functions:
3232
- Name: _os_log_impl
33-
Availability: nonswift
34-
AvailabilityMsg: 'Use os_log'
33+
SwiftPrivate: true
34+
NullabilityOfRet: O
3535
- Name: _os_log_error_impl
3636
Availability: nonswift
3737
AvailabilityMsg: 'Use os_log'

‎stdlib/private/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ if(SWIFT_BUILD_SDK_OVERLAY)
1616
# the correct order for Windows.
1717
add_subdirectory(StdlibUnittest)
1818

19+
add_subdirectory(OSLog)
20+
1921
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
2022
add_subdirectory(StdlibUnittestFoundationExtras)
2123
if (SWIFT_INCLUDE_TESTS)

‎stdlib/private/OSLog/CMakeLists.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_swift_target_library(swiftOSLogPrototype
2+
IS_SDK_OVERLAY
3+
SHARED
4+
TARGET_LIBRARY
5+
6+
OSLog.swift
7+
OSLogMessage.swift
8+
9+
SWIFT_MODULE_DEPENDS_IOS Darwin os
10+
SWIFT_MODULE_DEPENDS_OSX Darwin os
11+
SWIFT_MODULE_DEPENDS_TVOS Darwin os
12+
SWIFT_MODULE_DEPENDS_WATCHOS Darwin os
13+
TARGET_SDKS ALL_APPLE_PLATFORMS
14+
INSTALL_IN_COMPONENT never_install)

‎stdlib/private/OSLog/OSLog.swift

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)