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 #ifndef OHOS_ABILITY_JS_ENVIRONMENT_JS_ENV_LOGGER_H
17 #define OHOS_ABILITY_JS_ENVIRONMENT_JS_ENV_LOGGER_H
18 
19 namespace OHOS {
20 namespace JsEnv {
21 enum class JsEnvLogLevel {
22     DEBUG = 0,
23     INFO,
24     WARN,
25     ERROR,
26     FATAL
27 };
28 
29 struct JsEnvLogger final {
30     static void(*logger)(JsEnvLogLevel level, const char* fileName, const char* functionName, int line,
31         const char* fmt, ...);
32 };
33 
34 #define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
35 
36 #define JSENV_LOG_D(fmt, ...) \
37     JsEnvLogger::logger(JsEnvLogLevel::DEBUG, __FILENAME__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
38 #define JSENV_LOG_I(fmt, ...) \
39     JsEnvLogger::logger(JsEnvLogLevel::INFO, __FILENAME__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
40 #define JSENV_LOG_W(fmt, ...) \
41     JsEnvLogger::logger(JsEnvLogLevel::WARN, __FILENAME__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
42 #define JSENV_LOG_E(fmt, ...) \
43     JsEnvLogger::logger(JsEnvLogLevel::ERROR, __FILENAME__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
44 #define JSENV_LOG_F(fmt, ...) \
45     JsEnvLogger::logger(JsEnvLogLevel::FATAL, __FILENAME__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
46 } // namespace JsEnv
47 } // namespace OHOS
48 #endif // OHOS_ABILITY_JS_ENVIRONMENT_JS_ENV_LOGGER_H