@capgo/capacitor-updater 9.0.0 → 9.0.1
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/CapgoCapacitorUpdater.podspec +3 -6
- package/Package.swift +30 -0
- package/README.md +2382 -292
- package/android/build.gradle +37 -15
- package/android/consumer-rules.pro +48 -0
- package/android/proguard-rules.pro +23 -0
- package/android/src/main/AndroidManifest.xml +0 -1
- package/android/src/main/java/ee/forgr/capacitor_updater/AndroidAppExitReporter.java +92 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/AppLifecycleObserver.java +115 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java +227 -195
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleStatus.java +24 -23
- package/android/src/main/java/ee/forgr/capacitor_updater/Callback.java +13 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +7435 -1243
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +5144 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +564 -121
- package/android/src/main/java/ee/forgr/capacitor_updater/DataManager.java +44 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayCondition.java +43 -49
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUntilNext.java +4 -4
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +427 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DeviceIdHelper.java +230 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +1969 -118
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +278 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +385 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Http3.java +356 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/HttpEngineTransport.java +553 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +79 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +419 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeDetector.java +72 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +959 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/StatsBatchFailureDiagnostics.java +40 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ThreeFingerPinchDetector.java +323 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +306 -0
- package/dist/docs.json +4431 -840
- package/dist/esm/definitions.d.ts +2607 -225
- package/dist/esm/definitions.js +103 -1
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/history.d.ts +1 -0
- package/dist/esm/history.js +283 -0
- package/dist/esm/history.js.map +1 -0
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.js +5 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +53 -42
- package/dist/esm/web.js +180 -37
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +570 -37
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +570 -37
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +262 -0
- package/ios/Sources/CapacitorUpdaterPlugin/AppHealthTracker.swift +82 -0
- package/ios/Sources/CapacitorUpdaterPlugin/BundleInfo.swift +185 -0
- package/ios/Sources/CapacitorUpdaterPlugin/BundleStatus.swift +127 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +5642 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoRawRsa.swift +36 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoSemanticVersion.swift +162 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +4572 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +380 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +343 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DeviceIdHelper.swift +120 -0
- package/ios/Sources/CapacitorUpdaterPlugin/InternalUtils.swift +452 -0
- package/ios/Sources/CapacitorUpdaterPlugin/Logger.swift +380 -0
- package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
- package/ios/Sources/CapacitorUpdaterPlugin/RSA.swift +96 -0
- package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +97 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +871 -0
- package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/UserDefaultsExtension.swift +0 -2
- package/ios/Sources/CapacitorUpdaterPlugin/WKWebView+CapgoObjcBridge.swift +20 -0
- package/ios/Sources/CapacitorUpdaterPlugin/WebViewStatsReporter.swift +304 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ZipArchiveReader.swift +247 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ZipCentralDirectory.swift +280 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/BuiltinHashesTests.swift +210 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/BundleHardeningTests.swift +292 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CapacitorUpdaterPluginTests.swift +3905 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CapgoSemanticVersionTests.swift +64 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ChecksumRequiredTests.swift +151 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CliCryptoFixtureTests.swift +249 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/HttpsOnlyBuiltinMinimumTests.swift +528 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/InstallPerfTests.swift +174 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ListChannelsTests.swift +74 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/LoggerSecurityTests.swift +149 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/NativeContractTests.swift +160 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/NetworkTransportTests.swift +1095 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/PopulateDeltaCacheTests.swift +573 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/RedirectPolicyTests.swift +63 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/RsaContractTests.swift +281 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/SessionKeyRequiredTests.swift +192 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/TestZipWriter.swift +235 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/UpdateLifecycleTests.swift +454 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/WebKitOverlayGuardTests.swift +32 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ZipArchiveReaderTests.swift +302 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ZipBundleExtractionTests.swift +122 -0
- package/package.json +80 -43
- package/scripts/builtin-hashes-lib.mjs +148 -0
- package/scripts/capacitor-builtin-hashes-hook.mjs +65 -0
- package/scripts/resolve-real-path.mjs +25 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java +0 -1130
- package/ios/Plugin/BundleInfo.swift +0 -113
- package/ios/Plugin/BundleStatus.swift +0 -48
- package/ios/Plugin/CapacitorUpdater.swift +0 -858
- package/ios/Plugin/CapacitorUpdaterPlugin.h +0 -10
- package/ios/Plugin/CapacitorUpdaterPlugin.m +0 -27
- package/ios/Plugin/CapacitorUpdaterPlugin.swift +0 -677
- package/ios/Plugin/CryptoCipher.swift +0 -240
- /package/{LICENCE → LICENSE} +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayCondition.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayUntilNext.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/Info.plist +0 -0
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
package ee.forgr.capacitor_updater;
|
|
2
|
+
|
|
3
|
+
import android.util.ArrayMap;
|
|
4
|
+
import android.util.Log;
|
|
5
|
+
import androidx.annotation.NonNull;
|
|
6
|
+
import androidx.annotation.Nullable;
|
|
7
|
+
import com.getcapacitor.*;
|
|
8
|
+
import java.nio.ByteBuffer;
|
|
9
|
+
import java.nio.CharBuffer;
|
|
10
|
+
import java.nio.charset.CharsetEncoder;
|
|
11
|
+
import java.nio.charset.CoderResult;
|
|
12
|
+
import java.nio.charset.CodingErrorAction;
|
|
13
|
+
import java.nio.charset.StandardCharsets;
|
|
14
|
+
import java.util.ArrayList;
|
|
15
|
+
import java.util.Arrays;
|
|
16
|
+
import java.util.Date;
|
|
17
|
+
import java.util.Locale;
|
|
18
|
+
import java.util.Map;
|
|
19
|
+
import org.jetbrains.annotations.Contract;
|
|
20
|
+
import org.json.JSONObject;
|
|
21
|
+
|
|
22
|
+
public class Logger {
|
|
23
|
+
|
|
24
|
+
static final int MAX_WEBVIEW_LOG_PAYLOAD_CHARS = 4096;
|
|
25
|
+
|
|
26
|
+
private Bridge bridge;
|
|
27
|
+
|
|
28
|
+
public enum LogLevel {
|
|
29
|
+
silent,
|
|
30
|
+
error,
|
|
31
|
+
warn,
|
|
32
|
+
info,
|
|
33
|
+
debug
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public static class Options {
|
|
37
|
+
|
|
38
|
+
LogLevel level;
|
|
39
|
+
Map<String, String> labels;
|
|
40
|
+
boolean useSystemLog;
|
|
41
|
+
|
|
42
|
+
Options() {
|
|
43
|
+
level = LogLevel.info;
|
|
44
|
+
labels = null;
|
|
45
|
+
useSystemLog = true; // Default to true for backward compatibility
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
Options(@NonNull Options other) {
|
|
49
|
+
level = other.level;
|
|
50
|
+
labels = other.labels;
|
|
51
|
+
useSystemLog = other.useSystemLog;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
Options(LogLevel level) {
|
|
55
|
+
this.level = level;
|
|
56
|
+
this.labels = null;
|
|
57
|
+
useSystemLog = true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
Options(Map<String, String> labels) {
|
|
61
|
+
this.level = LogLevel.info;
|
|
62
|
+
this.labels = labels;
|
|
63
|
+
useSystemLog = true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
Options(LogLevel level, Map<String, String> labels) {
|
|
67
|
+
this.level = level;
|
|
68
|
+
this.labels = labels;
|
|
69
|
+
useSystemLog = true;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
Options(LogLevel level, Map<String, String> labels, boolean useSystemLog) {
|
|
73
|
+
this.level = level;
|
|
74
|
+
this.labels = labels;
|
|
75
|
+
this.useSystemLog = useSystemLog;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
Options(boolean useSystemLog) {
|
|
79
|
+
this.level = LogLevel.info;
|
|
80
|
+
this.labels = null;
|
|
81
|
+
this.useSystemLog = useSystemLog;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public LogLevel level;
|
|
86
|
+
private final Map<LogLevel, String> labels = new ArrayMap<>();
|
|
87
|
+
private String tag;
|
|
88
|
+
private final ArrayMap<String, Long> timers = new ArrayMap<>();
|
|
89
|
+
private final String kDefaultTimerLabel = "default";
|
|
90
|
+
private boolean useSystemLog;
|
|
91
|
+
|
|
92
|
+
public void setBridge(Bridge bridge) {
|
|
93
|
+
this.bridge = bridge;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public Logger(String tag) {
|
|
97
|
+
super();
|
|
98
|
+
init(tag, new Options());
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public Logger(String tag, Options options) {
|
|
102
|
+
super();
|
|
103
|
+
init(tag, options);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private void init(String tag, @NonNull Options options) {
|
|
107
|
+
this.level = options.level;
|
|
108
|
+
this.useSystemLog = options.useSystemLog;
|
|
109
|
+
this.labels.putAll(
|
|
110
|
+
Map.of(LogLevel.silent, "", LogLevel.error, "🔴", LogLevel.warn, "🟠", LogLevel.info, "🟢", LogLevel.debug, "\uD83D\uDD0E")
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
if (options.labels != null) {
|
|
114
|
+
setLabels(options.labels);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
this.tag = tag;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
@Nullable
|
|
121
|
+
public Object getLevelWithName(String name) {
|
|
122
|
+
try {
|
|
123
|
+
return LogLevel.valueOf(name);
|
|
124
|
+
} catch (IllegalArgumentException ex) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
public String getLevelName() {
|
|
130
|
+
return level.name();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public void setLevelName(String name) {
|
|
134
|
+
try {
|
|
135
|
+
level = LogLevel.valueOf(name);
|
|
136
|
+
} catch (IllegalArgumentException e) {
|
|
137
|
+
// Ignore it
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public Map<String, String> getLabels() {
|
|
142
|
+
ArrayMap<String, String> result = new ArrayMap<>();
|
|
143
|
+
|
|
144
|
+
for (Map.Entry<LogLevel, String> entry : labels.entrySet()) {
|
|
145
|
+
result.put(entry.getKey().name(), entry.getValue());
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
public void setLabels(@NonNull Map<String, String> labels) {
|
|
152
|
+
for (Map.Entry<String, String> entry : labels.entrySet()) {
|
|
153
|
+
Object level = getLevelWithName(entry.getKey());
|
|
154
|
+
|
|
155
|
+
if (level != null) {
|
|
156
|
+
this.labels.put((LogLevel) level, entry.getValue());
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
public String getTag() {
|
|
162
|
+
return tag;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public void setTag(@NonNull String tag) {
|
|
166
|
+
if (!tag.isEmpty()) {
|
|
167
|
+
this.tag = tag;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
public void error(String message) {
|
|
172
|
+
logWithTagAtLevel(LogLevel.error, "", tag, message);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
public void warn(String message) {
|
|
176
|
+
logWithTagAtLevel(LogLevel.warn, "", tag, message);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
public void info(String message) {
|
|
180
|
+
logWithTagAtLevel(LogLevel.info, "", tag, message);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
public void log(String message) {
|
|
184
|
+
info(message);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
public void debug(String message) {
|
|
188
|
+
logWithTagAtLevel(LogLevel.debug, "", tag, message);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
public void logAtLevel(LogLevel level, String message) {
|
|
192
|
+
logWithTagAtLevel(level, "", tag, message);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
public void logAtLevel(String level, String message) {
|
|
196
|
+
logWithTagAtLevel(resolveLevelName(level), "", tag, message);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
public void logWithTagAtLevel(LogLevel level, String tag, String message) {
|
|
200
|
+
logWithTagAtLevel(level, "", tag, message);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
public void logWithTagAtLevel(String level, String tag, String message) {
|
|
204
|
+
logWithTagAtLevel(resolveLevelName(level), "", tag, message);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private LogLevel resolveLevelName(String name) {
|
|
208
|
+
Object level = getLevelWithName(name);
|
|
209
|
+
|
|
210
|
+
if (level != null) {
|
|
211
|
+
return (LogLevel) level;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return LogLevel.info;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
public void logWithTagAtLevel(LogLevel level, String label, String tag, String message) {
|
|
218
|
+
if (this.level.compareTo(level) < 0) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (label.isEmpty()) {
|
|
223
|
+
label = labels.get(level);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// If the label is ASCII, surround it with []
|
|
227
|
+
String format;
|
|
228
|
+
|
|
229
|
+
if (label != null) {
|
|
230
|
+
format = label.charAt(0) <= 127 ? "[%s]: %s" : "%s %s";
|
|
231
|
+
} else {
|
|
232
|
+
label = "";
|
|
233
|
+
format = "%s%s";
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
String formattedMessage = String.format(format, label, message);
|
|
237
|
+
|
|
238
|
+
if (tag.isEmpty()) {
|
|
239
|
+
tag = this.tag;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Log to Android system log if enabled
|
|
243
|
+
if (useSystemLog) {
|
|
244
|
+
switch (level) {
|
|
245
|
+
case error:
|
|
246
|
+
Log.e(tag, formattedMessage);
|
|
247
|
+
break;
|
|
248
|
+
case warn:
|
|
249
|
+
Log.w(tag, formattedMessage);
|
|
250
|
+
break;
|
|
251
|
+
case info:
|
|
252
|
+
Log.i(tag, formattedMessage);
|
|
253
|
+
break;
|
|
254
|
+
case debug:
|
|
255
|
+
Log.d(tag, formattedMessage);
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Send to JavaScript if webView is available
|
|
261
|
+
if (bridge != null && bridge.getWebView() != null) {
|
|
262
|
+
String script = buildWebViewConsoleScript(level, tag, formattedMessage);
|
|
263
|
+
if (script != null) {
|
|
264
|
+
bridge.eval(script, null);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
@Nullable
|
|
270
|
+
static String consoleMethodForLevel(@NonNull LogLevel level) {
|
|
271
|
+
switch (level) {
|
|
272
|
+
case error:
|
|
273
|
+
return "error";
|
|
274
|
+
case warn:
|
|
275
|
+
return "warn";
|
|
276
|
+
case info:
|
|
277
|
+
return "info";
|
|
278
|
+
case debug:
|
|
279
|
+
return "debug";
|
|
280
|
+
default:
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
@NonNull
|
|
286
|
+
static String toJsStringLiteral(@NonNull String value) {
|
|
287
|
+
return JSONObject.quote(value);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
@NonNull
|
|
291
|
+
static String capWebViewLogPayload(@NonNull String payload) {
|
|
292
|
+
CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder()
|
|
293
|
+
.onMalformedInput(CodingErrorAction.REPLACE)
|
|
294
|
+
.onUnmappableCharacter(CodingErrorAction.REPLACE);
|
|
295
|
+
ByteBuffer bytes = ByteBuffer.allocate(MAX_WEBVIEW_LOG_PAYLOAD_CHARS);
|
|
296
|
+
CharBuffer chars = CharBuffer.wrap(payload);
|
|
297
|
+
CoderResult result = encoder.encode(chars, bytes, true);
|
|
298
|
+
if (result.isUnderflow()) {
|
|
299
|
+
result = encoder.flush(bytes);
|
|
300
|
+
}
|
|
301
|
+
if (result.isUnderflow()) {
|
|
302
|
+
return payload;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return new String(bytes.array(), 0, bytes.position(), StandardCharsets.UTF_8) + "...";
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
@Nullable
|
|
309
|
+
static String buildWebViewConsoleScript(@NonNull LogLevel level, @NonNull String tag, @NonNull String formattedMessage) {
|
|
310
|
+
String consoleMethod = consoleMethodForLevel(level);
|
|
311
|
+
if (consoleMethod == null) {
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
String payload = capWebViewLogPayload("[" + tag + "] " + formattedMessage);
|
|
316
|
+
return "console." + consoleMethod + "(" + toJsStringLiteral(payload) + ")";
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
public void dir(Object value) {
|
|
320
|
+
String message;
|
|
321
|
+
|
|
322
|
+
if (value != null) {
|
|
323
|
+
if (value.getClass().isArray()) {
|
|
324
|
+
Object[] arr = (Object[]) value;
|
|
325
|
+
message = Arrays.deepToString(arr);
|
|
326
|
+
} else {
|
|
327
|
+
try {
|
|
328
|
+
message = value.toString();
|
|
329
|
+
} catch (Exception ex) {
|
|
330
|
+
message = String.format("<%s>", ex.getMessage());
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
} else {
|
|
334
|
+
message = "null";
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
info(message);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
public void time() {
|
|
341
|
+
time(kDefaultTimerLabel);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
public void time(String label) {
|
|
345
|
+
timers.put(label, new Date().getTime());
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
public void timeLog() {
|
|
349
|
+
timeLog(kDefaultTimerLabel);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
public void timeLog(String label) {
|
|
353
|
+
label = resolveTimerLabel(label);
|
|
354
|
+
|
|
355
|
+
if (timers.containsKey(label)) {
|
|
356
|
+
Long start = timers.get(label);
|
|
357
|
+
|
|
358
|
+
// This will always be true
|
|
359
|
+
if (start != null) {
|
|
360
|
+
long now = new Date().getTime();
|
|
361
|
+
long diff = now - start;
|
|
362
|
+
info(String.format(Locale.getDefault(), "%s: %s", label, formatMilliseconds(diff)));
|
|
363
|
+
}
|
|
364
|
+
} else {
|
|
365
|
+
warn(String.format("timer '%s' does not exist", label));
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
public void timeEnd() {
|
|
370
|
+
timeEnd(kDefaultTimerLabel);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
public void timeEnd(String label) {
|
|
374
|
+
label = resolveTimerLabel(label);
|
|
375
|
+
timeLog(label);
|
|
376
|
+
timers.remove(label);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
@Contract(pure = true)
|
|
380
|
+
private String resolveTimerLabel(@NonNull String label) {
|
|
381
|
+
return label.isEmpty() ? kDefaultTimerLabel : label;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
@NonNull
|
|
385
|
+
private String formatMilliseconds(long milliseconds) {
|
|
386
|
+
long seconds = Math.floorDiv(milliseconds, 1000);
|
|
387
|
+
long minutes = Math.floorDiv(seconds, 60);
|
|
388
|
+
long hours = Math.floorDiv(minutes, 60);
|
|
389
|
+
long millis = milliseconds % 1000;
|
|
390
|
+
|
|
391
|
+
if (seconds < 1) {
|
|
392
|
+
return String.format(Locale.getDefault(), "%dms", milliseconds);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (minutes < 1) {
|
|
396
|
+
return String.format(Locale.getDefault(), "%d.%ds", seconds, millis);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
seconds = seconds % 60;
|
|
400
|
+
|
|
401
|
+
if (hours < 1) {
|
|
402
|
+
return String.format(Locale.getDefault(), "%d:%02d.%03d (min:sec.ms)", minutes, seconds, millis);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
minutes = minutes % 60;
|
|
406
|
+
return String.format(Locale.getDefault(), "%d:%02d:%02d (hr:min:sec)", hours, minutes, seconds);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
public void trace() {
|
|
410
|
+
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
|
411
|
+
ArrayList<String> stack = new ArrayList<>();
|
|
412
|
+
|
|
413
|
+
for (StackTraceElement element : stackTrace) {
|
|
414
|
+
stack.add(element.toString());
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
info("trace\n" + String.join("\n", stack));
|
|
418
|
+
}
|
|
419
|
+
}
|