From 80cc7d2e3a07e29970e2d226d9553bfb7fd36c48 Mon Sep 17 00:00:00 2001 From: surajthomask Date: Mon, 20 Aug 2018 11:53:32 +0400 Subject: [PATCH 1/2] Update to swift 4.0 --- Log.xcodeproj/project.pbxproj | 38 +++++++++++++++---- .../xcshareddata/xcschemes/Log OSX.xcscheme | 2 +- .../xcshareddata/xcschemes/Log iOS.xcscheme | 2 +- .../xcshareddata/xcschemes/Log tvOS.xcscheme | 2 +- .../xcschemes/Log watchOS.xcscheme | 2 +- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Log.xcodeproj/project.pbxproj b/Log.xcodeproj/project.pbxproj index 9f94c56..1f747d4 100644 --- a/Log.xcodeproj/project.pbxproj +++ b/Log.xcodeproj/project.pbxproj @@ -391,7 +391,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0820; + LastUpgradeCheck = 0930; ORGANIZATIONNAME = delba; TargetAttributes = { 6D7C68021C89FBAB005782C9 = { @@ -411,11 +411,11 @@ }; 6DACCA391C89F89D001B63D9 = { CreatedOnToolsVersion = 7.2; - LastSwiftMigration = 0800; + LastSwiftMigration = 0930; }; 6DACCA421C89F89E001B63D9 = { CreatedOnToolsVersion = 7.2; - LastSwiftMigration = 0800; + LastSwiftMigration = 0930; }; }; }; @@ -604,14 +604,22 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -655,14 +663,22 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -793,6 +809,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "io.delba.Log-tvOSTests"; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = appletvos; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; TVOS_DEPLOYMENT_TARGET = 9.1; }; name = Debug; @@ -806,6 +823,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = appletvos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; TVOS_DEPLOYMENT_TARGET = 9.1; }; name = Release; @@ -871,6 +889,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "io.delba.Log-OSXTests"; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; }; name = Debug; }; @@ -887,6 +906,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; }; name = Release; }; @@ -907,7 +927,8 @@ PRODUCT_BUNDLE_IDENTIFIER = io.delba.Log; PRODUCT_NAME = Log; SKIP_INSTALL = YES; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -929,7 +950,8 @@ PRODUCT_NAME = Log; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Release; }; @@ -941,7 +963,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "io.delba.Log-iOSTests"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -954,7 +977,8 @@ PRODUCT_BUNDLE_IDENTIFIER = "io.delba.Log-iOSTests"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/Log.xcodeproj/xcshareddata/xcschemes/Log OSX.xcscheme b/Log.xcodeproj/xcshareddata/xcschemes/Log OSX.xcscheme index 74625de..786458c 100644 --- a/Log.xcodeproj/xcshareddata/xcschemes/Log OSX.xcscheme +++ b/Log.xcodeproj/xcshareddata/xcschemes/Log OSX.xcscheme @@ -1,6 +1,6 @@ Date: Mon, 20 Aug 2018 13:46:36 +0400 Subject: [PATCH 2/2] Add emojis to styles --- Source/Extensions/Themes.swift | 78 +++++++++++++++------------ Source/Formatter.swift | 26 ++++++--- Source/Logger.swift | 6 +-- Source/Theme.swift | 99 +++++++++++++++++++++++++++++----- 4 files changed, 151 insertions(+), 58 deletions(-) diff --git a/Source/Extensions/Themes.swift b/Source/Extensions/Themes.swift index 9ab16bb..561ff65 100644 --- a/Source/Extensions/Themes.swift +++ b/Source/Extensions/Themes.swift @@ -24,58 +24,66 @@ extension Themes { public static let `default` = Theme( - trace: "#C8C8C8", - debug: "#0000FF", - info: "#00FF00", - warning: "#FFFB00", - error: "#FF0000" + trace: Theme.Style(color: "#C8C8C8"), + debug: Theme.Style(color: "#0000FF"), + info: Theme.Style(color: "#00FF00"), + warning: Theme.Style(color: "#FFFB00"), + error: Theme.Style(color: "#FF0000") ) public static let dusk = Theme( - trace: "#FFFFFF", - debug: "#526EDA", - info: "#93C96A", - warning: "#D28F5A", - error: "#E44347" + trace: Theme.Style(color: "#FFFFFF"), + debug: Theme.Style(color: "#526EDA"), + info: Theme.Style(color: "#93C96A"), + warning: Theme.Style(color: "#D28F5A"), + error: Theme.Style(color: "#E44347") ) public static let midnight = Theme( - trace: "#FFFFFF", - debug: "#527EFF", - info: "#08FA95", - warning: "#EB905A", - error: "#FF4647" + trace: Theme.Style(color: "#FFFFFF"), + debug: Theme.Style(color: "#527EFF"), + info: Theme.Style(color: "#08FA95"), + warning: Theme.Style(color: "#EB905A"), + error: Theme.Style(color: "#FF4647") ) public static let tomorrow = Theme( - trace: "#4D4D4C", - debug: "#4271AE", - info: "#718C00", - warning: "#EAB700", - error: "#C82829" + trace: Theme.Style(color: "#4D4D4C"), + debug: Theme.Style(color: "#4271AE"), + info: Theme.Style(color: "#718C00"), + warning: Theme.Style(color: "#EAB700"), + error: Theme.Style(color: "#C82829") ) public static let tomorrowNight = Theme( - trace: "#C5C8C6", - debug: "#81A2BE", - info: "#B5BD68", - warning: "#F0C674", - error: "#CC6666" + trace: Theme.Style(color: "#C5C8C6"), + debug: Theme.Style(color: "#81A2BE"), + info: Theme.Style(color: "#B5BD68"), + warning: Theme.Style(color: "#F0C674"), + error: Theme.Style(color: "#CC6666") ) public static let tomorrowNightEighties = Theme( - trace: "#CCCCCC", - debug: "#6699CC", - info: "#99CC99", - warning: "#FFCC66", - error: "#F2777A" + trace: Theme.Style(color: "#CCCCCC"), + debug: Theme.Style(color: "#6699CC"), + info: Theme.Style(color: "#99CC99"), + warning: Theme.Style(color: "#FFCC66"), + error: Theme.Style(color: "#F2777A") ) public static let tomorrowNightBright = Theme( - trace: "#EAEAEA", - debug: "#7AA6DA", - info: "#B9CA4A", - warning: "#E7C547", - error: "#D54E53" + trace: Theme.Style(color: "#EAEAEA"), + debug: Theme.Style(color: "#7AA6DA"), + info: Theme.Style(color: "#B9CA4A"), + warning: Theme.Style(color: "#E7C547"), + error: Theme.Style(color: "#D54E53") + ) + + public static let emojiHearts = Theme( + trace: Theme.Style(emoji: "💚"), + debug: Theme.Style(emoji: "🖤"), + info: Theme.Style(emoji: "💙"), + warning: Theme.Style(emoji: "💛"), + error: Theme.Style(emoji: "❤️") ) } diff --git a/Source/Formatter.swift b/Source/Formatter.swift index d4b5a60..799572e 100644 --- a/Source/Formatter.swift +++ b/Source/Formatter.swift @@ -222,11 +222,18 @@ private extension Formatter { */ func format(level: Level) -> String { let text = level.description - - if let color = logger.theme?.colors[level] { - return text.withColor(color) + + if let style = logger.theme.style(for: level) { + + var string = style.emoji + text + + if let color = style.color { + string = string.withColor(color) + } + + return string } - + return text } @@ -239,11 +246,14 @@ private extension Formatter { */ func format(description: String?) -> String { var text = "MEASURE" - - if let color = logger.theme?.colors[.debug] { - text = text.withColor(color) + + if let style = logger.theme.style(for: .debug) { + + text = style.emoji + text + if let color = style.color { + text = text.withColor(color) + } } - if let description = description { text = "\(text) \(description)" } diff --git a/Source/Logger.swift b/Source/Logger.swift index 2e2920b..47fd6f6 100644 --- a/Source/Logger.swift +++ b/Source/Logger.swift @@ -52,7 +52,7 @@ open class Logger { } /// The logger theme. - public var theme: Theme? + public var theme: Theme /// The minimum level of severity. public var minLevel: Level @@ -64,7 +64,7 @@ open class Logger { /// The logger colors public var colors: String { - return theme?.description ?? "" + return theme.description } /// The queue used for logging. @@ -79,7 +79,7 @@ open class Logger { - returns: A newly created logger. */ - public init(formatter: Formatter = .default, theme: Theme? = nil, minLevel: Level = .trace) { + public init(formatter: Formatter = .default, theme: Theme = .emojiHearts, minLevel: Level = .trace) { self.formatter = formatter self.theme = theme self.minLevel = minLevel diff --git a/Source/Theme.swift b/Source/Theme.swift index 221289c..ac4f800 100644 --- a/Source/Theme.swift +++ b/Source/Theme.swift @@ -25,13 +25,56 @@ public class Themes {} public class Theme: Themes { - /// The theme colors. - internal var colors: [Level: String] - + + /// Style object to hold color and emoji for each level. + public struct Style { + + /// Color for the syyle. + public let color: String? + /// Emoji for the style. + public let emoji: String + + /** + Creates and returns a style with the specified color or/and emoji. + + - parameter coloe: The color for the style. + - parameter debug: The emoji for the style. + + - returns: A style with the specified color or/and emoji. + */ + public init(color: String? = nil, emoji: String? = nil) { + + self.color = color + if let emoji = emoji { + self.emoji = emoji + " " + } else { + self.emoji = "" + } + } + } + + /// The theme styles. + private var styles: [Level: Style] + + /** + Returns style for the specified level. + + - parameter level: The level for which style needs to be selected. + + - returns: A style for specific level. + */ + internal func style(for level: Level) -> Style? { + return styles[level] + } + /// The theme textual representation. internal var description: String { - return colors.keys.sorted().map { - $0.description.withColor(colors[$0]!) + return styles.keys.sorted().map { + var string = (styles[$0]?.emoji ?? "") + $0.description + if let color = styles[$0]?.color { + string = string.withColor(color) + } + return string }.joined(separator: " ") } @@ -45,17 +88,49 @@ public class Theme: Themes { - parameter error: The color for the error level. - returns: A theme with the specified colors. + + - note: Deprecated. Use `init( trace: Style, debug: Style, info: Style, warning: Style, error: Style)` instead. */ + @available(*, deprecated) public init(trace: String, debug: String, info: String, warning: String, error: String) { - self.colors = [ - .trace: Theme.formatHex(trace), - .debug: Theme.formatHex(debug), - .info: Theme.formatHex(info), - .warning: Theme.formatHex(warning), - .error: Theme.formatHex(error) + + self.styles = [ + .trace: Style(color: Theme.formatHex(trace)), + .debug: Style(color: Theme.formatHex(debug)), + .info: Style(color: Theme.formatHex(info)), + .warning: Style(color: Theme.formatHex(warning)), + .error: Style(color: Theme.formatHex(error)) ] } - + + /** + Creates and returns a theme with the specified styles. + + - parameter trace: The style for the trace level. + - parameter debug: The style for the debug level. + - parameter info: The style for the info level. + - parameter warning: The style for the warning level. + - parameter error: The style for the error level. + + - returns: A theme with the specified styles. + */ + public init( + trace: Style, + debug: Style, + info: Style, + warning: Style, + error: Style + ) { + + self.styles = [ + .trace: trace, + .debug: debug, + .info: info, + .warning: warning, + .error: error + ] + } + /** Returns a string representation of the hex color.