From b9b4a091056d136d28c233f8799d48329bb149ff Mon Sep 17 00:00:00 2001 From: Edwin Vermeer Date: Thu, 4 Apr 2019 10:33:19 +0200 Subject: [PATCH] added option for production / release logging --- README.md | 2 ++ Source/Print/Print.swift | 23 ++++++++++++++++++----- Stuff.podspec | 2 +- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d1306be..29cbdfa 100755 --- a/README.md +++ b/README.md @@ -74,6 +74,8 @@ There is also a shortcut print statement for an error object. This will print th Update: From now on printing .error will also give you a stacktrace. +Update 04-04-2019 : You can now also set the minimumLogLevel to productionLogAll. This will make sure you can also see the logging in the console (menu 'Window', 'Devices and Simulators', 'Open Console') In most cases you should only use this if you want to see logging for an app that you distributed using Testflight. + ## Enum You can install this by adding the following line to your Podfile: diff --git a/Source/Print/Print.swift b/Source/Print/Print.swift index 6e72310..447fe4e 100755 --- a/Source/Print/Print.swift +++ b/Source/Print/Print.swift @@ -6,16 +6,19 @@ // import Foundation +import os.log /** Extending the Stuff object with print functionality */ public extension Stuff { - + /** Enumeration of the log levels */ public enum logLevel: Int { + // Make sure all log events goes to the device log + case productionLogAll = 0 // Informational loging, lowest level case info = 1 // Debug loging, default level @@ -28,7 +31,7 @@ public extension Stuff { case fatal = 5 // Set the minimumLogLevel to .none to stop everything from loging case none = 6 - + /** Get the emoticon for the log level. */ @@ -44,12 +47,12 @@ public extension Stuff { return "🚫" case .fatal: return "🆘" - case .none: + case .none, .productionLogAll: return "" } } } - + /** Set the minimum log level. By default set to .info which is the minimum. Everything will be loged. */ @@ -67,7 +70,17 @@ public extension Stuff { let file = URL(string: filename)?.lastPathComponent ?? "" let traceOutput: String = trace.map { "\t\t\($0)" }.reduce("\n") { "\($0)\n\($1)" } let output: String = object is Error ? "\((object as! Error).localizedDescription)\(traceOutput)" : "\(object)" - Swift.print("\n\(level.description()) .\(level) ⏱ \(dateFormatter.string(from: Foundation.Date())) 📱 \(process.processName) [\(process.processIdentifier):\(threadId)] 📂 \(file)(\(line)) ⚙️ \(funcname) ➡️\r\t\(output)") + var logText = "\n\(level.description()) .\(level) ⏱ \(dateFormatter.string(from: Foundation.Date())) 📱 \(process.processName) [\(process.processIdentifier):\(threadId)] 📂 \(file)(\(line)) ⚙️ \(funcname) ➡️\r\t\(output)" + if Stuff.minimumLogLevel == .productionLogAll { + if #available(iOSApplicationExtension 10.0, *) { + let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Stuff") + os_log("%{public}@", log: log, logText) + } else { + NSLog("#Stuff# /(logText)") + } + } else { + Swift.print(logText) + } } } } diff --git a/Stuff.podspec b/Stuff.podspec index 50f669a..49bc82b 100755 --- a/Stuff.podspec +++ b/Stuff.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "Stuff" - s.version = "1.0.0" + s.version = "1.1.0" s.summary = "Too small for a library, too important to just forget" s.description = <<-EOS