1 /*
2  * Copyright (c) 2023-2024 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 #include "ohos_js_env_logger.h"
17 
18 #include <string>
19 #include "hilog/log.h"
20 #include "js_env_logger.h"
21 
22 #ifndef ENV_LOG_DOMAIN
23 #define ENV_LOG_DOMAIN 0xD001320
24 #endif
25 
26 #ifndef ENV_LOG_TAG
27 #define ENV_LOG_TAG "AAFwkJsEnv"
28 #endif
29 
30 #ifdef LOG_LABEL
31 #undef LOG_LABEL
32 #endif
33 
34 namespace OHOS {
35 namespace AbilityRuntime {
36 static constexpr HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, ENV_LOG_DOMAIN, ENV_LOG_TAG};
JsEnvLogger(JsEnv::JsEnvLogLevel level,const char * fileName,const char * functionName,int line,const char * fmt,...)37 void JsEnvLogger(JsEnv::JsEnvLogLevel level, const char* fileName, const char* functionName, int line,
38     const char* fmt, ...)
39 {
40     std::string cFormat = "[%{public}s(%{public}s:%{public}d)]";
41     cFormat += fmt;
42     va_list printArgs;
43     va_start(printArgs, fmt);
44     switch (level) {
45         case JsEnv::JsEnvLogLevel::DEBUG:
46             HiviewDFX::HiLog::Debug(LOG_LABEL, cFormat.c_str(), fileName, functionName, line, printArgs);
47             break;
48         case JsEnv::JsEnvLogLevel::INFO:
49             HiviewDFX::HiLog::Info(LOG_LABEL, cFormat.c_str(), fileName, functionName, line, printArgs);
50             break;
51         case JsEnv::JsEnvLogLevel::WARN:
52             HiviewDFX::HiLog::Warn(LOG_LABEL, cFormat.c_str(), fileName, functionName, line, printArgs);
53             break;
54         case JsEnv::JsEnvLogLevel::ERROR:
55             HiviewDFX::HiLog::Error(LOG_LABEL, cFormat.c_str(), fileName, functionName, line, printArgs);
56             break;
57         case JsEnv::JsEnvLogLevel::FATAL:
58             HiviewDFX::HiLog::Fatal(LOG_LABEL, cFormat.c_str(), fileName, functionName, line, printArgs);
59             break;
60         default:
61             HiviewDFX::HiLog::Info(LOG_LABEL, cFormat.c_str(), fileName, functionName, line, printArgs);
62             break;
63     }
64     va_end(printArgs);
65 }
66 
RegisterJsEnvLogger()67 void OHOSJsEnvLogger::RegisterJsEnvLogger()
68 {
69     JsEnv::JsEnvLogger::logger = JsEnvLogger;
70 }
71 } // namespace AbilityRuntime
72 } // namespace OHOS
73