diff --git a/server/Helpers/Log.mjs b/server/Helpers/Log.mjs new file mode 100644 index 0000000..89f4b68 --- /dev/null +++ b/server/Helpers/Log.mjs @@ -0,0 +1,44 @@ +"use strict"; + +import Ansi from './Ansi.mjs'; + +class Log { + // Destructure the awilix container + constructor({ settings, ansi }) { + this.settings = settings; + /** @type {Ansi} */ + this.a = ansi; + + this.start_time = new Date(); + + // FUTURE: We could add a log target (e.g. a file etc.) here if required + } + + log_raw(func, ...items) { + let prefix = (new Date()).toISOString(); + if(this.settings.logging.date_display_mode == "relative") { + prefix = (new Date() - this.start_time).toFixed(5); + } + + func(`${this.a.locol}[${prefix}]${this.a.reset}`, ...items); + } + log(...items) { this.log_raw(console.log, ...items); } + info(...items) { this.log_raw(console.info, ...items); } + debug(...items) { this.log_raw(console.debug, ...items); } + warn(...items) { + this.log_raw(console.warn, + this.a.hicol + this.a.fyellow, + ...items, + this.a.reset + ); + } + error(...items) { + this.log_raw(console.error, + this.a.hicol + this.a.fred, + ...items, + this.a.reset + ); + } +} + +export default Log; diff --git a/server/bootstrap/container.mjs b/server/bootstrap/container.mjs index fa8dc32..5e9c1b5 100644 --- a/server/bootstrap/container.mjs +++ b/server/bootstrap/container.mjs @@ -3,6 +3,7 @@ import a from 'awilix'; import Ansi from '../Helpers/Ansi.mjs'; +import Log from '../Helpers/Log.mjs'; import TTNAppServer from '../ttn-app-server/TTNAppServer.mjs'; import settings from './settings.mjs'; @@ -14,10 +15,12 @@ const c = a.createContainer({ c.register({ settings: a.asValue(settings), - ansi: a.asFunction(() => new Ansi()).singleton(), + ansi: a.asClass(Ansi).singleton(), + log: a.Class(Log).singleton(), database: a.asFunction(database_init).singleton(), TTNAppServer: a.asClass(TTNAppServer), }); + c.loadModules("../Repos.SQLite/*.mjs", { register: a.asClass, lifetime: a.Lifetime.SINGLETON diff --git a/server/index.mjs b/server/index.mjs index 60902e6..1f1097f 100755 --- a/server/index.mjs +++ b/server/index.mjs @@ -8,6 +8,7 @@ import show_help from './help.mjs'; const settings = c.resolve("settings"); const a = c.resolve("ansi"); +const l = c.resolve("log"); // 2: CLI Argument Parsing let args = process.argv.slice(2); // Slice out the node binary name and the filename @@ -45,8 +46,12 @@ if(extras.length < 1) { process.exit(); } +l.log(`${a.fgreen}${a.hicol}*** LoRaWAN Signal Mapper ***`); + switch(extras[0]) { case "ttn-app-server": + l.log(`${a.fgreen}${a.hicol}Starting The Things Network application server${a.reset}`); + let app_server = c.resolve("TTNAppServer"); app_server.run(); break; diff --git a/server/settings.default.toml b/server/settings.default.toml index fefe884..fc7706d 100644 --- a/server/settings.default.toml +++ b/server/settings.default.toml @@ -14,3 +14,9 @@ filename = "lorawan.sqlite" # The options to pass to better-sqlite3. You probably don't need to change this. [database.options] + + +[logging] +# The format the date displayed when logging things should take. +# Allowed values: relative (e.g like when a Linux machine boots), absolute (e.g. like Nginx server logs) +date_display_mode = "relative" diff --git a/server/ttn-app-server/TTNAppServer.mjs b/server/ttn-app-server/TTNAppServer.mjs index dba8d26..7da1796 100644 --- a/server/ttn-app-server/TTNAppServer.mjs +++ b/server/ttn-app-server/TTNAppServer.mjs @@ -1,15 +1,14 @@ "use strict"; -import ansi from '../Helpers/Ansi.mjs'; - class TTNAppServer { // Destructure the awilix container - constructor({ settings }) { + constructor({ settings, ansi }) { this.settings = settings; + /** @type {Ansi} */ + this.a = ansi; } run() { - } }