diff --git a/Sources/XCGLogger/Misc/Optional/UserInfoHelpers.swift b/Sources/XCGLogger/Misc/Optional/UserInfoHelpers.swift index 21afb530..ea59134f 100644 --- a/Sources/XCGLogger/Misc/Optional/UserInfoHelpers.swift +++ b/Sources/XCGLogger/Misc/Optional/UserInfoHelpers.swift @@ -7,82 +7,92 @@ // Some rights reserved: https://github.com/DaveWoodCom/XCGLogger/blob/master/LICENSE.txt // -/// Protocol for creating tagging objects (ie, a tag, a developer, etc) to filter log messages by -public protocol UserInfoTaggingProtocol { - /// The name of the tagging object - var name: String { get set } +/// Protocol for creating log userInfo +public protocol UserInfoConvertibleProtocol { + /// Convert the object to a userInfo compatible dictionary - var dictionary: [String: String] { get } + var dictionary: [String: Any] { get } - /// initialize the object with a name - init(_ name: String) } -/// Struction for tagging log messages with Tags -public struct Tag: UserInfoTaggingProtocol { +/// Protocol for creating tagging objects (ie, a tag, a developer, etc) to filter log messages by - /// The name of the tag - public var name: String +public protocol UserInfoTaggingProtocol: UserInfoConvertibleProtocol { - /// Dictionary representation compatible with the userInfo paramater of log messages - public var dictionary: [String: String] { - return [XCGLogger.Constants.userInfoKeyTags: name] - } + associatedtype NameType - /// Initialize a Tag object with a name - public init(_ name: String) { - self.name = name + /// The name of the tagging object + var name: NameType { get set } + + /// initialize the object with a name + init(_ name: NameType) +} + +/// Protocol for creating tagging object (ie, a tag, a developer, etc) to filter log messages by + +public protocol UserInfoTagProtocol: UserInfoTaggingProtocol { + /// The userInfo key for tag + static var userInfoKey: String { get } +} + +public extension UserInfoTagProtocol { + /// Dictionary representation compatible with the userInfo parameter of log messages + var dictionary: [String: Any] { + return [Self.userInfoKey: name] } /// Create a Tag object with a name - public static func name(_ name: String) -> Tag { - return Tag(name) + public static func name(_ name: NameType) -> Self { + return Self(name) } - - /// Generate a userInfo compatible dictionary for the array of tag names + + /// Generate a userInfo compatible dictionary for the array of names public static func names(_ names: String...) -> [String: [String]] { var tags: [String] = [] - + for name in names { tags.append(name) } - - return [XCGLogger.Constants.userInfoKeyTags: tags] + + return [Self.userInfoKey: tags] } } -/// Struction for tagging log messages with Developers -public struct Dev: UserInfoTaggingProtocol { +/// Struction for tagging log messages with Tags +public struct Tag: UserInfoTagProtocol { - /// The name of the developer + /// The name of the tag public var name: String - /// Dictionary representation compatible with the userInfo paramater of log messages - public var dictionary: [String: String] { - return [XCGLogger.Constants.userInfoKeyDevs: name] - } - /// Initialize a Dev object with a name + /// Initialize a Tag object with a name public init(_ name: String) { self.name = name } - - /// Create a Dev object with a name - public static func name(_ name: String) -> Dev { - return Dev(name) + + /// The userInfo key for tag + public static var userInfoKey: String { + return XCGLogger.Constants.userInfoKeyTags } +} - /// Generate a userInfo compatible dictionary for the array of dev names - public static func names(_ names: String...) -> [String: [String]] { - var tags: [String] = [] +/// Struction for tagging log messages with Developers +public struct Dev: UserInfoTagProtocol { - for name in names { - tags.append(name) - } + /// The name of the developer + public var name: String - return [XCGLogger.Constants.userInfoKeyDevs: tags] + /// Initialize a Dev object with a name + public init(_ name: String) { + self.name = name } + + /// The userInfo key for dev + public static var userInfoKey: String { + return XCGLogger.Constants.userInfoKeyDevs + } + } /// Overloaded operator to merge userInfo compatible dictionaries together @@ -124,17 +134,17 @@ public func | (lhs: Dictionary, rhs: Dict } /// Overloaded operator, converts UserInfoTaggingProtocol types to dictionaries and then merges them -public func | (lhs: UserInfoTaggingProtocol, rhs: UserInfoTaggingProtocol) -> Dictionary { +public func | (lhs: T, rhs: U) -> Dictionary { return lhs.dictionary | rhs.dictionary } /// Overloaded operator, converts UserInfoTaggingProtocol types to dictionaries and then merges them -public func | (lhs: UserInfoTaggingProtocol, rhs: Dictionary) -> Dictionary { +public func | (lhs: T, rhs: Dictionary) -> Dictionary { return lhs.dictionary | rhs } /// Overloaded operator, converts UserInfoTaggingProtocol types to dictionaries and then merges them -public func | (lhs: Dictionary, rhs: UserInfoTaggingProtocol) -> Dictionary { +public func | (lhs: Dictionary, rhs: T) -> Dictionary { return rhs.dictionary | lhs } @@ -148,7 +158,7 @@ public extension UserInfoFilter { /// - Parameters: /// - tags: Array of UserInfoTaggingProtocol objects to match against. /// - public convenience init(includeFrom tags: [UserInfoTaggingProtocol]) { + public convenience init(includeFrom tags: [T]) where T.NameType == String { var names: [String] = [] for tag in tags { names.append(tag.name) @@ -164,7 +174,7 @@ public extension UserInfoFilter { /// - Parameters: /// - tags: Array of UserInfoTaggingProtocol objects to match against. /// - public convenience init(excludeFrom tags: [UserInfoTaggingProtocol]) { + public convenience init(excludeFrom tags: [T]) where T.NameType == String { var names: [String] = [] for tag in tags { names.append(tag.name)