Prompt to delete large debug logs

Fixes #1648
This commit is contained in:
Zerebos 2024-02-21 19:58:40 -05:00
parent 7a7912ef6d
commit 76bada0f16
2 changed files with 24 additions and 2 deletions

View File

@ -212,7 +212,8 @@
"restartLater": "Restart Later",
"additionalInfo": "Additional Info",
"restartPrompt": "In order to take effect, Discord needs to be restarted. Do you want to restart now?",
"changelog": "Changelog"
"changelog": "Changelog",
"debuglog": "Your debug log file has exceeded 100MB, would you like to clear the log?"
},
"ReactDevTools": {
"notFound": "Extension Not Found",

View File

@ -4,6 +4,8 @@ import path from "path";
import Builtin from "@structs/builtin";
import DataStore from "@modules/datastore";
import Modals from "@ui/modals";
const timestamp = () => new Date().toISOString().replace("T", " ").replace("Z", "");
const levels = ["log", "info", "warn", "error", "debug"];
@ -28,8 +30,9 @@ export default new class DebugLogs extends Builtin {
get category() {return "developer";}
get id() {return "debugLogs";}
enabled() {
async enabled() {
this.logFile = path.join(DataStore.dataFolder, "debug.log");
await this.checkFilesize();
this.stream = fs.createWriteStream(this.logFile, {flags: "a"});
this.stream.write(`\n\n================= Starting Debug Log (${timestamp()}) =================\n`);
for (const level of levels) {
@ -62,4 +65,22 @@ export default new class DebugLogs extends Builtin {
}
return sanitized.join(" ");
}
async checkFilesize() {
try {
const stats = fs.statSync(this.logFile);
const mb = stats.size / (1024 * 1024);
if (mb < 100) return; // Under 100MB, all good
return new Promise(resolve => Modals.showConfirmationModal(Strings.Modals.additionalInfo, Strings.Modals.debuglog, {
confirmText: Strings.Modals.okay,
cancelText: Strings.Modals.cancel,
danger: true,
onConfirm: () => fs.rmSync(this.logFile),
onClose: resolve
}));
}
catch (e) {
this.error(e);
}
}
};