2017-08-23 14:45:03 +00:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const util = require("util");
|
|
|
|
const fs = require("fs");
|
2017-09-16 17:12:58 +00:00
|
|
|
const events_1 = require("events");
|
2017-10-21 10:45:20 +00:00
|
|
|
const path = require("path");
|
2017-08-23 14:45:03 +00:00
|
|
|
const Reset = "\x1b[0m";
|
|
|
|
const Bright = "\x1b[1m";
|
|
|
|
const Dim = "\x1b[2m";
|
|
|
|
const Underscore = "\x1b[4m";
|
|
|
|
const Blink = "\x1b[5m";
|
|
|
|
const Reverse = "\x1b[7m";
|
|
|
|
const Hidden = "\x1b[8m";
|
|
|
|
const FgBlack = "\x1b[30m";
|
|
|
|
const FgRed = "\x1b[31m";
|
|
|
|
const FgGreen = "\x1b[32m";
|
|
|
|
const FgYellow = "\x1b[33m";
|
|
|
|
const FgBlue = "\x1b[34m";
|
|
|
|
const FgMagenta = "\x1b[35m";
|
|
|
|
const FgCyan = "\x1b[36m";
|
|
|
|
const FgWhite = "\x1b[37m";
|
|
|
|
const BgBlack = "\x1b[40m";
|
|
|
|
const BgRed = "\x1b[41m";
|
|
|
|
const BgGreen = "\x1b[42m";
|
|
|
|
const BgYellow = "\x1b[43m";
|
|
|
|
const BgBlue = "\x1b[44m";
|
|
|
|
const BgMagenta = "\x1b[45m";
|
|
|
|
const BgCyan = "\x1b[46m";
|
|
|
|
const BgWhite = "\x1b[47m";
|
2018-02-18 15:00:16 +00:00
|
|
|
const maxFileSize = 500000000;
|
2017-08-23 14:45:03 +00:00
|
|
|
class Logging {
|
|
|
|
static config(logfolder, stdout) {
|
|
|
|
this.logFileLocation = logfolder;
|
|
|
|
this.stdout = stdout;
|
|
|
|
}
|
|
|
|
static debug(...message) {
|
|
|
|
Logging.message(LoggingTypes.Debug, message);
|
|
|
|
}
|
|
|
|
static log(...message) {
|
|
|
|
Logging.message(LoggingTypes.Log, message);
|
|
|
|
}
|
|
|
|
static warning(...message) {
|
|
|
|
Logging.message(LoggingTypes.Log, message);
|
|
|
|
}
|
2017-09-16 17:22:02 +00:00
|
|
|
static logWithCustomColors(type, colors, ...message) {
|
|
|
|
Logging.message(type, message, colors);
|
|
|
|
}
|
2017-08-23 14:45:03 +00:00
|
|
|
static error(error) {
|
|
|
|
if (typeof error === "string") {
|
|
|
|
Logging.errorMessage(error);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-16 17:12:58 +00:00
|
|
|
let m = "";
|
|
|
|
error.stack.forEach(e => {
|
2017-10-21 12:40:54 +00:00
|
|
|
m += e.toString() + "\n";
|
2017-09-16 17:12:58 +00:00
|
|
|
});
|
|
|
|
var message = error.name + " " + error.message + "\n" + m;
|
2017-08-23 14:45:03 +00:00
|
|
|
Logging.message(LoggingTypes.Error, [message]);
|
|
|
|
}
|
|
|
|
static errorMessage(...message) {
|
|
|
|
Logging.message(LoggingTypes.Error, message);
|
|
|
|
}
|
2017-09-16 17:22:02 +00:00
|
|
|
static async message(type, message, customColors) {
|
2017-08-23 14:45:03 +00:00
|
|
|
var consoleLogFormat = Reset;
|
2017-09-16 17:22:02 +00:00
|
|
|
if (!customColors) {
|
|
|
|
switch (type) {
|
|
|
|
case LoggingTypes.Log:
|
|
|
|
//m += FgWhite + BgBlack;
|
|
|
|
break;
|
|
|
|
case LoggingTypes.Error:
|
|
|
|
consoleLogFormat += FgRed; //FgWhite + BgRed + FgWhite;
|
|
|
|
break;
|
|
|
|
case LoggingTypes.Debug:
|
|
|
|
consoleLogFormat += FgCyan;
|
|
|
|
break;
|
|
|
|
case LoggingTypes.Warning:
|
|
|
|
consoleLogFormat += FgYellow;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
consoleLogFormat += customColors;
|
2017-08-23 14:45:03 +00:00
|
|
|
}
|
|
|
|
var mb = "";
|
|
|
|
if (typeof message === "string") {
|
|
|
|
mb = message;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
message.forEach(e => {
|
|
|
|
if (typeof e !== "string")
|
|
|
|
e = util.inspect(e, false, null);
|
|
|
|
if (e.endsWith("\n")) {
|
|
|
|
mb += e;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mb += e + " ";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
var m = "[" + LoggingTypes[type] + "][" + _getCallerFile() + "][" + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '') + "]: " + mb;
|
2017-11-04 22:59:56 +00:00
|
|
|
if (this.stdout)
|
|
|
|
console.log(consoleLogFormat + m + Reset);
|
|
|
|
let index = m.indexOf("\x1b");
|
|
|
|
while (index >= 0) {
|
2018-04-09 18:02:34 +00:00
|
|
|
m = m.substring(0, index) + m.substring(index + 5, m.length);
|
2017-11-04 22:59:56 +00:00
|
|
|
index = m.indexOf("\x1b");
|
|
|
|
}
|
2018-04-09 17:57:39 +00:00
|
|
|
m = m.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "");
|
2017-08-23 14:45:03 +00:00
|
|
|
if (this.logFileLocation) {
|
2018-02-18 15:00:16 +00:00
|
|
|
if ((!this.fileStream || !this.errorStream) && !this.writing) {
|
|
|
|
Logging.initializeFile();
|
2017-08-23 14:45:03 +00:00
|
|
|
}
|
|
|
|
Logging.writeMessageToFile(m, type === LoggingTypes.Error);
|
|
|
|
}
|
2017-09-16 17:12:58 +00:00
|
|
|
Logging.events.emit("message", { type: type, message: mb });
|
2017-08-23 14:45:03 +00:00
|
|
|
}
|
|
|
|
static writeMessageToFile(message, error) {
|
2017-10-21 12:40:54 +00:00
|
|
|
Logging.queue.push({ message: message.replace("\n", " "), error: error });
|
2017-08-23 14:45:03 +00:00
|
|
|
Logging.checkQueue();
|
|
|
|
}
|
2018-02-18 15:00:16 +00:00
|
|
|
static async checkQueue() {
|
|
|
|
try {
|
|
|
|
if (Logging.writing)
|
|
|
|
return;
|
|
|
|
if (Logging.queue.length <= 0)
|
|
|
|
return;
|
|
|
|
Logging.writing = true;
|
|
|
|
var message = Logging.queue[0];
|
|
|
|
message.message += "\n";
|
|
|
|
let data = new Buffer(message.message, "utf8");
|
|
|
|
if (data.byteLength < maxFileSize && data.byteLength + Logging.fileSize > maxFileSize) {
|
|
|
|
Logging.fileStream.close();
|
|
|
|
if (await fsExists(this.logFileLocation + "all.log.old"))
|
|
|
|
await fsUnlink(this.logFileLocation + "all.log.old");
|
|
|
|
await fsMove(this.logFileLocation + "all.log", this.logFileLocation + "all.log.old");
|
|
|
|
Logging.fileStream = fs.createWriteStream(this.logFileLocation + "all.log");
|
|
|
|
Logging.fileSize = 0;
|
|
|
|
}
|
|
|
|
Logging.fileSize += data.byteLength;
|
|
|
|
Logging.fileStream.write(data, async () => {
|
|
|
|
if (message.error) {
|
|
|
|
if (data.byteLength < maxFileSize && data.byteLength + Logging.errorSize > maxFileSize) {
|
|
|
|
Logging.errorStream.close();
|
|
|
|
if (await fsExists(this.logFileLocation + "error.log.old"))
|
|
|
|
await fsUnlink(this.logFileLocation + "error.log.old");
|
|
|
|
await fsMove(this.logFileLocation + "error.log", this.logFileLocation + "error.log.old");
|
|
|
|
Logging.errorStream = fs.createWriteStream(this.logFileLocation + "error.log");
|
|
|
|
Logging.errorSize = 0;
|
|
|
|
}
|
|
|
|
Logging.errorSize += data.byteLength;
|
|
|
|
Logging.errorStream.write(data, () => {
|
|
|
|
Logging.queue.splice(Logging.queue.indexOf(message), 1);
|
|
|
|
Logging.writing = false;
|
|
|
|
Logging.checkQueue();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2017-08-23 14:45:03 +00:00
|
|
|
Logging.queue.splice(Logging.queue.indexOf(message), 1);
|
|
|
|
Logging.writing = false;
|
|
|
|
Logging.checkQueue();
|
2018-02-18 15:00:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2017-08-23 14:45:03 +00:00
|
|
|
}
|
|
|
|
static async initializeFile() {
|
|
|
|
if (this.fileStream && this.errorStream)
|
|
|
|
return;
|
|
|
|
if (!this.logFileLocation)
|
|
|
|
return;
|
2018-02-18 15:00:16 +00:00
|
|
|
this.writing = true;
|
2017-08-23 14:45:03 +00:00
|
|
|
try {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
fs.exists(this.logFileLocation, (exists) => {
|
|
|
|
if (!exists) {
|
|
|
|
fs.mkdir(this.logFileLocation, (err) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
2018-02-18 15:00:16 +00:00
|
|
|
if (await fsExists(this.logFileLocation + "all.log")) {
|
|
|
|
if (await fsExists(this.logFileLocation + "all.log.old"))
|
|
|
|
await fsUnlink(this.logFileLocation + "all.log.old");
|
|
|
|
await fsMove(this.logFileLocation + "all.log", this.logFileLocation + "all.log.old");
|
|
|
|
}
|
|
|
|
if (await fsExists(this.logFileLocation + "error.log")) {
|
|
|
|
let stats = await fsStat(this.logFileLocation + "error.log");
|
|
|
|
if (stats.size > maxFileSize) {
|
|
|
|
if (await fsExists(this.logFileLocation + "error.log.old"))
|
|
|
|
await fsUnlink(this.logFileLocation + "error.log.old");
|
|
|
|
await fsMove(this.logFileLocation + "error.log", this.logFileLocation + "error.log.old");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.errorSize = stats.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.fileStream = fs.createWriteStream(this.logFileLocation + "all.log", { flags: "a" });
|
|
|
|
this.errorStream = fs.createWriteStream(this.logFileLocation + "error.log", { flags: "a" });
|
|
|
|
this.writing = false;
|
|
|
|
this.checkQueue();
|
2017-08-23 14:45:03 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Logging.logFileLocation = "./logs/";
|
|
|
|
Logging.stdout = true;
|
2018-02-18 15:00:16 +00:00
|
|
|
Logging.fileSize = 0;
|
|
|
|
Logging.errorSize = 0;
|
2017-08-23 14:45:03 +00:00
|
|
|
Logging.writing = false;
|
|
|
|
Logging.queue = new Array();
|
2017-09-16 17:12:58 +00:00
|
|
|
Logging.events = new events_1.EventEmitter();
|
2017-08-23 14:45:03 +00:00
|
|
|
exports.Logging = Logging;
|
2018-02-18 15:00:16 +00:00
|
|
|
function fsUnlink(path) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.unlink(path, (err) => {
|
|
|
|
if (err)
|
|
|
|
reject(err);
|
|
|
|
else
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function fsStat(path) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.stat(path, (err, stats) => {
|
|
|
|
if (err)
|
|
|
|
reject(err);
|
|
|
|
else
|
|
|
|
resolve(stats);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function fsMove(oldPath, newPath) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let callback = (err) => {
|
|
|
|
if (err)
|
|
|
|
reject(err);
|
|
|
|
else
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
fs.rename(oldPath, newPath, function (err) {
|
|
|
|
if (err) {
|
|
|
|
if (err.code === 'EXDEV') {
|
|
|
|
copy();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
function copy() {
|
|
|
|
var readStream = fs.createReadStream(oldPath);
|
|
|
|
var writeStream = fs.createWriteStream(newPath);
|
|
|
|
readStream.on('error', callback);
|
|
|
|
writeStream.on('error', callback);
|
|
|
|
readStream.on('close', function () {
|
|
|
|
fs.unlink(oldPath, callback);
|
|
|
|
});
|
|
|
|
readStream.pipe(writeStream);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function fsExists(path) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.exists(path, resolve);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function fsMkDir(path) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.exists(path, resolve);
|
|
|
|
});
|
|
|
|
}
|
2017-08-23 14:45:03 +00:00
|
|
|
function _getCallerFile() {
|
|
|
|
try {
|
|
|
|
var err = new Error();
|
2017-10-21 10:36:18 +00:00
|
|
|
var caller_file;
|
|
|
|
var current_file;
|
2017-08-23 14:45:03 +00:00
|
|
|
Error.prepareStackTrace = function (err, stack) { return stack; };
|
2017-10-21 10:36:18 +00:00
|
|
|
current_file = err.stack.shift().getFileName();
|
2017-08-23 14:45:03 +00:00
|
|
|
while (err.stack.length) {
|
2017-10-21 10:36:18 +00:00
|
|
|
caller_file = err.stack.shift().getFileName();
|
2017-10-21 10:45:20 +00:00
|
|
|
if (current_file !== caller_file)
|
|
|
|
return path.basename(caller_file);
|
2017-08-23 14:45:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (err) { }
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
var LoggingTypes;
|
|
|
|
(function (LoggingTypes) {
|
|
|
|
LoggingTypes[LoggingTypes["Log"] = 0] = "Log";
|
|
|
|
LoggingTypes[LoggingTypes["Warning"] = 1] = "Warning";
|
|
|
|
LoggingTypes[LoggingTypes["Error"] = 2] = "Error";
|
|
|
|
LoggingTypes[LoggingTypes["Debug"] = 3] = "Debug";
|
|
|
|
})(LoggingTypes = exports.LoggingTypes || (exports.LoggingTypes = {}));
|
|
|
|
//# sourceMappingURL=index.js.map
|