Skip to content

Commit

Permalink
added option for production / release logging
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Apr 4, 2019
1 parent d9a1b5d commit b9b4a09
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 18 additions & 5 deletions Source/Print/Print.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*/
Expand All @@ -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.
*/
Expand All @@ -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)
}
}
}
}
2 changes: 1 addition & 1 deletion Stuff.podspec
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit b9b4a09

Please sign in to comment.