Simplify C strings

This commit is contained in:
Jason A. Donenfeld 2018-12-13 19:02:48 +01:00
parent 20a23a86da
commit d7f91f2888
1 changed files with 6 additions and 13 deletions

View File

@ -12,9 +12,7 @@ public class Logger {
init(withFilePath filePath: String, withTag tag: String) { init(withFilePath filePath: String, withTag tag: String) {
self.tag = tag self.tag = tag
self.log = filePath.withCString { fileC -> OpaquePointer? in self.log = open_log(filePath)
open_log(fileC)
}
if self.log == nil { if self.log == nil {
os_log("Cannot open log file for writing. Log will not be saved to file.", log: OSLog.default, type: .error) os_log("Cannot open log file for writing. Log will not be saved to file.", log: OSLog.default, type: .error)
} }
@ -22,20 +20,15 @@ public class Logger {
func log(message: String) { func log(message: String) {
guard let log = log else { return } guard let log = log else { return }
String(format: "[%@] %@", tag, message.trimmingCharacters(in: .newlines)).withCString { messageC in write_msg_to_log(log, String(format: "[%@] %@", tag, message.trimmingCharacters(in: .newlines)))
write_msg_to_log(log, messageC)
}
} }
func writeLog(mergedWith otherLogFile: String, to targetFile: String) -> Bool { func writeLog(mergedWith otherLogFile: String, to targetFile: String) -> Bool {
guard let log = log else { return false } guard let log = log else { return false }
guard let other = otherLogFile.withCString({ otherC -> OpaquePointer? in guard let other = open_log(otherLogFile) else { return false }
return open_log(otherC) let ret = write_logs_to_file(targetFile, log, other)
}) else { return false } close_log(other)
defer { close_log(other) } return ret == 0
return targetFile.withCString { fileC -> Bool in
return write_logs_to_file(fileC, log, other) == 0
}
} }
static func configureGlobal(withFilePath filePath: String?, withTag tag: String) { static func configureGlobal(withFilePath filePath: String?, withTag tag: String) {