Skip to content

Commit 731740a

Browse files
committed
now printing stacktrace for .error
1 parent 105f499 commit 731740a

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ The output of the code above is:
6767
Only errors are shown
6868
```
6969

70+
There is also a shortcut print statement for an error object. This will print the localizedDescription and set the warning level to .error.
71+
```swift
72+
Stuff.print(error)
73+
```
74+
75+
Update: From now on printing .error will also give you a stacktrace.
76+
7077
## Enum
7178

7279
You can install this by adding the following line to your Podfile:

Source/Print/Print.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,16 @@ public extension Stuff {
5858
/**
5959
The print command for writing to the output window
6060
*/
61-
public static func print<T>(_ object: T, _ level: logLevel = .debug, filename: String = #file, line: Int = #line, funcname: String = #function) {
61+
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) {
6262
if level.rawValue >= Stuff.minimumLogLevel.rawValue {
6363
let dateFormatter = DateFormatter()
6464
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss:SSS"
6565
let process = ProcessInfo.processInfo
66-
let threadId = "?"
66+
let threadId = Thread.current.name ?? ""
6767
let file = URL(string: filename)?.lastPathComponent ?? ""
68-
Swift.print("\n\(level.description()) .\(level)\(dateFormatter.string(from: Foundation.Date())) 📱 \(process.processName) [\(process.processIdentifier):\(threadId)] 📂 \(file)(\(line)) ⚙️ \(funcname) ➡️\r\t\(object)")
68+
let traceOutput: String = trace.map { "\t\t\($0)" }.reduce("\n") { "\($0)\n\($1)" }
69+
let output: String = object is Error ? "\((object as! Error).localizedDescription)\(traceOutput)" : "\(object)"
70+
Swift.print("\n\(level.description()) .\(level)\(dateFormatter.string(from: Foundation.Date())) 📱 \(process.processName) [\(process.processIdentifier):\(threadId)] 📂 \(file)(\(line)) ⚙️ \(funcname) ➡️\r\t\(output)")
6971
}
7072
}
7173
}

Stuff.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "Stuff"
3-
s.version = "0.8.0"
3+
s.version = "0.9.0"
44
s.summary = "Too small for a library, too important to just forget"
55

66
s.description = <<-EOS

0 commit comments

Comments
 (0)