From c37e432f297c81812d65a8ccab2a2a9dfb93fdb0 Mon Sep 17 00:00:00 2001 From: FurryR Date: Sun, 25 Feb 2024 18:56:35 +0800 Subject: [PATCH] :sparkles: Fancy logger --- src/index.ts | 13 ++++---- src/log/LICENSE | 21 +++++++++++++ src/log/index.ts | 2 +- src/log/tw.cjs | 78 +++++++++++++++++++++++++++++++++++------------- 4 files changed, 88 insertions(+), 26 deletions(-) create mode 100644 src/log/LICENSE diff --git a/src/index.ts b/src/index.ts index 6692239..a953d9d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import { MainLog } from './log' import * as VM from 'scratch-vm' +import { version } from '../package.json' import patchRuntime from './compiler/runtime' @@ -16,13 +17,15 @@ export function trap(callback: (vm: VM) => void) { Object.prototype.hasOwnProperty.call(self, 'editingTarget') && Object.prototype.hasOwnProperty.call(self, 'runtime') ) { - MainLog.info( - 'Hyren is based on Turbowarp compiler. Check https://turbowarp.com/editor for more details.' - ) - MainLog.info( + MainLog.groupCollapsed(`🐺 Hyren v${version}`) + console.log( 'Copyright (c) 2024 FurryR. Visit my profile at https://github.com/FurryR' ) - MainLog.info('Repository URL: https://github.com/FurryR/hyren') + console.log( + 'Hyren is based on Turbowarp compiler. Check https://turbowarp.com/editor for more details.' + ) + console.log('GitHub Repository: https://github.com/FurryR/hyren') + MainLog.groupEnd() Function.prototype.bind = oldBind callback(self as VM) return oldBind.call(this, self, ...args) diff --git a/src/log/LICENSE b/src/log/LICENSE new file mode 100644 index 0000000..4d414b3 --- /dev/null +++ b/src/log/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Thomas Weber + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/src/log/index.ts b/src/log/index.ts index 36756d1..adf8af9 100644 --- a/src/log/index.ts +++ b/src/log/index.ts @@ -1,3 +1,3 @@ import nanolog = require('@turbowarp/nanolog') nanolog.enable() -export const MainLog = nanolog('main') +export const MainLog = nanolog() diff --git a/src/log/tw.cjs b/src/log/tw.cjs index 1cfc16f..f631697 100644 --- a/src/log/tw.cjs +++ b/src/log/tw.cjs @@ -1,21 +1,59 @@ -let stat = false -function nanolog(name) { - return { - info(...args) { - if (stat) console.info(`[🐺 hyren] ${name}:`, ...args) - }, - warn(...args) { - if (stat) console.warn(`[🐺 hyren] ${name}:`, ...args) - }, - error(...args) { - if (stat) console.error(`[🐺 hyren] ${name}:`, ...args) - } - } +// only use colors in non-browser environments +const addColors = typeof document === 'undefined' + +const RESET = addColors ? '\u001b[0m' : '' +const GRAY = addColors ? '\u001b[90m' : '' + +const createLog = (namespace = '') => { + const log = childNamespace => + createLog(namespace ? `${namespace}::${childNamespace}` : childNamespace) + + const formattedNamespace = namespace ? [`${GRAY}${namespace}${RESET}`] : [] + + log.debug = log.log = console.log.bind( + console, + '%c🐺 Hyren', + ` background-color: lightgrey; border-radius: 1rem; margin-right: 0.25rem; padding: 0 0.5rem; color: white; font-weight: normal;`, + ...formattedNamespace + ) + log.group = console.group.bind( + console, + '%c🐺 Hyren', + ` background-color: lightgrey; border-radius: 1rem; margin-right: 0.25rem; padding: 0 0.5rem; color: white; font-weight: normal;`, + ...formattedNamespace + ) + log.groupCollapsed = console.groupCollapsed.bind( + console, + '%c🐺 Hyren', + ` background-color: lightgrey; border-radius: 1rem; margin-right: 0.25rem; padding: 0 0.5rem; color: white; font-weight: normal;`, + ...formattedNamespace + ) + log.groupEnd = console.groupEnd.bind(console) + log.info = console.info.bind( + console, + '%c🐺 Hyren', + ` background-color: darkblue; border-radius: 1rem; margin-right: 0.25rem; padding: 0 0.5rem; color: white; font-weight: normal;`, + ...formattedNamespace + ) + log.warn = console.warn.bind( + console, + '%c🐺 Hyren', + ` background-color: yellow; border-radius: 1rem; margin-right: 0.25rem; padding: 0 0.5rem; color: black; font-weight: normal;`, + ...formattedNamespace + ) + log.error = console.error.bind( + console, + '%c🐺 Hyren', + ` background-color: red; border-radius: 1rem; margin-right: 0.25rem; padding: 0 0.5rem; color: white; font-weight: normal;`, + ...formattedNamespace + ) + + return log } -nanolog.enable = () => { - stat = true -} -nanolog.disable = () => { - stat = false -} -module.exports = nanolog + +/** + * @deprecated does nothing + */ +createLog.enable = createLog.disable = () => {} + +module.exports = createLog