From 5524dce2cc409c4991c171919a43496a4292a382 Mon Sep 17 00:00:00 2001 From: Sam Deane Date: Wed, 14 Aug 2019 16:00:45 +0100 Subject: [PATCH] Removed shared library variant. Guard against multiple copies of Logger being linked. --- Package.swift | 8 -------- Sources/Logger/Channel.swift | 21 ++++++++++++++++++++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Package.swift b/Package.swift index cd4473f..b0c9636 100644 --- a/Package.swift +++ b/Package.swift @@ -8,17 +8,9 @@ let package = Package( .macOS(.v10_13), .iOS(.v12), ], products: [ - .library( - name: "LoggerShared", - type: .dynamic, - targets: ["Logger"]), .library( name: "Logger", targets: ["Logger"]), - .library( - name: "LoggerKitShared", - type: .dynamic, - targets: ["LoggerKit"]), .library( name: "LoggerKit", targets: ["LoggerKit"]), diff --git a/Sources/Logger/Channel.swift b/Sources/Logger/Channel.swift index 04ac733..2295421 100644 --- a/Sources/Logger/Channel.swift +++ b/Sources/Logger/Channel.swift @@ -52,7 +52,26 @@ public class Channel { though, which is why it's not a true singleton. */ - public static let defaultManager = Manager() + public static let defaultManager = initDefaultManager() + + /// Initialise the default log manager. + static func initDefaultManager() -> Manager { + #if ensureUniqueManager + /// We really do want there to only be a single instance of this, even if the logger library has mistakenly been + /// linked multiple times, so we store it in the thread dictionary for the main thread, and retrieve it from there if necessary + let dictionary = Thread.main.threadDictionary + if let manager = dictionary["Logger.Manager"] { + return unsafeBitCast(manager as AnyObject, to: Manager.self) // a normal cast might fail here if the code has been linked multiple times, since the class could be different (but identical) + } + + let manager = Manager() + dictionary["Logger.Manager"] = manager + return manager + + #else + return Manager() + #endif + } /** Default subsystem if nothing else is specified.