Commit 5401ece1 authored by Johannes Schriewer's avatar Johannes Schriewer

Add #file, #line and #function to logger

parent 138f9d7e
Pipeline #1824 passed with stage
......@@ -34,6 +34,9 @@ public class Log {
/// Active log level
public static var logLevel: LogLevel = .Debug
/// Log file and line too?
public static var logFileAndLine: Bool = false
/// Active log target
public static var logTarget: LogTarget = .StdErr
......@@ -57,51 +60,51 @@ public class Log {
/// Log a debug message
///
/// - Parameter msg: message or object to log, works like in print
public class func debug(_ msg: Any...) {
public class func debug(_ msg: Any..., file: String = #file, line: Int = #line, function: String = #function) {
guard Log.logLevel.rawValue <= LogLevel.Debug.rawValue else {
return
}
self.log("DEBUG", msg)
self.log("DEBUG", msg, file: file, line: line, function: function)
}
/// Log an informal message
///
/// - Parameter msg: message or object to log, works like in print
public class func info(_ msg: Any...) {
public class func info(_ msg: Any..., file: String = #file, line: Int = #line, function: String = #function) {
guard Log.logLevel.rawValue <= LogLevel.Info.rawValue else {
return
}
self.log("INFO ", msg)
self.log("INFO ", msg, file: file, line: line, function: function)
}
/// Log a warning message
///
/// - Parameter msg: message or object to log, works like in print
public class func warn(_ msg: Any...) {
public class func warn(_ msg: Any..., file: String = #file, line: Int = #line, function: String = #function) {
guard Log.logLevel.rawValue <= LogLevel.Warn.rawValue else {
return
}
self.log("WARN ", msg)
self.log("WARN ", msg, file: file, line: line, function: function)
}
/// Log an error message
///
/// - Parameter msg: message or object to log, works like in print
public class func error(_ msg: Any...) {
public class func error(_ msg: Any..., file: String = #file, line: Int = #line, function: String = #function) {
guard Log.logLevel.rawValue <= LogLevel.Error.rawValue else {
return
}
self.log("ERROR", msg)
self.log("ERROR", msg, file: file, line: line, function: function)
}
/// Log a fatal message, program usually quits after these
///
/// - Parameter msg: message or object to log, works like in print
public class func fatal(_ msg: Any...) {
public class func fatal(_ msg: Any..., file: String = #file, line: Int = #line, function: String = #function) {
guard Log.logLevel.rawValue <= LogLevel.Fatal.rawValue else {
return
}
self.log("FATAL", msg)
self.log("FATAL", msg, file: file, line: line, function: function)
}
/// Close and re-open log file (usually bound to a HUP UNIX Signal)
......@@ -116,13 +119,17 @@ public class Log {
}
// MARK: - Private
private class func log(_ level: String, _ msg: [Any]) {
private class func log(_ level: String, _ msg: [Any], file: String, line: Int, function: String) {
let date = Date.now()
if let logFile = self.logFile where self.logTarget == .File {
do {
try date.isoDateString!.write(to: logFile)
try " [\(level)]: ".write(to: logFile)
if self.logFileAndLine {
try " [\(level)] \(file):\(line): ".write(to: logFile)
} else {
try " [\(level)]: ".write(to: logFile)
}
for item in msg {
try (String(item) + " ").write(to: logFile)
}
......@@ -131,13 +138,13 @@ public class Log {
} catch {
self.logTarget = .StdErr
self._fwrite(stderr, "\(date.isoDateString!) [FATAL]: Could not write to logfile \(logFileName), reverting to STDERR!")
self.log(level, msg)
self.log(level, msg, file: file, line: line, function: function)
}
} else {
if let logFileName = self.logFileName where self.logTarget == .File {
do {
self.logFile = try File(path: logFileName, mode: .AppendOnly)
self.log(level, msg)
self.log(level, msg, file: file, line: line, function: function)
return
} catch {
self._fwrite(stderr, "\(date.isoDateString!) [FATAL]: Could not open logfile \(logFileName)!")
......@@ -146,7 +153,12 @@ public class Log {
}
let out = (self.logTarget == .StdOut) ? stdout : stderr
if self.logFileAndLine {
self._fwrite(out, "\(date.isoDateString!) [\(level)] \(file):\(line): ")
} else {
self._fwrite(out, "\(date.isoDateString!) [\(level)]: ")
}
for item in msg {
self._fwrite(out, String(item) + " ")
}
......
......@@ -20,6 +20,7 @@ class LoggerTests: XCTestCase {
func testLogStdErr() {
Log.logLevel = .Debug
Log.logTarget = .StdErr
Log.logFileAndLine = true
Log.debug("debug log", Log.logTarget)
Log.info("info log", Log.logTarget)
......@@ -31,6 +32,7 @@ class LoggerTests: XCTestCase {
func testLogStdOut() {
Log.logLevel = .Debug
Log.logTarget = .StdOut
Log.logFileAndLine = true
Log.debug("debug log", Log.logTarget)
Log.info("info log", Log.logTarget)
......@@ -44,6 +46,7 @@ class LoggerTests: XCTestCase {
Log.logLevel = .Debug
Log.logFileName = try FS.temporaryDirectory() + "logfile.log"
Log.logTarget = .File
Log.logFileAndLine = true
Log.debug("debug log", Log.logTarget)
Log.info("info log", Log.logTarget)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment