Setup basic logging framework.

In the future, we might be able to use a proxy object here or some other 
wizadry? I'm not sure.
master
Starbeamrainbowlabs 4 years ago
parent 31a75b5589
commit 009d6335a0
  1. 44
      server/Helpers/Log.mjs
  2. 5
      server/bootstrap/container.mjs
  3. 5
      server/index.mjs
  4. 6
      server/settings.default.toml
  5. 7
      server/ttn-app-server/TTNAppServer.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;

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

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

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

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

Loading…
Cancel
Save