diff --git a/Logger.xcodeproj/project.pbxproj b/Logger.xcodeproj/project.pbxproj index 232e149..13420fd 100644 --- a/Logger.xcodeproj/project.pbxproj +++ b/Logger.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 2EBF4B3E2122AA34008E4117 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B3D2122AA34008E4117 /* Logger.swift */; }; + 2EBF4B452122ACD6008E4117 /* LogStringConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B442122ACD6008E4117 /* LogStringConvertible.swift */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -25,6 +26,7 @@ /* Begin PBXFileReference section */ 2EBF4B3A2122AA34008E4117 /* libLogger.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libLogger.a; sourceTree = BUILT_PRODUCTS_DIR; }; 2EBF4B3D2122AA34008E4117 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = ""; }; + 2EBF4B442122ACD6008E4117 /* LogStringConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LogStringConvertible.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -57,6 +59,7 @@ 2EBF4B3C2122AA34008E4117 /* Logger */ = { isa = PBXGroup; children = ( + 2EBF4B442122ACD6008E4117 /* LogStringConvertible.swift */, 2EBF4B3D2122AA34008E4117 /* Logger.swift */, ); path = Logger; @@ -120,6 +123,7 @@ buildActionMask = 2147483647; files = ( 2EBF4B3E2122AA34008E4117 /* Logger.swift in Sources */, + 2EBF4B452122ACD6008E4117 /* LogStringConvertible.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Logger/LogStringConvertible.swift b/Logger/LogStringConvertible.swift new file mode 100644 index 0000000..87bab24 --- /dev/null +++ b/Logger/LogStringConvertible.swift @@ -0,0 +1,37 @@ +// +// MIT License +// +// Copyright (c) 2018 Wojciech Nagrodzki +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +import Foundation + +public protocol LogStringConvertible { + var logDescription: String { get } +} + +extension Array: LogStringConvertible where Element: LogStringConvertible { + public var logDescription: String { + let descriptions = map { $0.logDescription } + let joinedDescriptions = descriptions.joined(separator: ", ") + return "[" + joinedDescriptions + "]" + } +} diff --git a/Logger/Logger.swift b/Logger/Logger.swift index 518ad44..5e229bd 100644 --- a/Logger/Logger.swift +++ b/Logger/Logger.swift @@ -22,6 +22,56 @@ // SOFTWARE. // -class Logger { +import Foundation +public protocol Logger { + func log(time: String, level: LogLevel, location: String, object: String) + + func description(for date: Date) -> String + func description(for file: String, line: Int, function: String) -> String + func description(for object: Any) -> String } + +public enum LogLevel: String { + case `default` = "Default" + case info = "Info" + case debug = "Debug" + case error = "Error" + case fault = "Fault" +} + +extension Logger { + public func log(_ object: Any, level: LogLevel = .default, file: String = #file, line: Int = #line, function: String = #function) { + let now = Date() + let time = description(for: now) + let location = description(for: file, line: line, function: function) + let objectDescription = description(for: object) + log(time: time, level: level, location: location, object: objectDescription) + } + + public func description(for date: Date) -> String { + return dateFormatter.string(from: date) + } + + public func description(for file: String, line: Int, function: String) -> String { + return filename(fromFilePath: file) + ":\(line) \(function)" + } + + public func description(for object: Any) -> String { + if let logStringConvertible = object as? LogStringConvertible { + return logStringConvertible.logDescription + } + return String(describing: object) + } + + private func filename(fromFilePath path: String) -> String { + return URL(fileURLWithPath: path).deletingPathExtension().lastPathComponent + } +} + +private let dateFormatter: DateFormatter = { + let formatter = DateFormatter() + formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS" + formatter.timeZone = TimeZone(secondsFromGMT: 0) + return formatter +}()