cordova-plugin-unvired-logger 0.0.18 → 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cordova-plugin-unvired-logger",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "A logger plugin for Electron, Android, Browser, and iOS that appends logs to log.txt files organized by user ID.",
5
5
  "cordova": {
6
6
  "id": "cordova-plugin-unvired-logger",
package/plugin.xml CHANGED
@@ -1,5 +1,5 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
- <plugin id="cordova-plugin-unvired-logger" version="0.0.18"
2
+ <plugin id="cordova-plugin-unvired-logger" version="0.0.19"
3
3
  xmlns="http://apache.org/cordova/ns/plugins/1.0"
4
4
  xmlns:android="http://schemas.android.com/apk/res/android">
5
5
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cordova-plugin-unvired-logger",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "Electron platform package for cordova-plugin-unvired-logger",
5
5
  "main": "Logger.js",
6
6
  "scripts": {
@@ -6,6 +6,7 @@ import UIKit
6
6
  private let LOG_FILE_NAME = "log.txt"
7
7
  private let BACKUP_LOG_FILE_NAME = "log_backup.txt"
8
8
  private let MAX_LOG_SIZE: Int64 = 10 * 1024 * 1024 // 10MB
9
+ private let logWriteQueue = DispatchQueue(label: "cordova.plugin.unvired.logger.write")
9
10
 
10
11
  private var defaultLogLevel: Int = 8
11
12
 
@@ -26,8 +27,7 @@ import UIKit
26
27
 
27
28
  @objc(loggerWithLevel:)
28
29
  func loggerWithLevel(_ command: CDVInvokedUrlCommand) {
29
- self.commandDelegate.run(inBackground: {
30
- guard let args = command.arguments.first as? [String: Any] else {
30
+ guard let args = command.arguments.first as? [String: Any] else {
31
31
  self.sendErrorResult(command.callbackId, message: "Invalid arguments")
32
32
  return
33
33
  }
@@ -58,25 +58,32 @@ import UIKit
58
58
 
59
59
  do {
60
60
  let logFile = try self.getLogFile(userId: userId)
61
- try self.checkAndRotateLogFile(logFile: logFile, userId: userId)
62
-
63
61
  let logEntry = self.formatLogEntry(level: level, sourceClass: sourceClass, sourceMethod: sourceMethod, message: message)
64
62
 
65
63
  // Print to console before writing to file
66
- print(logEntry.trimmingCharacters(in: .whitespacesAndNewlines))
67
-
68
- // Perform file write in background
69
- self.appendToFile(file: logFile, data: logEntry) { error in
70
- if let error = error {
71
- self.sendErrorResult(command.callbackId, message: "Logging failed: \(error.localizedDescription)")
72
- } else {
73
- self.sendSuccessResult(command.callbackId, message: "Logged to \(logFile.path)")
64
+ NSLog("%@", logEntry.trimmingCharacters(in: .whitespacesAndNewlines))
65
+ self.commandDelegate.run(inBackground: {
66
+ // Serialize rotation + write to guarantee ordering
67
+ self.logWriteQueue.async {
68
+ do {
69
+ try self.checkAndRotateLogFile(logFile: logFile, userId: userId)
70
+ self.appendToFile(file: logFile, data: logEntry) { error in
71
+ if let error = error {
72
+ self.sendErrorResult(command.callbackId, message: "Logging failed: \(error.localizedDescription)")
73
+ } else {
74
+ self.sendSuccessResult(command.callbackId, message: "Logged to \(logFile.path)")
75
+ }
76
+ }
77
+ } catch {
78
+ self.sendErrorResult(command.callbackId, message: "Logging failed: \(error.localizedDescription)")
79
+ }
74
80
  }
75
- }
81
+ })
76
82
  } catch {
77
- self.sendErrorResult(command.callbackId, message: "Logging failed: \(error.localizedDescription)")
83
+ self.commandDelegate.run(inBackground: {
84
+ self.sendErrorResult(command.callbackId, message: "Logging failed: \(error.localizedDescription)")
85
+ })
78
86
  }
79
- })
80
87
  }
81
88
 
82
89
  @objc(setLogLevel:)
@@ -230,7 +237,9 @@ import UIKit
230
237
  let userDir = documentsPath.appendingPathComponent(userId)
231
238
 
232
239
  if !FileManager.default.fileExists(atPath: userDir.path) {
233
- try FileManager.default.createDirectory(at: userDir, withIntermediateDirectories: true, attributes: nil)
240
+ try FileManager.default.createDirectory(at: userDir, withIntermediateDirectories: true, attributes: [
241
+ .protectionKey: FileProtectionType.none
242
+ ])
234
243
  }
235
244
 
236
245
  return userDir
@@ -274,16 +283,38 @@ import UIKit
274
283
  private func appendToFile(file: URL, data: String, completion: @escaping (Error?) -> Void = { _ in }) {
275
284
  do {
276
285
  let fileManager = FileManager.default
286
+ // Ensure parent directory exists (defensive)
287
+ let parentDirectory = file.deletingLastPathComponent()
288
+ if !fileManager.fileExists(atPath: parentDirectory.path) {
289
+ try fileManager.createDirectory(at: parentDirectory, withIntermediateDirectories: true, attributes: [
290
+ .protectionKey: FileProtectionType.none
291
+ ])
292
+ }
293
+ // Create the file if it doesn't exist with safe attributes
277
294
  if !fileManager.fileExists(atPath: file.path) {
278
- // Create the file if it doesn't exist
279
- fileManager.createFile(atPath: file.path, contents: nil, attributes: nil)
295
+ fileManager.createFile(atPath: file.path, contents: nil, attributes: [
296
+ .protectionKey: FileProtectionType.none
297
+ ])
280
298
  }
299
+ let utf8Data = data.data(using: .utf8) ?? Data()
281
300
  let fileHandle = try FileHandle(forWritingTo: file)
282
- fileHandle.seekToEndOfFile()
283
- fileHandle.write(data.data(using: .utf8)!)
284
- fileHandle.closeFile()
301
+ defer {
302
+ if #available(iOS 13.0, *) {
303
+ try? fileHandle.close()
304
+ } else {
305
+ fileHandle.closeFile()
306
+ }
307
+ }
308
+ if #available(iOS 13.4, *) {
309
+ try fileHandle.seekToEnd()
310
+ try fileHandle.write(contentsOf: utf8Data)
311
+ } else {
312
+ fileHandle.seekToEndOfFile()
313
+ fileHandle.write(utf8Data)
314
+ }
285
315
  completion(nil)
286
316
  } catch {
317
+ NSLog("[Logger] Failed to append to file: \(error.localizedDescription)")
287
318
  completion(error)
288
319
  }
289
320
  }
@@ -296,10 +327,14 @@ import UIKit
296
327
  }
297
328
 
298
329
  private func copyFile(from: URL, to: URL) throws {
299
- if !FileManager.default.fileExists(atPath: from.path) {
330
+ let fileManager = FileManager.default
331
+ if !fileManager.fileExists(atPath: from.path) {
300
332
  return
301
333
  }
302
- try FileManager.default.copyItem(at: from, to: to)
334
+ if fileManager.fileExists(atPath: to.path) {
335
+ try fileManager.removeItem(at: to)
336
+ }
337
+ try fileManager.copyItem(at: from, to: to)
303
338
  }
304
339
 
305
340
  private func sendSuccessResult(_ callbackId: String, message: String) {