Skip to content

Commit a61b7b1

Browse files
committed
#17: Picking up where We left off
- Addressing PR comments - Cleaning up the code & docs a bit - Adding SwiftUI-style extension statics
1 parent 5d499e0 commit a61b7b1

10 files changed

Lines changed: 251 additions & 124 deletions

Sources/SimpleLogging/CombinedLoggable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import LazyContainers
1111

1212

1313

14-
/// Two lazily-combined loggables
14+
/// Two lazily-combined ``Loggable``s
1515
@usableFromInline
1616
internal struct CombinedLoggable: Loggable {
1717

Sources/SimpleLogging/LogChannel/LogChannel.swift

Lines changed: 30 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@ public struct LogChannel<Location: UnreliableLogChannelLocation>: AnyLogChannel
4040
public var severityFilter: LogSeverityFilter
4141

4242
/// The style of the severity part of a log line in this channel
43-
public let logSeverityNameStyle: SeverityNameStyle
43+
@inline(__always)
44+
public var logSeverityNameStyle: SeverityNameStyle {
45+
get { options.severityStyle }
46+
47+
@available(*, unavailable, message: """
48+
Ability to change a channel's log severity was removed in SimpleLogging 0.6 to reduce complexity and improve performance.
49+
See PR #17: https://github.com/RougeWare/Swift-Simple-Logging/pull/17
50+
""")
51+
set { }
52+
}
4453

4554

4655
/// The logging options to use with each message
@@ -54,10 +63,8 @@ public struct LogChannel<Location: UnreliableLogChannelLocation>: AnyLogChannel
5463
/// - Parameters:
5564
/// - name: The human-readable name of the channel
5665
/// - location: The location where the logs are channeled to
57-
/// - severityFilter: _optional_ - The filter which decides which messages appear in this channel's logs. Defaults to allowing `info` and higher, since `info` is the lowest built-in severity which users might care about if they're looking at logs, but not debugging the code itself.
66+
/// - severityFilter: _optional_ - The filter which decides which messages appear in this channel's logs. Defaults to `.default`
5867
/// - logSeverityNameStyle: _optional_ - The style of the severity names that appear in the log. Defaults to `.default`.
59-
///
60-
/// - Throws: Any error which occurs while trying to create the channel
6168
public init(
6269
name: String,
6370
location: Location,
@@ -67,7 +74,6 @@ public struct LogChannel<Location: UnreliableLogChannelLocation>: AnyLogChannel
6774
self.name = name
6875
self.location = location
6976
self.severityFilter = severityFilter
70-
self.logSeverityNameStyle = logSeverityNameStyle
7177
self.options = LoggingOptions(severityStyle: logSeverityNameStyle)
7278
}
7379

@@ -79,8 +85,6 @@ public struct LogChannel<Location: UnreliableLogChannelLocation>: AnyLogChannel
7985
/// - location: The location to which this channel sends its log messages
8086
/// - lowestAllowedSeverity: _optional_ - The lowest severity which will appear in this channel's logs. Defaults to `defaultFilter`, since that's the lowest built-in severity which users might care about if they're looking at logs, but not debugging the code itself.
8187
/// - logSeverityNameStyle: _optional_ - The style of the severity names that appear in the log. Defaults to `.default`.
82-
///
83-
/// - Throws: Any error which occurs while trying to create the channel
8488
public init(
8589
name: String,
8690
location: Location,
@@ -97,12 +101,16 @@ public struct LogChannel<Location: UnreliableLogChannelLocation>: AnyLogChannel
97101

98102

99103

104+
// MARK: - Appending
105+
100106
public extension LogChannel {
101107

102-
/// Appends the given log message to this channel. If that can't be done, (for example, if the channel's location
103-
/// is a file on a read-only volume) a semantic error is thrown.
108+
/// Appends the given log message to this channel if the severity filter allows. If that can't be done, (for example, if the channel's location
109+
/// is a file on a read-only volume) a description of the problem, along with the logged message,is printed to the destination of Swift's built-in ``print(_:)``.
104110
///
105-
/// - Note: The channel might choose to hold this log message in a buffer, for instance while it waits for a log file to be created
111+
/// Of course, if the severity filter does not allow this message, then no action is taken.
112+
///
113+
/// - Note: The channel might choose to hold this log message in a buffer, for instance while it waits for a log file to be created, or a network call to complete.
106114
///
107115
/// - Parameter message: The message to append to this channel
108116
func append(_ message: LogMessageProtocol) {
@@ -121,74 +129,22 @@ public extension LogChannel {
121129
""")
122130
}
123131
}
124-
}
125-
126-
127-
128-
// MARK: - LogSeverityFilter
129-
130-
/// A filter which can be applied to a log channel to specify which messages are allowed through, based on their severities
131-
//@dynamicMemberLookup // Would love this, but doesn't seem it works quite yet
132-
public enum LogSeverityFilter {
133-
134-
/// All messages are allowed
135-
case allowAll
136-
137-
/// No messages are allowed
138-
case allowNone
139-
140-
/// Only allow messages of this severity
141-
case only(LogSeverity)
142-
143-
/// Only allow messages whose severity is in this range of severities
144-
case range(ClosedRange<LogSeverity>)
145-
146-
/// Allow messages with this severity and higher
147-
case specificAndHigher(lowest: LogSeverity)
148132

149133

150-
151-
// static subscript(dynamicMember keyPath: KeyPath<LogSeverity.Type, LogSeverity>) -> LogSeverityFilter {
152-
// return .specificAndHigher(lowest: LogSeverity.self[keyPath: keyPath])
153-
// }
154-
155-
156-
/// The filter that's used by default, if none is specified
157-
public static var `default`: Self { specificAndHigher(lowest: .info) }
158-
}
159-
160-
161-
162-
public extension LogSeverityFilter {
163-
164-
/// Whether or not this filter allows the given message to be logged
134+
/// Appends the given log message to this channel if the severity filter allows.
165135
///
166-
/// - Parameter message: The message which might be allowed through this filter
167-
/// - Returns: `true` iff the given message is allowed through this filter
168-
@inline(__always)
169-
func allows(_ message: LogMessageProtocol) -> Bool {
170-
allows(message.severity)
171-
}
172-
173-
174-
/// Whether or not this fitler allows messages with the given severity to be logged
136+
/// Of course, if the severity filter does not allow this message, then no action is taken.
175137
///
176-
/// - Parameter severity: A severity to check against this filter
177-
/// - Returns: `true` iff messages with the given severity are allowed through this filter
178-
@inlinable
179-
func allows(_ severity: LogSeverity) -> Bool {
180-
switch self {
181-
case .allowAll: return true
182-
case .allowNone: return false
183-
184-
case .only(severity): return true
185-
case .only(_): return false
186-
187-
case .range(let range):
188-
return range.contains(severity)
189-
190-
case .specificAndHigher(lowest: let lowest):
191-
return lowest <= severity
138+
/// - Note: The channel might choose to hold this log message in a buffer, for instance while it waits for a log file to be created, or a network call to complete.
139+
///
140+
/// - Parameter message: The message to append to this channel
141+
func append(_ message: LogMessageProtocol)
142+
where Location: LogChannelLocation
143+
{
144+
guard severityFilter.allows(message) else {
145+
return
192146
}
147+
148+
location.append(message, options: options)
193149
}
194150
}

Sources/SimpleLogging/LogChannel/LogChannelLocation + Swift.print.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public struct SwiftPrintDefaultLogChannelLocation: SingletonLogChannelLocation {
2525

2626

2727

28+
public extension UnreliableLogChannelLocation where Self == SwiftPrintDefaultLogChannelLocation {
29+
30+
/// The default place to which Swift's `print` function is directed
31+
static var swiftPrint: Self { .shared }
32+
}
33+
34+
35+
2836
public extension LogChannel
2937
where Location == SwiftPrintDefaultLogChannelLocation {
3038

Sources/SimpleLogging/LogChannel/LogChannelLocation + custom.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public struct CustomLogChannelLocation: LogChannelLocation {
2222
/// Create a new custom log channel location
2323
///
2424
/// The function is passed the fully-rendered log line, like
25-
/// `2020-11-20 05:26:49.178Z ⚠️ LogToFileTests.swift:144 testLogOnlyCriticalSeveritiesToFile() This message is a warning`
25+
/// `"2020-11-20 05:26:49.178Z ⚠️ LogToFileTests.swift:144 testLogOnlyCriticalSeveritiesToFile() This message is a warning"`
2626
///
2727
/// - Parameter logger: Passed the fully-rendered log line
2828
init(loggingTo logger: @escaping Callback<String>) {
@@ -37,12 +37,27 @@ public struct CustomLogChannelLocation: LogChannelLocation {
3737

3838

3939

40+
public extension UnreliableLogChannelLocation where Self == CustomLogChannelLocation {
41+
42+
/// Log to a function, so you can implement some custom logging channel without defining a new `struct`.
43+
///
44+
/// The function is passed the fully-rendered log line, like
45+
/// `"2020-11-20 05:26:49.178Z ⚠️ LogToFileTests.swift:144 testLogOnlyCriticalSeveritiesToFile() This message is a warning"`
46+
///
47+
/// - Parameter logger: Passed the fully-rendered log line
48+
static func custom(loggingTo logger: @escaping Callback<String>) -> Self {
49+
.init(loggingTo: logger)
50+
}
51+
}
52+
53+
54+
4055
public extension LogChannel where Location == CustomLogChannelLocation {
4156

4257
/// Log to a function, so you can implement some custom logging channel without defining a new `struct`.
4358
///
4459
/// The function is passed the fully-rendered log line, like
45-
/// `2020-11-20 05:26:49.178Z ⚠️ LogToFileTests.swift:144 testLogOnlyCriticalSeveritiesToFile() This message is a warning`
60+
/// `"2020-11-20 05:26:49.178Z ⚠️ LogToFileTests.swift:144 testLogOnlyCriticalSeveritiesToFile() This message is a warning"`
4661
///
4762
/// - Parameters:
4863
/// - logger: Passed the fully-rendered log line
@@ -61,6 +76,7 @@ public extension LogChannel where Location == CustomLogChannelLocation {
6176
logSeverityNameStyle: logSeverityNameStyle)
6277
}
6378

79+
6480
/// Log to a function, so you can implement some custom logging channel without defining a new `struct`.
6581
///
6682
/// The function is passed the fully-rendered log line, like

0 commit comments

Comments
 (0)