Skip to content

Commit f709e49

Browse files
authored
refactor: Replace console.log with custom & unique log structure (#40)
* 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
1 parent 0eb02f8 commit f709e49

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

__tests__/index.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import UnityWebgl from '../lib/core/src/index'
77
import 'jest-canvas-mock'
88

9+
const logPrefix = '[unity-webgl]'
10+
911
// mock UnityInstance methods
1012
const mockLoaderResult = jest.fn()
1113
const mockSendMessage = jest.fn()
@@ -133,6 +135,7 @@ describe('UnityWebgl', () => {
133135
unityWebgl.sendMessage('ObjectName', 'MethodName')
134136

135137
expect(consoleSpy).toHaveBeenCalledWith(
138+
logPrefix,
136139
'Unable to Send Message while Unity is not Instantiated.'
137140
)
138141

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

161164
expect(consoleSpy).toHaveBeenCalledWith(
165+
logPrefix,
162166
'Unable to requestPointerLock while Unity is not Instantiated.'
163167
)
164168

@@ -186,6 +190,7 @@ describe('UnityWebgl', () => {
186190
unityWebgl.takeScreenshot()
187191

188192
expect(consoleSpy).toHaveBeenCalledWith(
193+
logPrefix,
189194
'Unable to take Screenshot while Unity is not Instantiated.'
190195
)
191196
consoleSpy.mockRestore()
@@ -213,6 +218,7 @@ describe('UnityWebgl', () => {
213218
unityWebgl.setFullscreen(true)
214219

215220
expect(consoleSpy).toHaveBeenCalledWith(
221+
logPrefix,
216222
'Unable to set Fullscreen while Unity is not Instantiated.'
217223
)
218224

lib/core/src/event.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class UnityWebglEvent {
104104
*/
105105
emit(name: string, ...args: any[]) {
106106
if (!this._e[name]) {
107-
console.warn(`No listener for event ${name}`)
107+
// log.warn(`No listener for event ${name}`)
108108
return this
109109
}
110110

lib/core/src/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { UnityWebglEvent } from './event'
22
import { unityLoader } from './loader'
3-
import { isBrowser, isObject, omit, queryCanvas } from './utils'
3+
import { isBrowser, isObject, omit, queryCanvas, log } from './utils'
44
import { UnityConfig, UnityArguments, UnityInstance } from './types'
55

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

6161
if (this._unity && this._canvas && this._loader) {
62-
console.warn('Unity instance already created')
62+
log.warn('Unity instance already created')
6363
return Promise.resolve()
6464
}
6565

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

@@ -135,7 +135,7 @@ class UnityWebgl extends UnityWebglEvent {
135135
*/
136136
requestPointerLock(): void {
137137
if (!this._unity || !this._unity.Module.canvas) {
138-
console.warn('Unable to requestPointerLock while Unity is not Instantiated.')
138+
log.warn('Unable to requestPointerLock while Unity is not Instantiated.')
139139
return
140140
}
141141
this._unity.Module.canvas.requestPointerLock()
@@ -149,7 +149,7 @@ class UnityWebgl extends UnityWebglEvent {
149149
*/
150150
takeScreenshot(dataType?: string, quality?: any): string | undefined {
151151
if (!this._unity || !this._unity.Module.canvas) {
152-
console.warn('Unable to take Screenshot while Unity is not Instantiated.')
152+
log.warn('Unable to take Screenshot while Unity is not Instantiated.')
153153
return
154154
}
155155

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

@@ -174,7 +174,7 @@ class UnityWebgl extends UnityWebglEvent {
174174
*/
175175
unload(): Promise<void> {
176176
if (!this._unity) {
177-
console.warn('Unable to Quit Unity while Unity is not Instantiated.')
177+
log.warn('Unable to Quit Unity while Unity is not Instantiated.')
178178
return Promise.reject()
179179
}
180180
this.emit('beforeUnmount', this)
@@ -195,7 +195,7 @@ class UnityWebgl extends UnityWebglEvent {
195195
this.emit('unmounted')
196196
})
197197
.catch((err) => {
198-
console.error('Unable to Unload Unity')
198+
log.error('Unable to Unload Unity')
199199
this.emit('error', err)
200200
throw err
201201
})
@@ -211,7 +211,7 @@ class UnityWebgl extends UnityWebglEvent {
211211
unsafe_unload(): Promise<void> {
212212
try {
213213
if (!this._unity || !this._unity.Module.canvas) {
214-
console.warn('No Unity Instance found.')
214+
log.warn('No Unity Instance found.')
215215
return Promise.reject()
216216
}
217217
// Re-attaches the canvas to the body element of the document. This way it

lib/core/src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ export function queryCanvas(canvas: string | HTMLCanvasElement): HTMLCanvasEleme
2222
}
2323
return document.querySelector(canvas)
2424
}
25+
26+
export const log = (() => {
27+
const prefix = '[unity-webgl]'
28+
const _log = (...args: any[]) => console.log(prefix, ...args)
29+
_log.warn = (...args: any[]) => console.warn(prefix, ...args)
30+
_log.error = (...args: any[]) => console.error(prefix, ...args)
31+
return _log
32+
})()

0 commit comments

Comments
 (0)