2019-03-09 03:31:29 +00:00
|
|
|
'use strict';
|
2019-08-04 23:38:45 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2024-05-19 22:14:33 +00:00
|
|
|
exports.Log = void 0;
|
2019-08-04 23:38:45 +00:00
|
|
|
const winston_1 = require("winston");
|
|
|
|
const path_1 = require("path");
|
|
|
|
const fs_extra_1 = require("fs-extra");
|
|
|
|
const os_1 = require("os");
|
2019-03-09 03:31:29 +00:00
|
|
|
const logTime = 'MM/DD/YY-HH:mm:ss';
|
2019-03-18 20:50:40 +00:00
|
|
|
/**
|
|
|
|
* Determine the location of the log file based on the operating system
|
|
|
|
* and return as an absolute string from os.homedir()
|
|
|
|
*
|
|
|
|
* @returns {string} Path to log file
|
|
|
|
**/
|
2019-03-09 03:31:29 +00:00
|
|
|
async function logFile() {
|
2024-04-06 23:09:50 +00:00
|
|
|
const homeDir = (0, os_1.homedir)();
|
2020-07-27 15:43:55 +00:00
|
|
|
const linuxDir = `/.mcopy/`;
|
2019-03-21 22:01:41 +00:00
|
|
|
const macDir = `/Library/Logs/mcopy/`;
|
|
|
|
const winDir = `/AppData/Roaming/mcopy/`;
|
2024-04-06 23:09:50 +00:00
|
|
|
let logPath = (0, path_1.normalize)((0, path_1.join)(homeDir, linuxDir));
|
2019-08-04 23:38:45 +00:00
|
|
|
let dirExists;
|
2019-03-09 03:31:29 +00:00
|
|
|
if (process.platform === 'darwin') {
|
2024-04-06 23:09:50 +00:00
|
|
|
logPath = (0, path_1.normalize)((0, path_1.join)(homeDir, macDir));
|
2019-03-09 03:31:29 +00:00
|
|
|
}
|
|
|
|
else if (process.platform === 'win32') {
|
2024-04-06 23:09:50 +00:00
|
|
|
logPath = (0, path_1.normalize)((0, path_1.join)(homeDir, winDir));
|
2019-03-09 03:31:29 +00:00
|
|
|
}
|
2020-08-10 03:25:55 +00:00
|
|
|
try {
|
2024-04-06 23:09:50 +00:00
|
|
|
dirExists = await (0, fs_extra_1.exists)(logPath);
|
2020-08-10 03:25:55 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
2019-08-04 23:38:45 +00:00
|
|
|
if (!dirExists) {
|
2020-04-24 18:33:58 +00:00
|
|
|
try {
|
2024-04-06 23:09:50 +00:00
|
|
|
await (0, fs_extra_1.mkdir)(logPath);
|
2020-04-24 18:33:58 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.error(`Error creating directory for mcopy log file, ${logPath}`);
|
2020-08-10 03:25:55 +00:00
|
|
|
console.error(err);
|
2020-04-24 18:33:58 +00:00
|
|
|
}
|
2019-03-09 03:31:29 +00:00
|
|
|
}
|
2024-04-06 23:09:50 +00:00
|
|
|
return (0, path_1.join)(logPath, 'mcopy.log');
|
2019-03-09 03:31:29 +00:00
|
|
|
}
|
2019-03-18 20:50:40 +00:00
|
|
|
/**
|
|
|
|
* Create and return the logger transport based on settings determined in
|
|
|
|
* arguments object
|
|
|
|
*
|
|
|
|
* @param {object} arg Arguments from process
|
|
|
|
*
|
|
|
|
* @returns {object} Logger transport
|
|
|
|
**/
|
2024-05-19 22:14:33 +00:00
|
|
|
async function Log(arg) {
|
|
|
|
let transport;
|
2019-08-04 23:38:45 +00:00
|
|
|
let consoleFormat = {
|
|
|
|
colorize: true
|
|
|
|
};
|
|
|
|
let fileFormat = {
|
|
|
|
filename: await logFile(),
|
|
|
|
json: true
|
|
|
|
};
|
2024-05-19 22:14:33 +00:00
|
|
|
if (arg && arg.label) {
|
|
|
|
consoleFormat.label = arg.label;
|
|
|
|
fileFormat.label = arg.label;
|
2019-03-09 03:31:29 +00:00
|
|
|
}
|
2024-05-19 22:14:33 +00:00
|
|
|
transport = (0, winston_1.createLogger)({
|
|
|
|
format: winston_1.format.combine(winston_1.format.label({ label: arg.label || 'mcopy' }), winston_1.format.timestamp({
|
|
|
|
format: 'YYYY-MM-DD HH:mm:ss'
|
|
|
|
}), winston_1.format.printf((info) => `${info.timestamp} [${info.label}] ${info.level}: ${info.message}` + (info.splat !== undefined ? `${info.splat}` : " "))),
|
|
|
|
transports: [
|
|
|
|
new (winston_1.transports.Console)(consoleFormat),
|
|
|
|
new (winston_1.transports.File)(fileFormat)
|
|
|
|
]
|
|
|
|
});
|
2019-03-09 03:31:29 +00:00
|
|
|
return transport;
|
2024-05-19 22:14:33 +00:00
|
|
|
}
|
|
|
|
exports.Log = Log;
|
|
|
|
module.exports = { Log };
|
2019-03-09 03:31:29 +00:00
|
|
|
//# sourceMappingURL=index.js.map
|