UNPKG

domotz-remote-pawn

Version:

Domotz Agent

71 lines (64 loc) 2.38 kB
/** This file is part of Domotz Agent. * Copyright (C) 2021 Domotz Ltd * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. * * This module expose functions for enriching the 'console' object (already modified in * `setup\logging.js` in order to add a prefix to all messages. * * by calling 'decorateLogs()' without any parameter, the returning object will print the current file name * (properly transformed, replacing the path separator with '.') before every message */ const DOMOTZ_LEVELS = { silent: 7, error: 6, alert: 5, warn: 4, info: 3, // <== Actual Production Level!!! debug: 2, verbose: 1, silly: 0, }; const path = require('path'); var root = 'src'; /** * Returns a logger object that prefixes all the messages with a given string * and the current module name: * prefix - a string, to be appended to the file name as prefix of all messages * logger - an already decorated log. In not undefined, the 'prefix' will be appended to the messages of this log * context - deprecated, do not use it - kept for compatibility but will be removed in the future */ function decorateLogs() { const logRegistry = require('./registry').registry(); return logRegistry.getLogger(getCaller(2).join('.')); } function getCaller(depth) { var stack = new Error().stack.split('\n'); return _normalizePath(stack[depth + 1]); } function _normalizePath(callerStackLine) { var t = callerStackLine.split(path.sep + root + path.sep); t = t.pop(); t = t.split('.js:')[0]; return t.split(path.sep); } module.exports = { decorateLogs: decorateLogs, _normalizePath: _normalizePath, DOMOTZ_LEVELS: DOMOTZ_LEVELS, //testing only _set_root: function (newRoot) { root = newRoot; }, };