14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ciclass NapiLog { 174514f5e3Sopenharmony_ci constructor() {} 184514f5e3Sopenharmony_ci} 194514f5e3Sopenharmony_ciNapiLog.LEV_NONE = 0; 204514f5e3Sopenharmony_ciNapiLog.LEV_ERROR = 1; 214514f5e3Sopenharmony_ciNapiLog.LEV_DEBUG = 2; 224514f5e3Sopenharmony_ciNapiLog.LEV_INFO = 3; 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_ciconst LEV_STR = ['[NON]', '[ERR]', '[DBG]', '[INF]']; 254514f5e3Sopenharmony_cilet logLevel = NapiLog.LEV_INFO; 264514f5e3Sopenharmony_cilet logFileName = null; 274514f5e3Sopenharmony_cilet logResultMessage = [true, '']; 284514f5e3Sopenharmony_cilet errorCallBack = null; 294514f5e3Sopenharmony_ci 304514f5e3Sopenharmony_cifunction getDateString() { 314514f5e3Sopenharmony_ci let nowDate = new Date(); 324514f5e3Sopenharmony_ci return nowDate.toLocaleString(); 334514f5e3Sopenharmony_ci} 344514f5e3Sopenharmony_ci 354514f5e3Sopenharmony_cifunction saveLog(dateStr, levStr, detail) { 364514f5e3Sopenharmony_ci if (logFileName) { 374514f5e3Sopenharmony_ci let logStr = dateStr + ' ' + levStr + ' ' + detail + '\n'; 384514f5e3Sopenharmony_ci } 394514f5e3Sopenharmony_ci} 404514f5e3Sopenharmony_ci 414514f5e3Sopenharmony_ciNapiLog.init = function (level, fileName) { 424514f5e3Sopenharmony_ci logLevel = 434514f5e3Sopenharmony_ci level in 444514f5e3Sopenharmony_ci [NapiLog.LEV_NONE, NapiLog.LEV_ERROR, NapiLog.LEV_DEBUG, NapiLog.LEV_INFO] 454514f5e3Sopenharmony_ci ? level 464514f5e3Sopenharmony_ci : NapiLog.LEV_ERROR; 474514f5e3Sopenharmony_ci logFileName = fileName ? fileName : 'napi_generator.log'; 484514f5e3Sopenharmony_ci}; 494514f5e3Sopenharmony_ci 504514f5e3Sopenharmony_cifunction recordLog(lev, ...args) { 514514f5e3Sopenharmony_ci let dataStr = getDateString(); 524514f5e3Sopenharmony_ci let detail = args.join(' '); 534514f5e3Sopenharmony_ci saveLog(dataStr, LEV_STR[lev], detail); 544514f5e3Sopenharmony_ci if (lev == NapiLog.LEV_ERROR) { 554514f5e3Sopenharmony_ci logResultMessage = [false, detail]; 564514f5e3Sopenharmony_ci if (errorCallBack != null) errorCallBack(detail); 574514f5e3Sopenharmony_ci } 584514f5e3Sopenharmony_ci if (logLevel < lev) return; 594514f5e3Sopenharmony_ci console.log(dataStr, LEV_STR[lev], detail); 604514f5e3Sopenharmony_ci} 614514f5e3Sopenharmony_ci 624514f5e3Sopenharmony_ciNapiLog.logError = function (...args) { 634514f5e3Sopenharmony_ci recordLog(NapiLog.LEV_ERROR, args); 644514f5e3Sopenharmony_ci}; 654514f5e3Sopenharmony_ci 664514f5e3Sopenharmony_ciNapiLog.logDebug = function (...args) { 674514f5e3Sopenharmony_ci recordLog(NapiLog.LEV_DEBUG, args); 684514f5e3Sopenharmony_ci}; 694514f5e3Sopenharmony_ci 704514f5e3Sopenharmony_ciNapiLog.logInfo = function (...args) { 714514f5e3Sopenharmony_ci recordLog(NapiLog.LEV_INFO, args); 724514f5e3Sopenharmony_ci}; 734514f5e3Sopenharmony_ci 744514f5e3Sopenharmony_ciNapiLog.getResult = function () { 754514f5e3Sopenharmony_ci return logResultMessage; 764514f5e3Sopenharmony_ci}; 774514f5e3Sopenharmony_ci 784514f5e3Sopenharmony_ciNapiLog.clearError = function () { 794514f5e3Sopenharmony_ci logResultMessage = [true, '']; 804514f5e3Sopenharmony_ci}; 814514f5e3Sopenharmony_ci 824514f5e3Sopenharmony_ciNapiLog.registError = function (func) { 834514f5e3Sopenharmony_ci errorCallBack = func; 844514f5e3Sopenharmony_ci}; 854514f5e3Sopenharmony_ci 864514f5e3Sopenharmony_cimodule.exports = { 874514f5e3Sopenharmony_ci NapiLog, 884514f5e3Sopenharmony_ci}; 89