use logger

This commit is contained in:
jane 2021-05-26 12:42:22 -04:00
parent 562d4572c7
commit 0fab34c3b3
2 changed files with 60 additions and 7 deletions

View file

@ -1,7 +1,9 @@
import ws281x from 'rpi-ws281x' import ws281x from 'rpi-ws281x'
import * as fs from 'fs' import * as fs from 'fs'
import { Logger, levels } from './logger'
const cfg = JSON.parse(fs.readFileSync('./config.json')); const cfg = JSON.parse(fs.readFileSync('./config.json'));
const log = new Logger("lights", cfg.log_level ? levels[cfg.log_level] : levels.INFO);
const fade_ticks = cfg.fade_ticks || 20; const fade_ticks = cfg.fade_ticks || 20;
var pixels = new Uint32Array(cfg.leds); var pixels = new Uint32Array(cfg.leds);
@ -35,7 +37,7 @@ export function set_pattern(pat) {
function tick_pattern() { function tick_pattern() {
// do the parsing stuff here // do the parsing stuff here
console.log("TICKING PATTERN") log.debug("TICKING PATTERN")
for (let i = 0; i < cfg.leds; i++) { for (let i = 0; i < cfg.leds; i++) {
var r = Math.floor(Math.random() * 256) var r = Math.floor(Math.random() * 256)
@ -50,7 +52,7 @@ export function tick() {
for (let i = 0; i < cfg.leds; i++) { for (let i = 0; i < cfg.leds; i++) {
if (next_pattern[i] != pixels[i]) { if (next_pattern[i] != pixels[i]) {
if (next_pattern[i] == pixel_cache[i]) { if (next_pattern[i] == pixel_cache[i]) {
console.log("INCONGRUENCE WITH " + i); log.debug("INCONGRUENCE WITH " + i);
pixels[i] = next_pattern[i]; pixels[i] = next_pattern[i];
} }
else { else {
@ -59,7 +61,7 @@ export function tick() {
} }
} }
else if (pixel_cache[i] != pixels[i]) { else if (pixel_cache[i] != pixels[i]) {
console.log("PATTERN NOT STORED " + i); log.debug("PATTERN NOT STORED " + i);
pixel_cache[i] = pixels[i]; pixel_cache[i] = pixels[i];
} }
} }
@ -85,11 +87,11 @@ function fade(index) {
var sign_r = diff_r === Math.abs(diff_r) ? 1 : -1; var sign_r = diff_r === Math.abs(diff_r) ? 1 : -1;
var sign_g = diff_g === Math.abs(diff_g) ? 1 : -1; var sign_g = diff_g === Math.abs(diff_g) ? 1 : -1;
var sign_b = diff_b === Math.abs(diff_b) ? 1 : -1; var sign_b = diff_b === Math.abs(diff_b) ? 1 : -1;
console.log(`${diff_r} ${sign_r} ${diff_g} ${sign_g} ${diff_b} ${sign_b}`); log.debug(`${diff_r} ${sign_r} ${diff_g} ${sign_g} ${diff_b} ${sign_b}`);
var interval_r = sign_r * Math.ceil(Math.abs(diff_r) / fade_ticks); var interval_r = sign_r * Math.ceil(Math.abs(diff_r) / fade_ticks);
var interval_g = sign_g * Math.ceil(Math.abs(diff_g) / fade_ticks); var interval_g = sign_g * Math.ceil(Math.abs(diff_g) / fade_ticks);
var interval_b = sign_b * Math.ceil(Math.abs(diff_b) / fade_ticks); var interval_b = sign_b * Math.ceil(Math.abs(diff_b) / fade_ticks);
console.log(`r${Math.abs(diff_r) / fade_ticks} ${Math.ceil(Math.abs(diff_r) / fade_ticks)}` + log.debug(`r${Math.abs(diff_r) / fade_ticks} ${Math.ceil(Math.abs(diff_r) / fade_ticks)}` +
`g${Math.abs(diff_g) / fade_ticks} ${Math.ceil(Math.abs(diff_g) / fade_ticks)}` + `g${Math.abs(diff_g) / fade_ticks} ${Math.ceil(Math.abs(diff_g) / fade_ticks)}` +
`b${Math.abs(diff_b) / fade_ticks} ${Math.ceil(Math.abs(diff_b) / fade_ticks)}`); `b${Math.abs(diff_b) / fade_ticks} ${Math.ceil(Math.abs(diff_b) / fade_ticks)}`);
@ -124,7 +126,7 @@ function fade(index) {
if (current_tick_r + 1 >= fade_ticks && if (current_tick_r + 1 >= fade_ticks &&
current_tick_g + 1 >= fade_ticks && current_tick_g + 1 >= fade_ticks &&
current_tick_b + 1 >= fade_ticks) { current_tick_b + 1 >= fade_ticks) {
console.log("FINISHED"); log.debug("FINISHED");
pixel_cache[index] = next_pattern[index]; pixel_cache[index] = next_pattern[index];
} }
else { else {
@ -132,7 +134,7 @@ function fade(index) {
current.g + interval_g, current.g + interval_g,
current.b + interval_b); current.b + interval_b);
let prev = int_to_rgb(pixel_cache[index]); let prev = int_to_rgb(pixel_cache[index]);
console.log(`${current_tick_r} ${current_tick_g} ${current_tick_b}: ` log.debug(`${current_tick_r} ${current_tick_g} ${current_tick_b}: `
+ `CURRENT COLOR: ${current.r} ${current.g} ${current.b} NEW COLOR: ${current.r + interval_r} ${current.g + interval_g} ${current.b + interval_b} ` + + `CURRENT COLOR: ${current.r} ${current.g} ${current.b} NEW COLOR: ${current.r + interval_r} ${current.g + interval_g} ${current.b + interval_b} ` +
`FINAL: ${final.r} ${final.g} ${final.b} ` + `FINAL: ${final.r} ${final.g} ${final.b} ` +
`\nINTERVAL: ${interval_r} ${interval_g} ${interval_b} ` + `\nINTERVAL: ${interval_r} ${interval_g} ${interval_b} ` +

51
logger.js Normal file
View file

@ -0,0 +1,51 @@
export const levels = {
DEBUG: 4,
INFO: 3,
WARN: 2,
ERROR: 1,
PANIC: 0,
};
export default class Logger {
constructor(name, level) {
console.log(`created new logger for ${name} with level ${level}`);
this.sn(name);
this.s(level);
}
n = 'DEFAULT';
l = 0;
sn(n) {
this.n = n;
}
s(l) {
if (l && l.constructor === Number) {
this.l = l;
} else {
this.l = levels[l];
}
}
lo(l, m) {
if (l <= this.l) {
let level = Object.keys(levels).find((key) => levels[key] === l);
let ms = typeof m == 'object' ? JSON.stringify(m) : m;
console.log(`${level} [${this.n}]: ${ms}`);
}
}
debug(msg) {
this.lo(levels.DEBUG, msg);
}
info(msg) {
this.lo(levels.INFO, msg);
}
warn(msg) {
this.lo(levels.WARN, msg);
}
error(msg) {
this.lo(levels.ERROR, msg);
}
panic(msg) {
this.lo(levels.PANIC, msg);
}
}