Last active
June 10, 2023 16:29
-
-
Save JCloudYu/5b2d97f9560d92c721e453cc56e6bf45 to your computer and use it in GitHub Desktop.
shared-storage extended console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import $ from "shstore"; | |
import util from "util"; | |
const STORE_KEY = 'console'; | |
declare global { | |
interface ExtendedSharedStorage{(scope:typeof STORE_KEY):{ | |
colorize:boolean; | |
max_depth:number|null; | |
show_milli:boolean; | |
debug(...data:any[]):void; | |
info(...data:any[]):void; | |
log(...data:any[]):void; | |
warn(...data:any[]):void; | |
error(...data:any[]):void; | |
}} | |
} | |
$(STORE_KEY).colorize = true; | |
$(STORE_KEY).max_depth = null; | |
$(STORE_KEY).show_milli = false; | |
$(STORE_KEY).debug = (...data:any[])=>{ | |
const show_milli = !!$(STORE_KEY).show_milli; | |
console.debug(`[${LocaleISOString(show_milli)}][DBG]`, ...Decorate(data)); | |
}; | |
$(STORE_KEY).info = (...data:any[])=>{ | |
const show_milli = !!$(STORE_KEY).show_milli; | |
console.info( `[${LocaleISOString(show_milli)}][INF]`, ...Decorate(data)); | |
}; | |
$(STORE_KEY).log = (...data:any[])=>{ | |
const show_milli = !!$(STORE_KEY).show_milli; | |
console.log( `[${LocaleISOString(show_milli)}][LOG]`, ...Decorate(data)); | |
}; | |
$(STORE_KEY).warn = (...data:any[])=>{ | |
const show_milli = !!$(STORE_KEY).show_milli; | |
console.warn( `[${LocaleISOString(show_milli)}][WRN]`, ...Decorate(data)); | |
}; | |
$(STORE_KEY).error = (...data:any[])=>{ | |
const show_milli = !!$(STORE_KEY).show_milli; | |
console.error(`[${LocaleISOString(show_milli)}][ERR]`, ...Decorate(data)); | |
}; | |
function Decorate(...args:any[]):string[] { | |
const depth = $(STORE_KEY).max_depth||null; | |
const colorize = $(STORE_KEY).colorize||true; | |
return args.map((i)=>util.inspect(i, false, depth, colorize)); | |
} | |
function LocaleISOString():string; | |
function LocaleISOString(show_milli:boolean):string; | |
function LocaleISOString(date:Date, show_milli:boolean):string; | |
function LocaleISOString(arg1?:Date|boolean, arg2?:boolean):string { | |
let date:Date, show_milli:boolean; | |
if ( arguments.length === 0 ) { | |
date = new Date(); | |
show_milli = false; | |
} | |
else | |
if ( typeof arg1 === "boolean" ) { | |
date = new Date(); | |
show_milli = arg1; | |
} | |
else { | |
date = arg1!; | |
show_milli = !!arg2; | |
} | |
let offset, zone = date.getTimezoneOffset(); | |
if ( zone === 0 ) { | |
offset = 'Z'; | |
} | |
else { | |
const sign = zone > 0 ? '-' : '+'; | |
zone = Math.abs(zone); | |
const zone_hour = Math.floor(zone/60).toString(); | |
const zone_min = (zone%60).toString(); | |
offset = sign + zone_hour.padStart(2, '0') + zone_min.padStart(2, '0'); | |
} | |
const milli = show_milli ? ('.' + (date.getMilliseconds() % 1000).toString().padStart(3, '0')) : ''; | |
return date.getFullYear() + | |
'-' + (date.getMonth()+1).toString().padStart(2, '0') + | |
'-' + (date.getDate()).toString().padStart(2, '0') + | |
'T' + (date.getHours()).toString().padStart(2, '0') + | |
':' + (date.getMinutes()).toString().padStart(2, '0') + | |
':' + (date.getSeconds()).toString().padStart(2, '0') + | |
milli + offset; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment