-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-utils.js
More file actions
370 lines (319 loc) · 10.3 KB
/
debug-utils.js
File metadata and controls
370 lines (319 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
* Advanced Debug Utilities for hide-dotpaths Plugin
* Copyright (C) 2025 JMSolo (QuickBox.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @author JMSolo
* @version 2.5.210
*/
// Advanced debug cache with intelligent deduplication
const AdvancedDebugCache = {
// Message history with timestamps (max 100 entries)
messageHistory: new Map(),
maxHistorySize: 100,
// Rate limiting per message type
rateLimits: new Map(),
defaultRateLimit: 2000, // 2 seconds default
// Scoped debugging configuration
scopes: new Set(["general", "filter", "patch", "cache", "performance"]),
enabledScopes: new Set(["general"]),
// Performance tracking
stats: {
totalCalls: 0,
skippedDuplicates: 0,
rateLimited: 0,
scopeFiltered: 0,
emitted: 0,
},
// Memory management
lastCleanup: Date.now(),
cleanupInterval: 30000, // 30 seconds
// Configuration
config: {
enableInternalDebug: false, // Disable cache-internal debugging by default
enableStats: true,
enableScopedDebug: true,
maxMessageAge: 60000, // 1 minute
deduplicationWindow: 5000, // 5 seconds
},
};
// Helper function to determine message scope
function getMessageScope(messageKey) {
if (messageKey.includes("filter")) return "filter";
if (messageKey.includes("patch")) return "patch";
if (messageKey.includes("cache")) return "cache";
if (messageKey.includes("performance") || messageKey.includes("instant"))
return "performance";
return "general";
}
// Helper function to check if message should be logged based on scope
function isScopeEnabled(scope) {
return (
AdvancedDebugCache.enabledScopes.has(scope) ||
AdvancedDebugCache.enabledScopes.has("all")
);
}
// Helper function to create message fingerprint
function createMessageFingerprint(messageKey, args) {
const argsString = args.length > 0 ? JSON.stringify(args) : "";
return `${messageKey}:${argsString}`;
}
// Helper function to check if message is important (always log)
function isImportantMessage(messageKey) {
return (
messageKey.includes("filtered-item") ||
messageKey.includes("filtered-filemanager") ||
messageKey.includes("patch-applied") ||
messageKey.includes("checkbox-replaced") ||
messageKey.includes("filtered-count") ||
messageKey.includes("error") ||
messageKey.includes("warning")
);
}
// Self-contained debug logging function for debug-utils.js
function internalDebugLog(messageKey, ...args) {
// Get config from window object (set by conf.php)
const config = window.hide_dotpaths_config || {};
// Only log if debug mode is enabled
if (typeof config.debugMode === "undefined" || !config.debugMode) {
return;
}
// Get formatted message with fallback for when theUILang isn't loaded yet
let message;
if (typeof theUILang !== "undefined" && theUILang[messageKey]) {
message = theUILang[messageKey];
} else {
// Fallback to a simple format when translations aren't available
message = messageKey
.replace(/-/g, " ")
.replace(/\b\w/g, (l) => l.toUpperCase());
}
const formattedMessage = args.length > 0 ? message + args.join("") : message;
// Log with appropriate prefix
console.debug("[hide-dotpaths-cache]", formattedMessage);
}
// Memory cleanup function
function cleanupOldEntries() {
const now = Date.now();
const cutoff = now - AdvancedDebugCache.config.maxMessageAge;
// Clean up old message history
for (const [key, entry] of AdvancedDebugCache.messageHistory.entries()) {
if (entry.timestamp < cutoff) {
AdvancedDebugCache.messageHistory.delete(key);
}
}
// Clean up old rate limits
for (const [key, timestamp] of AdvancedDebugCache.rateLimits.entries()) {
if (timestamp < cutoff) {
AdvancedDebugCache.rateLimits.delete(key);
}
}
AdvancedDebugCache.lastCleanup = now;
if (AdvancedDebugCache.config.enableInternalDebug) {
internalDebugLog(
"hide-dotpaths-debug-cache-cleanup-completed",
AdvancedDebugCache.messageHistory.size
);
}
}
// Helper function to check if message should be logged
function shouldLogMessage(config, messageKey) {
if (typeof config === "undefined") {
internalDebugLog("hide-dotpaths-debug-cache-config-undefined");
return false;
}
if (typeof config.debugMode === "undefined" || !config.debugMode) {
internalDebugLog("hide-dotpaths-debug-cache-debug-disabled");
return false;
}
return true;
}
// Helper function to check rate limiting
function isRateLimited(fingerprint, isImportant) {
if (isImportant) return false;
const lastLogTime = AdvancedDebugCache.rateLimits.get(fingerprint) || 0;
const rateLimit = AdvancedDebugCache.defaultRateLimit;
const now = Date.now();
if (now - lastLogTime < rateLimit) {
AdvancedDebugCache.stats.rateLimited++;
if (AdvancedDebugCache.config.enableInternalDebug) {
internalDebugLog("hide-dotpaths-debug-cache-rate-limited", fingerprint);
}
return true;
}
return false;
}
// Helper function to check for duplicates
function isDuplicate(fingerprint, isImportant) {
if (isImportant) return false;
const historyEntry = AdvancedDebugCache.messageHistory.get(fingerprint);
if (!historyEntry) return false;
const now = Date.now();
const timeSinceLastLog = now - historyEntry.timestamp;
if (timeSinceLastLog < AdvancedDebugCache.config.deduplicationWindow) {
AdvancedDebugCache.stats.skippedDuplicates++;
if (AdvancedDebugCache.config.enableInternalDebug) {
internalDebugLog(
"hide-dotpaths-debug-cache-duplicate-skipped",
fingerprint
);
}
return true;
}
return false;
}
// Helper function to update cache and history
function updateCacheAndHistory(
fingerprint,
messageKey,
formattedMessage,
scope,
isImportant
) {
const now = Date.now();
// Update history
AdvancedDebugCache.messageHistory.set(fingerprint, {
timestamp: now,
messageKey: messageKey,
formattedMessage: formattedMessage,
scope: scope,
});
// Update rate limits for non-important messages
if (!isImportant) {
AdvancedDebugCache.rateLimits.set(fingerprint, now);
}
// Maintain history size limit
if (
AdvancedDebugCache.messageHistory.size > AdvancedDebugCache.maxHistorySize
) {
const oldestKey = AdvancedDebugCache.messageHistory.keys().next().value;
AdvancedDebugCache.messageHistory.delete(oldestKey);
}
}
// Main advanced debug logging function
function advancedDebugLog(messageKey, ...args) {
// Update stats
AdvancedDebugCache.stats.totalCalls++;
// Get config and check if we should log
const config = window.hide_dotpaths_config || {};
if (!shouldLogMessage(config, messageKey)) return;
// Periodic cleanup
const now = Date.now();
if (
now - AdvancedDebugCache.lastCleanup >
AdvancedDebugCache.cleanupInterval
) {
cleanupOldEntries();
}
// Determine message scope and check if enabled
const scope = getMessageScope(messageKey);
if (AdvancedDebugCache.config.enableScopedDebug && !isScopeEnabled(scope)) {
AdvancedDebugCache.stats.scopeFiltered++;
if (AdvancedDebugCache.config.enableInternalDebug) {
internalDebugLog(
"hide-dotpaths-debug-scope-filtered",
messageKey,
" scope: ",
scope
);
}
return;
}
// Create message fingerprint and check if important
const fingerprint = createMessageFingerprint(messageKey, args);
const isImportant = isImportantMessage(messageKey);
// Check rate limiting and duplicates
if (isRateLimited(fingerprint, isImportant)) return;
if (isDuplicate(fingerprint, isImportant)) return;
// Get formatted message and log it
const message = theUILang[messageKey] || messageKey;
const formattedMessage = args.length > 0 ? message + args.join("") : message;
console.debug("[hide-dotpaths]", formattedMessage);
AdvancedDebugCache.stats.emitted++;
// Update cache and history
updateCacheAndHistory(
fingerprint,
messageKey,
formattedMessage,
scope,
isImportant
);
// Log internal debug info
if (AdvancedDebugCache.config.enableInternalDebug) {
internalDebugLog(
"hide-dotpaths-debug-cache-emitted",
messageKey,
" scope: ",
scope
);
}
}
// Configuration management functions
function setDebugScope(scopes) {
if (typeof scopes === "string") {
scopes = scopes.split(",").map((s) => s.trim());
}
AdvancedDebugCache.enabledScopes.clear();
scopes.forEach((scope) => {
if (AdvancedDebugCache.scopes.has(scope) || scope === "all") {
AdvancedDebugCache.enabledScopes.add(scope);
}
});
}
function enableInternalDebug(enable = true) {
AdvancedDebugCache.config.enableInternalDebug = enable;
}
function setRateLimit(limit) {
AdvancedDebugCache.defaultRateLimit = limit;
}
function setDeduplicationWindow(window) {
AdvancedDebugCache.config.deduplicationWindow = window;
}
// Stats and monitoring functions
function getDebugStats() {
return {
...AdvancedDebugCache.stats,
historySize: AdvancedDebugCache.messageHistory.size,
rateLimitSize: AdvancedDebugCache.rateLimits.size,
enabledScopes: Array.from(AdvancedDebugCache.enabledScopes),
config: { ...AdvancedDebugCache.config },
};
}
function resetDebugStats() {
AdvancedDebugCache.stats = {
totalCalls: 0,
skippedDuplicates: 0,
rateLimited: 0,
scopeFiltered: 0,
emitted: 0,
};
}
function clearDebugCache() {
AdvancedDebugCache.messageHistory.clear();
AdvancedDebugCache.rateLimits.clear();
resetDebugStats();
}
// Export for use in main plugin
window.hideDotpathsAdvancedDebug = {
log: advancedDebugLog,
setScope: setDebugScope,
enableInternalDebug: enableInternalDebug,
setRateLimit: setRateLimit,
setDeduplicationWindow: setDeduplicationWindow,
getStats: getDebugStats,
resetStats: resetDebugStats,
clearCache: clearDebugCache,
cache: AdvancedDebugCache,
};