Skip to content

[stdlib-private][os log] Add new log methods for each specific logging level. #28713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions stdlib/private/OSLog/OSLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@ public struct Logger {
@usableFromInline
internal let logObject: OSLog

/// Create a custom OS log object.
/// Create a custom OSLog object for logging.
public init(subsystem: String, category: String) {
logObject = OSLog(subsystem: subsystem, category: category)
}

/// Return the default OS log object.
/// Use the default OSLog object for logging.
public init() {
logObject = OSLog.default
}

/// Create a Logger instance from an existing OSLog Object.
public init(_ logObj: OSLog) {
logObject = logObj
}

// Functions defined below are marked @_optimize(none) to prevent inlining
// of string internals (such as String._StringGuts) which will interfere with
// constant evaluation and folding. Note that these functions will be inlined,
Expand All @@ -44,8 +49,57 @@ public struct Logger {
osLog(log: logObject, level: level, message)
}

// TODO: define overloads for logging at specific levels: debug, info, notice,
// error, fault based on the Swift forum "logging-levels" discussion.
// The following overloads are for logging at specific levels. The levels that
// are supported are debug (also called trace), info, notice (also called
// default), error (also called warning), fault (also called critical).

@_transparent
@_optimize(none)
public func trace(_ message: OSLogMessage) {
osLog(log: logObject, level: .debug, message)
}

@_transparent
@_optimize(none)
public func debug(_ message: OSLogMessage) {
osLog(log: logObject, level: .debug, message)
}

@_transparent
@_optimize(none)
public func info(_ message: OSLogMessage) {
osLog(log: logObject, level: .info, message)
}

@_transparent
@_optimize(none)
public func notice(_ message: OSLogMessage) {
osLog(log: logObject, level: .default, message)
}

@_transparent
@_optimize(none)
public func warning(_ message: OSLogMessage) {
osLog(log: logObject, level: .error, message)
}

@_transparent
@_optimize(none)
public func error(_ message: OSLogMessage) {
osLog(log: logObject, level: .error, message)
}

@_transparent
@_optimize(none)
public func critical(_ message: OSLogMessage) {
osLog(log: logObject, level: .fault, message)
}

@_transparent
@_optimize(none)
public func fault(_ message: OSLogMessage) {
osLog(log: logObject, level: .fault, message)
}
}

/// Given an instance of the custom string interpolation type: `OSLogMessage`,
Expand Down
15 changes: 5 additions & 10 deletions test/stdlib/OSLogPrototypeExecTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,18 @@ if #available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
h.log("A message with no data")

// Test logging at specific levels.
h.log(level: .debug, "Minimum integer value: \(Int.min, format: .hex)")
h.log(level: .info, "Maximum integer value: \(Int.max, format: .hex)")
h.debug("Minimum integer value: \(Int.min, format: .hex)")
h.info("Maximum integer value: \(Int.max, format: .hex)")

let privateID = 0x79abcdef
h.log(
level: .error,
"Private Identifier: \(privateID, format: .hex, privacy: .private)")
h.error("Private Identifier: \(privateID, format: .hex, privacy: .private)")
let addr = 0x7afebabe
h.log(
level: .fault,
"Invalid address: 0x\(addr, format: .hex, privacy: .public)")
h.fault("Invalid address: 0x\(addr, format: .hex, privacy: .public)")

// Test logging with multiple arguments.
let filePermissions = 0o777
let pid = 122225
h.log(
level: .error,
h.error(
"""
Access prevented: process \(pid) initiated by \
user: \(privateID, privacy: .private) attempted resetting \
Expand Down