From 9ebabca38b01db16a7088a209b97fb0b123dd0b3 Mon Sep 17 00:00:00 2001 From: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com> Date: Tue, 14 Aug 2018 08:35:56 +0200 Subject: [PATCH] Added ConsoleLogger, NullLogger and AgregateLogger --- Logger.xcodeproj/project.pbxproj | 20 +++++++++++++ Logger/Loggers/AgregateLogger.swift | 39 ++++++++++++++++++++++++ Logger/Loggers/ConsoleLogger.swift | 34 +++++++++++++++++++++ Logger/Loggers/NullLogger.swift | 46 +++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 Logger/Loggers/AgregateLogger.swift create mode 100644 Logger/Loggers/ConsoleLogger.swift create mode 100644 Logger/Loggers/NullLogger.swift diff --git a/Logger.xcodeproj/project.pbxproj b/Logger.xcodeproj/project.pbxproj index 13420fd..c3b2fad 100644 --- a/Logger.xcodeproj/project.pbxproj +++ b/Logger.xcodeproj/project.pbxproj @@ -9,6 +9,9 @@ /* 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 */; }; + 2EBF4B4A2122AF53008E4117 /* AgregateLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B472122AF53008E4117 /* AgregateLogger.swift */; }; + 2EBF4B4B2122AF53008E4117 /* ConsoleLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B482122AF53008E4117 /* ConsoleLogger.swift */; }; + 2EBF4B4C2122AF53008E4117 /* NullLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBF4B492122AF53008E4117 /* NullLogger.swift */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -27,6 +30,9 @@ 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 = ""; }; + 2EBF4B472122AF53008E4117 /* AgregateLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AgregateLogger.swift; sourceTree = ""; }; + 2EBF4B482122AF53008E4117 /* ConsoleLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsoleLogger.swift; sourceTree = ""; }; + 2EBF4B492122AF53008E4117 /* NullLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NullLogger.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -61,10 +67,21 @@ children = ( 2EBF4B442122ACD6008E4117 /* LogStringConvertible.swift */, 2EBF4B3D2122AA34008E4117 /* Logger.swift */, + 2EBF4B462122AF53008E4117 /* Loggers */, ); path = Logger; sourceTree = ""; }; + 2EBF4B462122AF53008E4117 /* Loggers */ = { + isa = PBXGroup; + children = ( + 2EBF4B472122AF53008E4117 /* AgregateLogger.swift */, + 2EBF4B482122AF53008E4117 /* ConsoleLogger.swift */, + 2EBF4B492122AF53008E4117 /* NullLogger.swift */, + ); + path = Loggers; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -122,7 +139,10 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 2EBF4B4C2122AF53008E4117 /* NullLogger.swift in Sources */, 2EBF4B3E2122AA34008E4117 /* Logger.swift in Sources */, + 2EBF4B4B2122AF53008E4117 /* ConsoleLogger.swift in Sources */, + 2EBF4B4A2122AF53008E4117 /* AgregateLogger.swift in Sources */, 2EBF4B452122ACD6008E4117 /* LogStringConvertible.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Logger/Loggers/AgregateLogger.swift b/Logger/Loggers/AgregateLogger.swift new file mode 100644 index 0000000..5bedb00 --- /dev/null +++ b/Logger/Loggers/AgregateLogger.swift @@ -0,0 +1,39 @@ +// +// 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 final class AgregateLogger: Logger { + private let loggers: [Logger] + + public init(loggers: [Logger]) { + self.loggers = loggers + } + + public func log(time: String, level: LogLevel, location: String, object: String) { + for logger in self.loggers { + logger.log(time: time, level: level, location: location, object: object) + } + } +} diff --git a/Logger/Loggers/ConsoleLogger.swift b/Logger/Loggers/ConsoleLogger.swift new file mode 100644 index 0000000..5d1745e --- /dev/null +++ b/Logger/Loggers/ConsoleLogger.swift @@ -0,0 +1,34 @@ +// +// 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 final class ConsoleLogger: Logger { + public init() { + } + + public func log(time: String, level: LogLevel, location: String, object: String) { + print(time + " <" + level.rawValue + "> " + location + " " + object) + } +} diff --git a/Logger/Loggers/NullLogger.swift b/Logger/Loggers/NullLogger.swift new file mode 100644 index 0000000..7c3160d --- /dev/null +++ b/Logger/Loggers/NullLogger.swift @@ -0,0 +1,46 @@ +// +// 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 class NullLogger: Logger { + public init() { + } + + public func log(time: String, level: LogLevel, location: String, object: String) { + // noop + } + + public func description(for date: Date) -> String { + return "" + } + + public func description(for file: String, line: Int, function: String) -> String { + return "" + } + + public func description(for object: Any) -> String { + return "" + } +}