Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[HOLD] Remove Logger.Level numeric values while still supporting Comparable conformance #33

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 34 additions & 13 deletions Sources/Logging/Logging.swift
Expand Up @@ -57,7 +57,7 @@ extension Logger {
_ message: @autoclosure () -> Logger.Message,
metadata: @autoclosure () -> Logger.Metadata? = nil,
file: String = #file, function: String = #function, line: UInt = #line) {
if self.logLevel >= level {
if level >= self.logLevel {
self.handler.log(level: level,
message: message(),
metadata: metadata(),
Expand Down Expand Up @@ -331,37 +331,45 @@ extension Logger {

/// The log level.
///
/// Raw values of log levels correspond to their severity, and are ordered by lowest numeric value (0) being
/// the most severe. The raw values match the syslog values.
public enum Level: Int {
/// The names match those defined by `syslog` while the underlying numeric representation is
/// left as an implementation detail for library authors.
///
/// However, this type does have a defined semantic ordering that is provided through
/// conformance to the `Comparable` protocol.
///
/// i.e. semantically, `debug` is considered lower than `emergency`.
///
/// See [https://en.wikipedia.org/wiki/Syslog](https://en.wikipedia.org/wiki/Syslog) for more
/// detailed information.
public enum Level {
/// Appropriate for messages that contain information normally of use only when
/// debugging a program.
case debug = 7
case debug

/// Appropriate for informational messages.
case info = 6
case info

/// Appropriate for conditions that are not error conditions, but that may require
/// special handling.
case notice = 5
case notice

/// Appropriate for messages that are not error conditions, but more severe than
/// `.notice`.
case warning = 4
case warning

/// Appropriate for error conditions.
case error = 3
case error

/// Appropriate for criticial error conditions that usually require immediate
/// attention.
case critical = 2
case critical

/// Appropriate for conditions that should be corrected immediately, such as a corrupted
/// system database.
case alert = 1
case alert

/// Appropriate for panic conditions.
case emergency = 0
case emergency
}

/// Construct a `Logger` given a `label` identifying the creator of the `Logger`.
Expand Down Expand Up @@ -392,8 +400,21 @@ extension Logger {
}

extension Logger.Level: Comparable {
private var severityOrdering: Int {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will need to become @inlinable

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it? Or just @usableFromInline var ...?

I am probably misunderstanding what the compiler can and can't do - but I assume that the protocol conformance methods are not inlinable, but code within it would be?

switch self {
case .debug: return 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I was very worried about the code-size that this will generate but the Swift compiler is really smart and as long as the order of the cases is aligned with the numbers, this works really well as the compiler can see through this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case .info: return 1
case .notice: return 2
case .warning: return 3
case .error: return 4
case .critical: return 5
case .alert: return 6
case .emergency: return 7
}
}

public static func < (lhs: Logger.Level, rhs: Logger.Level) -> Bool {
return lhs.rawValue < rhs.rawValue
return lhs.severityOrdering < rhs.severityOrdering
}
}

Expand Down