diff --git a/README.en.md b/README.en.md index bf47a34e141ffed1cd478ea9d5aa5019de3db74d..cf67bde8e7b430a17ffd860acd48b6df7b94ffab 100644 --- a/README.en.md +++ b/README.en.md @@ -24,7 +24,8 @@ verificationcodescenariolibrary │ ├──common │ │ │ utils │ │ │ ├──BreakpointSystem.ets // Breakpoint System -│ │ │ └──WindowUtil.ets // Window Utils +│ │ │ ├──Logger.ets // Logger Utils +│ │ │ └──WindowUtil.ets // Window Utils │ │ ├──Constants.ets // Common constant class │ │ └──GlobalBuilderContext.ets // Global page information │ ├──view diff --git a/README.md b/README.md index bc068449d5e67f4facb7e9e24a33ff7904505001..e610522ef08e95dcccadaae84701acc97c3bf9b7 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,8 @@ │ ├──common │ │ │ utils │ │ │ ├──BreakpointSystem.ets // 断点 -│ │ │ └──WindowUtil.ets // 窗口工具 +│ │ │ ├──Logger.ets // 日志 +│ │ │ └──WindowUtil.ets // 窗口工具 │ │ ├──Constants.ets // 公共常量类 │ │ └──GlobalBuilderContext.ets // 全局页面信息 │ ├──view diff --git a/verificationcodescenariolibrary/Index.ets b/verificationcodescenariolibrary/Index.ets index b0ed89000a509e7654446ca43f1da763f649ab5d..b09c3cb816724fb12d14052837a0db776c18b2c6 100644 --- a/verificationcodescenariolibrary/Index.ets +++ b/verificationcodescenariolibrary/Index.ets @@ -1,3 +1,5 @@ export { VerificationCodeScenarioView as VerificationCodeScenarioComponent } from './src/main/ets/view/VerificationCodeScenarioView'; -export { VerificationCodeController } from './src/main/ets/VerificationCodeController'; \ No newline at end of file +export { VerificationCodeController } from './src/main/ets/VerificationCodeController'; + +export { default as Logger } from './src/main/ets/common/utils/Logger'; \ No newline at end of file diff --git a/verificationcodescenariolibrary/src/main/ets/common/utils/BreakpointSystem.ets b/verificationcodescenariolibrary/src/main/ets/common/utils/BreakpointSystem.ets index 2755f5b8323d58c6db8c49cae7ca950fe660e75a..1eb330f2231df7d2568648e59b7f50fc056d38a8 100644 --- a/verificationcodescenariolibrary/src/main/ets/common/utils/BreakpointSystem.ets +++ b/verificationcodescenariolibrary/src/main/ets/common/utils/BreakpointSystem.ets @@ -15,8 +15,8 @@ import { window } from '@kit.ArkUI'; import type { BusinessError } from '@kit.BasicServicesKit'; -import { hilog } from '@kit.PerformanceAnalysisKit'; import Constants from '../Constants'; +import Logger from './Logger'; const TAG: string = '[BreakpointSystem]'; @@ -114,7 +114,7 @@ export class BreakpointSystem { this.updateCurrentBreakpoint(widthBp); } catch (error) { const err: BusinessError = error as BusinessError; - hilog.error(0x0000, TAG, `UpdateBreakpoint fail, error code: ${err.code}, message: ${err.message}`); + Logger.error(TAG, `UpdateBreakpoint fail, error code: ${err.code}, message: ${err.message}`); } } } \ No newline at end of file diff --git a/verificationcodescenariolibrary/src/main/ets/common/utils/Logger.ets b/verificationcodescenariolibrary/src/main/ets/common/utils/Logger.ets new file mode 100644 index 0000000000000000000000000000000000000000..37225d0a24a31f710f51f2610f3da839a83409ec --- /dev/null +++ b/verificationcodescenariolibrary/src/main/ets/common/utils/Logger.ets @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the 'License'); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; + +class Logger { + private domain: number; + private prefix: string; + private format: string = '%{public}s, %{public}s'; + + public constructor(prefix: string) { + this.prefix = prefix; + this.domain = 0x0000; + } + + public debug(...args: Object[]): void { + hilog.debug(this.domain, this.prefix, this.format, args); + } + + public info(...args: Object[]): void { + hilog.info(this.domain, this.prefix, this.format, args); + } + + public warn(...args: Object[]): void { + hilog.warn(this.domain, this.prefix, this.format, args); + } + + public error(...args: Object[]): void { + hilog.error(this.domain, this.prefix, this.format, args); + } +} + +export default new Logger('[verificationCodeScenario]'); \ No newline at end of file diff --git a/verificationcodescenariolibrary/src/main/ets/common/utils/WindowUtil.ets b/verificationcodescenariolibrary/src/main/ets/common/utils/WindowUtil.ets index f31bcd599c6158634ad7da0e03b3e7720ccc30d3..ee820f1dd888a963461c424e601ea1dd3c88646a 100644 --- a/verificationcodescenariolibrary/src/main/ets/common/utils/WindowUtil.ets +++ b/verificationcodescenariolibrary/src/main/ets/common/utils/WindowUtil.ets @@ -15,9 +15,9 @@ import { window } from '@kit.ArkUI'; import type { BusinessError } from '@kit.BasicServicesKit'; -import { hilog } from '@kit.PerformanceAnalysisKit'; import Constants from '../Constants'; import { BreakpointSystem } from './BreakpointSystem'; +import Logger from './Logger'; const TAG: string = '[WindowUtil]'; @@ -34,7 +34,7 @@ export class WindowUtil { WindowUtil.registerBreakPoint(WindowUtil.windowClass); } catch (err) { const error = err as BusinessError; - hilog.error(0x0000, TAG, `Initialize failed. Cause code: ${error.code}, message: ${error.message}`); + Logger.error(TAG, `Initialize failed. Cause code: ${error.code}, message: ${error.message}`); } } @@ -43,14 +43,14 @@ export class WindowUtil { try { const promise: Promise = data.setWindowLayoutFullScreen(true); promise.then(() => { - hilog.info(0x0000, TAG, 'Succeeded in setting the window layout to full-screen mode.'); + Logger.info(TAG, 'Succeeded in setting the window layout to full-screen mode.'); }).catch((err: BusinessError) => { - hilog.error(0x0000, TAG, + Logger.error(TAG, `Failed to set the window layout to full-screen mode. Cause: ${err.code}, ${err.message}`); }); } catch (err) { const error = err as BusinessError; - hilog.error(0x0000, TAG, + Logger.error(TAG, `Failed to set the window layout to full-screen mode.code: ${error.code}, message: ${error.message} `); } } @@ -69,7 +69,7 @@ export class WindowUtil { }); } catch (err) { const error = err as BusinessError; - hilog.error(0x0000, TAG, + Logger.error(TAG, `Register avoidAreaChange or windowSizeChange failed. code: ${error.code}, message: ${error.message}`); } } diff --git a/verificationcodescenariolibrary/src/main/ets/view/BackgroundColorChange.ets b/verificationcodescenariolibrary/src/main/ets/view/BackgroundColorChange.ets index b9e6c25927f3520f3ac50379d7c623ccacf72a9a..6b1ceb1dd03f17a68307d21725441a2dc0f3a51b 100644 --- a/verificationcodescenariolibrary/src/main/ets/view/BackgroundColorChange.ets +++ b/verificationcodescenariolibrary/src/main/ets/view/BackgroundColorChange.ets @@ -16,9 +16,9 @@ import { window } from '@kit.ArkUI'; import { BusinessError } from '@kit.BasicServicesKit'; import { inputMethod } from '@kit.IMEKit'; -import { hilog } from '@kit.PerformanceAnalysisKit'; import Constants from '../common/Constants'; import { BreakpointType } from '../common/utils/BreakpointSystem'; +import Logger from '../common/utils/Logger'; @Extend(Text) function verifyCodeUnitStyle() { @@ -76,15 +76,13 @@ struct BackgroundColorChange { this.listen(); } catch (error) { const err: BusinessError = error as BusinessError; - hilog.error(0x0000, TAG, 'attach catch error, Cause: %{public}s', - `the code is ${err.code}, the message is ${err.message}`); + Logger.error(TAG, `Attach catch error, Cause: the code is ${err.code}, the message is ${err.message}`); } } }); } catch (error) { const err: BusinessError = error as BusinessError; - hilog.error(0x0000, TAG, 'attach catch error, Cause: %{public}s', - `the code is ${err.code}, the message is ${err.message}`); + Logger.error(TAG, `Attach catch error, Cause: the code is ${err.code}, the message is ${err.message}`); } } @@ -101,7 +99,7 @@ struct BackgroundColorChange { }); } catch (error) { const err: BusinessError = error as BusinessError; - hilog.error(0x0000, TAG, '%{public}s', `Register callback failed: ${err.code}, ${err.message}`); + Logger.error(TAG, `Register callback failed: ${err.code}, ${err.message}`); } } @@ -116,8 +114,7 @@ struct BackgroundColorChange { }); } catch (error) { const err: BusinessError = error as BusinessError; - hilog.error(0x0000, TAG, 'detach catch err: Cause: %{public}s', - `the code is ${err.code}, the message is ${err.message}`); + Logger.error(TAG, `Detach catch error, Cause: the code is ${err.code}, the message is ${err.message}.`); } } @@ -136,7 +133,7 @@ struct BackgroundColorChange { Text(this.codeText[item]) .verifyCodeUnitStyle() .backgroundColor(this.codeText[item] === undefined ? $r('sys.color.comp_background_secondary') : - $r('app.color.theme_color_m')) + $r('app.color.theme_color_m')) }, (item: number) => JSON.stringify(item)) } .onVisibleAreaChange(this.arrArea, async (isVisible: boolean, currentRatio: number) => { diff --git a/verificationcodescenariolibrary/src/main/ets/view/TextBoxShowCursor.ets b/verificationcodescenariolibrary/src/main/ets/view/TextBoxShowCursor.ets index fda25e2e0fe5e264b7c012088c050db3663ac8e4..df2cc3f4455f4ca3b979df9598a1841d0b7aae32 100644 --- a/verificationcodescenariolibrary/src/main/ets/view/TextBoxShowCursor.ets +++ b/verificationcodescenariolibrary/src/main/ets/view/TextBoxShowCursor.ets @@ -16,9 +16,9 @@ import { display, window } from '@kit.ArkUI'; import { BusinessError, deviceInfo } from '@kit.BasicServicesKit'; import { inputMethod } from '@kit.IMEKit'; -import { hilog } from '@kit.PerformanceAnalysisKit'; import Constants from '../common/Constants'; import { BreakpointType } from '../common/utils/BreakpointSystem'; +import Logger from '../common/utils/Logger'; @Extend(Text) function verifyCodeUnitStyle() { @@ -81,15 +81,13 @@ struct TextBoxShowCursor { this.listen(); } catch (error) { const err: BusinessError = error as BusinessError; - hilog.error(0x0000, TAG, 'attach catch error, Cause: %{public}s', - `the code is ${err.code}, the message is ${err.message}`); + Logger.error(TAG, `Attach catch error, Cause: the code is ${err.code}, the message is ${err.message}.`); } } }); } catch (error) { const err: BusinessError = error as BusinessError; - hilog.error(0x0000, TAG, 'attach catch error, Cause: %{public}s', - `the code is ${err.code}, the message is ${err.message}`); + Logger.error(TAG, `Attach catch error, Cause: the code is ${err.code}, the message is ${err.message}.`); } } @@ -116,8 +114,8 @@ struct TextBoxShowCursor { }) } catch (error) { const err: BusinessError = error as BusinessError; - hilog.error(0x0000, TAG, 'input controller on listen catch error, Cause: %{public}s', - `the code is ${err.code}, the message is ${err.message}`); + Logger.error(TAG, + `Input controller on listen catch error, Cause: the code is ${err.code}, the message is ${err.message}.`); } } @@ -135,8 +133,7 @@ struct TextBoxShowCursor { }) } catch (error) { const err: BusinessError = error as BusinessError; - hilog.error(0x0000, TAG, 'detach catch err: Cause: %{public}s', - `the code is ${err.code}, the message is ${err.message}`); + Logger.error(TAG, `Detach catch failed, Cause: the code is ${err.code}, the message is ${err.message}.`); } } @@ -147,7 +144,7 @@ struct TextBoxShowCursor { foldStatus = display.getFoldStatus(); } catch (error) { const err: BusinessError = error as BusinessError; - hilog.error(0x0000, TAG, '%{public}s', `Get foldStatus failed: ${err.code}, ${err.message}`); + Logger.error(TAG, `Get foldStatus failed: ${err.code}, ${err.message}.`); } if (deviceInfo.productSeries === 'VDE' && foldStatus === display.FoldStatus.FOLD_STATUS_EXPANDED) { return Constants.ITEM_CODE_START + this.codeText.length * Constants.ITEM_CODE_WIDTH; @@ -159,7 +156,7 @@ struct TextBoxShowCursor { return (this.codeText.length === Constants.ZERO ? Constants.ITEM_DIVIDER : this.codeText.length * (this.codeText.length > Constants.TWO ? (this.codeText.length === Constants.ITEM_DIVIDER ? Constants.FIFTY_EIGHT_POINT_FIVE : - Constants.FIFTY_NINE) : Constants.SIXTY)) + Constants.FIFTY_NINE) : Constants.SIXTY)) } private unicodeToDigit(unicodeValue: number): number { @@ -211,8 +208,7 @@ struct TextBoxShowCursor { this.inputController.attach(true, this.textConfig); } catch (error) { const err: BusinessError = error as BusinessError; - hilog.error(0x0000, TAG, 'attach catch error, Cause: %{public}s', - `the code is ${err.code}, the message is ${err.message}`); + Logger.error(TAG, `Attach catch error, Cause: the code is ${err.code}, the message is ${err.message}.`); } }) .width(this.curBp === 'sm' ? Constants.FULL_WIDTH : $r('app.float.context_area')) @@ -258,8 +254,7 @@ struct TextBoxShowCursor { this.inputController.attach(true, this.textConfig); } catch (error) { const err: BusinessError = error as BusinessError; - hilog.error(0x0000, TAG, 'attach catch error, Cause: %{public}s', - `the code is ${err.code}, the message is ${err.message}`); + Logger.error(TAG, `Attach catch error, Cause: the code is ${err.code}, the message is ${err.message}.`); } }) .justifyContent(FlexAlign.Center) diff --git a/verificationcodescenariosample/src/main/ets/entryability/EntryAbility.ets b/verificationcodescenariosample/src/main/ets/entryability/EntryAbility.ets index 792c3dcf2ed9299508fe94c3661410e12fd99105..198f9640034db19f547ffe01aebd076ad860c0e7 100644 --- a/verificationcodescenariosample/src/main/ets/entryability/EntryAbility.ets +++ b/verificationcodescenariosample/src/main/ets/entryability/EntryAbility.ets @@ -16,50 +16,48 @@ import { ConfigurationConstant, UIAbility } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; import { BusinessError } from '@kit.BasicServicesKit'; -import { hilog } from '@kit.PerformanceAnalysisKit'; -import { VerificationCodeController } from '@ohos_samples/verificationcodescenariolibrary'; +import { Logger, VerificationCodeController } from '@ohos_samples/verificationcodescenariolibrary'; const TAG: string = '[EntryAbility]'; export default class EntryAbility extends UIAbility { onCreate(): void { - hilog.info(0x0000, TAG, '%{public}s', 'Ability onCreate'); + Logger.info(TAG, 'Ability onCreate'); try { this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); } catch (error) { const err: BusinessError = error as BusinessError; - hilog.error(0x0000, TAG, '%{public}s', `Set colorMode failed: ${err.code}, ${err.message}`); + Logger.error(TAG, `Set colorMode failed: ${err.code}, ${err.message}`); } } onDestroy(): void { - hilog.info(0x0000, TAG, '%{public}s', 'Ability onDestroy'); + Logger.info(TAG, 'Ability onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage): void { windowStage.loadContent('pages/Index', (err, _data) => { if (err.code) { - hilog.error(0x0000, TAG, 'Failed to load the content. Cause: %{public}s', - `the code is ${err.code}, the message is ${err.message}`); + Logger.error(TAG, `Failed to load the content. Cause: the code is ${err.code}, the message is ${err.message}.`); return; } VerificationCodeController.initWindowConfig(windowStage); - hilog.info(0x0000, TAG, 'Succeed in loading the content.'); + Logger.info(TAG, 'Succeed in loading the content.'); }); } onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources - hilog.info(0x0000, TAG, '%{public}s', 'Ability onWindowStageDestroy'); + Logger.info(TAG, 'Ability onWindowStageDestroy'); } onForeground(): void { // Ability has brought to foreground - hilog.info(0x0000, TAG, '%{public}s', 'Ability onForeground'); + Logger.info(TAG, 'Ability onForeground'); } onBackground(): void { // Ability has back to background - hilog.info(0x0000, TAG, '%{public}s', 'Ability onBackground'); + Logger.info(TAG, 'Ability onBackground'); } }