Skip to content

Commit eaa38eb

Browse files
committed
Only log fault that configureDefaultLoggingSubsystem has not been called once
Otherwise logs will get extremely noisy if `configureDefaultLoggingSubsystem` is not called. Also improve error messages and documentation a little bit and log if the default system is modified after it was initially set.
1 parent e4016e4 commit eaa38eb

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

Sources/SKLogging/LoggingScope.swift

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import Foundation
1616
public final class LoggingScope {
1717

1818
/// The name of the default logging subsystem if no task-local value is set.
19-
fileprivate static let defaultSubsystem: ThreadSafeBox<String?> = .init(initialValue: nil)
19+
private static let defaultSubsystem = ThreadSafeBox<String?>(initialValue: nil)
20+
21+
/// Whether we have logged a fault that `subsystem` has been accessed without calling
22+
/// `configureDefaultLoggingSubsystem` first.
23+
private static let hasLoggedNoSubsystemConfiguredFault = AtomicBool(initialValue: false)
2024

2125
/// The name of the current logging subsystem or `nil` if no logging scope is set.
2226
@TaskLocal fileprivate static var _subsystem: String?
@@ -31,9 +35,16 @@ public final class LoggingScope {
3135
} else if let defaultSubsystem = defaultSubsystem.value {
3236
return defaultSubsystem
3337
} else {
34-
Logger(subsystem: "org.swift.sklogging", category: "configuration")
35-
.log(level: .fault, "default logging subsystem was not configured before first use")
36-
return "org.swift"
38+
if !hasLoggedNoSubsystemConfiguredFault.setAndGet(newValue: true) {
39+
Logger(subsystem: "default", category: "sklogging")
40+
.fault(
41+
"""
42+
Default logging subsystem was not configured before first use of SKLogging. \
43+
Ensure that configureDefaultLoggingSubsystem is called before the first log call.
44+
"""
45+
)
46+
}
47+
return "default"
3748
}
3849
}
3950

@@ -42,8 +53,16 @@ public final class LoggingScope {
4253
return _scope ?? "default"
4354
}
4455

56+
/// Set the logging subsystem that is used task local subsystem is set using `withLoggingSubsystemAndScope`.
57+
///
58+
/// Must be called before the first log call.
4559
public static func configureDefaultLoggingSubsystem(_ subsystem: String) {
46-
LoggingScope.defaultSubsystem.withLock { $0 = subsystem }
60+
LoggingScope.defaultSubsystem.withLock { defaultSubsystem in
61+
if let defaultSubsystem, defaultSubsystem != subsystem {
62+
logger.log("Changing default log subsystem from \(defaultSubsystem) to \(subsystem)")
63+
}
64+
defaultSubsystem = subsystem
65+
}
4766
}
4867
}
4968

Sources/ToolsProtocolsCAtomics/include/ToolsProtocolsCAtomics.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <stdint.h>
1818
#include <sys/types.h>
1919
#include <stdlib.h>
20+
#include <stdatomic.h>
2021

2122
typedef struct {
2223
_Atomic(uint32_t) value;
@@ -32,6 +33,10 @@ static inline uint32_t atomic_uint32_get(CAtomicUInt32 *_Nonnull atomic) {
3233
return atomic->value;
3334
}
3435

36+
static inline uint32_t atomic_uint32_get_and_set(CAtomicUInt32 *_Nonnull atomic, uint32_t newValue) {
37+
return __c11_atomic_exchange(&atomic->value, newValue, __ATOMIC_SEQ_CST);
38+
}
39+
3540
static inline void atomic_uint32_set(CAtomicUInt32 *_Nonnull atomic, uint32_t newValue) {
3641
atomic->value = newValue;
3742
}

Sources/ToolsProtocolsSwiftExtensions/Atomics.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ import ToolsProtocolsCAtomics
3232
atomic_uint32_set(atomic, newValue ? 1 : 0)
3333
}
3434
}
35+
36+
/// Sets the boolean to the new value and returns the previous value.
37+
@_spi(SourceKitLSP) public func setAndGet(newValue: Bool) -> Bool {
38+
return atomic_uint32_get_and_set(atomic, newValue ? 1 : 0) != 0
39+
}
3540
}
3641

3742
@_spi(SourceKitLSP) public final class AtomicUInt8: Sendable {

0 commit comments

Comments
 (0)