Swift 4 support for a 0.x release

[contains work from @ianpartridge and @weissi]

Motivation:

For an easier migration path, it is probably useful to ship a `0.x`
version of `swift-log` that is compatible with Swift 4. It can't use
any of the features that Swift 5 brings and therefore the Swift 4
version will have a slightly reduced feature set:

- log messages will be `String`
- logging metadata values will be `String` only (no dictionaries,
  arrays, ...)

That means, if you use the Swift 4 version of swift-log carefully
(only use string literals), your migration to the Swift 5 version should
only be a matter of changing your `Package.swift`.

Modifications:

- remove all string interpolation and use `String` directly

Result:

- we can release a 0.x version that supports Swift 4

Support Swift 4.1 (#40)

* Support Swift 4.1

* Set swift-tools-version to 4.0
This commit is contained in:
Johannes Weiss committed 2019-04-12 15:24:21 +01:00
1 parent a4828d9184
commit d1af77eebd
9 files changed
+133 -126

No files matched your search

+8 -6
View File
@@ -49,7 +49,11 @@ internal final class Lock {
deinit {
let err = pthread_mutex_destroy(self.mutex)
precondition(err == 0)
#if swift(>=4.0) && !swift(>=4.0.50)
self.mutex.deallocate(capacity: 1)
#else
self.mutex.deallocate()
#endif
}
/// Acquire the lock.
@@ -80,7 +84,6 @@ extension Lock {
///
/// - Parameter body: The block to execute while holding the lock.
/// - Returns: The value returned by the block.
@inlinable
internal func withLock<T>(_ body: () throws -> T) rethrows -> T {
self.lock()
defer {
@@ -90,7 +93,6 @@ extension Lock {
}
// specialise Void return (for performance)
@inlinable
internal func withLockVoid(_ body: () throws -> Void) rethrows {
try self.withLock(body)
}
@@ -113,7 +115,11 @@ internal final class ReadWriteLock {
deinit {
let err = pthread_rwlock_destroy(self.rwlock)
precondition(err == 0)
#if swift(>=4.0) && !swift(>=4.0.50)
self.rwlock.deallocate(capacity: 1)
#else
self.rwlock.deallocate()
#endif
}
/// Acquire a reader lock.
@@ -153,7 +159,6 @@ extension ReadWriteLock {
///
/// - Parameter body: The block to execute while holding the lock.
/// - Returns: The value returned by the block.
@inlinable
internal func withReaderLock<T>(_ body: () throws -> T) rethrows -> T {
self.lockRead()
defer {
@@ -170,7 +175,6 @@ extension ReadWriteLock {
///
/// - Parameter body: The block to execute while holding the lock.
/// - Returns: The value returned by the block.
@inlinable
internal func withWriterLock<T>(_ body: () throws -> T) rethrows -> T {
self.lockWrite()
defer {
@@ -180,13 +184,11 @@ extension ReadWriteLock {
}
// specialise Void return (for performance)
@inlinable
internal func withReaderLockVoid(_ body: () throws -> Void) rethrows {
try self.withReaderLock(body)
}
// specialise Void return (for performance)
@inlinable
internal func withWriterLockVoid(_ body: () throws -> Void) rethrows {
try self.withWriterLock(body)
}
+83 -33
View File
@@ -26,7 +26,6 @@
/// logger.info("Hello World!")
///
public struct Logger {
@usableFromInline
var handler: LogHandler
public let label: String
@@ -52,7 +51,6 @@ extension Logger {
/// it defaults to `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func log(level: Logger.Level,
_ message: @autoclosure () -> Logger.Message,
metadata: @autoclosure () -> Logger.Metadata? = nil,
@@ -65,11 +63,21 @@ extension Logger {
}
}
public func log(level: Logger.Level,
_ message: @autoclosure () -> String,
metadata: @autoclosure () -> [String: String]? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: level,
Logger.Message(stringLiteral: message()),
metadata: metadata().map { $0.mapValues { Logger.MetadataValue.string($0) } },
file: file, function: function, line: line)
}
/// Add, change, or remove a logging metadata item.
///
/// - note: Logging metadata behaves as a value that means a change to the logging metadata will only affect the
/// very `Logger` it was changed on.
@inlinable
public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
get {
return self.handler[metadataKey: metadataKey]
@@ -85,7 +93,6 @@ extension Logger {
/// very `Logger`. It it acceptable for logging backends to have some form of global log level override
/// that affects multiple or even all loggers. This means a change in `logLevel` to one `Logger` might in
/// certain cases have no effect.
@inlinable
public var logLevel: Logger.Level {
get {
return self.handler.logLevel
@@ -112,13 +119,20 @@ extension Logger {
/// it defaults to `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func trace(_ message: @autoclosure () -> Logger.Message,
metadata: @autoclosure () -> Logger.Metadata? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .trace, message(), metadata: metadata(), file: file, function: function, line: line)
}
public func trace(_ message: @autoclosure () -> String,
metadata: @autoclosure () -> [String: String]? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.trace(Logger.Message(stringLiteral: message()),
metadata: metadata().map { $0.mapValues { Logger.MetadataValue.string($0) } },
file: file, function: function, line: line)
}
/// Log a message passing with the `Logger.info` log level.
///
/// If `.debug` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
@@ -134,13 +148,20 @@ extension Logger {
/// it defaults to `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func debug(_ message: @autoclosure () -> Logger.Message,
metadata: @autoclosure () -> Logger.Metadata? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .debug, message(), metadata: metadata(), file: file, function: function, line: line)
}
public func debug(_ message: @autoclosure () -> String,
metadata: @autoclosure () -> [String: String]? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.debug(Logger.Message(stringLiteral: message()),
metadata: metadata().map { $0.mapValues { Logger.MetadataValue.string($0) } },
file: file, function: function, line: line)
}
/// Log a message passing with the `Logger.Level.info` log level.
///
/// If `.info` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
@@ -156,13 +177,20 @@ extension Logger {
/// it defaults to `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func info(_ message: @autoclosure () -> Logger.Message,
metadata: @autoclosure () -> Logger.Metadata? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .info, message(), metadata: metadata(), file: file, function: function, line: line)
}
public func info(_ message: @autoclosure () -> String,
metadata: @autoclosure () -> [String: String]? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.info(Logger.Message(stringLiteral: message()),
metadata: metadata().map { $0.mapValues { Logger.MetadataValue.string($0) } },
file: file, function: function, line: line)
}
/// Log a message passing with the `Logger.Level.notice` log level.
///
/// If `.notice` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
@@ -178,13 +206,20 @@ extension Logger {
/// it defaults to `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func notice(_ message: @autoclosure () -> Logger.Message,
metadata: @autoclosure () -> Logger.Metadata? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .notice, message(), metadata: metadata(), file: file, function: function, line: line)
}
public func notice(_ message: @autoclosure () -> String,
metadata: @autoclosure () -> [String: String]? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.notice(Logger.Message(stringLiteral: message()),
metadata: metadata().map { $0.mapValues { Logger.MetadataValue.string($0) } },
file: file, function: function, line: line)
}
/// Log a message passing with the `Logger.Level.warning` log level.
///
/// If `.warning` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
@@ -200,13 +235,20 @@ extension Logger {
/// it defaults to `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func warning(_ message: @autoclosure () -> Logger.Message,
metadata: @autoclosure () -> Logger.Metadata? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .warning, message(), metadata: metadata(), file: file, function: function, line: line)
}
public func warning(_ message: @autoclosure () -> String,
metadata: @autoclosure () -> [String: String]? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.warning(Logger.Message(stringLiteral: message()),
metadata: metadata().map { $0.mapValues { Logger.MetadataValue.string($0) } },
file: file, function: function, line: line)
}
/// Log a message passing with the `Logger.Level.error` log level.
///
/// If `.error` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
@@ -222,13 +264,20 @@ extension Logger {
/// it defaults to `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func error(_ message: @autoclosure () -> Logger.Message,
metadata: @autoclosure () -> Logger.Metadata? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .error, message(), metadata: metadata(), file: file, function: function, line: line)
}
public func error(_ message: @autoclosure () -> String,
metadata: @autoclosure () -> [String: String]? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.error(Logger.Message(stringLiteral: message()),
metadata: metadata().map { $0.mapValues { Logger.MetadataValue.string($0) } },
file: file, function: function, line: line)
}
/// Log a message passing with the `Logger.Level.critical` log level.
///
/// `.critical` messages will always be logged.
@@ -243,12 +292,20 @@ extension Logger {
/// it defaults to `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func critical(_ message: @autoclosure () -> Logger.Message,
metadata: @autoclosure () -> Logger.Metadata? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .critical, message(), metadata: metadata(), file: file, function: function, line: line)
}
public func critical(_ message: @autoclosure () -> String,
metadata: @autoclosure () -> [String: String]? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
self.critical(Logger.Message(stringLiteral: message()),
metadata: metadata().map { $0.mapValues { Logger.MetadataValue.string($0) } },
file: file, function: function, line: line)
}
}
/// The `LoggingSystem` is a global facility where the default logging backend implementation (`LogHandler`) can be
@@ -304,7 +361,7 @@ extension Logger {
///
/// Raw values of log levels correspond to their severity, and are ordered by lowest numeric value (0) being
/// the most severe. The raw values match the syslog values.
public enum Level: CaseIterable {
public enum Level {
/// Appropriate for messages that contain information only when debugging a program.
case trace
@@ -422,10 +479,7 @@ extension Logger {
///
/// logger.info("Hello \(world)")
///
public struct Message: ExpressibleByStringLiteral,
Equatable,
CustomStringConvertible,
ExpressibleByStringInterpolation {
public struct Message: CustomStringConvertible {
public typealias StringLiteralType = String
private var value: String
@@ -440,6 +494,17 @@ extension Logger {
}
}
#if swift(>=4.0) && !swift(>=4.0.50)
extension Logger.Message: Equatable {
public static func == (lhs: Logger.Message, rhs: Logger.Message) -> Bool {
return lhs.value == rhs.value
}
}
#else
extension Logger.Message: Equatable {}
#endif
/// A pseudo-`LogHandler` that can be used to send messages to multiple other `LogHandler`s.
///
/// The first `LogHandler` passed to the initialisation function of `MultiplexLogHandler` control the `logLevel` as
@@ -564,23 +629,13 @@ internal struct StdoutLogHandler: LogHandler {
let localTime = localtime(&timestamp)
strftime(&buffer, buffer.count, "%Y-%m-%dT%H:%M:%S%z", localTime)
return buffer.withUnsafeBufferPointer {
$0.withMemoryRebound(to: CChar.self) {
String(cString: $0.baseAddress!)
$0.baseAddress!.withMemoryRebound(to: CChar.self, capacity: $0.count) { ptr in
String(cString: ptr)
}
}
}
}
// Extension has to be done on explicit type rather than Logger.Metadata.Value as workaround for
// https://bugs.swift.org/browse/SR-9687
extension Logger.MetadataValue: ExpressibleByStringLiteral {
public typealias StringLiteralType = String
public init(stringLiteral value: String) {
self = .string(value)
}
}
extension Logger.MetadataValue: CustomStringConvertible {
public var description: String {
switch self {
@@ -596,11 +651,6 @@ extension Logger.MetadataValue: CustomStringConvertible {
}
}
// Extension has to be done on explicit type rather than Logger.Metadata.Value as workaround for
// https://bugs.swift.org/browse/SR-9687
extension Logger.MetadataValue: ExpressibleByStringInterpolation {
}
// Extension has to be done on explicit type rather than Logger.Metadata.Value as workaround for
// https://bugs.swift.org/browse/SR-9687
extension Logger.MetadataValue: ExpressibleByDictionaryLiteral {