@zowe/imperative
Version:
framework for building configurable CLIs
225 lines • 7.02 kB
JavaScript
"use strict";
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Console = void 0;
const TextUtils_1 = require("../../utilities/src/TextUtils");
const util_1 = require("util");
const error_1 = require("../../error");
class Console {
static getConsole(_category) {
return new Console();
}
static isValidLevel(level) {
return Console.LEVELS.indexOf(level) < 0 ? false : true;
}
static validateLevel(level) {
if (!Console.isValidLevel(level)) {
throw new error_1.ImperativeError({
msg: "Invalid level specified",
});
}
}
constructor(mLevel = Console.LEVEL_DEFAULT) {
this.mLevel = mLevel;
this.mPrefix = true;
this.mColor = true;
this.mLevel = mLevel.toLocaleLowerCase();
this.mIsOn = true;
Console.validateLevel(this.mLevel);
}
addContext(_key, _value) {
// do nothing
}
removeContext(_key) {
// do nothing
}
clearContext() {
// do nothing
}
isLevelEnabled() {
return false;
}
isTraceEnabled() {
return Console.LEVELS.indexOf("trace") >= Console.LEVELS.indexOf(this.level);
}
isDebugEnabled() {
return Console.LEVELS.indexOf("debug") >= Console.LEVELS.indexOf(this.level);
}
isInfoEnabled() {
return Console.LEVELS.indexOf("info") >= Console.LEVELS.indexOf(this.level);
}
isWarnEnabled() {
return Console.LEVELS.indexOf("warn") >= Console.LEVELS.indexOf(this.level);
}
isErrorEnabled() {
return Console.LEVELS.indexOf("error") >= Console.LEVELS.indexOf(this.level);
}
isFatalEnabled() {
return Console.LEVELS.indexOf("fatal") >= Console.LEVELS.indexOf(this.level);
}
isFormatEnabled() {
return true;
}
info(message, ...args) {
if (!this.isInfoEnabled()) {
return;
}
let adjustedMessage = message;
if (this.prefix) {
adjustedMessage = Console.buildPrefix("INFO") + message;
}
if (this.color) {
adjustedMessage = TextUtils_1.TextUtils.chalk.grey(adjustedMessage);
}
return this.writeStdout(adjustedMessage, args);
}
trace(message, ...args) {
if (!this.isTraceEnabled()) {
return;
}
let adjustedMessage = message;
if (this.prefix) {
adjustedMessage = Console.buildPrefix("TRACE") + message;
}
if (this.color) {
adjustedMessage = TextUtils_1.TextUtils.chalk.cyan(adjustedMessage);
}
return this.writeStdout(adjustedMessage, args);
}
debug(message, ...args) {
if (!this.isDebugEnabled()) {
return;
}
let adjustedMessage = message;
if (this.prefix) {
adjustedMessage = Console.buildPrefix("DEBUG") + message;
}
if (this.color) {
adjustedMessage = TextUtils_1.TextUtils.chalk.blue(adjustedMessage);
}
return this.writeStdout(adjustedMessage, args);
}
warn(message, ...args) {
if (!this.isWarnEnabled()) {
return;
}
let adjustedMessage = message;
if (this.prefix) {
adjustedMessage = Console.buildPrefix("WARN") + message;
}
if (this.color) {
adjustedMessage = TextUtils_1.TextUtils.chalk.yellow(adjustedMessage);
}
return this.writeStderr(adjustedMessage, args);
}
error(message, ...args) {
if (!this.isErrorEnabled()) {
return;
}
let adjustedMessage = message;
if (this.prefix) {
adjustedMessage = Console.buildPrefix("ERROR") + message;
}
if (this.color) {
adjustedMessage = TextUtils_1.TextUtils.chalk.red(adjustedMessage);
}
return this.writeStderr(adjustedMessage, args);
}
fatal(message, ...args) {
if (!this.isFatalEnabled()) {
return;
}
let adjustedMessage = message;
if (this.prefix) {
adjustedMessage = Console.buildPrefix("FATAL") + message;
}
if (this.color) {
adjustedMessage = TextUtils_1.TextUtils.chalk.magenta(adjustedMessage);
}
return this.writeStderr(adjustedMessage, args);
}
log(level, message, ...args) {
var _a;
return (_a = this[level]) === null || _a === void 0 ? void 0 : _a.call(this, message, args);
}
writeStderr(message, ...args) {
const data = this.format(message, args);
if (this.on) {
process.stderr.write(data);
}
return data;
}
writeStdout(message, ...args) {
const data = this.format(message, args);
if (this.on) {
process.stdout.write(data);
}
return data;
}
format(data, ...args) {
let formatted = data;
// TODO(Kelosky): this is not ideal, but works for simple cases of
// .debug(%s, "sub string").
if (this.isFormatEnabled() && args != null && args.length > 0) {
let defined = false;
args.forEach((arg) => {
arg.forEach((ntry) => {
if (ntry.length > 0) {
defined = true;
}
});
});
// if every argument is undefined, dont format it
if (defined) {
formatted = (0, util_1.format)(data, args);
}
}
return formatted + "\n";
}
static buildPrefix(type) {
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
const tzOffset = new Date().getTimezoneOffset() * 60000;
const dateString = new Date(Date.now() - tzOffset).toISOString()
.replace(/(\d{4})-(\d{2})-(\d{2})T([^Z]+)Z/, "$1/$2/$3 $4");
return "[" + dateString + "]" + " " + "[" + type + "]" + " ";
}
set level(level) {
level = level.toLowerCase();
Console.validateLevel(level);
this.mLevel = level;
}
get level() {
return this.mLevel;
}
set prefix(isEnabled) {
this.mPrefix = isEnabled;
}
get prefix() {
return this.mPrefix;
}
set color(isEnabled) {
this.mColor = isEnabled;
}
get color() {
return this.mColor;
}
set on(isOn) {
this.mIsOn = isOn;
}
get on() {
return this.mIsOn;
}
}
exports.Console = Console;
Console.LEVELS = ["trace", "debug", "info", "warn", "error", "fatal", "off"];
Console.LEVEL_DEFAULT = "warn";
//# sourceMappingURL=Console.js.map