Add winston and clean up the logging

This commit is contained in:
2023-11-09 10:02:32 +01:00
parent 479ac4a737
commit 142592ef1b
3 changed files with 236 additions and 57 deletions

111
client.js
View File

@@ -1,36 +1,51 @@
const smpp = require("smpp");
const commandLineArgs = require("command-line-args");
const commandLineUsage = require("command-line-usage");
const { createLogger, format, transports } = require("winston");
const { combine, timestamp, label, printf } = format;
const myFormat = printf(({ level, message, timestamp }) => {
return `${timestamp} ${level}: ${message}`;
});
const logger = createLogger({
format: combine(format.colorize(), timestamp(), myFormat),
transports: [new transports.Console()],
});
function verifyExists(value, err) {
if (!value) {
logger.error(err);
process.exit(0);
}
}
const optionDefinitions = [
{ name: "help", type: String, description: "Display this usage guide." },
{ name: "help", type: Boolean, description: "Display this usage guide." },
{ name: "host", alias: "h", type: String, description: "The host (IP) to connect to." },
{ name: "port", alias: "p", type: Number, description: "The port to connect to." },
{ name: "systemId", alias: "s", type: String, description: "SMPP related login info." },
{ name: "password", alias: "pa", type: String, description: "SMPP related login info." },
{ name: "password", alias: "w", type: String, description: "SMPP related login info." },
{
name: "messageCount",
alias: "mc",
type: Number,
description: "Number of messages to send; Optional, defaults to 1.",
defaultOption: 1,
},
{
name: "source",
alias: "src",
type: String,
description: "Source field of the sent messages.",
defaultOption: "smppDebugClient",
},
{
name: "destination",
alias: "dst",
type: String,
description: "Destination field of the sent messages.",
defaultOption: "smpp",
},
{
name: "message",
alias: "msg",
type: String,
description: "Text content of the sent messages.",
defaultOption: "smpp debug message",
@@ -44,76 +59,60 @@ if (options.help) {
const usage = commandLineUsage([
{
header: "CLI SMPP (Client)",
}, {
header: "Options",
optionList: optionDefinitions
},
{
content: "Project home: {underline }"
}
header: "Options",
optionList: optionDefinitions,
},
{
content: "Project home: {underline https://github.com/PhatDave/SMPP_CLI}",
},
]);
console.log(usage);
process.exit(0);
}
try {
host = String(host);
port = Number(port);
systemId = String(systemId);
password = String(password);
messageCount = Number(messageCount) || 1;
source = source || "smppClientDebug";
destination = destination || "smpp";
message = message || "smpp debug message";
} catch (e) {
console.log(`Invalid params: ˘${e}`);
console.log(`Usage: client <host> <port> <systemId> <password> <messageCount?> <source?> <destination?> <message?>
If not provided:
messageCount defaults to 1 (value of 0 is permitted)
source defaults to smppClientDebug
destination defaults to smpp
message defaults to "smpp debug message"
Note:
Host is in the form of IP
Port must be a number
MessageCount must be a number
`);
process.exit(0);
}
verifyExists(options.host, "Host can not be undefined or empty! (--host)");
verifyExists(options.port, "Port can not be undefined or empty! (--port)");
verifyExists(options.systemId, "SystemID can not be undefined or empty! (--systemId)");
verifyExists(options.password, "Password can not be undefined or empty! (--password)");
let message_id = 0;
console.log(`Connecting to ${host}:${port}...`);
logger.info(`Connecting to ${options.host}:${options.port}...`);
const session = smpp.connect(
{
url: `smpp://${host}:${port}`,
url: `smpp://${options.host}:${options.port}`,
auto_enquire_link_period: 10000,
debug: false,
debug: options.debug,
},
function () {
console.log("Connected, sending bind_transciever...");
logger.info(
`Connected, sending bind_transciever with systemId '${options.systemId}' and password '${options.password}'...`
);
session.bind_transceiver(
{
system_id: systemId,
password: password,
system_id: options.systemId,
password: options.password,
},
function (pdu) {
if (pdu.command_status === 0) {
// Successfully bound
console.log("sending shit");
session.submit_sm(
{
source_addr: "smpp_test_1",
destination_addr: "123123123123",
short_message: "Hello!",
data_coding: 0xc0,
},
function (pdu) {
if (pdu.command_status === 0) {
console.log(pdu.message_id);
message_id = pdu.message_id;
}
}
logger.info(
`Successfully bound, sending ${options.messageCount} messages '${options.source}'->'${options.destination}' ('${options.message}')`
);
// session.submit_sm(
// {
// source_addr: "smpp_test_1",
// destination_addr: "123123123123",
// short_message: "Hello!",
// data_coding: 0xc0,
// },
// function (pdu) {
// if (pdu.command_status === 0) {
// console.log(pdu.message_id);
// message_id = pdu.message_id;
// }
// }
// );
}
}
);