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 +1 -1
- package/plugin.xml +1 -1
- package/src/electron/Logger.js +1 -1
- package/src/electron/package.json +1 -1
- package/src/ios/Logger.swift +152 -145
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.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.
|
|
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
|
|
package/src/electron/Logger.js
CHANGED
package/src/ios/Logger.swift
CHANGED
|
@@ -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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
|
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
|
-
//
|
|
66
|
-
|
|
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
}
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
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
|
-
|
|
301
|
-
|
|
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
|
-
|
|
306
|
-
|
|
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
|
}
|