• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef DFX_LOG_H
16 #define DFX_LOG_H
17 
18 #ifndef DFX_NO_PRINT_LOG
19 #ifdef DFX_LOG_HILOG_BASE
20 #include <hilog_base/log_base.h>
21 #else
22 #include <hilog/log.h>
23 #endif
24 #include "dfx_log_define.h"
25 #endif
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #ifndef DFX_NO_PRINT_LOG
32 
33 bool CheckDebugLevel(void);
34 void InitDebugFd(int fd);
35 void SetLogLevel(const LogLevel logLevel);
36 LogLevel GetLogLevel(void);
37 
38 int DfxLogPrint(const LogLevel logLevel, const unsigned int domain, const char* tag,
39                 const char *fmt, ...) __attribute__((format(printf, 4, 5)));
40 int DfxLogPrintV(const LogLevel logLevel, const unsigned int domain, const char* tag, const char *fmt, va_list ap);
41 
42 #define DFXLOG_PRINT(prio, domain, tag, ...) DfxLogPrint(prio, domain, tag, ##__VA_ARGS__)
43 #define DFXLOG_PRINTV(prio, domain, tag, fmt, args) DfxLogPrintV(prio, domain, tag, fmt, args)
44 
45 #define DFXLOG_DEBUG(...) DFXLOG_PRINT(LOG_DEBUG, LOG_DOMAIN, LOG_TAG, ##__VA_ARGS__)
46 #define DFXLOG_INFO(...) DFXLOG_PRINT(LOG_INFO, LOG_DOMAIN, LOG_TAG, ##__VA_ARGS__)
47 #define DFXLOG_WARN(...) DFXLOG_PRINT(LOG_WARN, LOG_DOMAIN, LOG_TAG, ##__VA_ARGS__)
48 #define DFXLOG_ERROR(...) DFXLOG_PRINT(LOG_ERROR, LOG_DOMAIN, LOG_TAG, ##__VA_ARGS__)
49 #define DFXLOG_FATAL(...) DFXLOG_PRINT(LOG_FATAL, LOG_DOMAIN, LOG_TAG, ##__VA_ARGS__)
50 
51 #define LOGD(fmt, ...) \
52     DFXLOG_PRINT(LOG_DEBUG, LOG_DOMAIN, LOG_TAG, "[%s:%d]" fmt, (FILENAME_), (__LINE__), ##__VA_ARGS__)
53 #define LOGI(fmt, ...) \
54     DFXLOG_PRINT(LOG_INFO, LOG_DOMAIN, LOG_TAG, "[%s:%d]" fmt, (FILENAME_), (__LINE__), ##__VA_ARGS__)
55 #define LOGW(fmt, ...) \
56     DFXLOG_PRINT(LOG_WARN, LOG_DOMAIN, LOG_TAG, "[%s:%d]" fmt, (FILENAME_), (__LINE__), ##__VA_ARGS__)
57 #define LOGE(fmt, ...) \
58     DFXLOG_PRINT(LOG_ERROR, LOG_DOMAIN, LOG_TAG, "[%s:%d]" fmt, (FILENAME_), (__LINE__), ##__VA_ARGS__)
59 #define LOGF(fmt, ...) \
60     DFXLOG_PRINT(LOG_FATAL, LOG_DOMAIN, LOG_TAG, "[%s:%d]" fmt, (FILENAME_), (__LINE__), ##__VA_ARGS__)
61 
62 #else
63 #define DFXLOG_PRINT(prio, domain, tag, ...)
64 #define DFXLOG_PRINTV(prio, domain, tag, fmt, args)
65 
66 #define DFXLOG_DEBUG(...)
67 #define DFXLOG_INFO(...)
68 #define DFXLOG_WARN(...)
69 #define DFXLOG_ERROR(...)
70 #define DFXLOG_FATAL(...)
71 
72 #define LOGD(fmt, ...)
73 #define LOGI(fmt, ...)
74 #define LOGW(fmt, ...)
75 #define LOGE(fmt, ...)
76 #define LOGF(fmt, ...)
77 #endif
78 
79 #ifndef LOG_CHECK_MSG
80 #define LOG_CHECK_MSG(condition, fmt,  ...) \
81     if (__builtin_expect(!(condition), false)) { \
82         DFXLOG_PRINT(LOG_ERROR, LOG_DOMAIN, LOG_TAG, " check failed: %s" fmt, #condition, ##__VA_ARGS__); \
83     }
84 #endif
85 
86 #ifndef LOG_CHECK
87 #define LOG_CHECK(condition) LOG_CHECK_MSG(condition, "")
88 #endif
89 
90 #ifndef LOG_CHECK_ABORT
91 #define LOG_CHECK_ABORT(condition) \
92     if (__builtin_expect(!(condition), false)) { \
93         LOGF(" check abort: %s", #condition); \
94         abort(); \
95     }
96 #endif
97 
98 #ifdef DFX_LOG_UNWIND
99 #define LOGU(fmt, ...) \
100     DFXLOG_PRINT(LOG_INFO, LOG_DOMAIN, LOG_TAG, "[%s:%d]" fmt, (FILENAME_), (__LINE__), ##__VA_ARGS__)
101 #else
102 #define LOGU(fmt, ...)
103 #endif
104 
105 #ifdef __cplusplus
106 }
107 #endif
108 #endif
109