Skip to content

Commit a5be887

Browse files
committed
Added convenience methods to extend logging of NextcloudKit.
1 parent b3bc467 commit a5be887

1 file changed

Lines changed: 49 additions & 4 deletions

File tree

Sources/NextcloudKit/NKCommon.swift

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,15 +629,58 @@ public struct NKCommon: Sendable {
629629
}
630630
}
631631

632+
///
633+
/// Write a message with an "[DEBUG] " prefix to the log.
634+
///
635+
public func writeLog(debug message: String) {
636+
writeLog("[DEBUG] \(message)")
637+
}
638+
639+
///
640+
/// Write a message with an "[INFO] " prefix to the log.
641+
///
642+
public func writeLog(info message: String) {
643+
writeLog("[INFO] \(message)")
644+
}
645+
646+
///
647+
/// Write a message with an "[WARNING] " prefix to the log.
648+
///
649+
public func writeLog(warning message: String) {
650+
writeLog("[WARNING] \(message)")
651+
}
652+
653+
///
654+
/// Write a message with an "[ERROR] " prefix to the log.
655+
///
656+
public func writeLog(error message: String) {
657+
writeLog("[ERROR] \(message)")
658+
}
659+
660+
///
661+
/// Write an arbitrary string to the log.
662+
///
663+
/// Does not write anything, should `text` be `nil`.
664+
///
632665
public func writeLog(_ text: String?) {
633-
guard let text = text else { return }
634-
guard let date = self.convertDate(Date(), format: "yyyy-MM-dd' 'HH:mm:ss") else { return }
666+
guard let text = text else {
667+
return
668+
}
669+
670+
guard let date = self.convertDate(Date(), format: "yyyy-MM-dd' 'HH:mm:ss") else {
671+
return
672+
}
673+
635674
let textToWrite = "\(date) " + text + "\n"
636675

637-
if printLog { print(textToWrite) }
676+
if printLog {
677+
print(textToWrite)
678+
}
679+
638680
if levelLog > 0 {
639681
logQueue.async(flags: .barrier) {
640682
self.writeLogToDisk(filename: self.filenamePathLog, text: textToWrite)
683+
641684
if self.copyLogToDocumentDirectory, let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
642685
let filenameCopyToDocumentDirectory = path + "/" + self.filenameLog
643686
self.writeLogToDisk(filename: filenameCopyToDocumentDirectory, text: textToWrite)
@@ -647,7 +690,9 @@ public struct NKCommon: Sendable {
647690
}
648691

649692
private func writeLogToDisk(filename: String, text: String) {
650-
guard let data = text.data(using: .utf8) else { return }
693+
guard let data = text.data(using: .utf8) else {
694+
return
695+
}
651696

652697
if !FileManager.default.fileExists(atPath: filename) {
653698
FileManager.default.createFile(atPath: filename, contents: nil, attributes: nil)

0 commit comments

Comments
 (0)