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 #endif
25 #include "dfx_log_define.h"
26 #endif
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #ifndef DFX_NO_PRINT_LOG
33 
34 #ifdef DFX_LOG_HILOG_BASE
35 // replace the old interface, and delete the old interface after the replacement is complete
36 #ifdef DFX_LOG_UNWIND
37 #define DFXLOGD(fmt, ...) HILOG_BASE_DEBUG(LOG_CORE, fmt, ##__VA_ARGS__)
38 #else
39 #define DFXLOGD(fmt, ...)
40 #endif
41 #define DFXLOGI(fmt, ...) HILOG_BASE_INFO(LOG_CORE, fmt, ##__VA_ARGS__)
42 #define DFXLOGW(fmt, ...) HILOG_BASE_WARN(LOG_CORE, fmt, ##__VA_ARGS__)
43 #define DFXLOGE(fmt, ...) HILOG_BASE_ERROR(LOG_CORE, fmt, ##__VA_ARGS__)
44 #define DFXLOGF(fmt, ...) \
45     HILOG_BASE_FATAL(LOG_CORE, "[%{public}s:%{public}d] " fmt, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__)
46 
47 #ifdef DFX_LOG_UNWIND
48 #ifdef ALLOW_TO_STDERR
49 #define DFXLOGU(fmt, ...) \
50     HILOG_BASE_INFO(LOG_CORE, "[%{public}s:%{public}d] " fmt, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__); \
51     fprintf(stderr, "%s\n", fmt)
52 #else
53 #define DFXLOGU(fmt, ...) \
54     HILOG_BASE_INFO(LOG_CORE, "[%{public}s:%{public}d] " fmt, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__)
55 #endif
56 #else
57 #define DFXLOGU(fmt, ...)
58 #endif
59 
60 #else
61 // replace the old interface, and delete the old interface after the replacement is complete
62 #define DFXLOGD(fmt, ...) HILOG_DEBUG(LOG_CORE, fmt, ##__VA_ARGS__)
63 #define DFXLOGI(fmt, ...) HILOG_INFO(LOG_CORE, fmt, ##__VA_ARGS__)
64 #define DFXLOGW(fmt, ...) HILOG_WARN(LOG_CORE, fmt, ##__VA_ARGS__)
65 #define DFXLOGE(fmt, ...) HILOG_ERROR(LOG_CORE, fmt, ##__VA_ARGS__)
66 #define DFXLOGF(fmt, ...) \
67     HILOG_FATAL(LOG_CORE, "[%{public}s:%{public}d] " fmt, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__)
68 
69 #ifdef DFX_LOG_UNWIND
70 #define DFXLOGU(fmt, ...) \
71     HILOG_INFO(LOG_CORE, "[%{public}s:%{public}d] " fmt, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__)
72 #else
73 #define DFXLOGU(fmt, ...)
74 #endif
75 
76 #endif
77 
78 #else
79 
80 #define DFXLOGD(fmt, ...)
81 #define DFXLOGI(fmt, ...)
82 #define DFXLOGW(fmt, ...)
83 #define DFXLOGE(fmt, ...)
84 #define DFXLOGF(fmt, ...)
85 #define DFXLOGU(fmt, ...)
86 #endif
87 
88 #ifdef __cplusplus
89 }
90 #endif
91 #endif
92