Skip to content

Commit

Permalink
now printing stacktrace for .error
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Nov 4, 2018
1 parent 105f499 commit 731740a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ The output of the code above is:
Only errors are shown
```

There is also a shortcut print statement for an error object. This will print the localizedDescription and set the warning level to .error.
```swift
Stuff.print(error)
```

Update: From now on printing .error will also give you a stacktrace.

## Enum

You can install this by adding the following line to your Podfile:
Expand Down
8 changes: 5 additions & 3 deletions Source/Print/Print.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ public extension Stuff {
/**
The print command for writing to the output window
*/
public static func print<T>(_ object: T, _ level: logLevel = .debug, filename: String = #file, line: Int = #line, funcname: String = #function) {
public static func print<T>(_ object: T, _ level: logLevel = (T.self is Error.Type ? .error : .debug), filename: String = #file, line: Int = #line, funcname: String = #function, trace: [String] = Thread.callStackSymbols) {
if level.rawValue >= Stuff.minimumLogLevel.rawValue {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss:SSS"
let process = ProcessInfo.processInfo
let threadId = "?"
let threadId = Thread.current.name ?? ""
let file = URL(string: filename)?.lastPathComponent ?? ""
Swift.print("\n\(level.description()) .\(level)\(dateFormatter.string(from: Foundation.Date())) 📱 \(process.processName) [\(process.processIdentifier):\(threadId)] 📂 \(file)(\(line)) ⚙️ \(funcname) ➡️\r\t\(object)")
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)")
}
}
}
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 = "0.8.0"
s.version = "0.9.0"
s.summary = "Too small for a library, too important to just forget"

s.description = <<-EOS
Expand Down

0 comments on commit 731740a

Please sign in to comment.