cordova-plugin-unvired-logger 0.0.15 → 0.0.17

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.15",
3
+ "version": "0.0.17",
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.15"
2
+ <plugin id="cordova-plugin-unvired-logger" version="0.0.17"
3
3
  xmlns="http://apache.org/cordova/ns/plugins/1.0"
4
4
  xmlns:android="http://schemas.android.com/apk/res/android">
5
5
 
@@ -146,7 +146,7 @@ function loggerWithLevel(args) {
146
146
  level = parseInt(level, 8);
147
147
  }
148
148
 
149
- if (level < defaultLogLevel) {
149
+ if (level > defaultLogLevel) {
150
150
  return;
151
151
  }
152
152
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cordova-plugin-unvired-logger",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "description": "Electron platform package for cordova-plugin-unvired-logger",
5
5
  "main": "Logger.js",
6
6
  "scripts": {
@@ -8,7 +8,6 @@ import UIKit
8
8
  private let MAX_LOG_SIZE: Int64 = 10 * 1024 * 1024 // 10MB
9
9
 
10
10
  private var defaultLogLevel: Int = 8
11
- private let fileWriteQueue = DispatchQueue(label: "com.logger.filewrite", qos: .background)
12
11
 
13
12
  @objc(logDebug:)
14
13
  func logDebug(_ command: CDVInvokedUrlCommand) {
@@ -27,53 +26,54 @@ import UIKit
27
26
 
28
27
  @objc(loggerWithLevel:)
29
28
  func loggerWithLevel(_ command: CDVInvokedUrlCommand) {
30
- guard let args = command.arguments.first as? [String: Any] else {
31
- sendErrorResult(command.callbackId, message: "Invalid arguments")
32
- return
33
- }
34
-
35
- guard let userId = args["userId"] as? String, !userId.isEmpty else {
36
- sendErrorResult(command.callbackId, message: "userId is required")
37
- return
38
- }
39
-
40
- guard let level = args["level"] as? Int else {
41
- sendErrorResult(command.callbackId, message: "level is required")
42
- return
43
- }
44
-
45
- guard let message = args["message"] as? String, !message.isEmpty else {
46
- sendErrorResult(command.callbackId, message: "message is required")
47
- return
48
- }
49
-
50
- let sourceClass = args["sourceClass"] as? String ?? ""
51
- let sourceMethod = args["sourceMethod"] as? String ?? ""
52
-
53
- // Log level filtering
54
- if shouldSkipLog(level: level) {
55
- sendSuccessResult(command.callbackId, message: "Log skipped due to level filter")
56
- return
57
- }
58
-
59
- do {
60
- let logFile = try getLogFile(userId: userId)
61
- try checkAndRotateLogFile(logFile: logFile, userId: userId)
29
+ self.commandDelegate.run(inBackground: {
30
+ guard let args = command.arguments.first as? [String: Any] else {
31
+ self.sendErrorResult(command.callbackId, message: "Invalid arguments")
32
+ return
33
+ }
34
+
35
+ guard let userId = args["userId"] as? String, !userId.isEmpty else {
36
+ self.sendErrorResult(command.callbackId, message: "userId is required")
37
+ return
38
+ }
39
+
40
+ guard let level = args["level"] as? Int else {
41
+ self.sendErrorResult(command.callbackId, message: "level is required")
42
+ return
43
+ }
62
44
 
63
- let logEntry = formatLogEntry(level: level, sourceClass: sourceClass, sourceMethod: sourceMethod, message: message)
45
+ guard let message = args["message"] as? String, !message.isEmpty else {
46
+ self.sendErrorResult(command.callbackId, message: "message is required")
47
+ return
48
+ }
49
+
50
+ let sourceClass = args["sourceClass"] as? String ?? ""
51
+ let sourceMethod = args["sourceMethod"] as? String ?? ""
64
52
 
65
- // Send success result immediately without waiting for file write
66
- sendSuccessResult(command.callbackId, message: "Logged to \(logFile.path)")
53
+ // Log level filtering
54
+ if self.shouldSkipLog(level: level) {
55
+ self.sendSuccessResult(command.callbackId, message: "Log skipped due to level filter")
56
+ return
57
+ }
67
58
 
68
- // Perform file write in background with silent error handling
69
- appendToFile(file: logFile, data: logEntry) { error in
70
- if let error = error {
71
- print("Background logging error: \(error.localizedDescription)")
59
+ do {
60
+ let logFile = try self.getLogFile(userId: userId)
61
+ try self.checkAndRotateLogFile(logFile: logFile, userId: userId)
62
+
63
+ let logEntry = self.formatLogEntry(level: level, sourceClass: sourceClass, sourceMethod: sourceMethod, message: message)
64
+
65
+ // Perform file write in background
66
+ self.appendToFile(file: logFile, data: logEntry) { error in
67
+ if let error = error {
68
+ self.sendErrorResult(command.callbackId, message: "Logging failed: \(error.localizedDescription)")
69
+ } else {
70
+ self.sendSuccessResult(command.callbackId, message: "Logged to \(logFile.path)")
71
+ }
72
72
  }
73
+ } catch {
74
+ self.sendErrorResult(command.callbackId, message: "Logging failed: \(error.localizedDescription)")
73
75
  }
74
- } catch {
75
- sendErrorResult(command.callbackId, message: "Logging failed: \(error.localizedDescription)")
76
- }
76
+ })
77
77
  }
78
78
 
79
79
  @objc(setLogLevel:)
@@ -95,103 +95,115 @@ import UIKit
95
95
 
96
96
  @objc(getLogFileContent:)
97
97
  func getLogFileContent(_ command: CDVInvokedUrlCommand) {
98
- guard let args = command.arguments.first as? [String: Any],
99
- let userId = args["userId"] as? String else {
100
- sendErrorResult(command.callbackId, message: "userId is required")
101
- return
102
- }
103
-
104
- do {
105
- let logFile = try getLogFile(userId: userId)
106
- let content = try readFile(file: logFile)
107
- sendSuccessResult(command.callbackId, message: content)
108
- } catch {
109
- sendErrorResult(command.callbackId, message: "Failed to read log file: \(error.localizedDescription)")
110
- }
98
+ self.commandDelegate.run(inBackground: {
99
+ guard let args = command.arguments.first as? [String: Any],
100
+ let userId = args["userId"] as? String else {
101
+ self.sendErrorResult(command.callbackId, message: "userId is required")
102
+ return
103
+ }
104
+
105
+ do {
106
+ let logFile = try self.getLogFile(userId: userId)
107
+ let content = try self.readFile(file: logFile)
108
+ self.sendSuccessResult(command.callbackId, message: content)
109
+ } catch {
110
+ self.sendErrorResult(command.callbackId, message: "Failed to read log file: \(error.localizedDescription)")
111
+ }
112
+ })
111
113
  }
112
114
 
113
115
  @objc(clearLogFile:)
114
116
  func clearLogFile(_ command: CDVInvokedUrlCommand) {
115
- guard let args = command.arguments.first as? [String: Any],
116
- let userId = args["userId"] as? String else {
117
- sendErrorResult(command.callbackId, message: "userId is required")
118
- return
119
- }
120
-
121
- do {
122
- let logFile = try getLogFile(userId: userId)
123
- try "".write(to: logFile, atomically: true, encoding: .utf8)
124
- sendSuccessResult(command.callbackId, message: "Log file cleared")
125
- } catch {
126
- sendErrorResult(command.callbackId, message: "Failed to clear log file: \(error.localizedDescription)")
127
- }
117
+ self.commandDelegate.run(inBackground: {
118
+ guard let args = command.arguments.first as? [String: Any],
119
+ let userId = args["userId"] as? String else {
120
+ self.sendErrorResult(command.callbackId, message: "userId is required")
121
+ return
122
+ }
123
+
124
+ do {
125
+ let logFile = try self.getLogFile(userId: userId)
126
+ try "".write(to: logFile, atomically: true, encoding: .utf8)
127
+ self.sendSuccessResult(command.callbackId, message: "Log file cleared")
128
+ } catch {
129
+ self.sendErrorResult(command.callbackId, message: "Failed to clear log file: \(error.localizedDescription)")
130
+ }
131
+ })
128
132
  }
129
133
 
130
134
  @objc(getBackupLogFileContent:)
131
135
  func getBackupLogFileContent(_ command: CDVInvokedUrlCommand) {
132
- guard let args = command.arguments.first as? [String: Any],
133
- let userId = args["userId"] as? String else {
134
- sendErrorResult(command.callbackId, message: "userId is required")
135
- return
136
- }
137
-
138
- do {
139
- let backupFile = try getBackupLogFile(userId: userId)
140
- let content = try readFile(file: backupFile)
141
- sendSuccessResult(command.callbackId, message: content)
142
- } catch {
143
- sendErrorResult(command.callbackId, message: "Failed to read backup log file: \(error.localizedDescription)")
144
- }
136
+ self.commandDelegate.run(inBackground: {
137
+ guard let args = command.arguments.first as? [String: Any],
138
+ let userId = args["userId"] as? String else {
139
+ self.sendErrorResult(command.callbackId, message: "userId is required")
140
+ return
141
+ }
142
+
143
+ do {
144
+ let backupFile = try self.getBackupLogFile(userId: userId)
145
+ let content = try self.readFile(file: backupFile)
146
+ self.sendSuccessResult(command.callbackId, message: content)
147
+ } catch {
148
+ self.sendErrorResult(command.callbackId, message: "Failed to read backup log file: \(error.localizedDescription)")
149
+ }
150
+ })
145
151
  }
146
152
 
147
153
  @objc(copyLogToBackup:)
148
154
  func copyLogToBackup(_ command: CDVInvokedUrlCommand) {
149
- guard let args = command.arguments.first as? [String: Any],
150
- let userId = args["userId"] as? String else {
151
- sendErrorResult(command.callbackId, message: "userId is required")
152
- return
153
- }
154
-
155
- do {
156
- let logFile = try getLogFile(userId: userId)
157
- let backupFile = try getBackupLogFile(userId: userId)
158
- try copyFile(from: logFile, to: backupFile)
159
- sendSuccessResult(command.callbackId, message: "Log file copied to backup")
160
- } catch {
161
- sendErrorResult(command.callbackId, message: "Failed to copy log file to backup: \(error.localizedDescription)")
162
- }
155
+ self.commandDelegate.run(inBackground: {
156
+ guard let args = command.arguments.first as? [String: Any],
157
+ let userId = args["userId"] as? String else {
158
+ self.sendErrorResult(command.callbackId, message: "userId is required")
159
+ return
160
+ }
161
+
162
+ do {
163
+ let logFile = try self.getLogFile(userId: userId)
164
+ let backupFile = try self.getBackupLogFile(userId: userId)
165
+ try self.copyFile(from: logFile, to: backupFile)
166
+ self.sendSuccessResult(command.callbackId, message: "Log file copied to backup")
167
+ } catch {
168
+ self.sendErrorResult(command.callbackId, message: "Failed to copy log file to backup: \(error.localizedDescription)")
169
+ }
170
+ })
163
171
  }
164
172
 
165
173
  @objc(getLogFileURL:)
166
174
  func getLogFileURL(_ command: CDVInvokedUrlCommand) {
167
- guard let args = command.arguments.first as? [String: Any],
168
- let userId = args["userId"] as? String else {
169
- sendErrorResult(command.callbackId, message: "userId is required")
170
- return
171
- }
172
-
173
- do {
174
- let logFile = try getLogFile(userId: userId)
175
- sendSuccessResult(command.callbackId, message: logFile.path)
176
- } catch {
177
- sendErrorResult(command.callbackId, message: "Failed to get log file URL: \(error.localizedDescription)")
178
- }
175
+ self.commandDelegate.run(inBackground: {
176
+ guard let args = command.arguments.first as? [String: Any],
177
+ let userId = args["userId"] as? String else {
178
+ self.sendErrorResult(command.callbackId, message: "userId is required")
179
+ return
180
+ }
181
+
182
+ do {
183
+ let logFile = try self.getLogFile(userId: userId)
184
+ self.sendSuccessResult(command.callbackId, message: logFile.path)
185
+ } catch {
186
+ self.sendErrorResult(command.callbackId, message: "Failed to get log file URL: \(error.localizedDescription)")
187
+ }
188
+ })
179
189
  }
180
190
 
181
191
  @objc(getBackupLogFileURL:)
182
192
  func getBackupLogFileURL(_ command: CDVInvokedUrlCommand) {
183
- guard let args = command.arguments.first as? [String: Any],
184
- let userId = args["userId"] as? String else {
185
- sendErrorResult(command.callbackId, message: "userId is required")
186
- return
187
- }
188
-
189
- do {
190
- let backupFile = try getBackupLogFile(userId: userId)
191
- sendSuccessResult(command.callbackId, message: backupFile.path)
192
- } catch {
193
- sendErrorResult(command.callbackId, message: "Failed to get backup log file URL: \(error.localizedDescription)")
194
- }
193
+ self.commandDelegate.run(inBackground: {
194
+ guard let args = command.arguments.first as? [String: Any],
195
+ let userId = args["userId"] as? String else {
196
+ self.sendErrorResult(command.callbackId, message: "userId is required")
197
+ return
198
+ }
199
+
200
+ do {
201
+ let backupFile = try self.getBackupLogFile(userId: userId)
202
+ self.sendSuccessResult(command.callbackId, message: backupFile.path)
203
+ } catch {
204
+ self.sendErrorResult(command.callbackId, message: "Failed to get backup log file URL: \(error.localizedDescription)")
205
+ }
206
+ })
195
207
  }
196
208
 
197
209
  // MARK: - Helper Methods
@@ -257,28 +269,19 @@ import UIKit
257
269
  }
258
270
 
259
271
  private func appendToFile(file: URL, data: String, completion: @escaping (Error?) -> Void = { _ in }) {
260
- fileWriteQueue.async {
261
- do {
262
- let fileManager = FileManager.default
263
- if !fileManager.fileExists(atPath: file.path) {
264
- // Create the file if it doesn't exist
265
- fileManager.createFile(atPath: file.path, contents: nil, attributes: nil)
266
- }
267
- let fileHandle = try FileHandle(forWritingTo: file)
268
- fileHandle.seekToEndOfFile()
269
- fileHandle.write(data.data(using: .utf8)!)
270
- fileHandle.closeFile()
271
-
272
- // Notify success on main thread
273
- DispatchQueue.main.async {
274
- completion(nil)
275
- }
276
- } catch {
277
- // Notify error on main thread
278
- DispatchQueue.main.async {
279
- completion(error)
280
- }
272
+ do {
273
+ let fileManager = FileManager.default
274
+ if !fileManager.fileExists(atPath: file.path) {
275
+ // Create the file if it doesn't exist
276
+ fileManager.createFile(atPath: file.path, contents: nil, attributes: nil)
281
277
  }
278
+ let fileHandle = try FileHandle(forWritingTo: file)
279
+ fileHandle.seekToEndOfFile()
280
+ fileHandle.write(data.data(using: .utf8)!)
281
+ fileHandle.closeFile()
282
+ completion(nil)
283
+ } catch {
284
+ completion(error)
282
285
  }
283
286
  }
284
287
 
@@ -297,12 +300,16 @@ import UIKit
297
300
  }
298
301
 
299
302
  private func sendSuccessResult(_ callbackId: String, message: String) {
300
- let result = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: message)
301
- self.commandDelegate.send(result, callbackId: callbackId)
303
+ DispatchQueue.main.async {
304
+ let result = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: message)
305
+ self.commandDelegate.send(result, callbackId: callbackId)
306
+ }
302
307
  }
303
308
 
304
309
  private func sendErrorResult(_ callbackId: String, message: String) {
305
- let result = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: message)
306
- self.commandDelegate.send(result, callbackId: callbackId)
310
+ DispatchQueue.main.async {
311
+ let result = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: message)
312
+ self.commandDelegate.send(result, callbackId: callbackId)
313
+ }
307
314
  }
308
315
  }