• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef DFX_LOG_H
17 #define DFX_LOG_H
18 
19 #ifndef DFX_NO_PRINT_LOG
20 #ifdef DFX_LOG_HILOG_BASE
21 #include <hilog_base/log_base.h>
22 #else
23 #include <hilog/log.h>
24 #include "dfx_log_public.h"
25 #endif
26 #include "dfx_log_define.h"
27 #endif
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #ifndef DFX_NO_PRINT_LOG
34 
35 bool CheckDebugLevel(void);
36 void InitDebugFd(int fd);
37 void SetLogLevel(const LogLevel logLevel);
38 LogLevel GetLogLevel(void);
39 
40 #if defined(DFX_LOG_HILOG_BASE) || defined(is_ohos_lite)
41 int DfxLogPrint(const LogLevel logLevel, const unsigned int domain, const char* tag, \
42                 const char *fmt, ...) __attribute__((format(printf, 4, 5)));
43 int DfxLogPrintV(const LogLevel logLevel, const unsigned int domain, const char* tag, const char *fmt, va_list ap);
44 
45 #define DFXLOG_PRINT(prio, ...) DfxLogPrint(prio, LOG_DOMAIN, LOG_TAG, ##__VA_ARGS__)
46 
47 #define LOGD(fmt, ...) DFXLOG_PRINT(LOG_DEBUG, "[%s:%d] " fmt, (FILENAME_), (__LINE__), ##__VA_ARGS__)
48 #define LOGI(fmt, ...) DFXLOG_PRINT(LOG_INFO, "[%s:%d] " fmt, (FILENAME_), (__LINE__), ##__VA_ARGS__)
49 #define LOGW(fmt, ...) DFXLOG_PRINT(LOG_WARN, "[%s:%d] " fmt, (FILENAME_), (__LINE__), ##__VA_ARGS__)
50 #define LOGE(fmt, ...) DFXLOG_PRINT(LOG_ERROR, "[%s:%d] " fmt, (FILENAME_), (__LINE__), ##__VA_ARGS__)
51 #define LOGF(fmt, ...) DFXLOG_PRINT(LOG_FATAL, "[%s:%d] " fmt, (FILENAME_), (__LINE__), ##__VA_ARGS__)
52 
53 #ifdef DFX_LOG_UNWIND
54 #define LOGU(fmt, ...) DFXLOG_PRINT(LOG_INFO, "[%s:%d] " fmt, (FILENAME_), (__LINE__), ##__VA_ARGS__)
55 #else
56 #define LOGU(fmt, ...)
57 #endif
58 
59 #define DFXLOG_DEBUG(fmt, ...) DFXLOG_PRINT(LOG_DEBUG, fmt, ##__VA_ARGS__)
60 #define DFXLOG_INFO(fmt, ...) DFXLOG_PRINT(LOG_INFO, fmt, ##__VA_ARGS__)
61 #define DFXLOG_WARN(fmt, ...) DFXLOG_PRINT(LOG_WARN, fmt, ##__VA_ARGS__)
62 #define DFXLOG_ERROR(fmt, ...) DFXLOG_PRINT(LOG_ERROR, fmt, ##__VA_ARGS__)
63 #define DFXLOG_FATAL(fmt, ...) DFXLOG_PRINT(LOG_FATAL, fmt, ##__VA_ARGS__)
64 #else
65 
66 #ifdef HILOG_FMTID
67 #define DFXLOG_STD_ARRAY(level, fmt, ...) \
68 do { \
69     constexpr HILOG_FMT_IN_SECTION static auto hilogFmt = fmt ## _DfxToPublic; \
70     FmtId fmtid { HILOG_UUID, HILOG_FMT_OFFSET(hilogFmt.data()) }; \
71     HiLogPrintDict(LOG_CORE, level, LOG_DOMAIN, LOG_TAG, &fmtid, hilogFmt.data(), ##__VA_ARGS__); \
72 } while (0)
73 #define DFXLOG_STD_ARRAY_FILE(level, fmt, ...) \
74 do { \
75     constexpr HILOG_FMT_IN_SECTION static auto hilogFmt = OHOS::HiviewDFX::ConcatStr("[%{public}s:%{public}d] ", \
76         fmt ## _DfxToPublic); \
77     FmtId fmtid { HILOG_UUID, HILOG_FMT_OFFSET(hilogFmt.data()) }; \
78     HiLogPrintDict(LOG_CORE, level, LOG_DOMAIN, LOG_TAG, \
79         &fmtid, hilogFmt.data(), (FILENAME_), (__LINE__), ##__VA_ARGS__); \
80 } while (0)
81 #else
82 #define DFXLOG_STD_ARRAY(level, fmt, ...) \
83 do { \
84     HiLogPrint(LOG_CORE, level, LOG_DOMAIN, LOG_TAG, fmt ## _DfxToPublic.data(), ##__VA_ARGS__); \
85 } while (0)
86 #define DFXLOG_STD_ARRAY_FILE(level, fmt, ...) \
87 do { \
88     constexpr auto hilogFmt = OHOS::HiviewDFX::ConcatStr("[%{public}s:%{public}d] ", fmt ## _DfxToPublic); \
89     HiLogPrint(LOG_CORE, level, LOG_DOMAIN, LOG_TAG, hilogFmt.data(), (FILENAME_), (__LINE__), ##__VA_ARGS__); \
90 } while (0)
91 #endif
92 
93 #define LOGD(fmt, ...) DFXLOG_STD_ARRAY_FILE(LOG_DEBUG, fmt, ##__VA_ARGS__)
94 #define LOGI(fmt, ...) DFXLOG_STD_ARRAY_FILE(LOG_INFO, fmt, ##__VA_ARGS__)
95 #define LOGW(fmt, ...) DFXLOG_STD_ARRAY_FILE(LOG_WARN, fmt, ##__VA_ARGS__)
96 #define LOGE(fmt, ...) DFXLOG_STD_ARRAY_FILE(LOG_ERROR, fmt, ##__VA_ARGS__)
97 #define LOGF(fmt, ...) DFXLOG_STD_ARRAY_FILE(LOG_FATAL, fmt, ##__VA_ARGS__)
98 
99 #ifdef DFX_LOG_UNWIND
100 #define LOGU(fmt, ...) DFXLOG_STD_ARRAY_FILE(LOG_INFO, fmt, ##__VA_ARGS__)
101 #else
102 #define LOGU(fmt, ...)
103 #endif
104 
105 #define DFXLOG_DEBUG(fmt, ...) DFXLOG_STD_ARRAY(LOG_DEBUG, fmt, ##__VA_ARGS__)
106 #define DFXLOG_INFO(fmt, ...) DFXLOG_STD_ARRAY(LOG_INFO, fmt, ##__VA_ARGS__)
107 #define DFXLOG_WARN(fmt, ...) DFXLOG_STD_ARRAY(LOG_WARN, fmt, ##__VA_ARGS__)
108 #define DFXLOG_ERROR(fmt, ...) DFXLOG_STD_ARRAY(LOG_ERROR, fmt, ##__VA_ARGS__)
109 #define DFXLOG_FATAL(fmt, ...) DFXLOG_STD_ARRAY(LOG_FATAL, fmt, ##__VA_ARGS__)
110 #endif
111 
112 #else
113 #define DFXLOG_PRINT(prio, ...)
114 
115 #define DFXLOG_DEBUG(fmt, ...)
116 #define DFXLOG_INFO(fmt, ...)
117 #define DFXLOG_WARN(fmt, ...)
118 #define DFXLOG_ERROR(fmt, ...)
119 #define DFXLOG_FATAL(fmt, ...)
120 
121 #define LOGD(fmt, ...)
122 #define LOGI(fmt, ...)
123 #define LOGW(fmt, ...)
124 #define LOGE(fmt, ...)
125 #define LOGF(fmt, ...)
126 #define LOGU(fmt, ...)
127 #endif
128 
129 #ifdef __cplusplus
130 }
131 #endif
132 #endif
133