1 /*
2  * Copyright (c) 2022 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 ARKCOMPILER_TOOLCHAIN_COMMON_LOG_WRAPPER_H
17 #define ARKCOMPILER_TOOLCHAIN_COMMON_LOG_WRAPPER_H
18 
19 #include "common/macros.h"
20 
21 #if defined(ENABLE_HILOG)
22 #if defined(__clang__)
23 #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
24 #elif defined(__GNUC__)
25 #pragma GCC diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
26 #endif
27 #include "hilog/log.h"
28 #endif
29 #undef LOG_DOMAIN
30 #define LOG_DOMAIN 0xD003F00
31 #undef LOG_TAG
32 #define LOG_TAG "ArkCompiler"
33 
34 namespace OHOS::ArkCompiler::Toolchain {
35 #ifdef LOGF
36 #undef LOGF
37 #endif
38 #ifdef LOGE
39 #undef LOGE
40 #endif
41 #ifdef LOGW
42 #undef LOGW
43 #endif
44 #ifdef LOGI
45 #undef LOGI
46 #endif
47 #ifdef LOGD
48 #undef LOGD
49 #endif
50 
51 #if !defined(ENABLE_HILOG)
52 enum class LogLevel {
53     UNKNOWN,
54     DEFAULT,
55     VERBOSE,
56     DEBUG,
57     INFO,
58     WARN,
59     ERROR,
60     FATAL
61 };
62 class TOOLCHAIN_EXPORT StdLog {
63 public:
64     StdLog() = default;
65     ~StdLog() = default;
66 
67     static void PrintLog(LogLevel level, const char* fmt, ...);
68 };
69 #endif
70 
71 #pragma clang diagnostic push
72 #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
73 #if !defined(ENABLE_HILOG)
74 #define LOGF(fmt, ...) StdLog::PrintLog(LogLevel::FATAL, fmt, ##__VA_ARGS__)
75 #define LOGE(fmt, ...) StdLog::PrintLog(LogLevel::ERROR, fmt, ##__VA_ARGS__)
76 #define LOGW(fmt, ...) StdLog::PrintLog(LogLevel::WARN, fmt, ##__VA_ARGS__)
77 #define LOGI(fmt, ...) StdLog::PrintLog(LogLevel::INFO, fmt, ##__VA_ARGS__)
78 #define LOGD(fmt, ...) StdLog::PrintLog(LogLevel::DEBUG, fmt, ##__VA_ARGS__)
79 #else
80 #define LOGF(fmt, ...) HILOG_FATAL(LOG_CORE, fmt, ##__VA_ARGS__)
81 #define LOGE(fmt, ...) HILOG_ERROR(LOG_CORE, fmt, ##__VA_ARGS__)
82 #define LOGW(fmt, ...) HILOG_WARN(LOG_CORE, fmt, ##__VA_ARGS__)
83 #define LOGI(fmt, ...) HILOG_INFO(LOG_CORE, fmt, ##__VA_ARGS__)
84 #define LOGD(fmt, ...) HILOG_DEBUG(LOG_CORE, fmt, ##__VA_ARGS__)
85 #endif
86 #pragma clang diagnostic pop
87 } // namespace OHOS::ArkCompiler::Toolchain
88 #endif // ARKCOMPILER_TOOLCHAIN_COMMON_LOG_WRAPPER_H
89