Skip to content

Commit

Permalink
Merge pull request #85 from wakatime/bugfix/error-logging
Browse files Browse the repository at this point in the history
Improve error logging
  • Loading branch information
alanhamlett authored Dec 18, 2024
2 parents b819afd + a5d4109 commit da8e459
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
7 changes: 5 additions & 2 deletions electron/helpers/config-file-reader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "node:fs";

import { Logging } from "../utils/logging";
import { Logging, LogLevel } from "../utils/logging";

export abstract class ConfigFileReader {
static get(file: string, section: string, key: string) {
Expand All @@ -9,7 +9,10 @@ export abstract class ConfigFileReader {
try {
contents = fs.readFileSync(file, { encoding: "utf-8" });
} catch (error) {
Logging.instance().log(`Failed to read file: ${file}. Error: ${error}`);
Logging.instance().log(
`Failed to read file: ${file}. Error: ${error}`,
LogLevel.ERROR,
);
return null;
}

Expand Down
21 changes: 11 additions & 10 deletions electron/helpers/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { LogLevel, Logging } from "../utils/logging";
import fs, { createWriteStream } from "node:fs";
import path from "node:path";
import { pipeline } from "node:stream";
import { promisify } from "node:util";
import {
addHours,
formatDistanceToNow,
Expand All @@ -7,6 +10,10 @@ import {
intervalToDuration,
isBefore,
} from "date-fns";
import fetch from "node-fetch";
import unzpier from "unzipper";
import { z } from "zod";

import {
exec,
getArch,
Expand All @@ -15,15 +22,8 @@ import {
getResourcesFolderPath,
parseJSONObject,
} from "../utils";
import fs, { createWriteStream } from "node:fs";

import { Logging, LogLevel } from "../utils/logging";
import { ConfigFile } from "./config-file";
import fetch from "node-fetch";
import path from "node:path";
import { pipeline } from "node:stream";
import { promisify } from "node:util";
import unzpier from "unzipper";
import { z } from "zod";

const streamPipeline = promisify(pipeline);

Expand Down Expand Up @@ -164,7 +164,7 @@ export abstract class Dependencies {

const [output, err] = await exec(cli, "--version");
if (err) {
Logging.instance().log(`Error 1: ${err}`, LogLevel.ERROR);
Logging.instance().log(`Error: ${err}`, LogLevel.ERROR, true);
}

// disable updating wakatime-cli when it was built from source
Expand Down Expand Up @@ -240,6 +240,7 @@ export abstract class Dependencies {
Logging.instance().log(
`Failed to remove file: ${cli}. Error: ${error}`,
LogLevel.ERROR,
true,
);
}
}
Expand Down
6 changes: 5 additions & 1 deletion electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,11 @@ function createTray() {
tray.popUpContextMenu();
wakatime?.fetchToday();
} catch (error) {
Logging.instance().log(`Tray click error: ${error}`);
Logging.instance().log(
`Tray click error: ${error}`,
LogLevel.ERROR,
true,
);
}
};
tray.addListener("click", handleClick);
Expand Down
28 changes: 22 additions & 6 deletions electron/watchers/wakatime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,29 @@ export class Wakatime {
}

const cli = getCLIPath();
Logging.instance().log(`Sending heartbeat: ${cli} ${args}`, LogLevel.DEBUG);
Logging.instance().log(`Sending heartbeat: ${cli} ${args}`);

try {
const [output, err] = await exec(cli, ...args);
if (err) {
Logging.instance().log(
`Error sending heartbeat: ${err}`,
LogLevel.ERROR,
true,
);
}
if (output) {
Logging.instance().log(output);
Logging.instance().log(
`Output from wakatime-cli when sending heartbeat: ${output}`,
LogLevel.ERROR,
true,
);
}
} catch (error) {
Logging.instance().log(
`Failed to send heartbeat: ${error}`,
`Exception when sending heartbeat: ${error}`,
LogLevel.ERROR,
true,
);
}

Expand Down Expand Up @@ -246,6 +252,13 @@ export class Wakatime {
);
return;
}
if (output) {
Logging.instance().log(
`Output from wakatime-cli when fetching code time: ${output}`,
LogLevel.ERROR,
true,
);
}
this.lastCodeTimeText = output;
this.tray?.setTitle(` ${output}`);
this.tray?.setToolTip(` ${output}`);
Expand All @@ -268,11 +281,11 @@ export class Wakatime {
autoUpdater.autoRunAppAfterInstall = true;

autoUpdater.on("checking-for-update", () => {
Logging.instance().log("Checking for update");
Logging.instance().log("Checking for updates");
});
autoUpdater.on("update-available", async (res) => {
Logging.instance().log(
`Update available. Version: ${res.version}, Files: ${res.files.map((file) => file.url).join(", ")}`,
`New version available. Version: ${res.version}, Files: ${res.files.map((file) => file.url).join(", ")}`,
);
if (!this.canPromptToUpdate(res.version)) {
Logging.instance().log(
Expand Down Expand Up @@ -304,7 +317,10 @@ export class Wakatime {
Logging.instance().log("Update cancelled");
});
autoUpdater.on("error", (err) => {
Logging.instance().log(`electron-updater error. Error: ${err.message}`);
Logging.instance().log(
`electron-updater error. Error: ${err.message}`,
LogLevel.ERROR,
);
});
}

Expand Down
5 changes: 1 addition & 4 deletions electron/watchers/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class Watcher {
isWrite: false,
});
} catch (error) {
Logging.instance().log((error as Error).message);
Logging.instance().log((error as Error).message, LogLevel.ERROR, true);
}
};

Expand Down Expand Up @@ -79,7 +79,6 @@ export class Watcher {

Logging.instance().log(
`App changed from ${this.activeWindow?.info.name || "nil"} to ${windowInfo.info.name}`,
LogLevel.DEBUG,
);

this.activeWindow = windowInfo;
Expand All @@ -91,13 +90,11 @@ export class Watcher {
if (isMonitored) {
Logging.instance().log(
`Monitoring ${windowInfo.info.name}: ${this.activeWindow.info.path}`,
LogLevel.DEBUG,
);
this.watchKeyboardEvents();
} else {
Logging.instance().log(
`Not monitoring ${windowInfo.info.name}: ${this.activeWindow.info.path}`,
LogLevel.DEBUG,
);
}
}
Expand Down

0 comments on commit da8e459

Please sign in to comment.