添加日志保存
authorliangzhenying <867174124@qq.com>
Tue, 2 Jul 2024 09:11:39 +0000 (17:11 +0800)
committerliangzhenying <867174124@qq.com>
Tue, 2 Jul 2024 09:11:39 +0000 (17:11 +0800)
IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate
IOTBluetooth/IOTBluetooth/IOTBluetooth/Tools/Date.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth/Tools/Logger.swift

index 5086531..b322eb2 100644 (file)
Binary files a/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate and b/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ
index 9fc933d..6f0e492 100644 (file)
@@ -49,6 +49,9 @@ extension Date {
         /// e.g. 2020年01月01日 01:01:01
         static let long1: Format = .init("yyyy年MM月dd日 HH:mm:ss")
         
+        /// e.g. 2020-01-01 010101
+        static let long2: Format = .init("yyyy-MM-dd HHmmss")
+        
         /// e.g. 2020-01-01 01:01:01.088
         static let full: Format = .init("yyyy-MM-dd HH:mm:ss.SSS")
         
index 1236c7f..2e3aba4 100644 (file)
@@ -12,11 +12,35 @@ class Logger {
     static var isDebug: Bool = false
     
     fileprivate static let shared = Logger()
-    private init() { }
+    private var fileHandle:FileHandle?
+    private init() {
+        let path = getPath()
+        fileHandle = FileHandle(forWritingAtPath: path)!
+//        fileHandle?.seekToEndOfFile()
+    }
     
     fileprivate var dateString: String { Date().format(.full) }
     
     static var logs: [String] = []
+    
+    
+    private func getPath()->String{
+//        let path = NSHomeDirectory() + "/Documents/IOTBluetooth/" + "\(Date().format(.middle)).log"
+        let foldPath = NSHomeDirectory() + "/Documents/IOTBluetooth/"
+        let fileManager = FileManager.default
+        if !fileManager.fileExists(atPath: foldPath) {
+            do {
+                try FileManager.default.createDirectory(atPath: foldPath, withIntermediateDirectories: true, attributes: nil)
+            } catch {
+                return ""
+            }
+        }
+        let path = foldPath + "\(Date().format(.long2)).log"
+        if !FileManager.default.fileExists(atPath: path) {
+            FileManager.default.createFile(atPath: path, contents: nil, attributes: nil)
+        }
+        return path
+    }
 }
 
 extension Logger {
@@ -27,7 +51,19 @@ extension Logger {
             print(logs, separator: separator, terminator: terminator)
             let logString = Logger.shared.dateString + " [IOTBluetooth-\(String(format: "%03d", line))]: " + logs.map({ "\($0)" }).joined(separator: separator) + terminator
             Self.logs.append(logString)
+            Logger.shared.saveLog(log: logString)
         }
         objc_sync_exit(Logger.shared)
+        
+        
+    }
+    
+    private func saveLog(log:String){
+        fileHandle?.write(log.data(using: .utf8)!)
+//        if #available(iOS 13.0, *) {
+//            try? fileHandle.close()
+//        } else {
+//            // Fallback on earlier versions
+//        }
     }
 }