mirror of
https://github.com/apple/swift-log
synced 2026-11-22 03:40:39 +00:00
Compare commits
33
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
edc4555381 | ||
|
|
a7e6d7fef5 | ||
|
|
9cdea8c2af | ||
|
|
d8af258aec | ||
|
|
02770d91fc | ||
|
|
7ebe44cb03 | ||
|
|
65a8e5e921 | ||
|
|
08ec50d7b1 | ||
|
|
3577a992d3 | ||
|
|
b1d1c0372a | ||
|
|
bc049d05d0 | ||
|
|
bda2661797 | ||
|
|
5d66f7ba25 | ||
|
|
c7988420c2 | ||
|
|
7d1d4e29ea | ||
|
|
12d3a8651d | ||
|
|
b5440510b6 | ||
|
|
c6437b76d8 | ||
|
|
16cfcc60e4 | ||
|
|
974b90e008 | ||
|
|
cb77380f88 | ||
|
|
7bc87ea4ca | ||
|
|
04b23c1c17 | ||
|
|
5fa4a664a0 | ||
|
|
6a1fd374f8 | ||
|
|
4a0205445c | ||
|
|
b8eedd521d | ||
|
|
5a1b9cfc71 | ||
|
|
1d40828ecc | ||
|
|
a4aa2d5bd9 | ||
|
|
de30f5bfc2 | ||
|
|
dc3970db38 | ||
|
|
a885be7bff |
No files matched your search
@@ -65,6 +65,32 @@ We require that your commit messages match our template. The easiest way to do t
|
||||
SwiftLog uses XCTest to run tests on both macOS and Linux. While the macOS version of XCTest is able to use the Objective-C runtime to discover tests at execution time, the Linux version is not.
|
||||
For this reason, whenever you add new tests **you have to run a script** that generates the hooks needed to run those tests on Linux, or our CI will complain that the tests are not all present on Linux. To do this, merely execute `ruby ./scripts/generate_linux_tests.rb` at the root of the package and check the changes it made.
|
||||
|
||||
### Run `./scripts/soundness.sh`
|
||||
|
||||
The scripts directory contains a [soundness.sh script](https://github.com/apple/swift-log/blob/main/scripts/soundness.sh)
|
||||
that enforces additional checks, like license headers and formatting style.
|
||||
|
||||
Please make sure to `./scripts/soundness.sh` before pushing a change upstream, otherwise it is likely the PR validation will fail
|
||||
on minor changes such as a missing `self.` or similar formatting issues.
|
||||
|
||||
> The script also executes the above mentioned `generate_linux_tests.rb`.
|
||||
|
||||
For frequent contributors, we recommend adding the script as a [git pre-push hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks), which you can do via executing the following command in the project root directory:
|
||||
|
||||
```bash
|
||||
cat << EOF > .git/hooks/pre-push
|
||||
#!/bin/bash
|
||||
|
||||
if [[ -f "scripts/soundness.sh" ]]; then
|
||||
scripts/soundness.sh
|
||||
fi
|
||||
EOF
|
||||
```
|
||||
|
||||
Which makes the script execute, and only allow the `git push` to complete if the check has passed.
|
||||
|
||||
In the case of formatting issues, you can then `git add` the formatting changes, and attempt the push again.
|
||||
|
||||
## How to contribute your work
|
||||
|
||||
Please open a pull request at https://github.com/apple/swift-log. Make sure the CI passes, and then wait for code review.
|
||||
@@ -1,7 +1,5 @@
|
||||
# SwiftLog
|
||||
|
||||
A Logging API package for Swift. Version `1.0.0` requires Swift 5.0 but there is a version `0.x.y` series available for Swift 4 to ease your transition towards Swift 5. If you intend to use or support SwiftLog for Swift 4, please check the [paragraph](#help-i-need-swift-4) at the end of the document.
|
||||
|
||||
First things first: This is the beginning of a community-driven open-source project actively seeking contributions, be it code, documentation, or ideas. Apart from contributing to `SwiftLog` itself, there's another huge gap at the moment: `SwiftLog` is an _API package_ which tries to establish a common API the ecosystem can use. To make logging really work for real-world workloads, we need `SwiftLog`-compatible _logging backends_ which then either persist the log messages in files, render them in nicer colors on the terminal, or send them over to Splunk or ELK.
|
||||
|
||||
What `SwiftLog` provides today can be found in the [API docs][api-docs].
|
||||
@@ -12,7 +10,7 @@ If you have a server-side Swift application, or maybe a cross-platform (for exam
|
||||
|
||||
#### Adding the dependency
|
||||
|
||||
`SwiftLog` is designed for Swift 5, the `1.0.0` release requires Swift 5 (however we will soon tag a `0.x` version that will work with Swift 4 for the transition period). To depend on the logging API package, you need to declare your dependency in your `Package.swift`:
|
||||
`SwiftLog` is designed for Swift 5. To depend on the logging API package, you need to declare your dependency in your `Package.swift`:
|
||||
|
||||
```swift
|
||||
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
|
||||
@@ -21,9 +19,16 @@ If you have a server-side Swift application, or maybe a cross-platform (for exam
|
||||
and to your application/library target, add `"Logging"` to your `dependencies`, e.g. like this:
|
||||
|
||||
```swift
|
||||
// Target syntax for Swift up to version 5.1
|
||||
.target(name: "BestExampleApp", dependencies: ["Logging"]),
|
||||
|
||||
// Target for Swift 5.2
|
||||
.target(name: "BestExampleApp", dependencies: [
|
||||
.product(name: "Logging", package: "swift-log")
|
||||
],
|
||||
```
|
||||
|
||||
|
||||
#### Let's log
|
||||
|
||||
```swift
|
||||
@@ -61,17 +66,24 @@ As the API has just launched, not many implementations exist yet. If you are int
|
||||
|
||||
| Repository | Handler Description|
|
||||
| ----------- | ----------- |
|
||||
| [IBM-Swift/HeliumLogger](https://github.com/IBM-Swift/HeliumLogger) |a logging backend widely used in the Kitura ecosystem |
|
||||
| [Kitura/HeliumLogger](https://github.com/Kitura/HeliumLogger) |a logging backend widely used in the Kitura ecosystem |
|
||||
| [ianpartridge/swift-log-**syslog**](https://github.com/ianpartridge/swift-log-syslog) | a [syslog](https://en.wikipedia.org/wiki/Syslog) backend|
|
||||
| [Adorkable/swift-log-**format-and-pipe**](https://github.com/Adorkable/swift-log-format-and-pipe) | a backend that allows customization of the output format and the resulting destination |
|
||||
| [chrisaljoudi/swift-log-**oslog**](https://github.com/chrisaljoudi/swift-log-oslog) | an OSLog [Unified Logging](https://developer.apple.com/documentation/os/logging) backend for use on Apple platforms. **Important Note:** we recommend using os_log directly as decribed [here](https://developer.apple.com/documentation/os/logging). Using os_log through swift-log using this backend will be less efficient and will also prevent specifying the privacy of the message. The backend always uses `%{public}@` as the format string and eagerly converts all string interpolations to strings. This has two drawbacks: 1. the static components of the string interpolation would be eagerly copied by the unified logging system, which will result in loss of performance. 2. It makes all messages public, which changes the default privacy policy of os_log, and doesn't allow specifying fine-grained privacy of sections of the message. In a separate on-going work, Swift APIs for os_log are being improved and made to align closely with swift-log APIs. References: [Unifying Logging Levels](https://forums.swift.org/t/custom-string-interpolation-and-compile-time-interpretation-applied-to-logging/18799), [Making os_log accept string interpolations using compile-time interpretation](https://forums.swift.org/t/logging-levels-for-swifts-server-side-logging-apis-and-new-os-log-apis/20365). |
|
||||
| [Brainfinance/StackdriverLogging](https://github.com/Brainfinance/StackdriverLogging) | a structured JSON logging backend for use on Google Cloud Platform with the [Stackdriver logging agent](https://cloud.google.com/logging/docs/agent) |
|
||||
| [Brainfinance/StackdriverLogging](https://github.com/Brainfinance/StackdriverLogging) | a structured JSON logging backend for use on Google Cloud Platform with the [Stackdriver logging agent](https://cloud.google.com/logging/docs/agent) |
|
||||
| [DnV1eX/GoogleCloudLogging](https://github.com/DnV1eX/GoogleCloudLogging) | a client-side library for logging application events in [Google Cloud](https://console.cloud.google.com/logs) via REST API v2. |
|
||||
| [vapor/console-kit](https://github.com/vapor/console-kit/) | print log messages to a terminal with stylized ([ANSI](https://en.wikipedia.org/wiki/ANSI_escape_code)) output |
|
||||
| [neallester/swift-log-testing](https://github.com/neallester/swift-log-testing) | provides access to log messages for use in assertions (within test targets) |
|
||||
| [neallester/swift-log-testing](https://github.com/neallester/swift-log-testing) | provides access to log messages for use in assertions (within test targets) |
|
||||
| [wlisac/swift-log-slack](https://github.com/wlisac/swift-log-slack) | a logging backend that sends critical log messages to Slack |
|
||||
| [NSHipster/swift-log-github-actions](https://github.com/NSHipster/swift-log-github-actions) | a logging backend that translates logging messages into [workflow commands for GitHub Actions](https://help.github.com/en/actions/reference/workflow-commands-for-github-actions). |
|
||||
| [stevapple/swift-log-telegram](https://github.com/stevapple/swift-log-telegram) | a logging backend that sends log messages to any Telegram chat (Inspired by and forked from [wlisac/swift-log-slack](https://github.com/wlisac/swift-log-slack)) |
|
||||
| [jagreenwood/swift-log-datadog](https://github.com/jagreenwood/swift-log-datadog) | a logging backend which sends log messages to the [Datadog](https://www.datadoghq.com/log-management/) log management service |
|
||||
| [google/SwiftLogFireCloud](https://github.com/google/swiftlogfirecloud) | a logging backend for time series logging which pushes logs as flat files to Firebase Cloud Storage. |
|
||||
| [crspybits/swift-log-file](https://github.com/crspybits/swift-log-file) | a simple local file logger (using `Foundation` `FileManager`) |
|
||||
| [sushichop/Puppy](https://github.com/sushichop/Puppy) | a logging backend that supports multiple transports(console, file, syslog, etc.) and has the feature with formatting and file log rotation |
|
||||
| [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. |
|
||||
| [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) |
|
||||
| Your library? | [Get in touch!](https://forums.swift.org/c/server) |
|
||||
|
||||
## What is an API package?
|
||||
@@ -192,79 +204,9 @@ around between libraries to preserve metadata and the like.
|
||||
If you want to filter all log messages originating from a certain subsystem, filter by `source` which defaults to the module that is emitting the
|
||||
log message.
|
||||
|
||||
## SwiftLog for Swift 4
|
||||
<a name="help-i-need-swift-4"></a>
|
||||
## Security
|
||||
|
||||
First of, SwiftLog 1.0.x and SwiftLog 0.0.x are mostly compatible so don't be afraid. In fact, SwiftLog 0.0.0 is the same source code as SwiftLog 1.0.0 with a few changes made to make it Swift 4 compatible.
|
||||
|
||||
### How can I use SwiftLog 0 library or application?
|
||||
|
||||
If you have a application or a library that needs to be compatible with both Swift 4 and 5, then we recommend using the following in your `Package.swift`:
|
||||
|
||||
```swift
|
||||
.package(url: "https://github.com/apple/swift-log.git", Version("0.0.0") ..< Version("2.0.0")),
|
||||
```
|
||||
|
||||
This will instruct SwiftPM to allow any SwiftLog 0 and any SwiftLog 1 version. This is an unusual form because usually packages don't support multiple major versions of a package. Because SwiftLog 0 and 1 are mostly compatible however, this should not be a real issue and will enable everybody to get the best. If compiled with a Swift 4 compiler, this will be a SwiftLog 0 version but if compiled with a Swift 5 compiler everybody will get the best experience and performance delivered by SwiftLog 1.
|
||||
|
||||
In most cases, there is only one thing you need to remember: Always use _string literals_ and _string interpolations_ in the log methods and don't rely on the fact that SwiftLog 0 also allows `String`.
|
||||
|
||||
Good:
|
||||
|
||||
```swift
|
||||
logger.info("hello world")
|
||||
```
|
||||
|
||||
Bad:
|
||||
|
||||
```swift
|
||||
let message = "hello world"
|
||||
logger.info(message)
|
||||
```
|
||||
|
||||
If you have a `String` that you received from elsewhere, please use
|
||||
|
||||
```swift
|
||||
logger.info("\(stringIAlreadyHave)")
|
||||
```
|
||||
|
||||
For more details, have a look in the next section.
|
||||
|
||||
|
||||
### What are the differences between SwiftLog 1 and 0?
|
||||
|
||||
- SwiftLog 0 does not use `@inlinable`.
|
||||
- Apart from accepting `Logger.Message` for the message, SwiftLog 0 has a `String` overload.
|
||||
- In SwiftLog 0, `Logger.Message` is not `ExpressibleByStringLiteral` or `ExpressibleByStringInterpolation`.
|
||||
- In SwiftLog 0, `Logger.MetadataValue` is not `ExpressibleByStringLiteral` or `ExpressibleByStringInterpolation`.
|
||||
|
||||
#### Why these differences?
|
||||
|
||||
##### @inlinable
|
||||
|
||||
Swift 4.0 & 4.1 don't support `@inlinable`, so SwiftLog 0 can't use them.
|
||||
|
||||
##### Logger.Message
|
||||
Because all Swift 4 versions don't have a (non-deprecated) mechanism for a type to be `ExpressibleByStringInterpolation` we couldn't make `Logger.Message` expressible by string literals. Unfortunately, the most basic form of our logging API is `logger.info("Hello \(world)")`. For this to work however, `"Hello \(world)"` needs to be accepted and because we can't make `Logger.Message` `ExpressibleByStringInterpolation` we added an overload for all the logging methods to also accept `String`. In most cases, you won't even notice that with SwiftLog 0 you're creating a `String` (which is then transformed to a `Logger.Message`) and with SwiftLog 1 you're creating a `Logger.Message` directly. That is because both `String` and `Logger.Message` will accept all forms of string literals and string interpolations.
|
||||
Unfortunately, there is code that will make this seemingly small difference visible. If you write
|
||||
|
||||
```swift
|
||||
let message = "Hello world"
|
||||
logger.info(message)
|
||||
```
|
||||
|
||||
then this will only work in SwiftLog 0 and not in SwiftLog 1. Why? Because SwiftLog 1 will want a `Logger.Message` but `let message = "Hello world"` will make `message` to be of type `String` and in SwiftLog 1, the logging methods don't accept `String`s.
|
||||
|
||||
So if you intend to be compatible with SwiftLog 0 and 1 at the same time, please make sure to always use a _string literal_ or a _string interpolation_ inside of the logging methods.
|
||||
|
||||
In the case that you already have a `String` handy that you want to log, don't worry at all, just use
|
||||
|
||||
```swift
|
||||
let message = "Hello world"
|
||||
logger.info("\(message)")
|
||||
```
|
||||
|
||||
and again, you will be okay with SwiftLog 0 and 1.
|
||||
Please see [SECURITY.md](SECURITY.md) for SwiftLog's security process.
|
||||
|
||||
## Design
|
||||
|
||||
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
# Security
|
||||
|
||||
This document specifies the security process for the SwiftLog project.
|
||||
|
||||
## Disclosures
|
||||
|
||||
### Private Disclosure Process
|
||||
|
||||
The SwiftLog maintainers ask that known and suspected vulnerabilities be
|
||||
privately and responsibly disclosed by emailing
|
||||
[sswg-security-reports@forums.swift.org](mailto:sswg-security-reports@forums.swift.org)
|
||||
with the all the required detail.
|
||||
**Do not file a public issue.**
|
||||
|
||||
#### When to report a vulnerability
|
||||
|
||||
* You think you have discovered a potential security vulnerability in SwiftLog.
|
||||
* You are unsure how a vulnerability affects SwiftLog.
|
||||
|
||||
#### What happens next?
|
||||
|
||||
* A member of the team will acknowledge receipt of the report within 3
|
||||
working days (United States). This may include a request for additional
|
||||
information about reproducing the vulnerability.
|
||||
* We will privately inform the Swift Server Work Group ([SSWG][sswg]) of the
|
||||
vulnerability within 10 days of the report as per their [security
|
||||
guidelines][sswg-security].
|
||||
* Once we have identified a fix we may ask you to validate it. We aim to do this
|
||||
within 30 days. In some cases this may not be possible, for example when the
|
||||
vulnerability exists at the protocol level and the industry must coordinate on
|
||||
the disclosure process.
|
||||
* If a CVE number is required, one will be requested from [MITRE][mitre]
|
||||
providing you with full credit for the discovery.
|
||||
* We will decide on a planned release date and let you know when it is.
|
||||
* Prior to release, we will inform major dependents that a security-related
|
||||
patch is impending.
|
||||
* Once the fix has been released we will publish a security advisory on GitHub
|
||||
and in the Server → Security Updates category on the [Swift forums][swift-forums-sec].
|
||||
|
||||
[sswg]: https://github.com/swift-server/sswg
|
||||
[sswg-security]: https://github.com/swift-server/sswg/blob/main/security/README.md
|
||||
[swift-forums-sec]: https://forums.swift.org/c/server/security-updates/
|
||||
[mitre]: https://cveform.mitre.org/
|
||||
@@ -26,12 +26,18 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if canImport(WASILibc)
|
||||
// No locking on WASILibc
|
||||
#else
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
|
||||
import Darwin
|
||||
#elseif os(Windows)
|
||||
import WinSDK
|
||||
#else
|
||||
#elseif canImport(Glibc)
|
||||
import Glibc
|
||||
#else
|
||||
#error("Unsupported runtime")
|
||||
#endif
|
||||
|
||||
/// A threading lock based on `libpthread` instead of `libdispatch`.
|
||||
@@ -255,3 +261,4 @@ extension ReadWriteLock {
|
||||
try self.withWriterLock(body)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
+471
-35
@@ -15,9 +15,13 @@
|
||||
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
|
||||
import Darwin
|
||||
#elseif os(Windows)
|
||||
import MSVCRT
|
||||
#else
|
||||
import CRT
|
||||
#elseif canImport(Glibc)
|
||||
import Glibc
|
||||
#elseif canImport(WASILibc)
|
||||
import WASILibc
|
||||
#else
|
||||
#error("Unsupported runtime")
|
||||
#endif
|
||||
|
||||
/// A `Logger` is the central type in `SwiftLog`. Its central function is to emit log messages using one of the methods
|
||||
@@ -45,6 +49,7 @@ public struct Logger {
|
||||
}
|
||||
|
||||
extension Logger {
|
||||
#if compiler(>=5.3)
|
||||
/// Log a message passing the log level as a parameter.
|
||||
///
|
||||
/// If the `logLevel` passed to this method is more severe than the `Logger`'s `logLevel`, it will be logged,
|
||||
@@ -54,15 +59,33 @@ extension Logger {
|
||||
/// - level: The log level to log `message` at. For the available log levels, see `Logger.Level`.
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message.
|
||||
/// - source: The source this log messages originates to. Currently, it defaults to the folder containing the
|
||||
/// file that is emitting the log message, which usually is the module.
|
||||
/// - source: The source this log messages originates from. Defaults
|
||||
/// to the module emitting the log message (on Swift 5.3 or
|
||||
/// newer and the folder name containing the log emitting file on Swift 5.2 or
|
||||
/// older).
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - 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,
|
||||
source: @autoclosure () -> String? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
if self.logLevel <= level {
|
||||
self.handler.log(level: level,
|
||||
message: message(),
|
||||
metadata: metadata(),
|
||||
source: source() ?? Logger.currentModule(fileID: (file)),
|
||||
file: file, function: function, line: line)
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
@inlinable
|
||||
public func log(level: Logger.Level,
|
||||
_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
@@ -76,6 +99,41 @@ extension Logger {
|
||||
file: file, function: function, line: line)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Log a message passing the log level as a parameter.
|
||||
///
|
||||
/// If the `logLevel` passed to this method is more severe than the `Logger`'s `logLevel`, it will be logged,
|
||||
/// otherwise nothing will happen.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - level: The log level to log `message` at. For the available log levels, see `Logger.Level`.
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message.
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func log(level: Logger.Level,
|
||||
_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: level, message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
@inlinable
|
||||
public func log(level: Logger.Level,
|
||||
_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: level, message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Add, change, or remove a logging metadata item.
|
||||
///
|
||||
@@ -94,7 +152,7 @@ extension Logger {
|
||||
/// Get or set the log level configured for this `Logger`.
|
||||
///
|
||||
/// - note: `Logger`s treat `logLevel` as a value. This means that a change in `logLevel` will only affect this
|
||||
/// very `Logger`. It it acceptable for logging backends to have some form of global log level override
|
||||
/// very `Logger`. It is 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
|
||||
@@ -117,14 +175,26 @@ extension Logger {
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message
|
||||
/// - source: The source this log messages originates to. Currently, it defaults to the folder containing the
|
||||
/// file that is emitting the log message, which usually is the module.
|
||||
/// - source: The source this log messages originates from. Defaults
|
||||
/// to the module emitting the log message (on Swift 5.3 or
|
||||
/// newer and the folder name containing the log emitting file on Swift 5.2 or
|
||||
/// older).
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func trace(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
source: @autoclosure () -> String? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: .trace, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
@inlinable
|
||||
public func trace(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
@@ -132,6 +202,36 @@ extension Logger {
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: .trace, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// If `.trace` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
|
||||
/// otherwise nothing will happen.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func trace(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.trace(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
@inlinable
|
||||
public func trace(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.trace(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Log a message passing with the `Logger.Level.debug` log level.
|
||||
///
|
||||
@@ -141,21 +241,64 @@ extension Logger {
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message.
|
||||
/// - source: The source this log messages originates to. Currently, it defaults to the folder containing the
|
||||
/// file that is emitting the log message, which usually is the module.
|
||||
/// - source: The source this log messages originates from. Defaults
|
||||
/// to the module emitting the log message (on Swift 5.3 or
|
||||
/// newer and the folder name containing the log emitting file on Swift 5.2 or
|
||||
/// older).
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func debug(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
source: @autoclosure () -> String? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: .debug, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
public func debug(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
source: @autoclosure () -> String? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: .debug, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Log a message passing with the `Logger.Level.debug` log level.
|
||||
///
|
||||
/// If `.debug` is at least as severe as the `Logger`'s `logLevel`, it will be logged,
|
||||
/// otherwise nothing will happen.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message.
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func debug(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.debug(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
@inlinable
|
||||
public func debug(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.debug(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Log a message passing with the `Logger.Level.info` log level.
|
||||
///
|
||||
@@ -165,14 +308,26 @@ extension Logger {
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message.
|
||||
/// - source: The source this log messages originates to. Currently, it defaults to the folder containing the
|
||||
/// file that is emitting the log message, which usually is the module.
|
||||
/// - source: The source this log messages originates from. Defaults
|
||||
/// to the module emitting the log message (on Swift 5.3 or
|
||||
/// newer and the folder name containing the log emitting file on Swift 5.2 or
|
||||
/// older).
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func info(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
source: @autoclosure () -> String? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: .info, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
@inlinable
|
||||
public func info(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
@@ -180,6 +335,38 @@ extension Logger {
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: .info, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// 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,
|
||||
/// otherwise nothing will happen.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message.
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func info(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.info(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
@inlinable
|
||||
public func info(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.info(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Log a message passing with the `Logger.Level.notice` log level.
|
||||
///
|
||||
@@ -189,14 +376,26 @@ extension Logger {
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message.
|
||||
/// - source: The source this log messages originates to. Currently, it defaults to the folder containing the
|
||||
/// file that is emitting the log message, which usually is the module.
|
||||
/// - source: The source this log messages originates from. Defaults
|
||||
/// to the module emitting the log message (on Swift 5.3 or
|
||||
/// newer and the folder name containing the log emitting file on Swift 5.2 or
|
||||
/// older).
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func notice(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
source: @autoclosure () -> String? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: .notice, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
@inlinable
|
||||
public func notice(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
@@ -204,6 +403,41 @@ extension Logger {
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: .notice, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// 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,
|
||||
/// otherwise nothing will happen.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message.
|
||||
/// - source: The source this log messages originates from. Defaults
|
||||
/// to the module emitting the log message (on Swift 5.3 or
|
||||
/// newer and the folder name containing the log emitting file on Swift 5.2 or
|
||||
/// older).
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func notice(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.notice(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
public func notice(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.notice(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Log a message passing with the `Logger.Level.warning` log level.
|
||||
///
|
||||
@@ -213,14 +447,26 @@ extension Logger {
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message.
|
||||
/// - source: The source this log messages originates to. Currently, it defaults to the folder containing the
|
||||
/// file that is emitting the log message, which usually is the module.
|
||||
/// - source: The source this log messages originates from. Defaults
|
||||
/// to the module emitting the log message (on Swift 5.3 or
|
||||
/// newer and the folder name containing the log emitting file on Swift 5.2 or
|
||||
/// older).
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func warning(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
source: @autoclosure () -> String? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: .warning, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
@inlinable
|
||||
public func warning(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
@@ -228,6 +474,38 @@ extension Logger {
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: .warning, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// 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,
|
||||
/// otherwise nothing will happen.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message.
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func warning(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.warning(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
@inlinable
|
||||
public func warning(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.warning(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Log a message passing with the `Logger.Level.error` log level.
|
||||
///
|
||||
@@ -237,14 +515,26 @@ extension Logger {
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message.
|
||||
/// - source: The source this log messages originates to. Currently, it defaults to the folder containing the
|
||||
/// file that is emitting the log message, which usually is the module.
|
||||
/// - source: The source this log messages originates from. Defaults
|
||||
/// to the module emitting the log message (on Swift 5.3 or
|
||||
/// newer and the folder name containing the log emitting file on Swift 5.2 or
|
||||
/// older).
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func error(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
source: @autoclosure () -> String? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: .error, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
@inlinable
|
||||
public func error(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
@@ -252,6 +542,38 @@ extension Logger {
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: .error, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// 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,
|
||||
/// otherwise nothing will happen.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message.
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func error(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.error(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
@inlinable
|
||||
public func error(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.error(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Log a message passing with the `Logger.Level.critical` log level.
|
||||
///
|
||||
@@ -260,14 +582,26 @@ extension Logger {
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message.
|
||||
/// - source: The source this log messages originates to. Currently, it defaults to the folder containing the
|
||||
/// file that is emitting the log message, which usually is the module.
|
||||
/// - source: The source this log messages originates from. Defaults
|
||||
/// to the module emitting the log message (on Swift 5.3 or
|
||||
/// newer and the folder name containing the log emitting file on Swift 5.2 or
|
||||
/// older).
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#file`).
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func critical(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
source: @autoclosure () -> String? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: .critical, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
@inlinable
|
||||
public func critical(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
@@ -275,13 +609,52 @@ extension Logger {
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.log(level: .critical, message(), metadata: metadata(), source: source(), file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Log a message passing with the `Logger.Level.critical` log level.
|
||||
///
|
||||
/// `.critical` messages will always be logged.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - message: The message to be logged. `message` can be used with any string interpolation literal.
|
||||
/// - metadata: One-off metadata to attach to this log message.
|
||||
/// - source: The source this log messages originates from. Defaults
|
||||
/// to the module emitting the log message (on Swift 5.3 or
|
||||
/// newer and the folder name containing the log emitting file on Swift 5.2 or
|
||||
/// older).
|
||||
/// - file: The file this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#fileID` (on Swift 5.3 or newer and `#file` on Swift 5.2 or older).
|
||||
/// - function: The function this log message originates from (there's usually no need to pass it explicitly as
|
||||
/// it defaults to `#function`).
|
||||
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
|
||||
/// defaults to `#line`).
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
public func critical(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.critical(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
@inlinable
|
||||
public func critical(_ message: @autoclosure () -> Logger.Message,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.critical(message(), metadata: metadata(), source: nil, file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// The `LoggingSystem` is a global facility where the default logging backend implementation (`LogHandler`) can be
|
||||
/// configured. `LoggingSystem` is set up just once in a given program to set up the desired logging backend
|
||||
/// implementation.
|
||||
public enum LoggingSystem {
|
||||
#if canImport(WASILibc)
|
||||
// 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
|
||||
|
||||
@@ -292,18 +665,28 @@ public enum LoggingSystem {
|
||||
/// - parameters:
|
||||
/// - factory: A closure that given a `Logger` identifier, produces an instance of the `LogHandler`.
|
||||
public static func bootstrap(_ factory: @escaping (String) -> LogHandler) {
|
||||
#if canImport(WASILibc)
|
||||
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
|
||||
internal static func bootstrapInternal(_ factory: @escaping (String) -> LogHandler) {
|
||||
#if canImport(WASILibc)
|
||||
self.factory = factory
|
||||
#else
|
||||
self.lock.withWriterLock {
|
||||
self.factory = factory
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -391,7 +774,11 @@ extension Logger {
|
||||
/// - parameters:
|
||||
/// - label: An identifier for the creator of a `Logger`.
|
||||
public init(label: String) {
|
||||
#if canImport(WASILibc)
|
||||
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`.
|
||||
@@ -607,28 +994,36 @@ public struct MultiplexLogHandler: LogHandler {
|
||||
/// ensures access to the underlying `FILE` is locked to prevent
|
||||
/// cross-thread interleaving of output.
|
||||
internal struct StdioOutputStream: TextOutputStream {
|
||||
#if canImport(WASILibc)
|
||||
internal let file: OpaquePointer
|
||||
#else
|
||||
internal let file: UnsafeMutablePointer<FILE>
|
||||
#endif
|
||||
internal let flushMode: FlushMode
|
||||
|
||||
internal func write(_ string: String) {
|
||||
string.withCString { ptr in
|
||||
self.contiguousUTF8(string).withContiguousStorageIfAvailable { utf8Bytes in
|
||||
#if os(Windows)
|
||||
_lock_file(self.file)
|
||||
#elseif canImport(WASILibc)
|
||||
// no file locking on WASI
|
||||
#else
|
||||
flockfile(self.file)
|
||||
#endif
|
||||
defer {
|
||||
#if os(Windows)
|
||||
_unlock_file(self.file)
|
||||
#elseif canImport(WASILibc)
|
||||
// no file locking on WASI
|
||||
#else
|
||||
funlockfile(self.file)
|
||||
#endif
|
||||
}
|
||||
_ = fputs(ptr, self.file)
|
||||
_ = fwrite(utf8Bytes.baseAddress!, 1, utf8Bytes.count, self.file)
|
||||
if case .always = self.flushMode {
|
||||
self.flush()
|
||||
}
|
||||
}
|
||||
}!
|
||||
}
|
||||
|
||||
/// Flush the underlying stream.
|
||||
@@ -637,6 +1032,16 @@ internal struct StdioOutputStream: TextOutputStream {
|
||||
_ = fflush(self.file)
|
||||
}
|
||||
|
||||
internal func contiguousUTF8(_ string: String) -> String.UTF8View {
|
||||
var contiguousString = string
|
||||
#if compiler(>=5.1)
|
||||
contiguousString.makeContiguousUTF8()
|
||||
#else
|
||||
contiguousString = string + ""
|
||||
#endif
|
||||
return contiguousString.utf8
|
||||
}
|
||||
|
||||
internal static let stderr = StdioOutputStream(file: systemStderr, flushMode: .always)
|
||||
internal static let stdout = StdioOutputStream(file: systemStdout, flushMode: .always)
|
||||
|
||||
@@ -652,11 +1057,16 @@ internal struct StdioOutputStream: TextOutputStream {
|
||||
let systemStderr = Darwin.stderr
|
||||
let systemStdout = Darwin.stdout
|
||||
#elseif os(Windows)
|
||||
let systemStderr = MSVCRT.stderr
|
||||
let systemStdout = MSVCRT.stdout
|
||||
#else
|
||||
let systemStderr = CRT.stderr
|
||||
let systemStdout = CRT.stdout
|
||||
#elseif canImport(Glibc)
|
||||
let systemStderr = Glibc.stderr!
|
||||
let systemStdout = Glibc.stdout!
|
||||
#elseif canImport(WASILibc)
|
||||
let systemStderr = WASILibc.stderr!
|
||||
let systemStdout = WASILibc.stdout!
|
||||
#else
|
||||
#error("Unsupported runtime")
|
||||
#endif
|
||||
|
||||
/// `StreamLogHandler` is a simple implementation of `LogHandler` for directing
|
||||
@@ -711,18 +1121,30 @@ public struct StreamLogHandler: LogHandler {
|
||||
: self.prettify(self.metadata.merging(metadata!, uniquingKeysWith: { _, new in new }))
|
||||
|
||||
var stream = self.stream
|
||||
stream.write("\(self.timestamp()) \(level) \(self.label) :\(prettyMetadata.map { " \($0)" } ?? "") \(message)\n")
|
||||
stream.write("\(self.timestamp()) \(level) \(self.label) :\(prettyMetadata.map { " \($0)" } ?? "") [\(source)] \(message)\n")
|
||||
}
|
||||
|
||||
private func prettify(_ metadata: Logger.Metadata) -> String? {
|
||||
return !metadata.isEmpty ? metadata.map { "\($0)=\($1)" }.joined(separator: " ") : nil
|
||||
return !metadata.isEmpty
|
||||
? metadata.lazy.sorted(by: { $0.key < $1.key }).map { "\($0)=\($1)" }.joined(separator: " ")
|
||||
: nil
|
||||
}
|
||||
|
||||
private func timestamp() -> String {
|
||||
var buffer = [Int8](repeating: 0, count: 255)
|
||||
#if os(Windows)
|
||||
var timestamp: __time64_t = __time64_t()
|
||||
_ = _time64(×tamp)
|
||||
|
||||
var localTime: tm = tm()
|
||||
_ = _localtime64_s(&localTime, ×tamp)
|
||||
|
||||
_ = strftime(&buffer, buffer.count, "%Y-%m-%dT%H:%M:%S%z", &localTime)
|
||||
#else
|
||||
var timestamp = time(nil)
|
||||
let localTime = localtime(×tamp)
|
||||
strftime(&buffer, buffer.count, "%Y-%m-%dT%H:%M:%S%z", localTime)
|
||||
#endif
|
||||
return buffer.withUnsafeBufferPointer {
|
||||
$0.withMemoryRebound(to: CChar.self) {
|
||||
String(cString: $0.baseAddress!)
|
||||
@@ -735,6 +1157,8 @@ public struct StreamLogHandler: LogHandler {
|
||||
public struct SwiftLogNoOpLogHandler: LogHandler {
|
||||
public init() {}
|
||||
|
||||
public init(_: String) {}
|
||||
|
||||
@inlinable public func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, file: String, function: String, line: UInt) {}
|
||||
|
||||
@inlinable public subscript(metadataKey _: String) -> Logger.Metadata.Value? {
|
||||
@@ -771,6 +1195,18 @@ extension Logger {
|
||||
String($0)
|
||||
} ?? "n/a"
|
||||
}
|
||||
|
||||
#if compiler(>=5.3)
|
||||
@inlinable
|
||||
internal static func currentModule(fileID: String = #fileID) -> String {
|
||||
let utf8All = fileID.utf8
|
||||
if let slashIndex = utf8All.firstIndex(of: UInt8(ascii: "/")) {
|
||||
return String(fileID[..<slashIndex])
|
||||
} else {
|
||||
return "n/a"
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Extension has to be done on explicit type rather than Logger.Metadata.Value as workaround for
|
||||
|
||||
@@ -22,7 +22,7 @@ import XCTest
|
||||
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
|
||||
///
|
||||
|
||||
#if os(Linux) || os(FreeBSD)
|
||||
#if os(Linux) || os(FreeBSD) || os(Windows)
|
||||
@testable import LoggingTests
|
||||
|
||||
XCTMain([
|
||||
|
||||
@@ -39,6 +39,9 @@ extension LoggingTest {
|
||||
("testCustomFactory", testCustomFactory),
|
||||
("testAllLogLevelsExceptCriticalCanBeBlocked", testAllLogLevelsExceptCriticalCanBeBlocked),
|
||||
("testAllLogLevelsWork", testAllLogLevelsWork),
|
||||
("testAllLogLevelByFunctionRefWithSource", testAllLogLevelByFunctionRefWithSource),
|
||||
("testAllLogLevelByFunctionRefWithoutSource", testAllLogLevelByFunctionRefWithoutSource),
|
||||
("testLogsEmittedFromSubdirectoryGetCorrectModuleInNewerSwifts", testLogsEmittedFromSubdirectoryGetCorrectModuleInNewerSwifts),
|
||||
("testLogMessageWithStringInterpolation", testLogMessageWithStringInterpolation),
|
||||
("testLoggingAString", testLoggingAString),
|
||||
("testMultiplexerIsValue", testMultiplexerIsValue),
|
||||
@@ -48,6 +51,8 @@ extension LoggingTest {
|
||||
("testStreamLogHandlerWritesToAStream", testStreamLogHandlerWritesToAStream),
|
||||
("testStreamLogHandlerOutputFormat", testStreamLogHandlerOutputFormat),
|
||||
("testStreamLogHandlerOutputFormatWithMetaData", testStreamLogHandlerOutputFormatWithMetaData),
|
||||
("testStreamLogHandlerOutputFormatWithOrderedMetadata", testStreamLogHandlerOutputFormatWithOrderedMetadata),
|
||||
("testStdioOutputStreamWrite", testStdioOutputStreamWrite),
|
||||
("testStdioOutputStreamFlush", testStdioOutputStreamFlush),
|
||||
("testOverloadingError", testOverloadingError),
|
||||
]
|
||||
|
||||
@@ -16,6 +16,8 @@ import XCTest
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
|
||||
import Darwin
|
||||
#elseif os(Windows)
|
||||
import WinSDK
|
||||
#else
|
||||
import Glibc
|
||||
#endif
|
||||
@@ -301,7 +303,7 @@ class LoggingTest: XCTestCase {
|
||||
}
|
||||
|
||||
private func dontEvaluateThisString(file: StaticString = #file, line: UInt = #line) -> Logger.Message {
|
||||
XCTFail("should not have been evaluated", file: (file), line: line)
|
||||
XCTFail("should not have been evaluated", file: file, line: line)
|
||||
return "should not have been evaluated"
|
||||
}
|
||||
|
||||
@@ -408,6 +410,104 @@ class LoggingTest: XCTestCase {
|
||||
testLogging.history.assertExist(level: .critical, message: "yes: critical")
|
||||
}
|
||||
|
||||
func testAllLogLevelByFunctionRefWithSource() {
|
||||
let testLogging = TestLogging()
|
||||
LoggingSystem.bootstrapInternal(testLogging.make)
|
||||
|
||||
var logger = Logger(label: "\(#function)")
|
||||
logger.logLevel = .trace
|
||||
|
||||
let trace = logger.trace(_:metadata:source:file:function:line:)
|
||||
let debug = logger.debug(_:metadata:source:file:function:line:)
|
||||
let info = logger.info(_:metadata:source:file:function:line:)
|
||||
let notice = logger.notice(_:metadata:source:file:function:line:)
|
||||
let warning = logger.warning(_:metadata:source:file:function:line:)
|
||||
let error = logger.error(_:metadata:source:file:function:line:)
|
||||
let critical = logger.critical(_:metadata:source:file:function:line:)
|
||||
|
||||
trace("yes: trace", [:], "foo", #file, #function, #line)
|
||||
debug("yes: debug", [:], "foo", #file, #function, #line)
|
||||
info("yes: info", [:], "foo", #file, #function, #line)
|
||||
notice("yes: notice", [:], "foo", #file, #function, #line)
|
||||
warning("yes: warning", [:], "foo", #file, #function, #line)
|
||||
error("yes: error", [:], "foo", #file, #function, #line)
|
||||
critical("yes: critical", [:], "foo", #file, #function, #line)
|
||||
|
||||
testLogging.history.assertExist(level: .trace, message: "yes: trace", source: "foo")
|
||||
testLogging.history.assertExist(level: .debug, message: "yes: debug", source: "foo")
|
||||
testLogging.history.assertExist(level: .info, message: "yes: info", source: "foo")
|
||||
testLogging.history.assertExist(level: .notice, message: "yes: notice", source: "foo")
|
||||
testLogging.history.assertExist(level: .warning, message: "yes: warning", source: "foo")
|
||||
testLogging.history.assertExist(level: .error, message: "yes: error", source: "foo")
|
||||
testLogging.history.assertExist(level: .critical, message: "yes: critical", source: "foo")
|
||||
}
|
||||
|
||||
func testAllLogLevelByFunctionRefWithoutSource() {
|
||||
let testLogging = TestLogging()
|
||||
LoggingSystem.bootstrapInternal(testLogging.make)
|
||||
|
||||
var logger = Logger(label: "\(#function)")
|
||||
logger.logLevel = .trace
|
||||
|
||||
let trace = logger.trace(_:metadata:file:function:line:)
|
||||
let debug = logger.debug(_:metadata:file:function:line:)
|
||||
let info = logger.info(_:metadata:file:function:line:)
|
||||
let notice = logger.notice(_:metadata:file:function:line:)
|
||||
let warning = logger.warning(_:metadata:file:function:line:)
|
||||
let error = logger.error(_:metadata:file:function:line:)
|
||||
let critical = logger.critical(_:metadata:file:function:line:)
|
||||
|
||||
#if compiler(>=5.3)
|
||||
trace("yes: trace", [:], #fileID, #function, #line)
|
||||
debug("yes: debug", [:], #fileID, #function, #line)
|
||||
info("yes: info", [:], #fileID, #function, #line)
|
||||
notice("yes: notice", [:], #fileID, #function, #line)
|
||||
warning("yes: warning", [:], #fileID, #function, #line)
|
||||
error("yes: error", [:], #fileID, #function, #line)
|
||||
critical("yes: critical", [:], #fileID, #function, #line)
|
||||
#else
|
||||
trace("yes: trace", [:], #file, #function, #line)
|
||||
debug("yes: debug", [:], #file, #function, #line)
|
||||
info("yes: info", [:], #file, #function, #line)
|
||||
notice("yes: notice", [:], #file, #function, #line)
|
||||
warning("yes: warning", [:], #file, #function, #line)
|
||||
error("yes: error", [:], #file, #function, #line)
|
||||
critical("yes: critical", [:], #file, #function, #line)
|
||||
#endif
|
||||
|
||||
testLogging.history.assertExist(level: .trace, message: "yes: trace")
|
||||
testLogging.history.assertExist(level: .debug, message: "yes: debug")
|
||||
testLogging.history.assertExist(level: .info, message: "yes: info")
|
||||
testLogging.history.assertExist(level: .notice, message: "yes: notice")
|
||||
testLogging.history.assertExist(level: .warning, message: "yes: warning")
|
||||
testLogging.history.assertExist(level: .error, message: "yes: error")
|
||||
testLogging.history.assertExist(level: .critical, message: "yes: critical")
|
||||
}
|
||||
|
||||
func testLogsEmittedFromSubdirectoryGetCorrectModuleInNewerSwifts() {
|
||||
let testLogging = TestLogging()
|
||||
LoggingSystem.bootstrapInternal(testLogging.make)
|
||||
|
||||
var logger = Logger(label: "\(#function)")
|
||||
logger.logLevel = .trace
|
||||
|
||||
emitLogMessage("hello", to: logger)
|
||||
|
||||
#if compiler(>=5.3)
|
||||
let moduleName = "LoggingTests" // the actual name
|
||||
#else
|
||||
let moduleName = "SubDirectoryOfLoggingTests" // the last path component of `#file` showing the failure mode
|
||||
#endif
|
||||
|
||||
testLogging.history.assertExist(level: .trace, message: "hello", source: moduleName)
|
||||
testLogging.history.assertExist(level: .debug, message: "hello", source: moduleName)
|
||||
testLogging.history.assertExist(level: .info, message: "hello", source: moduleName)
|
||||
testLogging.history.assertExist(level: .notice, message: "hello", source: moduleName)
|
||||
testLogging.history.assertExist(level: .warning, message: "hello", source: moduleName)
|
||||
testLogging.history.assertExist(level: .error, message: "hello", source: moduleName)
|
||||
testLogging.history.assertExist(level: .critical, message: "hello", source: moduleName)
|
||||
}
|
||||
|
||||
func testLogMessageWithStringInterpolation() {
|
||||
let testLogging = TestLogging()
|
||||
LoggingSystem.bootstrapInternal(testLogging.make)
|
||||
@@ -605,12 +705,13 @@ class LoggingTest: XCTestCase {
|
||||
LoggingSystem.bootstrapInternal { _ in
|
||||
StreamLogHandler(label: label, stream: interceptStream)
|
||||
}
|
||||
let source = "testSource"
|
||||
let log = Logger(label: label)
|
||||
|
||||
let testString = "my message is better than yours"
|
||||
log.critical("\(testString)")
|
||||
log.critical("\(testString)", source: source)
|
||||
|
||||
let pattern = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\+|-)\\d{4}\\s\(Logger.Level.critical)\\s\(label)\\s:\\s\(testString)$"
|
||||
let pattern = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\+|-)\\d{4}\\s\(Logger.Level.critical)\\s\(label)\\s:\\s\\[\(source)\\]\\s\(testString)$"
|
||||
|
||||
let messageSucceeded = interceptStream.interceptedText?.trimmingCharacters(in: .whitespacesAndNewlines).range(of: pattern, options: .regularExpression) != nil
|
||||
|
||||
@@ -624,12 +725,13 @@ class LoggingTest: XCTestCase {
|
||||
LoggingSystem.bootstrapInternal { _ in
|
||||
StreamLogHandler(label: label, stream: interceptStream)
|
||||
}
|
||||
let source = "testSource"
|
||||
let log = Logger(label: label)
|
||||
|
||||
let testString = "my message is better than yours"
|
||||
log.critical("\(testString)", metadata: ["test": "test"])
|
||||
log.critical("\(testString)", metadata: ["test": "test"], source: source)
|
||||
|
||||
let pattern = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\+|-)\\d{4}\\s\(Logger.Level.critical)\\s\(label)\\s:\\stest=test\\s\(testString)$"
|
||||
let pattern = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\+|-)\\d{4}\\s\(Logger.Level.critical)\\s\(label)\\s:\\stest=test\\s\\[\(source)\\]\\s\(testString)$"
|
||||
|
||||
let messageSucceeded = interceptStream.interceptedText?.trimmingCharacters(in: .whitespacesAndNewlines).range(of: pattern, options: .regularExpression) != nil
|
||||
|
||||
@@ -637,6 +739,44 @@ class LoggingTest: XCTestCase {
|
||||
XCTAssertEqual(interceptStream.strings.count, 1)
|
||||
}
|
||||
|
||||
func testStreamLogHandlerOutputFormatWithOrderedMetadata() {
|
||||
let interceptStream = InterceptStream()
|
||||
let label = "testLabel"
|
||||
LoggingSystem.bootstrapInternal { _ in
|
||||
StreamLogHandler(label: label, stream: interceptStream)
|
||||
}
|
||||
let log = Logger(label: label)
|
||||
|
||||
let testString = "my message is better than yours"
|
||||
log.critical("\(testString)", metadata: ["a": "a0", "b": "b0"])
|
||||
log.critical("\(testString)", metadata: ["b": "b1", "a": "a1"])
|
||||
|
||||
XCTAssertEqual(interceptStream.strings.count, 2)
|
||||
guard interceptStream.strings.count == 2 else {
|
||||
XCTFail("Intercepted \(interceptStream.strings.count) logs, expected 2")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssert(interceptStream.strings[0].contains("a=a0 b=b0"))
|
||||
XCTAssert(interceptStream.strings[1].contains("a=a1 b=b1"))
|
||||
}
|
||||
|
||||
func testStdioOutputStreamWrite() {
|
||||
self.withWriteReadFDsAndReadBuffer { writeFD, readFD, readBuffer in
|
||||
let logStream = StdioOutputStream(file: writeFD, flushMode: .always)
|
||||
LoggingSystem.bootstrapInternal { StreamLogHandler(label: $0, stream: logStream) }
|
||||
let log = Logger(label: "test")
|
||||
let testString = "hello\u{0} world"
|
||||
log.critical("\(testString)")
|
||||
|
||||
let size = read(readFD, readBuffer, 256)
|
||||
|
||||
let output = String(decoding: UnsafeRawBufferPointer(start: UnsafeRawPointer(readBuffer), count: numericCast(size)), as: UTF8.self)
|
||||
let messageSucceeded = output.trimmingCharacters(in: .whitespacesAndNewlines).hasSuffix(testString)
|
||||
XCTAssertTrue(messageSucceeded)
|
||||
}
|
||||
}
|
||||
|
||||
func testStdioOutputStreamFlush() {
|
||||
// flush on every statement
|
||||
self.withWriteReadFDsAndReadBuffer { writeFD, readFD, readBuffer in
|
||||
@@ -668,10 +808,17 @@ class LoggingTest: XCTestCase {
|
||||
|
||||
func withWriteReadFDsAndReadBuffer(_ body: (UnsafeMutablePointer<FILE>, CInt, UnsafeMutablePointer<Int8>) -> Void) {
|
||||
var fds: [Int32] = [-1, -1]
|
||||
#if os(Windows)
|
||||
fds.withUnsafeMutableBufferPointer {
|
||||
let err = _pipe($0.baseAddress, 256, _O_BINARY)
|
||||
XCTAssertEqual(err, 0, "_pipe failed \(err)")
|
||||
}
|
||||
#else
|
||||
fds.withUnsafeMutableBufferPointer { ptr in
|
||||
let err = pipe(ptr.baseAddress!)
|
||||
XCTAssertEqual(err, 0, "pipe faild \(err)")
|
||||
XCTAssertEqual(err, 0, "pipe failed \(err)")
|
||||
}
|
||||
#endif
|
||||
|
||||
let writeFD = fdopen(fds[1], "w")
|
||||
let writeBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: 256)
|
||||
@@ -681,11 +828,20 @@ class LoggingTest: XCTestCase {
|
||||
}
|
||||
|
||||
var err = setvbuf(writeFD, writeBuffer, _IOFBF, 256)
|
||||
XCTAssertEqual(err, 0, "setvbuf faild \(err)")
|
||||
XCTAssertEqual(err, 0, "setvbuf failed \(err)")
|
||||
|
||||
let readFD = fds[0]
|
||||
#if os(Windows)
|
||||
let hPipe: HANDLE = HANDLE(bitPattern: _get_osfhandle(readFD))!
|
||||
XCTAssertFalse(hPipe == INVALID_HANDLE_VALUE)
|
||||
|
||||
var dwMode: DWORD = DWORD(PIPE_NOWAIT)
|
||||
let bSucceeded = SetNamedPipeHandleState(hPipe, &dwMode, nil, nil)
|
||||
XCTAssertTrue(bSucceeded)
|
||||
#else
|
||||
err = fcntl(readFD, F_SETFL, fcntl(readFD, F_GETFL) | O_NONBLOCK)
|
||||
XCTAssertEqual(err, 0, "fcntl faild \(err)")
|
||||
XCTAssertEqual(err, 0, "fcntl failed \(err)")
|
||||
#endif
|
||||
|
||||
let readBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: 256)
|
||||
defer {
|
||||
@@ -711,16 +867,25 @@ class LoggingTest: XCTestCase {
|
||||
|
||||
var logger = Logger(label: "test")
|
||||
logger.logLevel = .error
|
||||
logger.error(Dummy())
|
||||
logger.error(error: Dummy())
|
||||
|
||||
logging.history.assertExist(level: .error, message: "errorDescription")
|
||||
}
|
||||
}
|
||||
|
||||
extension Logger {
|
||||
public func error(_ error: Error,
|
||||
#if compiler(>=5.3)
|
||||
public func error(error: Error,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #fileID, function: String = #function, line: UInt = #line) {
|
||||
self.error("\(error.localizedDescription)", metadata: metadata(), file: file, function: function, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
public func error(error: Error,
|
||||
metadata: @autoclosure () -> Logger.Metadata? = nil,
|
||||
file: String = #file, function: String = #function, line: UInt = #line) {
|
||||
self.error("\(error.localizedDescription)", metadata: metadata(), file: file, function: function, line: line)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Logging API open source project
|
||||
//
|
||||
// Copyright (c) 2021 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 Logging
|
||||
|
||||
internal func emitLogMessage(_ message: Logger.Message, to logger: Logger) {
|
||||
logger.trace(message)
|
||||
logger.debug(message)
|
||||
logger.info(message)
|
||||
logger.notice(message)
|
||||
logger.warning(message)
|
||||
logger.error(message)
|
||||
logger.critical(message)
|
||||
}
|
||||
@@ -14,6 +14,9 @@
|
||||
import Foundation
|
||||
@testable import Logging
|
||||
import XCTest
|
||||
#if os(Windows)
|
||||
import WinSDK
|
||||
#endif
|
||||
|
||||
internal struct TestLogging {
|
||||
private let _config = Config() // shared among loggers
|
||||
@@ -174,6 +177,34 @@ internal struct LogEntry {
|
||||
}
|
||||
|
||||
extension History {
|
||||
#if compiler(>=5.3)
|
||||
func assertExist(level: Logger.Level,
|
||||
message: String,
|
||||
metadata: Logger.Metadata? = nil,
|
||||
source: String? = nil,
|
||||
file: StaticString = #file,
|
||||
fileID: String = #fileID,
|
||||
line: UInt = #line) {
|
||||
let source = source ?? Logger.currentModule(fileID: "\(fileID)")
|
||||
let entry = self.find(level: level, message: message, metadata: metadata, source: source)
|
||||
XCTAssertNotNil(entry, "entry not found: \(level), \(source), \(String(describing: metadata)), \(message)",
|
||||
file: file, line: line)
|
||||
}
|
||||
|
||||
func assertNotExist(level: Logger.Level,
|
||||
message: String,
|
||||
metadata: Logger.Metadata? = nil,
|
||||
source: String? = nil,
|
||||
file: StaticString = #file,
|
||||
fileID: String = #file,
|
||||
line: UInt = #line) {
|
||||
let source = source ?? Logger.currentModule(fileID: "\(fileID)")
|
||||
let entry = self.find(level: level, message: message, metadata: metadata, source: source)
|
||||
XCTAssertNil(entry, "entry was found: \(level), \(source), \(String(describing: metadata)), \(message)",
|
||||
file: file, line: line)
|
||||
}
|
||||
|
||||
#else
|
||||
func assertExist(level: Logger.Level,
|
||||
message: String,
|
||||
metadata: Logger.Metadata? = nil,
|
||||
@@ -183,7 +214,7 @@ extension History {
|
||||
let source = source ?? Logger.currentModule(filePath: "\(file)")
|
||||
let entry = self.find(level: level, message: message, metadata: metadata, source: source)
|
||||
XCTAssertNotNil(entry, "entry not found: \(level), \(source), \(String(describing: metadata)), \(message)",
|
||||
file: (file), line: line)
|
||||
file: file, line: line)
|
||||
}
|
||||
|
||||
func assertNotExist(level: Logger.Level,
|
||||
@@ -195,8 +226,9 @@ extension History {
|
||||
let source = source ?? Logger.currentModule(filePath: "\(file)")
|
||||
let entry = self.find(level: level, message: message, metadata: metadata, source: source)
|
||||
XCTAssertNil(entry, "entry was found: \(level), \(source), \(String(describing: metadata)), \(message)",
|
||||
file: (file), line: line)
|
||||
file: file, line: line)
|
||||
}
|
||||
#endif
|
||||
|
||||
func find(level: Logger.Level, message: String, metadata: Logger.Metadata? = nil, source: String) -> LogEntry? {
|
||||
return self.entries.first { entry in
|
||||
@@ -270,6 +302,8 @@ public class MDC {
|
||||
private var threadId: Int {
|
||||
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
|
||||
return Int(pthread_mach_thread_np(pthread_self()))
|
||||
#elseif os(Windows)
|
||||
return Int(GetCurrentThreadId())
|
||||
#else
|
||||
return Int(pthread_self())
|
||||
#endif
|
||||
|
||||
+4
-6
@@ -17,17 +17,15 @@ RUN apt-get update && apt-get install -y wget
|
||||
RUN apt-get update && apt-get install -y lsof dnsutils netcat-openbsd net-tools curl jq # used by integration tests
|
||||
|
||||
# ruby and jazzy for docs generation
|
||||
RUN apt-get update && apt-get install -y ruby ruby-dev libsqlite3-dev
|
||||
RUN gem install jazzy --no-ri --no-rdoc
|
||||
RUN apt-get update && apt-get install -y ruby ruby-dev libsqlite3-dev build-essential
|
||||
# jazzy no longer works on xenial as ruby is too old.
|
||||
RUN if [ "${ubuntu_version}" = "focal" ] ; then echo "gem: --no-document" > ~/.gemrc; fi
|
||||
RUN if [ "${ubuntu_version}" = "focal" ] ; then gem install jazzy; fi
|
||||
|
||||
# tools
|
||||
RUN mkdir -p $HOME/.tools
|
||||
RUN echo 'export PATH="$HOME/.tools:$PATH"' >> $HOME/.profile
|
||||
|
||||
# script to allow mapping framepointers on linux (until part of the toolchain)
|
||||
RUN wget -q https://raw.githubusercontent.com/apple/swift/master/utils/symbolicate-linux-fatal -O $HOME/.tools/symbolicate-linux-fatal
|
||||
RUN chmod 755 $HOME/.tools/symbolicate-linux-fatal
|
||||
|
||||
# swiftformat (until part of the toolchain)
|
||||
|
||||
ARG swiftformat_version=0.44.6
|
||||
|
||||
@@ -6,7 +6,8 @@ services:
|
||||
image: swift-log:18.04-5.3
|
||||
build:
|
||||
args:
|
||||
base_image: "swiftlang/swift:nightly-5.3-bionic"
|
||||
ubuntu_version: "bionic"
|
||||
swift_version: "5.3"
|
||||
|
||||
test:
|
||||
image: swift-log:18.04-5.3
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
|
||||
runtime-setup:
|
||||
image: swift-log:20.04-5.4
|
||||
build:
|
||||
args:
|
||||
ubuntu_version: "focal"
|
||||
swift_version: "5.4"
|
||||
|
||||
test:
|
||||
image: swift-log:20.04-5.4
|
||||
environment: []
|
||||
#- SANITIZER_ARG=--sanitize=thread
|
||||
|
||||
shell:
|
||||
image: swift-log:20.04-5.4
|
||||
@@ -0,0 +1,17 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
|
||||
runtime-setup:
|
||||
image: swift-log:20.04-main
|
||||
build:
|
||||
args:
|
||||
base_image: "swiftlang/swift:nightly-main-focal"
|
||||
|
||||
test:
|
||||
image: swift-log:20.04-main
|
||||
environment: []
|
||||
#- SANITIZER_ARG=--sanitize=thread
|
||||
|
||||
shell:
|
||||
image: swift-log:20.04-main
|
||||
@@ -22,9 +22,9 @@ services:
|
||||
- CAP_NET_RAW
|
||||
- CAP_NET_BIND_SERVICE
|
||||
|
||||
sanity:
|
||||
soundness:
|
||||
<<: *common
|
||||
command: /bin/bash -xcl "./scripts/sanity.sh"
|
||||
command: /bin/bash -xcl "./scripts/soundness.sh"
|
||||
|
||||
test:
|
||||
<<: *common
|
||||
|
||||
@@ -94,7 +94,7 @@ def createLinuxMain(testsDirectory, allTestSubDirectories, files)
|
||||
file.write header(fileName)
|
||||
file.write "\n"
|
||||
|
||||
file.write "#if os(Linux) || os(FreeBSD)\n"
|
||||
file.write "#if os(Linux) || os(FreeBSD) || os(Windows)\n"
|
||||
for testSubDirectory in allTestSubDirectories.sort { |x, y| x <=> y }
|
||||
file.write '@testable import ' + testSubDirectory + "\n"
|
||||
end
|
||||
|
||||
@@ -47,6 +47,22 @@ else
|
||||
printf "\033[0;32mokay.\033[0m\n"
|
||||
fi
|
||||
|
||||
printf "=> Checking for unacceptable language... "
|
||||
# This greps for unacceptable terminology. The square bracket[s] are so that
|
||||
# "git grep" doesn't find the lines that greps :).
|
||||
unacceptable_terms=(
|
||||
-e blacklis[t]
|
||||
-e whitelis[t]
|
||||
-e slav[e]
|
||||
-e sanit[y]
|
||||
)
|
||||
if git grep --color=never -i "${unacceptable_terms[@]}" > /dev/null; then
|
||||
printf "\033[0;31mUnacceptable language found.\033[0m\n"
|
||||
git grep -i "${unacceptable_terms[@]}"
|
||||
exit 1
|
||||
fi
|
||||
printf "\033[0;32mokay.\033[0m\n"
|
||||
|
||||
printf "=> Checking format... "
|
||||
FIRST_OUT="$(git status --porcelain)"
|
||||
swiftformat . > /dev/null 2>&1
|
||||
@@ -60,7 +76,7 @@ else
|
||||
fi
|
||||
|
||||
printf "=> Checking license headers\n"
|
||||
tmp=$(mktemp /tmp/.swift-log-sanity_XXXXXX)
|
||||
tmp=$(mktemp /tmp/.swift-log-soundness_XXXXXX)
|
||||
|
||||
for language in swift-or-c bash dtrace; do
|
||||
printf " * $language... "
|
||||
Reference in new issue
Block a user