79 lines
2.4 KiB
JavaScript
79 lines
2.4 KiB
JavaScript
// Самодельный логгер, почти аналог: https://thecode.media/winston/
|
|
class Log
|
|
{
|
|
constructor()
|
|
{
|
|
this.data = [];
|
|
this.win = null;
|
|
}
|
|
show() {
|
|
if (this.win != null) this.win.Close();
|
|
|
|
this.win = new TWin();
|
|
this.win.divsh.onclick = null;
|
|
this.win.shadow = false;
|
|
//this.win.setParent(this.parent.win);
|
|
this.win.BuildGUI(10, 10);
|
|
this.win.setCaption(trt('Log'));
|
|
this.win.setContent('<div id="log_' + this.win.uid + '" style="height:100%;width:100%;"></div>');
|
|
this.win.setSize("600px", "260px");
|
|
this.win.setCenter();
|
|
this.win.hide(false);
|
|
|
|
this.addLine(true);
|
|
}
|
|
hide(){
|
|
if(this.win!=null) this.win.Close();
|
|
this.win=null;
|
|
}
|
|
addLine(all){
|
|
if (this.win == null) return;
|
|
let elem = document.getElementById("log_" + this.win.uid);
|
|
let i = 0;
|
|
if(!all) i = this.data.length-1;
|
|
for (;i < this.data.length;i++) {
|
|
let div = document.createElement('div');
|
|
if(this.data[i].p==1){
|
|
div.style.cssText='color: maroon; width:100%;';
|
|
}
|
|
else if(this.data[i].p==2){
|
|
div.style.cssText='color: green; width:100%;';
|
|
}
|
|
else if(this.data[i].p==3){
|
|
div.style.cssText='color: orange; width:100%;';
|
|
}
|
|
else if(this.data[i].p==4){
|
|
div.style.cssText='color: red; width:100%;';
|
|
}
|
|
div.innerHTML = "["+this.data[i].t+"] "+this.data[i].d;
|
|
elem.appendChild(div);
|
|
}
|
|
}
|
|
getTime(){
|
|
let data=new Date();
|
|
return ('0'+data.getHours()).slice(-2)+":"+('0'+data.getMinutes()).slice(-2)+":"+('0'+data.getSeconds()).slice(-2)+":"+('00'+data.getMilliseconds()).slice(-3);
|
|
}
|
|
debug(msg){
|
|
this.data.push({"p":1,"d":msg,"t": this.getTime()});
|
|
this.addLine(false);
|
|
}
|
|
info(msg){
|
|
this.data.push({"p":2,"d":msg,"t": this.getTime()});
|
|
this.addLine(false);
|
|
}
|
|
warn(msg){
|
|
this.data.push({"p":3,"d":msg,"t": this.getTime()});
|
|
this.addLine(false);
|
|
}
|
|
error(msg){
|
|
this.data.push({"p":4,"d":msg,"t": this.getTime()});
|
|
this.addLine(false);
|
|
}
|
|
}
|
|
|
|
var log = new Log();
|
|
/*log.debug("log.debug");
|
|
log.info("log.info");
|
|
log.warn("log.warn");
|
|
log.error("log.error");*/
|