mirror of
https://github.com/apple/swift-log
synced 2026-11-30 04:18:44 +00:00
Compare commits
12
Commits
tomerd-patch-3
...
1.4.4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6fe203dc33 | ||
|
|
6b0ccb1ca8 | ||
|
|
8fc79cae9d | ||
|
|
f2e8667d50 | ||
|
|
9f3731aa55 | ||
|
|
2bf6215b68 | ||
|
|
432a93204e | ||
|
|
dc6dd5caaa | ||
|
|
5db2bc495a | ||
|
|
4e75f88c6a | ||
|
|
77192b9e21 | ||
|
|
b172d1ae3d |
No files matched your search
@@ -0,0 +1,33 @@
|
|||||||
|
// swift-tools-version:5.6
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// This source file is part of the Swift Logging API open source project
|
||||||
|
//
|
||||||
|
// Copyright (c) 2018-2019 Apple Inc. and the Swift Logging API project authors
|
||||||
|
// Licensed under Apache License v2.0
|
||||||
|
//
|
||||||
|
// See LICENSE.txt for license information
|
||||||
|
// See CONTRIBUTORS.txt for the list of Swift Logging API project authors
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
import PackageDescription
|
||||||
|
|
||||||
|
let package = Package(
|
||||||
|
name: "swift-log",
|
||||||
|
products: [
|
||||||
|
.library(name: "Logging", targets: ["Logging"]),
|
||||||
|
],
|
||||||
|
targets: [
|
||||||
|
.target(
|
||||||
|
name: "Logging",
|
||||||
|
dependencies: []
|
||||||
|
),
|
||||||
|
.testTarget(
|
||||||
|
name: "LoggingTests",
|
||||||
|
dependencies: ["Logging"]
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
@@ -84,6 +84,7 @@ As the API has just launched, not many implementations exist yet. If you are int
|
|||||||
| [luoxiu/LogDog](https://github.com/luoxiu/LogDog) | user-friendly logging with sinks and appenders |
|
| [luoxiu/LogDog](https://github.com/luoxiu/LogDog) | user-friendly logging with sinks and appenders |
|
||||||
| [ShivaHuang/swift-log-SwiftyBeaver](https://github.com/ShivaHuang/swift-log-SwiftyBeaver) | a logging backend for printing colored logging to Xcode console / file, or sending encrypted logging to [SwiftyBeaver](https://swiftybeaver.com) platform. |
|
| [ShivaHuang/swift-log-SwiftyBeaver](https://github.com/ShivaHuang/swift-log-SwiftyBeaver) | a logging backend for printing colored logging to Xcode console / file, or sending encrypted logging to [SwiftyBeaver](https://swiftybeaver.com) platform. |
|
||||||
| [Apodini/swift-log-elk](https://github.com/Apodini/swift-log-elk) | a logging backend that formats, caches and sends log data to [elastic/logstash](https://github.com/elastic/logstash) |
|
| [Apodini/swift-log-elk](https://github.com/Apodini/swift-log-elk) | a logging backend that formats, caches and sends log data to [elastic/logstash](https://github.com/elastic/logstash) |
|
||||||
|
| [binaryscraping/swift-log-supabase](https://github.com/binaryscraping/swift-log-supabase) | a logging backend that sends log entries to [Supabase](https://github.com/supabase/supbase). |
|
||||||
| Your library? | [Get in touch!](https://forums.swift.org/c/server) |
|
| Your library? | [Get in touch!](https://forums.swift.org/c/server) |
|
||||||
|
|
||||||
## What is an API package?
|
## What is an API package?
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
# ``Logging/Logger/Level``
|
||||||
|
|
||||||
|
## Topics
|
||||||
|
|
||||||
|
### Log levels
|
||||||
|
|
||||||
|
- ``trace``
|
||||||
|
- ``debug``
|
||||||
|
- ``info``
|
||||||
|
- ``notice``
|
||||||
|
- ``warning``
|
||||||
|
- ``error``
|
||||||
|
- ``critical``
|
||||||
+19
-10
@@ -28,9 +28,7 @@
|
|||||||
|
|
||||||
#if canImport(WASILibc)
|
#if canImport(WASILibc)
|
||||||
// No locking on WASILibc
|
// No locking on WASILibc
|
||||||
#else
|
#elseif canImport(Darwin)
|
||||||
|
|
||||||
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
|
|
||||||
import Darwin
|
import Darwin
|
||||||
#elseif os(Windows)
|
#elseif os(Windows)
|
||||||
import WinSDK
|
import WinSDK
|
||||||
@@ -138,7 +136,9 @@ extension Lock {
|
|||||||
/// one used by NIO. On Windows, the lock is based on the substantially similar
|
/// one used by NIO. On Windows, the lock is based on the substantially similar
|
||||||
/// `SRWLOCK` type.
|
/// `SRWLOCK` type.
|
||||||
internal final class ReadWriteLock {
|
internal final class ReadWriteLock {
|
||||||
#if os(Windows)
|
#if canImport(WASILibc)
|
||||||
|
// WASILibc is single threaded, provides no locks
|
||||||
|
#elseif os(Windows)
|
||||||
fileprivate let rwlock: UnsafeMutablePointer<SRWLOCK> =
|
fileprivate let rwlock: UnsafeMutablePointer<SRWLOCK> =
|
||||||
UnsafeMutablePointer.allocate(capacity: 1)
|
UnsafeMutablePointer.allocate(capacity: 1)
|
||||||
fileprivate var shared: Bool = true
|
fileprivate var shared: Bool = true
|
||||||
@@ -149,7 +149,9 @@ internal final class ReadWriteLock {
|
|||||||
|
|
||||||
/// Create a new lock.
|
/// Create a new lock.
|
||||||
public init() {
|
public init() {
|
||||||
#if os(Windows)
|
#if canImport(WASILibc)
|
||||||
|
// WASILibc is single threaded, provides no locks
|
||||||
|
#elseif os(Windows)
|
||||||
InitializeSRWLock(self.rwlock)
|
InitializeSRWLock(self.rwlock)
|
||||||
#else
|
#else
|
||||||
let err = pthread_rwlock_init(self.rwlock, nil)
|
let err = pthread_rwlock_init(self.rwlock, nil)
|
||||||
@@ -158,7 +160,9 @@ internal final class ReadWriteLock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
#if os(Windows)
|
#if canImport(WASILibc)
|
||||||
|
// WASILibc is single threaded, provides no locks
|
||||||
|
#elseif os(Windows)
|
||||||
// SRWLOCK does not need to be free'd
|
// SRWLOCK does not need to be free'd
|
||||||
#else
|
#else
|
||||||
let err = pthread_rwlock_destroy(self.rwlock)
|
let err = pthread_rwlock_destroy(self.rwlock)
|
||||||
@@ -172,7 +176,9 @@ internal final class ReadWriteLock {
|
|||||||
/// Whenever possible, consider using `withReaderLock` instead of this
|
/// Whenever possible, consider using `withReaderLock` instead of this
|
||||||
/// method and `unlock`, to simplify lock handling.
|
/// method and `unlock`, to simplify lock handling.
|
||||||
public func lockRead() {
|
public func lockRead() {
|
||||||
#if os(Windows)
|
#if canImport(WASILibc)
|
||||||
|
// WASILibc is single threaded, provides no locks
|
||||||
|
#elseif os(Windows)
|
||||||
AcquireSRWLockShared(self.rwlock)
|
AcquireSRWLockShared(self.rwlock)
|
||||||
self.shared = true
|
self.shared = true
|
||||||
#else
|
#else
|
||||||
@@ -186,7 +192,9 @@ internal final class ReadWriteLock {
|
|||||||
/// Whenever possible, consider using `withWriterLock` instead of this
|
/// Whenever possible, consider using `withWriterLock` instead of this
|
||||||
/// method and `unlock`, to simplify lock handling.
|
/// method and `unlock`, to simplify lock handling.
|
||||||
public func lockWrite() {
|
public func lockWrite() {
|
||||||
#if os(Windows)
|
#if canImport(WASILibc)
|
||||||
|
// WASILibc is single threaded, provides no locks
|
||||||
|
#elseif os(Windows)
|
||||||
AcquireSRWLockExclusive(self.rwlock)
|
AcquireSRWLockExclusive(self.rwlock)
|
||||||
self.shared = false
|
self.shared = false
|
||||||
#else
|
#else
|
||||||
@@ -201,7 +209,9 @@ internal final class ReadWriteLock {
|
|||||||
/// instead of this method and `lockRead` and `lockWrite`, to simplify lock
|
/// instead of this method and `lockRead` and `lockWrite`, to simplify lock
|
||||||
/// handling.
|
/// handling.
|
||||||
public func unlock() {
|
public func unlock() {
|
||||||
#if os(Windows)
|
#if canImport(WASILibc)
|
||||||
|
// WASILibc is single threaded, provides no locks
|
||||||
|
#elseif os(Windows)
|
||||||
if self.shared {
|
if self.shared {
|
||||||
ReleaseSRWLockShared(self.rwlock)
|
ReleaseSRWLockShared(self.rwlock)
|
||||||
} else {
|
} else {
|
||||||
@@ -261,4 +271,3 @@ extension ReadWriteLock {
|
|||||||
try self.withWriterLock(body)
|
try self.withWriterLock(body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
@@ -113,7 +113,7 @@
|
|||||||
/// Please note that the above `LogHandler` will still pass the 'log level is a value' test above it iff the global log
|
/// Please note that the above `LogHandler` will still pass the 'log level is a value' test above it iff the global log
|
||||||
/// level has not been overridden. And most importantly it passes the requirement listed above: A change to the log
|
/// level has not been overridden. And most importantly it passes the requirement listed above: A change to the log
|
||||||
/// level on one `Logger` should not affect the log level of another `Logger` variable.
|
/// level on one `Logger` should not affect the log level of another `Logger` variable.
|
||||||
public protocol LogHandler {
|
public protocol LogHandler: _SwiftLogSendableLogHandler {
|
||||||
/// This method is called when a `LogHandler` must emit a log message. There is no need for the `LogHandler` to
|
/// This method is called when a `LogHandler` must emit a log message. There is no need for the `LogHandler` to
|
||||||
/// check if the `level` is above or below the configured `logLevel` as `Logger` already performed this check and
|
/// check if the `level` is above or below the configured `logLevel` as `Logger` already performed this check and
|
||||||
/// determined that a message should be logged.
|
/// determined that a message should be logged.
|
||||||
@@ -186,3 +186,11 @@ extension LogHandler {
|
|||||||
line: line)
|
line: line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Sendable support helpers
|
||||||
|
|
||||||
|
#if compiler(>=5.6)
|
||||||
|
@preconcurrency public protocol _SwiftLogSendableLogHandler: Sendable {}
|
||||||
|
#else
|
||||||
|
public protocol _SwiftLogSendableLogHandler {}
|
||||||
|
#endif
|
||||||
@@ -650,13 +650,7 @@ extension Logger {
|
|||||||
/// configured. `LoggingSystem` is set up just once in a given program to set up the desired logging backend
|
/// configured. `LoggingSystem` is set up just once in a given program to set up the desired logging backend
|
||||||
/// implementation.
|
/// implementation.
|
||||||
public enum LoggingSystem {
|
public enum LoggingSystem {
|
||||||
#if canImport(WASILibc)
|
private static let _factory = FactoryBox(StreamLogHandler.standardOutput)
|
||||||
// WASILibc is single threaded, provides no locks
|
|
||||||
#else
|
|
||||||
fileprivate static let lock = ReadWriteLock()
|
|
||||||
#endif
|
|
||||||
fileprivate static var factory: (String) -> LogHandler = StreamLogHandler.standardOutput
|
|
||||||
fileprivate static var initialized = false
|
|
||||||
|
|
||||||
/// `bootstrap` is a one-time configuration function which globally selects the desired logging backend
|
/// `bootstrap` is a one-time configuration function which globally selects the desired logging backend
|
||||||
/// implementation. `bootstrap` can be called at maximum once in any given program, calling it more than once will
|
/// implementation. `bootstrap` can be called at maximum once in any given program, calling it more than once will
|
||||||
@@ -665,28 +659,40 @@ public enum LoggingSystem {
|
|||||||
/// - parameters:
|
/// - parameters:
|
||||||
/// - factory: A closure that given a `Logger` identifier, produces an instance of the `LogHandler`.
|
/// - factory: A closure that given a `Logger` identifier, produces an instance of the `LogHandler`.
|
||||||
public static func bootstrap(_ factory: @escaping (String) -> LogHandler) {
|
public static func bootstrap(_ factory: @escaping (String) -> LogHandler) {
|
||||||
#if canImport(WASILibc)
|
self._factory.replaceFactory(factory, validate: true)
|
||||||
precondition(!self.initialized, "logging system can only be initialized once per process.")
|
|
||||||
self.factory = factory
|
|
||||||
self.initialized = true
|
|
||||||
#else
|
|
||||||
self.lock.withWriterLock {
|
|
||||||
precondition(!self.initialized, "logging system can only be initialized once per process.")
|
|
||||||
self.factory = factory
|
|
||||||
self.initialized = true
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// for our testing we want to allow multiple bootstraping
|
// for our testing we want to allow multiple bootstraping
|
||||||
internal static func bootstrapInternal(_ factory: @escaping (String) -> LogHandler) {
|
internal static func bootstrapInternal(_ factory: @escaping (String) -> LogHandler) {
|
||||||
#if canImport(WASILibc)
|
self._factory.replaceFactory(factory, validate: false)
|
||||||
self.factory = factory
|
}
|
||||||
#else
|
|
||||||
self.lock.withWriterLock {
|
fileprivate static var factory: (String) -> LogHandler {
|
||||||
self.factory = factory
|
return self._factory.underlying
|
||||||
|
}
|
||||||
|
|
||||||
|
private final class FactoryBox {
|
||||||
|
private let lock = ReadWriteLock()
|
||||||
|
fileprivate var _underlying: (String) -> LogHandler
|
||||||
|
private var initialized = false
|
||||||
|
|
||||||
|
init(_ underlying: @escaping (String) -> LogHandler) {
|
||||||
|
self._underlying = underlying
|
||||||
|
}
|
||||||
|
|
||||||
|
func replaceFactory(_ factory: @escaping (String) -> LogHandler, validate: Bool) {
|
||||||
|
self.lock.withWriterLock {
|
||||||
|
precondition(!validate || !self.initialized, "logging system can only be initialized once per process.")
|
||||||
|
self._underlying = factory
|
||||||
|
self.initialized = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var underlying: (String) -> LogHandler {
|
||||||
|
return self.lock.withReaderLock {
|
||||||
|
return self._underlying
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -715,8 +721,11 @@ extension Logger {
|
|||||||
case string(String)
|
case string(String)
|
||||||
|
|
||||||
/// A metadata value which is some `CustomStringConvertible`.
|
/// A metadata value which is some `CustomStringConvertible`.
|
||||||
|
#if compiler(>=5.7)
|
||||||
|
case stringConvertible(CustomStringConvertible & Sendable)
|
||||||
|
#else
|
||||||
case stringConvertible(CustomStringConvertible)
|
case stringConvertible(CustomStringConvertible)
|
||||||
|
#endif
|
||||||
/// A metadata value which is a dictionary from `String` to `Logger.MetadataValue`.
|
/// A metadata value which is a dictionary from `String` to `Logger.MetadataValue`.
|
||||||
///
|
///
|
||||||
/// Because `MetadataValue` implements `ExpressibleByDictionaryLiteral`, you don't need to type
|
/// Because `MetadataValue` implements `ExpressibleByDictionaryLiteral`, you don't need to type
|
||||||
@@ -774,11 +783,7 @@ extension Logger {
|
|||||||
/// - parameters:
|
/// - parameters:
|
||||||
/// - label: An identifier for the creator of a `Logger`.
|
/// - label: An identifier for the creator of a `Logger`.
|
||||||
public init(label: String) {
|
public init(label: String) {
|
||||||
#if canImport(WASILibc)
|
|
||||||
self.init(label: label, LoggingSystem.factory(label))
|
self.init(label: label, LoggingSystem.factory(label))
|
||||||
#else
|
|
||||||
self = LoggingSystem.lock.withReaderLock { Logger(label: label, LoggingSystem.factory(label)) }
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a `Logger` given a `label` identifying the creator of the `Logger` or a non-standard `LogHandler`.
|
/// Construct a `Logger` given a `label` identifying the creator of the `Logger` or a non-standard `LogHandler`.
|
||||||
@@ -990,15 +995,17 @@ public struct MultiplexLogHandler: LogHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if canImport(WASILibc) || os(Android)
|
||||||
|
internal typealias CFilePointer = OpaquePointer
|
||||||
|
#else
|
||||||
|
internal typealias CFilePointer = UnsafeMutablePointer<FILE>
|
||||||
|
#endif
|
||||||
|
|
||||||
/// A wrapper to facilitate `print`-ing to stderr and stdio that
|
/// A wrapper to facilitate `print`-ing to stderr and stdio that
|
||||||
/// ensures access to the underlying `FILE` is locked to prevent
|
/// ensures access to the underlying `FILE` is locked to prevent
|
||||||
/// cross-thread interleaving of output.
|
/// cross-thread interleaving of output.
|
||||||
internal struct StdioOutputStream: TextOutputStream {
|
internal struct StdioOutputStream: TextOutputStream {
|
||||||
#if canImport(WASILibc)
|
internal let file: CFilePointer
|
||||||
internal let file: OpaquePointer
|
|
||||||
#else
|
|
||||||
internal let file: UnsafeMutablePointer<FILE>
|
|
||||||
#endif
|
|
||||||
internal let flushMode: FlushMode
|
internal let flushMode: FlushMode
|
||||||
|
|
||||||
internal func write(_ string: String) {
|
internal func write(_ string: String) {
|
||||||
@@ -1072,6 +1079,12 @@ let systemStdout = WASILibc.stdout!
|
|||||||
/// `StreamLogHandler` is a simple implementation of `LogHandler` for directing
|
/// `StreamLogHandler` is a simple implementation of `LogHandler` for directing
|
||||||
/// `Logger` output to either `stderr` or `stdout` via the factory methods.
|
/// `Logger` output to either `stderr` or `stdout` via the factory methods.
|
||||||
public struct StreamLogHandler: LogHandler {
|
public struct StreamLogHandler: LogHandler {
|
||||||
|
#if compiler(>=5.6)
|
||||||
|
internal typealias _SendableTextOutputStream = TextOutputStream & Sendable
|
||||||
|
#else
|
||||||
|
internal typealias _SendableTextOutputStream = TextOutputStream
|
||||||
|
#endif
|
||||||
|
|
||||||
/// Factory that makes a `StreamLogHandler` to directs its output to `stdout`
|
/// Factory that makes a `StreamLogHandler` to directs its output to `stdout`
|
||||||
public static func standardOutput(label: String) -> StreamLogHandler {
|
public static func standardOutput(label: String) -> StreamLogHandler {
|
||||||
return StreamLogHandler(label: label, stream: StdioOutputStream.stdout)
|
return StreamLogHandler(label: label, stream: StdioOutputStream.stdout)
|
||||||
@@ -1082,7 +1095,7 @@ public struct StreamLogHandler: LogHandler {
|
|||||||
return StreamLogHandler(label: label, stream: StdioOutputStream.stderr)
|
return StreamLogHandler(label: label, stream: StdioOutputStream.stderr)
|
||||||
}
|
}
|
||||||
|
|
||||||
private let stream: TextOutputStream
|
private let stream: _SendableTextOutputStream
|
||||||
private let label: String
|
private let label: String
|
||||||
|
|
||||||
public var logLevel: Logger.Level = .info
|
public var logLevel: Logger.Level = .info
|
||||||
@@ -1104,7 +1117,7 @@ public struct StreamLogHandler: LogHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// internal for testing only
|
// internal for testing only
|
||||||
internal init(label: String, stream: TextOutputStream) {
|
internal init(label: String, stream: _SendableTextOutputStream) {
|
||||||
self.label = label
|
self.label = label
|
||||||
self.stream = stream
|
self.stream = stream
|
||||||
}
|
}
|
||||||
@@ -1260,3 +1273,17 @@ extension Logger.MetadataValue: ExpressibleByArrayLiteral {
|
|||||||
self = .array(elements)
|
self = .array(elements)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Sendable support helpers
|
||||||
|
|
||||||
|
#if compiler(>=5.7.0)
|
||||||
|
extension Logger.MetadataValue: Sendable {} // on 5.7 `stringConvertible`'s value marked as Sendable; but if a value not conforming to Sendable is passed there, a warning is emitted. We are okay with warnings, but on 5.6 for the same situation an error is emitted (!)
|
||||||
|
#elseif compiler(>=5.6)
|
||||||
|
extension Logger.MetadataValue: @unchecked Sendable {} // sadly, On 5.6 a missing Sendable conformance causes an 'error' (specifically this is about `stringConvertible`'s value)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if compiler(>=5.6)
|
||||||
|
extension Logger: Sendable {}
|
||||||
|
extension Logger.Level: Sendable {}
|
||||||
|
extension Logger.Message: Sendable {}
|
||||||
|
#endif
|
||||||
@@ -22,7 +22,7 @@ import XCTest
|
|||||||
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
|
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
|
||||||
///
|
///
|
||||||
|
|
||||||
#if os(Linux) || os(FreeBSD) || os(Windows)
|
#if os(Linux) || os(FreeBSD) || os(Windows) || os(Android)
|
||||||
@testable import LoggingTests
|
@testable import LoggingTests
|
||||||
|
|
||||||
XCTMain([
|
XCTMain([
|
||||||
|
|||||||
@@ -262,7 +262,7 @@ class LoggingTest: XCTestCase {
|
|||||||
|
|
||||||
// Example of custom "box" which may be used to implement "render at most once" semantics
|
// Example of custom "box" which may be used to implement "render at most once" semantics
|
||||||
// Not thread-safe, thus should not be shared across threads.
|
// Not thread-safe, thus should not be shared across threads.
|
||||||
internal class LazyMetadataBox: CustomStringConvertible {
|
internal final class LazyMetadataBox: CustomStringConvertible {
|
||||||
private var makeValue: (() -> String)?
|
private var makeValue: (() -> String)?
|
||||||
private var _value: String?
|
private var _value: String?
|
||||||
|
|
||||||
@@ -806,7 +806,7 @@ class LoggingTest: XCTestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func withWriteReadFDsAndReadBuffer(_ body: (UnsafeMutablePointer<FILE>, CInt, UnsafeMutablePointer<Int8>) -> Void) {
|
func withWriteReadFDsAndReadBuffer(_ body: (CFilePointer, CInt, UnsafeMutablePointer<Int8>) -> Void) {
|
||||||
var fds: [Int32] = [-1, -1]
|
var fds: [Int32] = [-1, -1]
|
||||||
#if os(Windows)
|
#if os(Windows)
|
||||||
fds.withUnsafeMutableBufferPointer {
|
fds.withUnsafeMutableBufferPointer {
|
||||||
@@ -889,3 +889,15 @@ extension Logger {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sendable
|
||||||
|
|
||||||
|
#if compiler(>=5.6)
|
||||||
|
// used to test logging metadata which requires Sendable conformance
|
||||||
|
// @unchecked Sendable since manages it own state
|
||||||
|
extension LoggingTest.LazyMetadataBox: @unchecked Sendable {}
|
||||||
|
|
||||||
|
// used to test logging stream which requires Sendable conformance
|
||||||
|
// @unchecked Sendable since manages it own state
|
||||||
|
extension LoggingTest.InterceptStream: @unchecked Sendable {}
|
||||||
|
#endif
|
||||||
@@ -31,8 +31,6 @@ internal struct TestLogging {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal struct TestLogHandler: LogHandler {
|
internal struct TestLogHandler: LogHandler {
|
||||||
private let logLevelLock = NSLock()
|
|
||||||
private let metadataLock = NSLock()
|
|
||||||
private let recorder: Recorder
|
private let recorder: Recorder
|
||||||
private let config: Config
|
private let config: Config
|
||||||
private var logger: Logger // the actual logger
|
private var logger: Logger // the actual logger
|
||||||
@@ -56,10 +54,10 @@ internal struct TestLogHandler: LogHandler {
|
|||||||
var logLevel: Logger.Level {
|
var logLevel: Logger.Level {
|
||||||
get {
|
get {
|
||||||
// get from config unless set
|
// get from config unless set
|
||||||
return self.logLevelLock.withLock { self._logLevel } ?? self.config.get(key: self.label)
|
return self._logLevel ?? self.config.get(key: self.label)
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
self.logLevelLock.withLock { self._logLevel = newValue }
|
self._logLevel = newValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,26 +70,20 @@ internal struct TestLogHandler: LogHandler {
|
|||||||
|
|
||||||
public var metadata: Logger.Metadata {
|
public var metadata: Logger.Metadata {
|
||||||
get {
|
get {
|
||||||
// return self.logger.metadata
|
return self._metadata
|
||||||
return self.metadataLock.withLock { self._metadata }
|
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
// self.logger.metadata = newValue
|
self._metadata = newValue
|
||||||
self.metadataLock.withLock { self._metadata = newValue }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: would be nice to delegate to local copy of logger but StdoutLogger is a reference type. why?
|
// TODO: would be nice to delegate to local copy of logger but StdoutLogger is a reference type. why?
|
||||||
subscript(metadataKey metadataKey: Logger.Metadata.Key) -> Logger.Metadata.Value? {
|
subscript(metadataKey metadataKey: Logger.Metadata.Key) -> Logger.Metadata.Value? {
|
||||||
get {
|
get {
|
||||||
// return self.logger[metadataKey: metadataKey]
|
return self._metadata[metadataKey]
|
||||||
return self.metadataLock.withLock { self._metadata[metadataKey] }
|
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
// return logger[metadataKey: metadataKey] = newValue
|
self._metadata[metadataKey] = newValue
|
||||||
self.metadataLock.withLock {
|
|
||||||
self._metadata[metadataKey] = newValue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -342,3 +334,12 @@ internal struct TestLibrary {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sendable
|
||||||
|
|
||||||
|
#if compiler(>=5.6)
|
||||||
|
extension TestLogHandler: @unchecked Sendable {}
|
||||||
|
extension Recorder: @unchecked Sendable {}
|
||||||
|
extension Config: @unchecked Sendable {}
|
||||||
|
extension MDC: @unchecked Sendable {}
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// This source file is part of the Swift Logging API open source project
|
||||||
|
//
|
||||||
|
// Copyright (c) 2022 Apple Inc. and the Swift Logging API project authors
|
||||||
|
// Licensed under Apache License v2.0
|
||||||
|
//
|
||||||
|
// See LICENSE.txt for license information
|
||||||
|
// See CONTRIBUTORS.txt for the list of Swift Logging API project authors
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
import Dispatch
|
||||||
|
@testable import Logging
|
||||||
|
import XCTest
|
||||||
|
|
||||||
|
class SendableTest: XCTestCase {
|
||||||
|
#if compiler(>=5.6)
|
||||||
|
func testSendableLogger() async {
|
||||||
|
let testLogging = TestLogging()
|
||||||
|
LoggingSystem.bootstrapInternal(testLogging.make)
|
||||||
|
|
||||||
|
let logger = Logger(label: "test")
|
||||||
|
let message1 = Logger.Message(stringLiteral: "critical 1")
|
||||||
|
let message2 = Logger.Message(stringLiteral: "critical 2")
|
||||||
|
let metadata: Logger.Metadata = ["key": "value"]
|
||||||
|
|
||||||
|
let task = Task.detached {
|
||||||
|
logger.info("info")
|
||||||
|
logger.critical(message1)
|
||||||
|
logger.critical(message2)
|
||||||
|
logger.warning(.init(stringLiteral: "warning"), metadata: metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
await task.value
|
||||||
|
testLogging.history.assertExist(level: .info, message: "info", metadata: [:])
|
||||||
|
testLogging.history.assertExist(level: .critical, message: "critical 1", metadata: [:])
|
||||||
|
testLogging.history.assertExist(level: .critical, message: "critical 2", metadata: [:])
|
||||||
|
testLogging.history.assertExist(level: .warning, message: "warning", metadata: metadata)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
+2
-3
@@ -18,10 +18,9 @@ RUN apt-get update && apt-get install -y lsof dnsutils netcat-openbsd net-tools
|
|||||||
|
|
||||||
# ruby and jazzy for docs generation
|
# ruby and jazzy for docs generation
|
||||||
RUN apt-get update && apt-get install -y ruby ruby-dev libsqlite3-dev build-essential
|
RUN apt-get update && apt-get install -y ruby ruby-dev libsqlite3-dev build-essential
|
||||||
# switch of gem docs building
|
|
||||||
RUN echo "gem: --no-document" > ~/.gemrc
|
|
||||||
# jazzy no longer works on xenial as ruby is too old.
|
# jazzy no longer works on xenial as ruby is too old.
|
||||||
RUN if [ "${ubuntu_version}" != "xenial" ] ; then gem install jazzy; fi
|
RUN if [ "${ubuntu_version}" = "focal" ] ; then echo "gem: --no-document" > ~/.gemrc; fi
|
||||||
|
RUN if [ "${ubuntu_version}" = "focal" ] ; then gem install jazzy; fi
|
||||||
|
|
||||||
# tools
|
# tools
|
||||||
RUN mkdir -p $HOME/.tools
|
RUN mkdir -p $HOME/.tools
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
version: "3"
|
||||||
|
|
||||||
|
services:
|
||||||
|
|
||||||
|
runtime-setup:
|
||||||
|
image: swift-log:20.04-5.5
|
||||||
|
build:
|
||||||
|
args:
|
||||||
|
ubuntu_version: "focal"
|
||||||
|
swift_version: "5.5"
|
||||||
|
|
||||||
|
test:
|
||||||
|
image: swift-log:20.04-5.5
|
||||||
|
environment: []
|
||||||
|
#- SANITIZER_ARG=--sanitize=thread
|
||||||
|
|
||||||
|
shell:
|
||||||
|
image: swift-log:20.04-5.5
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
version: "3"
|
||||||
|
|
||||||
|
services:
|
||||||
|
|
||||||
|
runtime-setup:
|
||||||
|
image: swift-log:20.04-5.6
|
||||||
|
build:
|
||||||
|
args:
|
||||||
|
ubuntu_version: "focal"
|
||||||
|
swift_version: "5.6"
|
||||||
|
|
||||||
|
test:
|
||||||
|
image: swift-log:20.04-5.6
|
||||||
|
environment:
|
||||||
|
- FORCE_TEST_DISCOVERY=--enable-test-discovery
|
||||||
|
#- SANITIZER_ARG=--sanitize=thread
|
||||||
|
|
||||||
|
shell:
|
||||||
|
image: swift-log:20.04-5.6
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
version: "3"
|
||||||
|
|
||||||
|
services:
|
||||||
|
|
||||||
|
runtime-setup:
|
||||||
|
image: swift-log:20.04-5.7
|
||||||
|
build:
|
||||||
|
args:
|
||||||
|
base_image: "swiftlang/swift:nightly-main-focal"
|
||||||
|
|
||||||
|
test:
|
||||||
|
image: swift-log:20.04-5.7
|
||||||
|
environment:
|
||||||
|
- FORCE_TEST_DISCOVERY=--enable-test-discovery
|
||||||
|
#- SANITIZER_ARG=--sanitize=thread
|
||||||
|
|
||||||
|
shell:
|
||||||
|
image: swift-log:20.04-5.7
|
||||||
@@ -10,7 +10,8 @@ services:
|
|||||||
|
|
||||||
test:
|
test:
|
||||||
image: swift-log:20.04-main
|
image: swift-log:20.04-main
|
||||||
environment: []
|
environment:
|
||||||
|
- FORCE_TEST_DISCOVERY=--enable-test-discovery
|
||||||
#- SANITIZER_ARG=--sanitize=thread
|
#- SANITIZER_ARG=--sanitize=thread
|
||||||
|
|
||||||
shell:
|
shell:
|
||||||
|
|||||||
@@ -28,10 +28,10 @@ services:
|
|||||||
|
|
||||||
test:
|
test:
|
||||||
<<: *common
|
<<: *common
|
||||||
command: /bin/bash -xcl "swift test -Xswiftc -warnings-as-errors $${SANITIZER_ARG-}"
|
command: /bin/bash -xcl "swift test -Xswiftc -warnings-as-errors $${FORCE_TEST_DISCOVERY-} $${SANITIZER_ARG-}"
|
||||||
|
|
||||||
# util
|
# util
|
||||||
|
|
||||||
shell:
|
shell:
|
||||||
<<: *common
|
<<: *common
|
||||||
entrypoint: /bin/bash
|
entrypoint: /bin/bash -l
|
||||||
@@ -26,20 +26,20 @@ if [[ "$(uname -s)" == "Linux" ]]; then
|
|||||||
swift build
|
swift build
|
||||||
fi
|
fi
|
||||||
# setup source-kitten if required
|
# setup source-kitten if required
|
||||||
source_kitten_source_path="$root_path/.SourceKitten"
|
mkdir -p "$root_path/.build/sourcekitten"
|
||||||
|
source_kitten_source_path="$root_path/.build/sourcekitten/source"
|
||||||
if [[ ! -d "$source_kitten_source_path" ]]; then
|
if [[ ! -d "$source_kitten_source_path" ]]; then
|
||||||
git clone https://github.com/jpsim/SourceKitten.git "$source_kitten_source_path"
|
git clone https://github.com/jpsim/SourceKitten.git "$source_kitten_source_path"
|
||||||
fi
|
fi
|
||||||
source_kitten_path="$source_kitten_source_path/.build/x86_64-unknown-linux/debug"
|
source_kitten_path="$source_kitten_source_path/.build/debug"
|
||||||
if [[ ! -d "$source_kitten_path" ]]; then
|
if [[ ! -d "$source_kitten_path" ]]; then
|
||||||
rm -rf "$source_kitten_source_path/.swift-version"
|
rm -rf "$source_kitten_source_path/.swift-version"
|
||||||
cd "$source_kitten_source_path" && swift build && cd "$root_path"
|
cd "$source_kitten_source_path" && swift build && cd "$root_path"
|
||||||
fi
|
fi
|
||||||
# generate
|
# generate
|
||||||
mkdir -p "$root_path/.build/sourcekitten"
|
|
||||||
for module in "${modules[@]}"; do
|
for module in "${modules[@]}"; do
|
||||||
if [[ ! -f "$root_path/.build/sourcekitten/$module.json" ]]; then
|
if [[ ! -f "$root_path/.build/sourcekitten/$module.json" ]]; then
|
||||||
"$source_kitten_path/sourcekitten" doc --spm-module $module > "$root_path/.build/sourcekitten/$module.json"
|
"$source_kitten_path/sourcekitten" doc --spm --module-name $module > "$root_path/.build/sourcekitten/$module.json"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ def createLinuxMain(testsDirectory, allTestSubDirectories, files)
|
|||||||
file.write header(fileName)
|
file.write header(fileName)
|
||||||
file.write "\n"
|
file.write "\n"
|
||||||
|
|
||||||
file.write "#if os(Linux) || os(FreeBSD) || os(Windows)\n"
|
file.write "#if os(Linux) || os(FreeBSD) || os(Windows) || os(Android)\n"
|
||||||
for testSubDirectory in allTestSubDirectories.sort { |x, y| x <=> y }
|
for testSubDirectory in allTestSubDirectories.sort { |x, y| x <=> y }
|
||||||
file.write '@testable import ' + testSubDirectory + "\n"
|
file.write '@testable import ' + testSubDirectory + "\n"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|||||||
|
|
||||||
function replace_acceptable_years() {
|
function replace_acceptable_years() {
|
||||||
# this needs to replace all acceptable forms with 'YEARS'
|
# this needs to replace all acceptable forms with 'YEARS'
|
||||||
sed -e 's/20[12][78901]-20[12][8901]/YEARS/' -e 's/20[12][8901]/YEARS/'
|
sed -e 's/20[12][789012]-20[12][89012]/YEARS/' -e 's/20[12][89012]/YEARS/'
|
||||||
}
|
}
|
||||||
|
|
||||||
printf "=> Checking linux tests... "
|
printf "=> Checking linux tests... "
|
||||||
@@ -86,7 +86,7 @@ for language in swift-or-c bash dtrace; do
|
|||||||
matching_files=( -name '*' )
|
matching_files=( -name '*' )
|
||||||
case "$language" in
|
case "$language" in
|
||||||
swift-or-c)
|
swift-or-c)
|
||||||
exceptions=( -name c_nio_http_parser.c -o -name c_nio_http_parser.h -o -name cpp_magic.h -o -name Package.swift -o -name CNIOSHA1.h -o -name c_nio_sha1.c -o -name ifaddrs-android.c -o -name ifaddrs-android.h)
|
exceptions=( -name c_nio_http_parser.c -o -name c_nio_http_parser.h -o -name cpp_magic.h -o -name Package.swift -o name Package-swift5.6.swift -o -name CNIOSHA1.h -o -name c_nio_sha1.c -o -name ifaddrs-android.c -o -name ifaddrs-android.h)
|
||||||
matching_files=( -name '*.swift' -o -name '*.c' -o -name '*.h' )
|
matching_files=( -name '*.swift' -o -name '*.c' -o -name '*.h' )
|
||||||
cat > "$tmp" <<"EOF"
|
cat > "$tmp" <<"EOF"
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|||||||
Reference in new issue
Block a user