Logging Update

- Changed to uniform logging with the 'debug' module
- Updated all apps
This commit is contained in:
Logan Cusano
2022-12-11 21:11:29 -05:00
parent e403560165
commit e5d885cc3e
21 changed files with 105 additions and 53 deletions

View File

@@ -5,7 +5,9 @@
*/ */
const app = require('../app'); 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 http = require('http');
const config = require('../config/clientConfig'); const config = require('../config/clientConfig');
const clientController = require('../controllers/clientController'); const clientController = require('../controllers/clientController');
@@ -67,7 +69,7 @@ function onListening() {
const bind = typeof addr === 'string' const bind = typeof addr === 'string'
? 'pipe ' + addr ? 'pipe ' + addr
: 'port ' + addr.port; : '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 // check in with the server to add this node or come back online
clientController.checkIn(); clientController.checkIn();

View File

@@ -1,5 +1,6 @@
// Debug output // Debug
const debug = require('debug')('client:clientController'); // const { DebugBuilder } = require("../utilities/debugBuilder.js");
// const log = new DebugBuilder("client", "clientController");
// Configs // Configs
const config = require("../config/clientConfig"); const config = require("../config/clientConfig");
const modes = require("../config/modes"); const modes = require("../config/modes");

View File

@@ -5,8 +5,8 @@ import ping from './commands/ping.js';
import join from './commands/join.js'; import join from './commands/join.js';
import leave from './commands/leave.js'; import leave from './commands/leave.js';
// Debug // Debug
import debugBuilder from "./utilities/debugBuilder.js"; import ModuleDebugBuilder from "./utilities/moduleDebugBuilder.js";
const log = new debugBuilder("bot", "app"); const log = new ModuleDebugBuilder("bot", "app");
// Modules // Modules
import { Client, GatewayIntentBits } from 'discord.js'; import { Client, GatewayIntentBits } from 'discord.js';
// Utilities // Utilities
@@ -65,4 +65,5 @@ function main(){
}); });
} }
main(); main();
//module.exports = client;

View File

@@ -28,6 +28,7 @@ export default async function join(interaction){
audioInstance.on('data', buffer => { audioInstance.on('data', buffer => {
buffer = Buffer.from(buffer); buffer = Buffer.from(buffer);
const encoded = encoder.encode(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); voiceConnection.playOpusPacket(encoded);
}) })

View File

@@ -1,7 +1,7 @@
import {getVoiceConnection} from "@discordjs/voice"; import {getVoiceConnection} from "@discordjs/voice";
import {replyToInteraction} from "../utilities/messageHandler.js"; import {replyToInteraction} from "../utilities/messageHandler.js";
// Debug // Debug
//import debugBuilder from "../utilities/debugBuilder.js"; //import debugBuilder from "../utilities/moduleDebugBuilder.js";
//const log = new debugBuilder("bot", "leave"); //const log = new debugBuilder("bot", "leave");
/** /**

View File

@@ -3,8 +3,8 @@ import {getDeviceID} from '../utilities/configHandler.js'
// Modules // Modules
import portAudio from 'naudiodon'; import portAudio from 'naudiodon';
// Debug // Debug
import debugBuilder from "../utilities/debugBuilder.js"; import ModuleDebugBuilder from "../utilities/moduleDebugBuilder.js";
const log = new debugBuilder("bot", "audioController"); const log = new ModuleDebugBuilder("bot", "audioController");
/** /**
* Checks to make sure the selected audio device is available and returns the device object (PortAudio Device Info) * Checks to make sure the selected audio device is available and returns the device object (PortAudio Device Info)

View File

@@ -1,7 +1,7 @@
import { readFileSync } from 'fs'; import { readFileSync } from 'fs';
// Debug // Debug
import debugBuilder from "./debugBuilder.js"; import ModuleDebugBuilder from "./moduleDebugBuilder.js";
const log = new debugBuilder("bot", "configHandler"); const log = new ModuleDebugBuilder("bot", "configHandler");
export function getConfig() { export function getConfig() {
return JSON.parse(readFileSync("./config/botConfig.json")); return JSON.parse(readFileSync("./config/botConfig.json"));

View File

@@ -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`);
}
}

View File

@@ -1,6 +1,6 @@
// Debug // Debug
import debugBuilder from "./debugBuilder.js"; import ModuleDebugBuilder from "./moduleDebugBuilder.js";
const log = new debugBuilder("bot", "messageHandler"); const log = new ModuleDebugBuilder("bot", "messageHandler");
export function replyToInteraction(interaction, message){ export function replyToInteraction(interaction, message){
interaction.reply({ content: message, fetchReply: true }) interaction.reply({ content: message, fetchReply: true })

View File

@@ -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`);
}
}

View File

@@ -3,8 +3,8 @@ import {REST} from "@discordjs/rest";
import {getApplicationID, getGuildID, getTOKEN} from "./configHandler.js"; import {getApplicationID, getGuildID, getTOKEN} from "./configHandler.js";
import { Routes, ChannelType } from "discord.js"; import { Routes, ChannelType } from "discord.js";
// Debug // Debug
import debugBuilder from "./debugBuilder.js"; import ModuleDebugBuilder from "./moduleDebugBuilder.js";
const log = new debugBuilder("bot", "registerCommands"); const log = new ModuleDebugBuilder("bot", "registerCommands");
const pingCommand = new SlashCommandBuilder() const pingCommand = new SlashCommandBuilder()
.setName("ping") .setName("ping")

View File

@@ -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`);
}
}

View File

@@ -1,5 +1,6 @@
// Debug // Debug
const debug = require('debug')('client:httpRequests'); const { DebugBuilder } = require("../utilities/debugBuilder.js");
const log = new DebugBuilder("client", "httpRequests");
// Config // Config
const config = require("../config/clientConfig"); const config = require("../config/clientConfig");
// Modules // Modules
@@ -26,7 +27,7 @@ exports.requestOptions = class requestOptions {
* @param callback * @param callback
*/ */
exports.sendHttpRequest = function sendHttpRequest(requestOptions, data, 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 // Create the request
const req = http.request(requestOptions, res => { const req = http.request(requestOptions, res => {
res.on('data', (data) => { res.on('data', (data) => {
@@ -34,11 +35,11 @@ exports.sendHttpRequest = function sendHttpRequest(requestOptions, data, callbac
"statusCode": res.statusCode, "statusCode": res.statusCode,
"body": JSON.parse(data) "body": JSON.parse(data)
}; };
debug("Response Object: ", responseObject); log.DEBUG("Response Object: ", responseObject);
callback(responseObject); callback(responseObject);
}) })
}).on('error', err => { }).on('error', err => {
debug('Error: ', err.message) log.ERROR('Error: ', err.message)
// TODO need to handle if the server is down // TODO need to handle if the server is down
}) })

View File

@@ -1,5 +1,6 @@
// Debug // Debug
const debug = require('debug')('client:updateConfig'); const { DebugBuilder } = require("../utilities/debugBuilder.js");
const log = new DebugBuilder("client", "updateConfig");
// Modules // Modules
const replace = require('replace-in-file'); const replace = require('replace-in-file');
@@ -35,7 +36,7 @@ exports.updateId = (updatedId) => {
function updateConfigFile(options, callback){ function updateConfigFile(options, callback){
replace(options, (error, changedFiles) => { replace(options, (error, changedFiles) => {
if (error) return console.error('Error occurred:', error); if (error) return console.error('Error occurred:', error);
debug('Modified files:', changedFiles); log.DEBUG('Modified files:', changedFiles);
callback(changedFiles); callback(changedFiles);
}); });
} }

View File

@@ -1,5 +1,6 @@
// Debug // Debug
const debug = require('debug')('client:updatePresets'); const { DebugBuilder } = require("../utilities/debugBuilder.js");
const log = new DebugBuilder("client", "updatePresets");
// Modules // Modules
const fs = require('fs'); const fs = require('fs');
@@ -12,7 +13,7 @@ function writePresets(presets, callback = undefined) {
fs.writeFile("../config/radioPresets.json", JSON.stringify(presets), (err) => { fs.writeFile("../config/radioPresets.json", JSON.stringify(presets), (err) => {
// Error checking // Error checking
if (err) throw err; if (err) throw err;
debug("Write Complete"); log.DEBUG("Write Complete");
if (callback) callback() if (callback) callback()
}); });
} }
@@ -29,7 +30,7 @@ function sanitizeFrequencies(frequenciesArray) {
sanitizedFrequencyArray.push(convertFrequencyToHertz(freq)); sanitizedFrequencyArray.push(convertFrequencyToHertz(freq));
} }
debug(sanitizedFrequencyArray); log.DEBUG("Sanitized Frequency Array", sanitizedFrequencyArray);
return sanitizedFrequencyArray; return sanitizedFrequencyArray;
} }
@@ -42,12 +43,12 @@ function convertFrequencyToHertz(frequency){
// check if the passed value is a number // check if the passed value is a number
if(typeof frequency == 'number' && !isNaN(frequency)){ if(typeof frequency == 'number' && !isNaN(frequency)){
if (Number.isInteger(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 // Check to see if the frequency has the correct length
if (frequency.toString().length >= 7 && frequency.toString().length <= 9) return frequency if (frequency.toString().length >= 7 && frequency.toString().length <= 9) return frequency
} }
else { 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 // Convert to a string to remove the decimal in place and then correct the length
frequency = frequency.toString(); frequency = frequency.toString();
// One extra digit checked for the '.' included in the string // One extra digit checked for the '.' included in the string
@@ -63,7 +64,7 @@ function convertFrequencyToHertz(frequency){
} }
} }
} else { } else {
debug(`${frequency} is not a number`); log.DEBUG(`${frequency} is not a number`);
frequency = convertFrequencyToHertz(parseFloat(frequency)); frequency = convertFrequencyToHertz(parseFloat(frequency));
return parseInt(frequency) return parseInt(frequency)

View File

@@ -5,7 +5,9 @@
*/ */
var app = require('../app'); 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'); var http = require('http');
/** /**
@@ -86,5 +88,5 @@ function onListening() {
var bind = typeof addr === 'string' var bind = typeof addr === 'string'
? 'pipe ' + addr ? 'pipe ' + addr
: 'port ' + addr.port; : 'port ' + addr.port;
debug('Listening on ' + bind); log.DEBUG('Listening on ' + bind);
} }

View File

@@ -1,5 +1,9 @@
const mysqlHander = require("../mysqlHandler"); // Debug
const utils = require("../utils"); 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) => { exports.listAllNodes = async (req, res) => {
mysqlHander.getAllNodes((allNodes) => { mysqlHander.getAllNodes((allNodes) => {
@@ -32,7 +36,7 @@ exports.newNode = async (req, res) => {
if (err === "No name provided") { if (err === "No name provided") {
return res.sendStatus(400); return res.sendStatus(400);
} }
else console.log(err) else log.ERROR(err)
return res.sendStatus(500); return res.sendStatus(500);
} }
} }
@@ -65,7 +69,7 @@ exports.nodeCheckIn = async (req, res) => {
// If no changes are made tell the client // If no changes are made tell the client
if (Object.keys(nodeObject).length === 0) return res.status(200).json("No keys updated"); 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 // Adding the ID key to the body so that the client can double-check their ID
nodeObject.id = req.body.id; nodeObject.id = req.body.id;
mysqlHander.updateNodeInfo(nodeObject, () => { mysqlHander.updateNodeInfo(nodeObject, () => {

View File

@@ -1,3 +1,7 @@
// Debug
const { DebugBuilder } = require("../utilities/debugBuilder.js");
const log = new DebugBuilder("server", "admin");
// Modules
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
@@ -8,7 +12,7 @@ router.get('/', (req, res) => {
/* POST */ /* POST */
router.post('/', (req, res) => { router.post('/', (req, res) => {
console.log(req.body); log.DEBUG(req.body);
res.send('POST request to the post') res.send('POST request to the post')
}) })

View File

@@ -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`);
}
}

View File

@@ -1,5 +1,5 @@
const mysql = require('mysql'); const mysql = require('mysql');
const databaseConfig = require('./config/databaseConfig'); const databaseConfig = require('../config/databaseConfig');
const utils = require('./utils'); const utils = require('./utils');
const connection = mysql.createConnection({ const connection = mysql.createConnection({