mirror of
https://github.com/CHOMPStation2/CHOMPStation2.git
synced 2025-12-13 11:43:31 +00:00
More TGUI Maintenance
This commit is contained in:
72
tgui/packages/tgui-dev-server/logging.js
Normal file
72
tgui/packages/tgui-dev-server/logging.js
Normal file
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* @file
|
||||
* @copyright 2020 Aleksej Komarov
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
const inception = Date.now();
|
||||
|
||||
// Runtime detection
|
||||
const isNode = process && process.release && process.release.name === 'node';
|
||||
let isChrome = false;
|
||||
try {
|
||||
isChrome = window.navigator.userAgent.toLowerCase().includes('chrome');
|
||||
}
|
||||
catch {}
|
||||
|
||||
// Timestamping function
|
||||
const getTimestamp = () => {
|
||||
const timestamp = String(Date.now() - inception)
|
||||
.padStart(4, '0')
|
||||
.padStart(7, ' ');
|
||||
const seconds = timestamp.substr(0, timestamp.length - 3);
|
||||
const millis = timestamp.substr(-3);
|
||||
return `${seconds}.${millis}`;
|
||||
};
|
||||
|
||||
const getPrefix = (() => {
|
||||
if (isNode) {
|
||||
// Escape sequences
|
||||
const ESC = {
|
||||
dimmed: '\x1b[38;5;240m',
|
||||
bright: '\x1b[37;1m',
|
||||
reset: '\x1b[0m',
|
||||
};
|
||||
return ns => [
|
||||
`${ESC.dimmed}${getTimestamp()} ${ESC.bright}${ns}${ESC.reset}`,
|
||||
];
|
||||
}
|
||||
if (isChrome) {
|
||||
// Styles
|
||||
const styles = {
|
||||
dimmed: 'color: #888',
|
||||
bright: 'font-weight: bold',
|
||||
};
|
||||
return ns => [
|
||||
`%c${getTimestamp()}%c ${ns}`,
|
||||
styles.dimmed,
|
||||
styles.bright,
|
||||
];
|
||||
}
|
||||
return ns => [
|
||||
`${getTimestamp()} ${ns}`,
|
||||
];
|
||||
})();
|
||||
|
||||
/**
|
||||
* Creates a logger object.
|
||||
*/
|
||||
export const createLogger = ns => ({
|
||||
log: (...args) => console.log(...getPrefix(ns), ...args),
|
||||
trace: (...args) => console.trace(...getPrefix(ns), ...args),
|
||||
debug: (...args) => console.debug(...getPrefix(ns), ...args),
|
||||
info: (...args) => console.info(...getPrefix(ns), ...args),
|
||||
warn: (...args) => console.warn(...getPrefix(ns), ...args),
|
||||
error: (...args) => console.error(...getPrefix(ns), ...args),
|
||||
});
|
||||
|
||||
/**
|
||||
* Explicitly log with chosen namespace.
|
||||
*/
|
||||
export const directLog = (ns, ...args) =>
|
||||
console.log(...getPrefix(ns), ...args);
|
||||
Reference in New Issue
Block a user