atriusmaps-node-sdk
Version:
This project provides an API to Atrius Personal Wayfinder maps within a Node environment. See the README.md for more information
74 lines (71 loc) • 2.24 kB
JavaScript
;
const nodeColorsOb = {
black: "\x1B[30m",
red: "\x1B[31m",
green: "\x1B[32m",
yellow: "\x1B[33m",
blue: "\x1B[34m",
magenta: "\x1B[35m",
cyan: "\x1B[36m",
white: "\x1B[37m"
};
function clog(name, options = {}) {
let prefix = name + ": ";
let style = null;
if (options.color) {
if (options.isBrowser) {
prefix = "%c" + prefix;
style = `color: ${options.color}`;
} else {
const cstring = nodeColorsOb[options.color];
if (cstring) {
prefix = cstring + prefix;
}
}
}
const trunc = (str, len) => str.length > len ? str.substring(0, len) + "..." : str;
const checkPassesFilter = (args, filter) => {
const firstArg = String(args[0] ?? "");
return typeof filter === "string" ? firstArg.includes(filter) : filter.test(firstArg);
};
function logGen(cmethod) {
const logMethod = function(...restArgs) {
const isEnabled = options.enabled === void 0 ? true : !!options.enabled;
if (isEnabled || cmethod === console.error) {
let args = [...restArgs];
if (style) {
args.unshift(style);
}
args.unshift(prefix);
if (options.truncateObjects && cmethod !== console.error) {
args = args.map((arg) => {
if (typeof arg === "object") {
return trunc(JSON.stringify(arg), parseInt(String(options.truncateObjects), 10) || 100);
}
return arg;
});
}
const passesFilter = !options.logFilter || checkPassesFilter(args, options.logFilter);
if (passesFilter || cmethod === console.error) {
cmethod.apply(console, args);
}
}
};
return logMethod;
}
const log = logGen(options.trace ? console.trace : console.log);
log.info = log;
log.warn = logGen(console.warn);
log.detailed = logGen(console.debug);
log.error = logGen(console.error);
log.setEnabled = (e) => {
options.enabled = e;
};
return log;
}
function initLog(name, options = {}) {
const log = clog(name, options);
log.sublog = (sublogName, sublogOptions = {}) => initLog(name + "." + sublogName, Object.assign(Object.create(options), sublogOptions));
return log;
}
exports.initLog = initLog;