Files
Bubberstation/tgui/packages/common/logging.js
Aleksej Komarov 602270d178 tgui: MIT License (#50969)
* MIT license headers

* various tweaks

- Tweak jsdoc headers a bit
- Use an old-school format with SPDX-License-Identifier for SCSS
- Add headers to tgui dmcode

* Simplify the license section

* Rebuild tgui, small tweaks

Co-authored-by: ZeWaka <zewakagamer@gmail.com>

About The Pull Request

All relevant source code now contains copyright headers, that explicitly assert copyright and license for every file.

This has been done to prepare TGUI for wider adoption. Goon station devs are interested in using TGUI, and hopefully this will result in a nice collaboration and improvements to both codebases.

The following files were relicensed under MIT:

    code/controllers/subsystem/tgui.dm
    code/modules/tgui/*.dm
    tgui/**/*.js
    tgui/**/*.scss

The following files were kept untouched:

    tgui/packages/tgui/interfaces/**/*.js
    tgui/packages/tgui/styles/interfaces/**/*.scss

Project is still basically AGPL-3.0 under /tg/station's parent license (with added MIT texts), but allows importing MIT code into MIT-compatible codebases.
2020-05-11 10:32:52 +12:00

73 lines
1.7 KiB
JavaScript

/**
* @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);