Last active
September 8, 2018 21:24
-
-
Save kristopherjohnson/c674dde5c5b62a20e1875c743cf452a8 to your computer and use it in GitHub Desktop.
Simple Swift logging functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension String { | |
/// Return last path component. | |
public var lastPathComponent: String { | |
return (self as NSString).lastPathComponent | |
} | |
} | |
/// Write a message to the system log. | |
/// | |
/// The message will be prepended with function/file/line information. | |
func log(_ message: String, | |
_ function: String = #function, | |
_ file: String = #file, | |
_ line: Int = #line) { | |
NSLog("\(function) (\(file.lastPathComponent):\(line)): \(message)") | |
} | |
/// Write an error message to the system log. | |
/// | |
/// The message will be prepended with function/file/line information. | |
func log_error(_ message: String, | |
_ function: String = #function, | |
_ file: String = #file, | |
_ line: Int = #line) { | |
NSLog("ERROR: \(function) (\(file.lastPathComponent):\(line)): \(message)") | |
} | |
/// Write a warning message to the system log. | |
/// | |
/// The message will be prepended with function/file/line information. | |
func log_warning(_ message: String, | |
_ function: String = #function, | |
_ file: String = #file, | |
_ line: Int = #line) { | |
NSLog("WARNING: \(function) (\(file.lastPathComponent):\(line)): \(message)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example use:
Log output: