Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a logging method that supports debug logging in development #218

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"scripts": {
"build": "rollup -c",
"watch": "rollup -c -w --watch.onEnd=\"streamdeck restart com.neil-enns.trackaudio\"",
"watch": "cross-env NODE_ENV=development rollup -c -w --watch.onEnd=\"streamdeck restart com.neil-enns.trackaudio\"",
"pack": "streamdeck pack com.neil-enns.trackaudio.sdPlugin -o .",
"link": "streamdeck link com.neil-enns.trackaudio.sdPlugin",
"lint": "npm run eslint && npm run markdownlint",
Expand All @@ -14,11 +14,13 @@
"@eslint/js": "^9.2.0",
"@rollup/plugin-commonjs": "^26.0.0",
"@rollup/plugin-node-resolve": "^15.2.2",
"@rollup/plugin-replace": "^5.0.7",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.5",
"@tsconfig/node20": "^20.1.2",
"@types/node": "20.14.13",
"@types/ws": "^8.5.10",
"cross-env": "^7.0.3",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"globals": "^15.2.0",
Expand Down
9 changes: 9 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import typescript from "@rollup/plugin-typescript";
import path from "node:path";
import url from "node:url";
import json from "@rollup/plugin-json";
import replace from "@rollup/plugin-replace";

const isWatching = !!process.env.ROLLUP_WATCH;
const sdPlugin = "com.neil-enns.trackaudio.sdPlugin";
Expand All @@ -24,6 +25,14 @@ const config = {
},
},
plugins: [
replace({
preventAssignment: true,
sourcemap: true,
values: {
"process.env.NODE_ENV":
JSON.stringify(process.env.NODE_ENV) ?? "production",
},
}),
{
name: "watch-externals",
buildStart: function () {
Expand Down
3 changes: 2 additions & 1 deletion src/actions/hotline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
WillDisappearEvent,
} from "@elgato/streamdeck";
import actionManager from "@managers/action";
import * as logger from "@utils/logger";

@action({ UUID: "com.neil-enns.trackaudio.hotline" })
/**
Expand All @@ -24,7 +25,7 @@ export class Hotline extends SingletonAction<HotlineSettings> {
ev.action
.setTitle(ev.payload.settings.hotlineCallsign)
.catch((error: unknown) => {
console.error(error);
logger.error(error);
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/managers/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import debounce from "debounce";
import { EventEmitter } from "events";
import vatsimManager from "./vatsim";
import { PushToTalkSettings } from "@actions/pushToTalk";
import * as logger from "@utils/logger";

/**
* Singleton class that manages StreamDeck actions
Expand Down Expand Up @@ -707,7 +708,7 @@ class ActionManager extends EventEmitter {
public showAlertOnAll() {
this.actions.forEach((entry) => {
entry.action.showAlert().catch((error: unknown) => {
console.error(error);
logger.error(error);
});
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/managers/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Handlebars from "handlebars";
import path from "path";
import * as chokidar from "chokidar";
import EventEmitter from "events";
import * as logger from "@utils/logger";

export type CompiledSvgTemplate =
| ReturnType<typeof Handlebars.compile>
Expand Down Expand Up @@ -99,7 +100,7 @@ class SvgTemplateManager extends EventEmitter {

return compiledTemplate;
} catch (err: unknown) {
console.error(err);
logger.error(err);
}

return undefined;
Expand Down
15 changes: 8 additions & 7 deletions src/managers/trackAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "@interfaces/messages";
import { EventEmitter } from "events";
import WebSocket from "ws";
import * as logger from "../utils/logger";

/**
* Manages the websocket connection to TrackAudio.
Expand Down Expand Up @@ -73,7 +74,7 @@ class TrackAudioManager extends EventEmitter {
*/
public connect(): void {
if (this.socket && this.socket.readyState !== WebSocket.CLOSED) {
console.warn("WebSocket is already connected or connecting.");
logger.warn("WebSocket is already connected or connecting.");
return;
}

Expand All @@ -86,12 +87,12 @@ class TrackAudioManager extends EventEmitter {
this.socket = new WebSocket(this.url);

this.socket.on("open", () => {
console.debug("WebSocket connection established.");
logger.debug("WebSocket connection established.");
this.emit("connected");
});

this.socket.on("close", () => {
console.debug("WebSocket connection closed");
logger.debug("WebSocket connection closed");

this._isVoiceConnected = false;
this.emit("disconnected");
Expand All @@ -100,11 +101,11 @@ class TrackAudioManager extends EventEmitter {

this.socket.on("error", (err: Error & { code: string }) => {
if (err.code === "ECONNREFUSED") {
console.error(
logger.error(
"Unable to connect to TrackAudio, connection refused. TrackAudio probably isn't running."
);
} else {
console.error("WebSocket error:", err.message);
logger.error("WebSocket error:", err.message);
}

this._isVoiceConnected = false;
Expand All @@ -122,7 +123,7 @@ class TrackAudioManager extends EventEmitter {
* @param message The message to process
*/
private processMessage(message: string): void {
console.debug("Received: %s", message);
logger.debug("Received: %s", message);

const data = JSON.parse(message) as IncomingMessage;

Expand Down Expand Up @@ -193,7 +194,7 @@ class TrackAudioManager extends EventEmitter {
}

this.reconnectTimer = setTimeout(() => {
console.debug(`Attempting to reconnect...`);
logger.debug(`Attempting to reconnect...`);
this.connect();
}, this.reconnectInterval);
}
Expand Down
3 changes: 2 additions & 1 deletion src/managers/vatsim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { VatsimData } from "@interfaces/vatsim";
import { handleAsyncException } from "@root/utils/handleAsyncException";
import axios from "axios";
import EventEmitter from "events";
import * as logger from "@utils/logger";

/**
* Singleton class that manages communication with VATSIM.
Expand Down Expand Up @@ -34,7 +35,7 @@ class VatsimManager extends EventEmitter {

this.emit("vatsimDataReceived", data);
} catch (error) {
console.error("Error fetching VATSIM data: ", error);
logger.error("Error fetching VATSIM data: ", error);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ import { handleTxEnd } from "@eventHandlers/trackAudio/txEnd";
import { handleVoiceConnectedState } from "@eventHandlers/trackAudio/voiceConnectedState";
import { handleVatsimDataReceived } from "@eventHandlers/vatsim/vatsimDataReceived";
import { handleActionAdded } from "@eventHandlers/action/actionAdded";
import * as logger from "@utils/logger";

// Flag to prevent handling repeated disconnect events
let disconnectHandled = false;

// Register for uncaught exceptions
process.on("uncaughtException", (error) => {
console.error("Uncaught Exception:", error);
logger.error("Uncaught Exception:", error);
});

// Register all the event handlers
Expand Down
4 changes: 3 additions & 1 deletion src/utils/handleAsyncException.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as logger from "@utils/logger";

export const handleAsyncException = (preamble: string, error: unknown) => {
const err = error as Error;
console.error(`${preamble}${err.message}`);
logger.error(`${preamble}${err.message}`);
};
Loading