Skip to content

Commit

Permalink
Merge pull request #84 from wakatime/bugfix/debug-logging
Browse files Browse the repository at this point in the history
New setting for debug mode
  • Loading branch information
alanhamlett authored Dec 18, 2024
2 parents 7426bcd + b4189cb commit b819afd
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 7 deletions.
2 changes: 2 additions & 0 deletions electron/electron-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ interface Window {
shouldLaunchOnLogIn: () => boolean;
setShouldLaunchOnLogIn: (shouldLaunchOnLogIn: boolean) => void;
shouldLogToFile: () => boolean;
debugMode: () => boolean;
setDebugMode: (debugModeEnabled: boolean) => void;
setShouldLogToFile: (shouldLogToFile: boolean) => void;
autoUpdateEnabled: () => boolean;
setAutoUpdateEnabled: (autoUpdateEnabled: boolean) => void;
Expand Down
2 changes: 1 addition & 1 deletion electron/helpers/config-file-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export abstract class ConfigFileReader {
}
}
const lines = contents
? contents.split("\n").map((line) => line.trim())
? contents.split("\n").map((line) => line.trimEnd())
: [];
let output: string[] = [];
let currentSection = "";
Expand Down
8 changes: 8 additions & 0 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,14 @@ ipcMain.on(IpcKeys.setAutoUpdateEnabled, (_, value) => {
}
});

ipcMain.on(IpcKeys.setDebugMode, (_, value) => {
if (value) {
Logging.instance().enableDebugLogging();
} else {
Logging.instance().disableDebugLogging();
}
});

ipcMain.on(IpcKeys.shouldLogToFile, (event) => {
event.returnValue = PropertiesManager.shouldLogToFile;
});
Expand Down
3 changes: 3 additions & 0 deletions electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,7 @@ contextBridge.exposeInMainWorld("ipcRenderer", {
setShouldLogToFile(shouldLogToFile: boolean) {
ipcRenderer.send(IpcKeys.setShouldLogToFile, shouldLogToFile);
},
setDebugMode(debugMode: boolean) {
ipcRenderer.send(IpcKeys.setDebugMode, debugMode);
},
});
1 change: 1 addition & 0 deletions electron/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const IpcKeys = {
setCodeTimeInStatusBar: "set_code_time_in_status_bar",
shouldLogToFile: "should_log_to_file",
setShouldLogToFile: "set_should_log_to_file",
setDebugMode: "set_debug_mode",
shouldLaunchOnLogin: "should_launch_on_login",
setShouldLaunchOnLogin: "set_should_launch_on_login",
logFilePath: "log_file_path",
Expand Down
19 changes: 14 additions & 5 deletions electron/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Dependencies } from "../../electron/helpers/dependencies";
import { app } from "electron";
import { format } from "date-fns";
import fs from "node:fs";
import { getResourcesFolderPath } from ".";
import path from "node:path";
import { format } from "date-fns";
import { app } from "electron";

import { getResourcesFolderPath } from ".";
import { Dependencies } from "../../electron/helpers/dependencies";

export enum LogLevel {
DEBUG = 0,
Expand All @@ -14,7 +15,7 @@ export enum LogLevel {

export class Logging {
static _instacneCache?: Logging;
level = LogLevel.DEBUG;
level = LogLevel.INFO;
filePath: string | null = null;

static instance(): Logging {
Expand All @@ -33,6 +34,14 @@ export class Logging {
}
}

enableDebugLogging(): void {
this.level = LogLevel.DEBUG;
}

disableDebugLogging(): void {
this.level = LogLevel.INFO;
}

activateLoggingToFile() {
const logFilePath = path.join(
getResourcesFolderPath(),
Expand Down
7 changes: 6 additions & 1 deletion electron/watchers/wakatime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export class Wakatime {
Logging.instance().activateLoggingToFile();
}

const debugMode = ConfigFile.getSetting("settings", "debug") === "true";
if (debugMode) {
Logging.instance().enableDebugLogging();
}

Logging.instance().log(`Starting WakaTime v${app.getVersion()}`);

if (SettingsManager.shouldRegisterAsLogInItem()) {
Expand Down Expand Up @@ -230,7 +235,7 @@ export class Wakatime {
];

const cli = getCLIPath();
Logging.instance().log(`Fetching code time: ${cli} ${args}`);
Logging.instance().log(`Fetching code time: ${cli} ${args.join(" ")}`);

try {
const [output, err] = await exec(cli, ...args);
Expand Down
27 changes: 27 additions & 0 deletions src/pages/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export function SettingsPage() {
const [shouldLogToFile, setShouldLogToFile] = useState(() =>
window.ipcRenderer?.shouldLogToFile(),
);
const [debugMode, setDebugMode] = useState(
() => window.ipcRenderer?.getSetting("settings", "debug") === "true",
);
const [shouldLaunchOnLogIn, setShouldLaunchOnLogIn] = useState(() =>
window.ipcRenderer?.shouldLaunchOnLogIn(),
);
Expand Down Expand Up @@ -65,6 +68,16 @@ export function SettingsPage() {
setAllowlist(value);
}, 200);

const handleDebugModeChange = useCallback((value: boolean) => {
window.ipcRenderer?.setSetting(
"settings",
"debug",
value ? "true" : "false",
);
window.ipcRenderer?.setDebugMode(value);
setDebugMode(value);
}, []);

const handleShouldLogToFileChange = useCallback((value: boolean) => {
window.ipcRenderer?.setShouldLogToFile(value);
setShouldLogToFile(value);
Expand Down Expand Up @@ -159,6 +172,20 @@ export function SettingsPage() {
</Label>
</fieldset>

<fieldset className="flex gap-2">
<Checkbox
id="debug-mode"
className="mt-1"
checked={debugMode}
onCheckedChange={(checked) => {
handleDebugModeChange(checked === true);
}}
/>
<Label htmlFor="debug-mode" className="my-0.5 leading-5">
Enable debug mode
</Label>
</fieldset>

<fieldset className="flex gap-2">
<Checkbox
id="auto-update-enabled"
Expand Down

0 comments on commit b819afd

Please sign in to comment.