cordova-plugin-unvired-logger 0.0.18 → 0.0.20
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 +1 -1
- package/plugin.xml +1 -1
- package/src/android/Logger.java +1 -1
- package/src/browser/Logger.js +1 -1
- package/src/electron/Logger.js +1 -1
- package/src/electron/package.json +1 -1
- package/src/ios/Logger.swift +59 -24
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cordova-plugin-unvired-logger",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.20",
|
|
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.
|
|
2
|
+
<plugin id="cordova-plugin-unvired-logger" version="0.0.20"
|
|
3
3
|
xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
|
4
4
|
xmlns:android="http://schemas.android.com/apk/res/android">
|
|
5
5
|
|
package/src/android/Logger.java
CHANGED
|
@@ -19,7 +19,7 @@ public class Logger extends CordovaPlugin {
|
|
|
19
19
|
|
|
20
20
|
private static final String LOG_FILE_NAME = "log.txt";
|
|
21
21
|
private static final String BACKUP_LOG_FILE_NAME = "log_backup.txt";
|
|
22
|
-
private static final long MAX_LOG_SIZE =
|
|
22
|
+
private static final long MAX_LOG_SIZE = 50 * 1024 * 1024; // 50MB
|
|
23
23
|
|
|
24
24
|
// LogLevel constants as int
|
|
25
25
|
private static final int LOG_LEVEL_DEBUG = 7;
|
package/src/browser/Logger.js
CHANGED
package/src/electron/Logger.js
CHANGED
package/src/ios/Logger.swift
CHANGED
|
@@ -5,7 +5,8 @@ import UIKit
|
|
|
5
5
|
|
|
6
6
|
private let LOG_FILE_NAME = "log.txt"
|
|
7
7
|
private let BACKUP_LOG_FILE_NAME = "log_backup.txt"
|
|
8
|
-
private let MAX_LOG_SIZE: Int64 =
|
|
8
|
+
private let MAX_LOG_SIZE: Int64 = 50 * 1024 * 1024 // 50MB
|
|
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
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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.
|
|
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:
|
|
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
|
-
|
|
279
|
-
|
|
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
|
-
|
|
283
|
-
|
|
284
|
-
|
|
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
|
-
|
|
330
|
+
let fileManager = FileManager.default
|
|
331
|
+
if !fileManager.fileExists(atPath: from.path) {
|
|
300
332
|
return
|
|
301
333
|
}
|
|
302
|
-
|
|
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) {
|