From e5d885cc3e57473376f31f2089207a277624a041 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 11 Dec 2022 21:11:29 -0500 Subject: [PATCH] Logging Update - Changed to uniform logging with the 'debug' module - Updated all apps --- Client/bin/www | 6 ++++-- Client/controllers/clientController.js | 5 +++-- Client/discord-bot/app.js | 7 ++++--- Client/discord-bot/commands/join.js | 1 + Client/discord-bot/commands/leave.js | 2 +- .../discord-bot/controllers/audioController.js | 4 ++-- Client/discord-bot/utilities/configHandler.js | 4 ++-- Client/discord-bot/utilities/debugBuilder.js | 17 ----------------- Client/discord-bot/utilities/messageHandler.js | 4 ++-- .../discord-bot/utilities/moduleDebugBuilder.js | 17 +++++++++++++++++ .../discord-bot/utilities/registerCommands.js | 4 ++-- Client/utilities/debugBuilder.js | 17 +++++++++++++++++ Client/utilities/httpRequests.js | 9 +++++---- Client/utilities/updateConfig.js | 5 +++-- Client/utilities/updatePresets.js | 13 +++++++------ Server/bin/www | 6 ++++-- Server/controllers/nodesController.js | 12 ++++++++---- Server/routes/admin.js | 6 +++++- Server/utilities/debugBuilder.js | 17 +++++++++++++++++ Server/{ => utilities}/mysqlHandler.js | 2 +- Server/{ => utilities}/utils.js | 0 21 files changed, 105 insertions(+), 53 deletions(-) delete mode 100644 Client/discord-bot/utilities/debugBuilder.js create mode 100644 Client/discord-bot/utilities/moduleDebugBuilder.js create mode 100644 Client/utilities/debugBuilder.js create mode 100644 Server/utilities/debugBuilder.js rename Server/{ => utilities}/mysqlHandler.js (98%) rename Server/{ => utilities}/utils.js (100%) diff --git a/Client/bin/www b/Client/bin/www index 0412959..b0fd6d4 100644 --- a/Client/bin/www +++ b/Client/bin/www @@ -5,7 +5,9 @@ */ const app = require('../app'); -const debug = require('debug')('client:server'); +// Debug +const { DebugBuilder } = require("../utilities/debugBuilder.js"); +const log = new DebugBuilder("client", "www"); const http = require('http'); const config = require('../config/clientConfig'); const clientController = require('../controllers/clientController'); @@ -67,7 +69,7 @@ function onListening() { const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; - debug('Listening on ' + bind); + log.DEBUG('Listening on ' + bind); // check in with the server to add this node or come back online clientController.checkIn(); diff --git a/Client/controllers/clientController.js b/Client/controllers/clientController.js index b23848b..03cab7f 100644 --- a/Client/controllers/clientController.js +++ b/Client/controllers/clientController.js @@ -1,5 +1,6 @@ -// Debug output -const debug = require('debug')('client:clientController'); +// Debug +// const { DebugBuilder } = require("../utilities/debugBuilder.js"); +// const log = new DebugBuilder("client", "clientController"); // Configs const config = require("../config/clientConfig"); const modes = require("../config/modes"); diff --git a/Client/discord-bot/app.js b/Client/discord-bot/app.js index 55b2146..6323d5b 100644 --- a/Client/discord-bot/app.js +++ b/Client/discord-bot/app.js @@ -5,8 +5,8 @@ import ping from './commands/ping.js'; import join from './commands/join.js'; import leave from './commands/leave.js'; // Debug -import debugBuilder from "./utilities/debugBuilder.js"; -const log = new debugBuilder("bot", "app"); +import ModuleDebugBuilder from "./utilities/moduleDebugBuilder.js"; +const log = new ModuleDebugBuilder("bot", "app"); // Modules import { Client, GatewayIntentBits } from 'discord.js'; // Utilities @@ -65,4 +65,5 @@ function main(){ }); } -main(); \ No newline at end of file +main(); +//module.exports = client; \ No newline at end of file diff --git a/Client/discord-bot/commands/join.js b/Client/discord-bot/commands/join.js index 496eba7..3e50f57 100644 --- a/Client/discord-bot/commands/join.js +++ b/Client/discord-bot/commands/join.js @@ -28,6 +28,7 @@ export default async function join(interaction){ audioInstance.on('data', buffer => { buffer = Buffer.from(buffer); const encoded = encoder.encode(buffer); + // TODO Add a function here to check the volume of either buffer and only play audio to discord when there is audio to be played voiceConnection.playOpusPacket(encoded); }) diff --git a/Client/discord-bot/commands/leave.js b/Client/discord-bot/commands/leave.js index 3806770..06bbdb8 100644 --- a/Client/discord-bot/commands/leave.js +++ b/Client/discord-bot/commands/leave.js @@ -1,7 +1,7 @@ import {getVoiceConnection} from "@discordjs/voice"; import {replyToInteraction} from "../utilities/messageHandler.js"; // Debug -//import debugBuilder from "../utilities/debugBuilder.js"; +//import debugBuilder from "../utilities/moduleDebugBuilder.js"; //const log = new debugBuilder("bot", "leave"); /** diff --git a/Client/discord-bot/controllers/audioController.js b/Client/discord-bot/controllers/audioController.js index aa642f3..7ad034e 100644 --- a/Client/discord-bot/controllers/audioController.js +++ b/Client/discord-bot/controllers/audioController.js @@ -3,8 +3,8 @@ import {getDeviceID} from '../utilities/configHandler.js' // Modules import portAudio from 'naudiodon'; // Debug -import debugBuilder from "../utilities/debugBuilder.js"; -const log = new debugBuilder("bot", "audioController"); +import ModuleDebugBuilder from "../utilities/moduleDebugBuilder.js"; +const log = new ModuleDebugBuilder("bot", "audioController"); /** * Checks to make sure the selected audio device is available and returns the device object (PortAudio Device Info) diff --git a/Client/discord-bot/utilities/configHandler.js b/Client/discord-bot/utilities/configHandler.js index 1c46179..1d67fe3 100644 --- a/Client/discord-bot/utilities/configHandler.js +++ b/Client/discord-bot/utilities/configHandler.js @@ -1,7 +1,7 @@ import { readFileSync } from 'fs'; // Debug -import debugBuilder from "./debugBuilder.js"; -const log = new debugBuilder("bot", "configHandler"); +import ModuleDebugBuilder from "./moduleDebugBuilder.js"; +const log = new ModuleDebugBuilder("bot", "configHandler"); export function getConfig() { return JSON.parse(readFileSync("./config/botConfig.json")); diff --git a/Client/discord-bot/utilities/debugBuilder.js b/Client/discord-bot/utilities/debugBuilder.js deleted file mode 100644 index f957b0f..0000000 --- a/Client/discord-bot/utilities/debugBuilder.js +++ /dev/null @@ -1,17 +0,0 @@ -// Debug -import Debug from 'debug'; - -export default class debugBuilder { - /** - * Create the different logging methods for a function - * Namespace template = ("[app]:[fileName]:['INFO', 'WARNING', 'DEBUG', 'ERROR']") - * @param {string} appName The name of the app to be used in the 'app' portion of the namespace - * @param {string} fileName The name of the file calling the builder to be used in the 'fileName' portion of the namespace - */ - constructor(appName, fileName) { - this.INFO = Debug(`${appName}:${fileName}:INFO`); - this.DEBUG = Debug(`${appName}:${fileName}:DEBUG`); - this.WARN = Debug(`${appName}:${fileName}:WARNING`); - this.ERROR = Debug(`${appName}:${fileName}::ERROR`); - } -} \ No newline at end of file diff --git a/Client/discord-bot/utilities/messageHandler.js b/Client/discord-bot/utilities/messageHandler.js index 33b84aa..ac68783 100644 --- a/Client/discord-bot/utilities/messageHandler.js +++ b/Client/discord-bot/utilities/messageHandler.js @@ -1,6 +1,6 @@ // Debug -import debugBuilder from "./debugBuilder.js"; -const log = new debugBuilder("bot", "messageHandler"); +import ModuleDebugBuilder from "./moduleDebugBuilder.js"; +const log = new ModuleDebugBuilder("bot", "messageHandler"); export function replyToInteraction(interaction, message){ interaction.reply({ content: message, fetchReply: true }) diff --git a/Client/discord-bot/utilities/moduleDebugBuilder.js b/Client/discord-bot/utilities/moduleDebugBuilder.js new file mode 100644 index 0000000..879ac98 --- /dev/null +++ b/Client/discord-bot/utilities/moduleDebugBuilder.js @@ -0,0 +1,17 @@ +// Debug +import Debug from 'debug'; + +/** + * Create the different logging methods for a function + * Namespace template = ("[app]:[fileName]:['INFO', 'WARNING', 'DEBUG', 'ERROR']") + * @param {string} appName The name of the app to be used in the 'app' portion of the namespace + * @param {string} fileName The name of the file calling the builder to be used in the 'fileName' portion of the namespace + */ +export default class ModuleDebugBuilder { + constructor(appName, fileName) { + this.INFO = Debug(`${appName}:${fileName}:INFO`); + this.DEBUG = Debug(`${appName}:${fileName}:DEBUG`); + this.WARN = Debug(`${appName}:${fileName}:WARNING`); + this.ERROR = Debug(`${appName}:${fileName}:ERROR`); + } +} \ No newline at end of file diff --git a/Client/discord-bot/utilities/registerCommands.js b/Client/discord-bot/utilities/registerCommands.js index 0643b8b..888c0cf 100644 --- a/Client/discord-bot/utilities/registerCommands.js +++ b/Client/discord-bot/utilities/registerCommands.js @@ -3,8 +3,8 @@ import {REST} from "@discordjs/rest"; import {getApplicationID, getGuildID, getTOKEN} from "./configHandler.js"; import { Routes, ChannelType } from "discord.js"; // Debug -import debugBuilder from "./debugBuilder.js"; -const log = new debugBuilder("bot", "registerCommands"); +import ModuleDebugBuilder from "./moduleDebugBuilder.js"; +const log = new ModuleDebugBuilder("bot", "registerCommands"); const pingCommand = new SlashCommandBuilder() .setName("ping") diff --git a/Client/utilities/debugBuilder.js b/Client/utilities/debugBuilder.js new file mode 100644 index 0000000..1c2a621 --- /dev/null +++ b/Client/utilities/debugBuilder.js @@ -0,0 +1,17 @@ +// Debug +const debug = require('debug'); + +/** + * Create the different logging methods for a function + * Namespace template = ("[app]:[fileName]:['INFO', 'WARNING', 'DEBUG', 'ERROR']") + * @param {string} appName The name of the app to be used in the 'app' portion of the namespace + * @param {string} fileName The name of the file calling the builder to be used in the 'fileName' portion of the namespace + */ +exports.DebugBuilder = class DebugBuilder { + constructor(appName, fileName) { + this.INFO = debug(`${appName}:${fileName}:INFO`); + this.DEBUG = debug(`${appName}:${fileName}:DEBUG`); + this.WARN = debug(`${appName}:${fileName}:WARNING`); + this.ERROR = debug(`${appName}:${fileName}:ERROR`); + } +} \ No newline at end of file diff --git a/Client/utilities/httpRequests.js b/Client/utilities/httpRequests.js index 718b6a6..ddd11b6 100644 --- a/Client/utilities/httpRequests.js +++ b/Client/utilities/httpRequests.js @@ -1,5 +1,6 @@ // Debug -const debug = require('debug')('client:httpRequests'); +const { DebugBuilder } = require("../utilities/debugBuilder.js"); +const log = new DebugBuilder("client", "httpRequests"); // Config const config = require("../config/clientConfig"); // Modules @@ -26,7 +27,7 @@ exports.requestOptions = class requestOptions { * @param callback */ exports.sendHttpRequest = function sendHttpRequest(requestOptions, data, callback){ - debug("Sending a request to: ", requestOptions.hostname, requestOptions.port) + log.DEBUG("Sending a request to: ", requestOptions.hostname, requestOptions.port) // Create the request const req = http.request(requestOptions, res => { res.on('data', (data) => { @@ -34,11 +35,11 @@ exports.sendHttpRequest = function sendHttpRequest(requestOptions, data, callbac "statusCode": res.statusCode, "body": JSON.parse(data) }; - debug("Response Object: ", responseObject); + log.DEBUG("Response Object: ", responseObject); callback(responseObject); }) }).on('error', err => { - debug('Error: ', err.message) + log.ERROR('Error: ', err.message) // TODO need to handle if the server is down }) diff --git a/Client/utilities/updateConfig.js b/Client/utilities/updateConfig.js index c0af774..22d17c4 100644 --- a/Client/utilities/updateConfig.js +++ b/Client/utilities/updateConfig.js @@ -1,5 +1,6 @@ // Debug -const debug = require('debug')('client:updateConfig'); +const { DebugBuilder } = require("../utilities/debugBuilder.js"); +const log = new DebugBuilder("client", "updateConfig"); // Modules const replace = require('replace-in-file'); @@ -35,7 +36,7 @@ exports.updateId = (updatedId) => { function updateConfigFile(options, callback){ replace(options, (error, changedFiles) => { if (error) return console.error('Error occurred:', error); - debug('Modified files:', changedFiles); + log.DEBUG('Modified files:', changedFiles); callback(changedFiles); }); } \ No newline at end of file diff --git a/Client/utilities/updatePresets.js b/Client/utilities/updatePresets.js index 6e275a4..6a7fee1 100644 --- a/Client/utilities/updatePresets.js +++ b/Client/utilities/updatePresets.js @@ -1,5 +1,6 @@ // Debug -const debug = require('debug')('client:updatePresets'); +const { DebugBuilder } = require("../utilities/debugBuilder.js"); +const log = new DebugBuilder("client", "updatePresets"); // Modules const fs = require('fs'); @@ -12,7 +13,7 @@ function writePresets(presets, callback = undefined) { fs.writeFile("../config/radioPresets.json", JSON.stringify(presets), (err) => { // Error checking if (err) throw err; - debug("Write Complete"); + log.DEBUG("Write Complete"); if (callback) callback() }); } @@ -29,7 +30,7 @@ function sanitizeFrequencies(frequenciesArray) { sanitizedFrequencyArray.push(convertFrequencyToHertz(freq)); } - debug(sanitizedFrequencyArray); + log.DEBUG("Sanitized Frequency Array", sanitizedFrequencyArray); return sanitizedFrequencyArray; } @@ -42,12 +43,12 @@ function convertFrequencyToHertz(frequency){ // check if the passed value is a number if(typeof frequency == 'number' && !isNaN(frequency)){ if (Number.isInteger(frequency)) { - debug(`${frequency} is integer.`); + log.DEBUG(`${frequency} is an integer.`); // Check to see if the frequency has the correct length if (frequency.toString().length >= 7 && frequency.toString().length <= 9) return frequency } else { - debug(`${frequency} is a float value.`); + log.DEBUG(`${frequency} is a float value.`); // Convert to a string to remove the decimal in place and then correct the length frequency = frequency.toString(); // One extra digit checked for the '.' included in the string @@ -63,7 +64,7 @@ function convertFrequencyToHertz(frequency){ } } } else { - debug(`${frequency} is not a number`); + log.DEBUG(`${frequency} is not a number`); frequency = convertFrequencyToHertz(parseFloat(frequency)); return parseInt(frequency) diff --git a/Server/bin/www b/Server/bin/www index b09c4b4..39c066d 100644 --- a/Server/bin/www +++ b/Server/bin/www @@ -5,7 +5,9 @@ */ var app = require('../app'); -var debug = require('debug')('server:server'); +// Debug +const { DebugBuilder } = require("../utilities/debugBuilder.js"); +const log = new DebugBuilder("server", "www"); var http = require('http'); /** @@ -86,5 +88,5 @@ function onListening() { var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; - debug('Listening on ' + bind); + log.DEBUG('Listening on ' + bind); } diff --git a/Server/controllers/nodesController.js b/Server/controllers/nodesController.js index 8f1fbe9..7387e07 100644 --- a/Server/controllers/nodesController.js +++ b/Server/controllers/nodesController.js @@ -1,5 +1,9 @@ -const mysqlHander = require("../mysqlHandler"); -const utils = require("../utils"); +// Debug +const { DebugBuilder } = require("../utilities/debugBuilder.js"); +const log = new DebugBuilder("server", "nodesController"); +// Utilities +const mysqlHander = require("../utilities/mysqlHandler"); +const utils = require("../utilities/utils"); exports.listAllNodes = async (req, res) => { mysqlHander.getAllNodes((allNodes) => { @@ -32,7 +36,7 @@ exports.newNode = async (req, res) => { if (err === "No name provided") { return res.sendStatus(400); } - else console.log(err) + else log.ERROR(err) return res.sendStatus(500); } } @@ -65,7 +69,7 @@ exports.nodeCheckIn = async (req, res) => { // If no changes are made tell the client if (Object.keys(nodeObject).length === 0) return res.status(200).json("No keys updated"); - console.log("Updating the following keys for ID:", req.body.id, nodeObject); + log.INFO("Updating the following keys for ID: ", req.body.id, nodeObject); // Adding the ID key to the body so that the client can double-check their ID nodeObject.id = req.body.id; mysqlHander.updateNodeInfo(nodeObject, () => { diff --git a/Server/routes/admin.js b/Server/routes/admin.js index 39e6daa..cf461b6 100644 --- a/Server/routes/admin.js +++ b/Server/routes/admin.js @@ -1,3 +1,7 @@ +// Debug +const { DebugBuilder } = require("../utilities/debugBuilder.js"); +const log = new DebugBuilder("server", "admin"); +// Modules var express = require('express'); var router = express.Router(); @@ -8,7 +12,7 @@ router.get('/', (req, res) => { /* POST */ router.post('/', (req, res) => { - console.log(req.body); + log.DEBUG(req.body); res.send('POST request to the post') }) diff --git a/Server/utilities/debugBuilder.js b/Server/utilities/debugBuilder.js new file mode 100644 index 0000000..1c2a621 --- /dev/null +++ b/Server/utilities/debugBuilder.js @@ -0,0 +1,17 @@ +// Debug +const debug = require('debug'); + +/** + * Create the different logging methods for a function + * Namespace template = ("[app]:[fileName]:['INFO', 'WARNING', 'DEBUG', 'ERROR']") + * @param {string} appName The name of the app to be used in the 'app' portion of the namespace + * @param {string} fileName The name of the file calling the builder to be used in the 'fileName' portion of the namespace + */ +exports.DebugBuilder = class DebugBuilder { + constructor(appName, fileName) { + this.INFO = debug(`${appName}:${fileName}:INFO`); + this.DEBUG = debug(`${appName}:${fileName}:DEBUG`); + this.WARN = debug(`${appName}:${fileName}:WARNING`); + this.ERROR = debug(`${appName}:${fileName}:ERROR`); + } +} \ No newline at end of file diff --git a/Server/mysqlHandler.js b/Server/utilities/mysqlHandler.js similarity index 98% rename from Server/mysqlHandler.js rename to Server/utilities/mysqlHandler.js index 4c81ac1..b53a54b 100644 --- a/Server/mysqlHandler.js +++ b/Server/utilities/mysqlHandler.js @@ -1,5 +1,5 @@ const mysql = require('mysql'); -const databaseConfig = require('./config/databaseConfig'); +const databaseConfig = require('../config/databaseConfig'); const utils = require('./utils'); const connection = mysql.createConnection({ diff --git a/Server/utils.js b/Server/utilities/utils.js similarity index 100% rename from Server/utils.js rename to Server/utilities/utils.js