218 lines
5.7 KiB
JavaScript
218 lines
5.7 KiB
JavaScript
const fs = require("fs")
|
|
const path = require("path")
|
|
|
|
var logStream = null
|
|
var logInstance = new Map()
|
|
|
|
setup()
|
|
|
|
function getDate(formated) {
|
|
|
|
var date = new Date()
|
|
|
|
// [Date Format] - Format de la date
|
|
|
|
var gmonth = date.getMonth()
|
|
var gday = date.getDate()
|
|
var gHour = date.getHours()
|
|
var gMinute = date.getMinutes()
|
|
var gSecondes = date.getSeconds()
|
|
|
|
|
|
if(date.getMonth() + 1 <= 10) {
|
|
gmonth = "0" + (date.getMonth() + 1)
|
|
}
|
|
|
|
if(date.getDate() + 1 <= 10) {
|
|
gday = "0" + date.getDate()
|
|
}
|
|
|
|
if(date.getHours() + 1 <= 10) {
|
|
gHour = "0" + date.getHours()
|
|
}
|
|
|
|
if(date.getMinutes() + 1 <= 10) {
|
|
gMinute = "0" + date.getMinutes()
|
|
}
|
|
|
|
if(date.getSeconds() + 1 <= 10) {
|
|
gSecondes = "0" + date.getSeconds()
|
|
}
|
|
|
|
if(!formated) {
|
|
return gday + "/" + gmonth + " - " + gHour + "h" + "-" + gMinute + "m" + "-" + gSecondes + "s"
|
|
} else {
|
|
return date.getFullYear() + "-" + gmonth + "-" + gday + "-" + gHour + "h" + "-" + gMinute + "m" + "-" + gSecondes + "s"
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function setup() {
|
|
|
|
if(!fs.existsSync(__dirname + path.sep + "logs" + path.sep)) {
|
|
fs.mkdir(__dirname + path.sep + "logs", (err) => {
|
|
if(!err) {
|
|
|
|
|
|
console.log("[Logs] - Dossier de logs crée ! !")
|
|
|
|
} else {
|
|
|
|
console.log("[Logs] -Erreur d'écriture par manque de permission ")
|
|
console.log(err)
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
logStream = fs.createWriteStream(__dirname + path.sep + "logs" + path.sep + getDate(true) + ".log", {
|
|
flags: 'a'
|
|
});
|
|
|
|
|
|
logStream.write("[" + require("../../package.json").name + "@"+ require("../../package.json").version + "] - [" + getDate(true) + "]" + "\n")
|
|
logStream.write("Subsonics-Web by Raphix" + "\n")
|
|
logStream.write("----------------------------------------------------------------" + "\n")
|
|
|
|
process.on('uncaughtException', (err) => {
|
|
console.error("["+ "FATAL" + "] - The application has encountered an error ! Please Restart ! #E = " + err + "\n")
|
|
console.error(err)
|
|
logStream.write("[" + getDate() + "] - ["+ "FATAL" + "] - The application has encountered an error ! Please Restart ! #E = " + err + "\n")
|
|
logStream.end( "["+ "UNCAUGHT_EXCEPTION" + "]" + " - [END OF LOGS] - [" + getDate() + ']')
|
|
logStream.close()
|
|
|
|
});
|
|
|
|
|
|
process.on('beforeExit', (err) => {
|
|
logStream.end( "["+ "BEFORE_EXIT" + "]" + " - [END OF LOGS] - [" + getDate(true) + ']')
|
|
logStream.close()
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
module.exports.getInstance = function (name) {
|
|
|
|
if(logInstance.has(name)) {
|
|
|
|
return logInstance.get(name)
|
|
|
|
} else {
|
|
|
|
var logtext = "[Logs] - [ERROR] - '" + name + "' n'est pas enregistré en tant qu'instance de log !"
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
console.log(logtext)
|
|
|
|
return null
|
|
}
|
|
|
|
}
|
|
|
|
module.exports.LogType = class {
|
|
|
|
constructor(typeName) {
|
|
this.type = typeName;
|
|
this.steps = new Map()
|
|
this.step = this.initializeStep()
|
|
logInstance.set(typeName, this)
|
|
}
|
|
|
|
log(txt) {
|
|
|
|
|
|
|
|
var logtext = "[" + this.type + "] - [INFO] - " + txt
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
console.log(logtext)
|
|
|
|
|
|
}
|
|
|
|
warn(txt) {
|
|
|
|
var logtext = "[" + this.type + "] - [WARN] - " + txt
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
console.log(logtext)
|
|
|
|
}
|
|
|
|
error(txt) {
|
|
|
|
|
|
var logtext = "[" + this.type + "] - [ERROR] - " + txt
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
console.log(logtext)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initializeStep() {
|
|
const parent = this;
|
|
return {
|
|
init: function(id, desc) {
|
|
parent.steps.set(id, desc)
|
|
var logtext = "[" + parent.type + "] - [INFO] - [STEP] - " + desc + " - En cours ..."
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
console.log(logtext)
|
|
|
|
},
|
|
end: function(id) {
|
|
|
|
if(parent.steps.has(id)) {
|
|
|
|
var logtext = "[" + parent.type + "] - [INFO] - [STEP] - " + parent.steps.get(id) + " - Terminé !"
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
console.log(logtext)
|
|
parent.steps.delete(id)
|
|
} else {
|
|
|
|
var logtext = "[" + parent.type + "] - [WARN] - [STEP] - '" + id + "' n'est pas enregistré en tant qu'étape !"
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
console.log(logtext)
|
|
|
|
}
|
|
|
|
},
|
|
error: function(id, errorDesc) {
|
|
|
|
if(parent.steps.has(id)) {
|
|
|
|
var logtext = "[" + parent.type + "] - [ERROR] - [STEP] - " + parent.steps.get(id) + " - Une erreur a été rencontré dans l'étape ! : #E = " + errorDesc
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
console.log(logtext)
|
|
parent.steps.delete(id)
|
|
} else {
|
|
|
|
var logtext = "[" + parent.type + "] - [WARN] - [STEP] - '" + id + "' n'est pas enregistré en tant qu'étape !"
|
|
logStream.write("[" + getDate() + "] - " + logtext + "\n")
|
|
console.log(logtext)
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|