mirror of
https://github.com/mtan93/homebridge.git
synced 2026-03-08 05:31:55 +00:00
* No compilation step * Beginnings of web interface * Simple express server; React-based frontend * CommonJS style across codebase; auto-converts to RequireJS for browser * Using diffsync for realtime UI * "Provider" -> "Plugin" * Plugins expose one or more Providers
65 lines
1.3 KiB
JavaScript
65 lines
1.3 KiB
JavaScript
var chalk = require('chalk');
|
|
|
|
'use strict';
|
|
|
|
module.exports = {
|
|
Logger: Logger,
|
|
setDebugEnabled: setDebugEnabled,
|
|
_system: new Logger() // system logger, for internal use only
|
|
}
|
|
|
|
var DEBUG_ENABLED = false;
|
|
|
|
// Turns on debug level logging
|
|
function setDebugEnabled(enabled) {
|
|
DEBUG_ENABLED = enabled;
|
|
}
|
|
|
|
// global cache of logger instances by plugin name
|
|
var loggerCache = {};
|
|
|
|
/**
|
|
* Logger class
|
|
*/
|
|
|
|
function Logger(pluginName) {
|
|
this.pluginName = pluginName;
|
|
}
|
|
|
|
Logger.prototype.debug = function(msg) {
|
|
if (DEBUG_ENABLED)
|
|
this.log('debug', msg);
|
|
}
|
|
|
|
Logger.prototype.info = function(msg) {
|
|
this.log('info', msg);
|
|
}
|
|
|
|
Logger.prototype.warn = function(msg) {
|
|
this.log('warn', msg);
|
|
}
|
|
|
|
Logger.prototype.error = function(msg) {
|
|
this.log('error', msg);
|
|
}
|
|
|
|
Logger.prototype.log = function(level, msg) {
|
|
|
|
if (level == 'debug')
|
|
msg = chalk.gray(msg);
|
|
else if (level == 'warn')
|
|
msg = chalk.yellow(msg);
|
|
else if (level == 'error')
|
|
msg = chalk.bold.red(msg);
|
|
|
|
// prepend plugin name if applicable
|
|
if (this.pluginName)
|
|
msg = chalk.cyan("[" + this.pluginName + "]") + " " + msg;
|
|
|
|
console.log(msg);
|
|
}
|
|
|
|
Logger.forPlugin = function(pluginName) {
|
|
return loggerCache[pluginName] || (loggerCache[pluginName] = new Logger(pluginName));
|
|
}
|