Skip to content

Commit

Permalink
Added the ability to redirect logs to file and open them.
Browse files Browse the repository at this point in the history
  • Loading branch information
OrigamingWasTaken committed Jul 1, 2024
1 parent 2cfdcee commit cbb891a
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 14 deletions.
1 change: 1 addition & 0 deletions frontend/src/windows/main/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
async function checkArgs() {
if (window.NL_ARGS.includes("--launch")) {
debug.log("[INFO] Launching Roblox from '--launch?'")
w.hide().catch(console.error);
await launchRoblox();
setTimeout(()=>{
Expand Down
21 changes: 11 additions & 10 deletions frontend/src/windows/main/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// Neutralino dev patch
// import "$lib/neu/init"

import './app.css'
import './ts/window'
import App from './App.svelte'
import { init } from "@neutralinojs/lib"
import Integrations from "./pages/Integrations.svelte"
import "./app.css";
import "./ts/window";
import "./ts/debugging";
import App from "./App.svelte";
import { init } from "@neutralinojs/lib";
import Integrations from "./pages/Integrations.svelte";

init()
init();

const app = new App({
// @ts-expect-error
target: document.getElementById('app'),
})
// @ts-expect-error
target: document.getElementById("app"),
});

export default app
export default app;
46 changes: 43 additions & 3 deletions frontend/src/windows/main/pages/Misc.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import AppIcon from "@/assets/play.icns";
import path from "path-browserify";
import { pathExists } from "../ts/utils";
import { enableConsoleRedirection } from "../ts/debugging";
function settingsChanged(o: Object) {
saveSettings("misc", o);
Expand All @@ -24,7 +25,7 @@
const id = e.detail;
switch (id) {
case "multi_roblox_btn":
await enableMultiInstance()
await enableMultiInstance();
break;
case "open_instance_btn":
os.spawnProcess("/Applications/Roblox.app/Contents/MacOS/RobloxPlayer; exit");
Expand Down Expand Up @@ -93,7 +94,7 @@
if (await pathExists(filePath)) {
await filesystem.remove(filePath);
}
await filesystem.createDirectory(path.dirname(filePath))
await filesystem.createDirectory(path.dirname(filePath));
const fflags = { ...(await parseFFlags(false)), ...(await parseFFlags(true)) };
await filesystem.writeFile(filePath, JSON.stringify(fflags));
toast.success(`Wrote ClientAppSettings at "${filePath}"`);
Expand All @@ -102,6 +103,17 @@
toast.error("An error occured while writing ClientAppSettings.json");
}
break;
case "redirect_console":
enableConsoleRedirection();
toast.success("Console redirection enabled", { duration: 1000 });
break;
case "open_logs":
const logPath = path.join(window.NL_PATH,"neutralinojs.log")
if (!await pathExists(logPath)) {
toast.error("The logs file doesn't seem to exist.")
return;
}
os.execCommand(`open "${logPath}"`).catch(console.error)
}
}
Expand Down Expand Up @@ -148,7 +160,7 @@
{
name: "Roblox Launching",
description: "Settings about launching Roblox",
id: "multi_instances",
id: "roblox_launching",
interactables: [
{
label: "Create a launch shortcut",
Expand All @@ -172,6 +184,34 @@
},
],
},
{
name: "Advanced",
description:
"You shouldn't touch this unless you know what you're doing. This is more meant as a debugging tool.",
id: "advanced",
interactables: [
{
label: "Redirect console.logs to file",
description:
"Redirects every console.log(), console.error(), etc... to the Neutralino logs. Useful for finding bugs and errors.",
id: "redirect_console",
options: {
type: "button",
style: "destructive",
},
},
{
label: "Open logs file",
description:
"Opens the logs file in the preffered text editor.",
id: "open_logs",
options: {
type: "button",
style: "outline",
},
},
],
},
],
};
</script>
Expand Down
98 changes: 98 additions & 0 deletions frontend/src/windows/main/ts/debugging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// This serves the purpose of redirecting every console logs to the neutralinojs.log file for debugging on the users' end.

import { debug } from "@neutralinojs/lib";

let isRedirectionEnabled = false;

function formatConsoleLog(...args: any[]): string {
return args
.map((arg) => {
if (arg === null) {
return "null";
} else if (arg === undefined) {
return "undefined";
} else if (typeof arg === "string") {
return arg;
} else if (typeof arg === "number") {
return arg.toString();
} else if (typeof arg === "boolean") {
return arg.toString();
} else if (Array.isArray(arg)) {
return JSON.stringify(arg);
} else if (typeof arg === "object") {
return JSON.stringify(arg, getCircularReplacer());
} else if (typeof arg === "function") {
return arg.toString();
} else {
return String(arg);
}
})
.join(" ");
}

function getCircularReplacer() {
const seen = new WeakSet();
return (key: string, value: any) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return "[Circular]";
}
seen.add(value);
}
return value;
};
}

const originalConsoleLog = console.log;
const originalConsoleError = console.error;
const originalConsoleWarn = console.warn;
const originalConsoleInfo = console.info;
const originalConsoleDebug = console.debug;

console.log = (...args: any[]) => {
if (isRedirectionEnabled) {
const formattedMessage = formatConsoleLog(...args);
debug.log(formattedMessage);
}
originalConsoleLog.apply(console, args);
};

console.error = (...args: any[]) => {
if (isRedirectionEnabled) {
const formattedMessage = formatConsoleLog(...args);
debug.log(formattedMessage);
}
originalConsoleError.apply(console, args);
};

console.warn = (...args: any[]) => {
if (isRedirectionEnabled) {
const formattedMessage = formatConsoleLog(...args);
debug.log(formattedMessage);
}
originalConsoleWarn.apply(console, args);
};

console.info = (...args: any[]) => {
if (isRedirectionEnabled) {
const formattedMessage = formatConsoleLog(...args);
debug.log(formattedMessage);
}
originalConsoleInfo.apply(console, args);
};

console.debug = (...args: any[]) => {
if (isRedirectionEnabled) {
const formattedMessage = formatConsoleLog(...args);
debug.log(formattedMessage);
}
originalConsoleDebug.apply(console, args);
};

export function enableConsoleRedirection() {
isRedirectionEnabled = true;
}

export function disableConsoleRedirection() {
isRedirectionEnabled = false;
}
2 changes: 1 addition & 1 deletion neutralino.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"exportAuthInfo": false,
"logging": {
"writeToLogFile": false,
"writeToLogFile": true,
"enabled": true
},
"tokenSecurity": "none",
Expand Down

0 comments on commit cbb891a

Please sign in to comment.