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 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();

View File

@@ -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");

View File

@@ -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();
main();
//module.exports = client;

View File

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

View File

@@ -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");
/**

View File

@@ -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)

View File

@@ -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"));

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
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 })

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 { 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")

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
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
})

View File

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

View File

@@ -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)

View File

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

View File

@@ -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, () => {

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 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')
})

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 databaseConfig = require('./config/databaseConfig');
const databaseConfig = require('../config/databaseConfig');
const utils = require('./utils');
const connection = mysql.createConnection({