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 namespace OHOS {
31 namespace AbilityRuntime {
JsEnvLogger(JsEnv::JsEnvLogLevel level, const char* fileName, const char* functionName, int line, const char* fmt, ...)32 void JsEnvLogger(JsEnv::JsEnvLogLevel level, const char* fileName, const char* functionName, int line,
33     const char* fmt, ...)
34 {
35     std::string cFormat = "[%{public}s(%{public}s:%{public}d)]";
36     cFormat += fmt;
37     va_list printArgs;
38     va_start(printArgs, fmt);
39     switch (level) {
40         case JsEnv::JsEnvLogLevel::DEBUG:
41             HILOG_IMPL(LOG_CORE, LOG_DEBUG, ENV_LOG_DOMAIN, ENV_LOG_TAG,
42                 cFormat.c_str(), fileName, functionName, line, printArgs);
43             break;
44         case JsEnv::JsEnvLogLevel::INFO:
45             HILOG_IMPL(LOG_CORE, LOG_INFO, ENV_LOG_DOMAIN, ENV_LOG_TAG,
46                 cFormat.c_str(), fileName, functionName, line, printArgs);
47             break;
48         case JsEnv::JsEnvLogLevel::WARN:
49             HILOG_IMPL(LOG_CORE, LOG_WARN, ENV_LOG_DOMAIN, ENV_LOG_TAG,
50                 cFormat.c_str(), fileName, functionName, line, printArgs);
51             break;
52         case JsEnv::JsEnvLogLevel::ERROR:
53             HILOG_IMPL(LOG_CORE, LOG_ERROR, ENV_LOG_DOMAIN, ENV_LOG_TAG,
54                 cFormat.c_str(), fileName, functionName, line, printArgs);
55             break;
56         case JsEnv::JsEnvLogLevel::FATAL:
57             HILOG_IMPL(LOG_CORE, LOG_FATAL, ENV_LOG_DOMAIN, ENV_LOG_TAG,
58                 cFormat.c_str(), fileName, functionName, line, printArgs);
59             break;
60         default:
61             HILOG_IMPL(LOG_CORE, LOG_INFO, ENV_LOG_DOMAIN, ENV_LOG_TAG,
62                 cFormat.c_str(), fileName, functionName, line, printArgs);
63             break;
64     }
65     va_end(printArgs);
66 }
67 
RegisterJsEnvLogger()68 void OHOSJsEnvLogger::RegisterJsEnvLogger()
69 {
70     JsEnv::JsEnvLogger::logger = JsEnvLogger;
71 }
72 } // namespace AbilityRuntime
73 } // namespace OHOS
74