1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 17/** 18 * state mgmt library uses its own class for logging 19* allows to remap separately from other use of aceConsole 20* 21* everything in this file is framework internal 22*/ 23 24enum LogTag { 25 STATE_MGMT = 0 26} 27 28class stateMgmtConsole { 29 public static log(...args: any): void { 30 aceConsole.log(LogTag.STATE_MGMT, ...args); 31 } 32 public static debug(...args: any): void { 33 aceConsole.debug(LogTag.STATE_MGMT, ...args); 34 } 35 public static info(...args: any): void { 36 aceConsole.info(LogTag.STATE_MGMT, ...args); 37 } 38 public static warn(...args: any): void { 39 aceConsole.warn(LogTag.STATE_MGMT, ...args); 40 } 41 public static error(...args: any): void { 42 aceConsole.error(LogTag.STATE_MGMT, ...args); 43 } 44 public static propertyAccess(...args: any): void { 45 // enable for fine grain debugging variable observation 46 // aceConsole.error(...args) 47 } 48 public static applicationError(...args: any): void { 49 aceConsole.error(LogTag.STATE_MGMT, `FIX THIS APPLICATION ERROR:`, ...args); 50 } 51 52 public static applicationWarn(...args: any): void { 53 aceConsole.warn(LogTag.STATE_MGMT, ...args); 54 } 55 public static featureCombinationError(msg: string): void { 56 aceConsole.warn(LogTag.STATE_MGMT, msg); 57 } 58} 59 60type TraceArgs = string | number | boolean; 61 62class stateMgmtTrace { 63 public static scopedTrace<T>(codeBlock: () => T, arg1: string, ...args: TraceArgs[]): T { 64 aceTrace.begin(arg1, ...args); 65 let result: T = codeBlock(); 66 aceTrace.end(); 67 return result; 68 } 69} 70 71class errorReport { 72 public static varValueCheckFailed<T>(params: { customComponent: string, variableDeco: string, variableName: string, expectedType: string, value: T }): void { 73 let msg = `@Component '${params.customComponent}': Illegal variable value error with decorated variable ${params.variableDeco} '${params.variableName}': `; 74 msg += `failed validation: '${params.expectedType}`; 75 try { 76 msg += `, attempt to assign value type: '${typeof params.value}'`; 77 msg += `, value: '${JSON.stringify(params.value, null, 4)}'`; 78 } catch (e) { } 79 80 msg += '!'; 81 stateMgmtConsole.applicationError(msg); 82 throw new TypeError(msg); 83 } 84 85 public static varObservationFailed<T>(params: { customComponent: string, variableDeco: string, variableName: string, value: T }): void { 86 let msg = `@Component '${params.customComponent}': decorated variable ${params.variableDeco} '${params.variableName}': `; 87 msg += `its class is neither decorated with '@Observed' nor it is an instance of 'SubscribableAbstract'`; 88 89 try { 90 msg += `, attempt to assign value type: '${typeof params.value}'`; 91 msg += `, value: '${JSON.stringify(params.value, null, 4)}'`; 92 } catch (e) { } 93 94 msg += '!'; 95 96 throw new TypeError(msg); 97 } 98}