Skip to content

Commit

Permalink
refactor: Replace console.log with custom & unique log structure (#40)
Browse files Browse the repository at this point in the history
* refactor(core): Replace console.log with custom & unique log structure

- 在 utils.ts 中添加自定义 log 函数,统一日志格式
- 在 event.ts 和 index.ts 中使用新的 log 函数替代 console 输出
- 优化了日志输出,增加了 '[unity-webgl]' 前缀

* test: Add log prefix to UnityWebgl error messages
  • Loading branch information
Marinerer authored Jan 17, 2025
1 parent 0eb02f8 commit f709e49
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
6 changes: 6 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import UnityWebgl from '../lib/core/src/index'
import 'jest-canvas-mock'

const logPrefix = '[unity-webgl]'

// mock UnityInstance methods
const mockLoaderResult = jest.fn()
const mockSendMessage = jest.fn()
Expand Down Expand Up @@ -133,6 +135,7 @@ describe('UnityWebgl', () => {
unityWebgl.sendMessage('ObjectName', 'MethodName')

expect(consoleSpy).toHaveBeenCalledWith(
logPrefix,
'Unable to Send Message while Unity is not Instantiated.'
)

Expand All @@ -159,6 +162,7 @@ describe('UnityWebgl', () => {
unityWebgl.requestPointerLock()

expect(consoleSpy).toHaveBeenCalledWith(
logPrefix,
'Unable to requestPointerLock while Unity is not Instantiated.'
)

Expand Down Expand Up @@ -186,6 +190,7 @@ describe('UnityWebgl', () => {
unityWebgl.takeScreenshot()

expect(consoleSpy).toHaveBeenCalledWith(
logPrefix,
'Unable to take Screenshot while Unity is not Instantiated.'
)
consoleSpy.mockRestore()
Expand Down Expand Up @@ -213,6 +218,7 @@ describe('UnityWebgl', () => {
unityWebgl.setFullscreen(true)

expect(consoleSpy).toHaveBeenCalledWith(
logPrefix,
'Unable to set Fullscreen while Unity is not Instantiated.'
)

Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class UnityWebglEvent {
*/
emit(name: string, ...args: any[]) {
if (!this._e[name]) {
console.warn(`No listener for event ${name}`)
// log.warn(`No listener for event ${name}`)
return this
}

Expand Down
18 changes: 9 additions & 9 deletions lib/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UnityWebglEvent } from './event'
import { unityLoader } from './loader'
import { isBrowser, isObject, omit, queryCanvas } from './utils'
import { isBrowser, isObject, omit, queryCanvas, log } from './utils'
import { UnityConfig, UnityArguments, UnityInstance } from './types'

type CanvasElementOrString = HTMLCanvasElement | string
Expand Down Expand Up @@ -59,7 +59,7 @@ class UnityWebgl extends UnityWebglEvent {
if (!isBrowser) return Promise.resolve()

if (this._unity && this._canvas && this._loader) {
console.warn('Unity instance already created')
log.warn('Unity instance already created')
return Promise.resolve()
}

Expand Down Expand Up @@ -110,7 +110,7 @@ class UnityWebgl extends UnityWebglEvent {
*/
sendMessage(objectName: string, methodName: string, value?: any) {
if (!this._unity) {
console.warn('Unable to Send Message while Unity is not Instantiated.')
log.warn('Unable to Send Message while Unity is not Instantiated.')
return this
}

Expand All @@ -135,7 +135,7 @@ class UnityWebgl extends UnityWebglEvent {
*/
requestPointerLock(): void {
if (!this._unity || !this._unity.Module.canvas) {
console.warn('Unable to requestPointerLock while Unity is not Instantiated.')
log.warn('Unable to requestPointerLock while Unity is not Instantiated.')
return
}
this._unity.Module.canvas.requestPointerLock()
Expand All @@ -149,7 +149,7 @@ class UnityWebgl extends UnityWebglEvent {
*/
takeScreenshot(dataType?: string, quality?: any): string | undefined {
if (!this._unity || !this._unity.Module.canvas) {
console.warn('Unable to take Screenshot while Unity is not Instantiated.')
log.warn('Unable to take Screenshot while Unity is not Instantiated.')
return
}

Expand All @@ -162,7 +162,7 @@ class UnityWebgl extends UnityWebglEvent {
*/
setFullscreen(enabled: boolean) {
if (!this._unity) {
console.warn('Unable to set Fullscreen while Unity is not Instantiated.')
log.warn('Unable to set Fullscreen while Unity is not Instantiated.')
return
}

Expand All @@ -174,7 +174,7 @@ class UnityWebgl extends UnityWebglEvent {
*/
unload(): Promise<void> {
if (!this._unity) {
console.warn('Unable to Quit Unity while Unity is not Instantiated.')
log.warn('Unable to Quit Unity while Unity is not Instantiated.')
return Promise.reject()
}
this.emit('beforeUnmount', this)
Expand All @@ -195,7 +195,7 @@ class UnityWebgl extends UnityWebglEvent {
this.emit('unmounted')
})
.catch((err) => {
console.error('Unable to Unload Unity')
log.error('Unable to Unload Unity')
this.emit('error', err)
throw err
})
Expand All @@ -211,7 +211,7 @@ class UnityWebgl extends UnityWebglEvent {
unsafe_unload(): Promise<void> {
try {
if (!this._unity || !this._unity.Module.canvas) {
console.warn('No Unity Instance found.')
log.warn('No Unity Instance found.')
return Promise.reject()
}
// Re-attaches the canvas to the body element of the document. This way it
Expand Down
8 changes: 8 additions & 0 deletions lib/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ export function queryCanvas(canvas: string | HTMLCanvasElement): HTMLCanvasEleme
}
return document.querySelector(canvas)
}

export const log = (() => {
const prefix = '[unity-webgl]'
const _log = (...args: any[]) => console.log(prefix, ...args)
_log.warn = (...args: any[]) => console.warn(prefix, ...args)
_log.error = (...args: any[]) => console.error(prefix, ...args)
return _log
})()

0 comments on commit f709e49

Please sign in to comment.