Skip to content

Commit

Permalink
Add native support for logging metadata and streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
EricRabil committed Dec 21, 2021
1 parent 92a9302 commit aff447c
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 41 deletions.
39 changes: 29 additions & 10 deletions Sources/Swog/CoreLogging+LoggingLevelWrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ public enum LoggingLevel: UInt8, Codable {
case error = 16
case fault = 17
case debug = 2

public enum Name: String, Codable {
case info, warn, error, fault, debug
}

public var name: Name {
switch self {
case .info: return .info
case .warn: return .warn
case .error: return .error
case .fault: return .fault
case .debug: return .debug
}
}
}

// MARK: - Old API
Expand Down Expand Up @@ -85,9 +99,10 @@ public func CLInfo(
line: Int = #line,
function: StaticString = #function,
dso: UnsafeRawPointer = #dsohandle,
_ message: BackportedOSLogMessage
_ message: BackportedOSLogMessage,
metadata: MetadataValue = nil
) {
CLLog(level: .info, fileID: fileID, line: line, function: function, dso: dso, category, message)
CLLog(level: .info, fileID: fileID, line: line, function: function, dso: dso, category, message, metadata: metadata)
}

@_transparent
Expand All @@ -98,9 +113,10 @@ public func CLWarn(
line: Int = #line,
function: StaticString = #function,
dso: UnsafeRawPointer = #dsohandle,
_ message: BackportedOSLogMessage
_ message: BackportedOSLogMessage,
metadata: MetadataValue = nil
) {
CLLog(level: .warn, fileID: fileID, line: line, function: function, dso: dso, category, message)
CLLog(level: .warn, fileID: fileID, line: line, function: function, dso: dso, category, message, metadata: metadata)
}

@_transparent
Expand All @@ -111,9 +127,10 @@ public func CLError(
line: Int = #line,
function: StaticString = #function,
dso: UnsafeRawPointer = #dsohandle,
_ message: BackportedOSLogMessage
_ message: BackportedOSLogMessage,
metadata: MetadataValue = nil
) {
CLLog(level: .error, fileID: fileID, line: line, function: function, dso: dso, category, message)
CLLog(level: .error, fileID: fileID, line: line, function: function, dso: dso, category, message, metadata: metadata)
}

@_transparent
Expand All @@ -124,9 +141,10 @@ public func CLFault(
line: Int = #line,
function: StaticString = #function,
dso: UnsafeRawPointer = #dsohandle,
_ message: BackportedOSLogMessage
_ message: BackportedOSLogMessage,
metadata: MetadataValue = nil
) {
CLLog(level: .fault, fileID: fileID, line: line, function: function, dso: dso, category, message)
CLLog(level: .fault, fileID: fileID, line: line, function: function, dso: dso, category, message, metadata: metadata)
}

@_transparent
Expand All @@ -137,7 +155,8 @@ public func CLDebug(
line: Int = #line,
function: StaticString = #function,
dso: UnsafeRawPointer = #dsohandle,
_ message: BackportedOSLogMessage
_ message: BackportedOSLogMessage,
metadata: MetadataValue = nil
) {
CLLog(level: .debug, fileID: fileID, line: line, function: function, dso: dso, category, message)
CLLog(level: .debug, fileID: fileID, line: line, function: function, dso: dso, category, message, metadata: metadata)
}
5 changes: 3 additions & 2 deletions Sources/Swog/CoreLogging+LoggingLevelWrappers.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public func CL${level}(
line: Int = #line,
function: StaticString = #function,
dso: UnsafeRawPointer = #dsohandle,
_ message: BackportedOSLogMessage
_ message: BackportedOSLogMessage,
metadata: MetadataValue = nil
) {
CLLog(level: .${level.lower()}, fileID: fileID, line: line, function: function, dso: dso, category, message)
CLLog(level: .${level.lower()}, fileID: fileID, line: line, function: function, dso: dso, category, message, metadata: metadata)
}
%end
4 changes: 2 additions & 2 deletions Sources/Swog/CoreLogging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public func CLLog(
// MARK: - New API
@_transparent
@_optimize(speed)
public func CLLog(level: LoggingLevel, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle, _ category: StaticString, _ message: BackportedOSLogMessage) {
public func CLLog(level: LoggingLevel, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle, _ category: StaticString, _ message: BackportedOSLogMessage, metadata: MetadataValue = nil) {
for driver in LoggingDrivers {
driver.log(level: level, fileID: fileID, line: line, function: function, dso: dso, category: category, message: message)
driver.log(level: level, fileID: fileID, line: line, function: function, dso: dso, category: category, message: message, metadata: metadata)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Swog/Drivers/Console/ConsoleDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ConsoleDriver: LoggingDriver {
}

@_optimize(speed)
public func log(level: LoggingLevel, fileID: StaticString, line: Int, function: StaticString, dso: UnsafeRawPointer, category: StaticString, message: BackportedOSLogMessage) {
public func log(level: LoggingLevel, fileID: StaticString, line: Int, function: StaticString, dso: UnsafeRawPointer, category: StaticString, message: BackportedOSLogMessage, metadata: MetadataValue) {
_log(level: level, category: String(category), message: message.render(level: privacyLevel))
}

Expand Down
7 changes: 1 addition & 6 deletions Sources/Swog/Drivers/Console/LoggingLevel+Rendering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@
import Foundation

internal extension LoggingLevel {
@_transparent
var name: String {
"\(self)"
}

@_transparent
var printText: String {
"[\(name.uppercased().padding(toLength: 6, withPad: " ", startingAt: 0))]"
"[\(name.rawValue.uppercased().padding(toLength: 6, withPad: " ", startingAt: 0))]"
}

@_transparent
Expand Down
60 changes: 58 additions & 2 deletions Sources/Swog/Drivers/LoggingDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,63 @@

import Foundation

public protocol LoggingDriver {
public struct LoggingPayload: Codable, Hashable, Equatable {
public var level: LoggingLevel.Name
public var fileID: String
public var line: Int
public var function: String
public var category: String
public var message: String
public var metadata: MetadataValue

public init(level: LoggingLevel, fileID: StaticString, line: Int, function: StaticString, dso: UnsafeRawPointer, category: StaticString, message: StaticString, args: [CVarArg]) {
self.level = level.name
self.fileID = String(fileID)
self.line = line
self.function = String(function)
self.category = String(category)
self.message = String(format: String(message), arguments: args)
self.metadata = .nil
}

public init(level: LoggingLevel, fileID: StaticString, line: Int, function: StaticString, dso: UnsafeRawPointer, category: StaticString, message: BackportedOSLogMessage, metadata: MetadataValue) {
self.level = level.name
self.fileID = String(fileID)
self.line = line
self.function = String(function)
self.category = String(category)
self.message = message.render(level: .auto)
self.metadata = metadata
}

public func hash(into hasher: inout Hasher) {
level.hash(into: &hasher)
fileID.hash(into: &hasher)
line.hash(into: &hasher)
function.hash(into: &hasher)
category.hash(into: &hasher)
message.hash(into: &hasher)
metadata.hash(into: &hasher)
}
}

public protocol LoggingDriver: AnyObject {
func log(level: LoggingLevel, fileID: StaticString, line: Int, function: StaticString, dso: UnsafeRawPointer, category: StaticString, message: StaticString, args: [CVarArg])
func log(level: LoggingLevel, fileID: StaticString, line: Int, function: StaticString, dso: UnsafeRawPointer, category: StaticString, message: BackportedOSLogMessage)
func log(level: LoggingLevel, fileID: StaticString, line: Int, function: StaticString, dso: UnsafeRawPointer, category: StaticString, message: BackportedOSLogMessage, metadata: MetadataValue)
}

open class LoggingStream: LoggingDriver {
open var callback: (LoggingPayload) -> ()

public init(_ callback: @escaping (LoggingPayload) -> ()) {
self.callback = callback
}

open func log(level: LoggingLevel, fileID: StaticString, line: Int, function: StaticString, dso: UnsafeRawPointer, category: StaticString, message: StaticString, args: [CVarArg]) {
callback(LoggingPayload(level: level, fileID: fileID, line: line, function: function, dso: dso, category: category, message: message, args: args))
}

open func log(level: LoggingLevel, fileID: StaticString, line: Int, function: StaticString, dso: UnsafeRawPointer, category: StaticString, message: BackportedOSLogMessage, metadata: MetadataValue) {
callback(LoggingPayload(level: level, fileID: fileID, line: line, function: function, dso: dso, category: category, message: message, metadata: metadata))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ extension BackportedOSLogArguments {
@usableFromInline
internal func sizeForEncoding<T>(
_ type: T.Type
) -> Int where T : FixedWidthInteger {
) -> Int where T : FixedWidthInteger {
return type.bitWidth &>> logBitsPerByte
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Swog/Drivers/OSLog/OSLogDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class OSLogDriver: LoggingDriver {
Sends a logging message constructed from a customized interpolation implementation
*/
@_optimize(speed)
public func log(level: LoggingLevel, fileID: StaticString, line: Int, function: StaticString, dso: UnsafeRawPointer, category: StaticString, message: BackportedOSLogMessage) {
public func log(level: LoggingLevel, fileID: StaticString, line: Int, function: StaticString, dso: UnsafeRawPointer, category: StaticString, message: BackportedOSLogMessage, metadata: MetadataValue) {
os_log_send(dso, log(forCategory: category, fileID: fileID), OSLogType(rawValue: level.rawValue), message)
}
}
Expand Down
24 changes: 12 additions & 12 deletions Sources/Swog/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,37 @@ public class Logger {
public extension Logger {
@_transparent
@_optimize(speed)
func info(_ message: BackportedOSLogMessage, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: .info, fileID: fileID, line: line, function: function, dso: dso, category, message)
func info(_ message: BackportedOSLogMessage, metadata: MetadataValue = nil, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: .info, fileID: fileID, line: line, function: function, dso: dso, category, message, metadata: metadata)
}

@_transparent
@_optimize(speed)
func warn(_ message: BackportedOSLogMessage, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: .warn, fileID: fileID, line: line, function: function, dso: dso, category, message)
func warn(_ message: BackportedOSLogMessage, metadata: MetadataValue = nil, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: .warn, fileID: fileID, line: line, function: function, dso: dso, category, message, metadata: metadata)
}

@_transparent
@_optimize(speed)
func error(_ message: BackportedOSLogMessage, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: .error, fileID: fileID, line: line, function: function, dso: dso, category, message)
func error(_ message: BackportedOSLogMessage, metadata: MetadataValue = nil, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: .error, fileID: fileID, line: line, function: function, dso: dso, category, message, metadata: metadata)
}

@_transparent
@_optimize(speed)
func fault(_ message: BackportedOSLogMessage, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: .fault, fileID: fileID, line: line, function: function, dso: dso, category, message)
func fault(_ message: BackportedOSLogMessage, metadata: MetadataValue = nil, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: .fault, fileID: fileID, line: line, function: function, dso: dso, category, message, metadata: metadata)
}

@_transparent
@_optimize(speed)
func debug(_ message: BackportedOSLogMessage, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: .debug, fileID: fileID, line: line, function: function, dso: dso, category, message)
func debug(_ message: BackportedOSLogMessage, metadata: MetadataValue = nil, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: .debug, fileID: fileID, line: line, function: function, dso: dso, category, message, metadata: metadata)
}

@_transparent
@_optimize(speed)
func callAsFunction(_ message: BackportedOSLogMessage, level: LoggingLevel = .info, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: level, fileID: fileID, line: line, function: function, dso: dso, category, message)
func callAsFunction(_ message: BackportedOSLogMessage, metadata: MetadataValue = nil, level: LoggingLevel = .info, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: level, fileID: fileID, line: line, function: function, dso: dso, category, message, metadata: metadata)
}
}
8 changes: 4 additions & 4 deletions Sources/Swog/Logger.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public extension Logger {
%for level in levels:
@_transparent
@_optimize(speed)
func ${level}(_ message: BackportedOSLogMessage, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: .${level}, fileID: fileID, line: line, function: function, dso: dso, category, message)
func ${level}(_ message: BackportedOSLogMessage, metadata: MetadataValue = nil, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: .${level}, fileID: fileID, line: line, function: function, dso: dso, category, message, metadata: metadata)
}

%end
@_transparent
@_optimize(speed)
func callAsFunction(_ message: BackportedOSLogMessage, level: LoggingLevel = .info, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: level, fileID: fileID, line: line, function: function, dso: dso, category, message)
func callAsFunction(_ message: BackportedOSLogMessage, metadata: MetadataValue = nil, level: LoggingLevel = .info, fileID: StaticString = #fileID, line: Int = #line, function: StaticString = #function, dso: UnsafeRawPointer = #dsohandle) {
CLLog(level: level, fileID: fileID, line: line, function: function, dso: dso, category, message, metadata: metadata)
}
}
Loading

0 comments on commit aff447c

Please sign in to comment.