Skip to content

Commit

Permalink
✨ Fancy logger
Browse files Browse the repository at this point in the history
  • Loading branch information
FurryR committed Feb 25, 2024
1 parent 73680ab commit c37e432
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 26 deletions.
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MainLog } from './log'
import * as VM from 'scratch-vm'
import { version } from '../package.json'

import patchRuntime from './compiler/runtime'

Expand All @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions src/log/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion src/log/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import nanolog = require('@turbowarp/nanolog')
nanolog.enable()
export const MainLog = nanolog('main')
export const MainLog = nanolog()
78 changes: 58 additions & 20 deletions src/log/tw.cjs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c37e432

Please sign in to comment.