Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef DLOG_PUB_H_
12 : #define DLOG_PUB_H_
13 :
14 : #include <string.h>
15 : #include <stdint.h>
16 : #include <stdarg.h>
17 : #include "log_types.h"
18 :
19 50006 : static inline const char *DlogGetFileName(const char *filePath) {
20 50006 : const char *result = filePath;
21 1095700 : for (size_t i = 0; filePath[i] != '\0'; i++) {
22 1045694 : if (filePath[i] == '/') {
23 0 : result = &filePath[i + 1U];
24 : }
25 : }
26 50006 : return result;
27 : }
28 :
29 : #define DLOG_FILE_NAME (DlogGetFileName(__FILE__))
30 : #define __FILENAME__ DLOG_FILE_NAME
31 :
32 : #ifdef __cplusplus
33 : #ifndef LOG_CPP
34 : extern "C" {
35 : #endif
36 : #endif // __cplusplus
37 :
38 : /**
39 : * @brief get module debug loglevel and enableEvent
40 : * @param [in] moduleId moudule id(see log_types.h, eg: CCE), others: invalid
41 : * @param [out] enableEvent 1: enable; 0: disable
42 : * @return module level 0: debug, 1: info, 2: warning, 3: error, 4: null output
43 : */
44 : LOG_FUNC_VISIBILITY int32_t dlog_getlevel(int32_t moduleId, int32_t *enableEvent);
45 :
46 : /**
47 : * @brief set module loglevel and enableEvent
48 : * @param [in] moduleId moudule id(see log_types.h, eg: CCE), -1: all modules, others: invalid
49 : * @param [in] level log level, eg: DLOG_ERROR/DLOG_WARN/DLOG_INFO/DLOG_DEBUG
50 : * @param [in] enableEvent 1: enable; 0: disable, others:invalid
51 : * @return 0: SUCCEED, others: FAILED
52 : */
53 : LOG_FUNC_VISIBILITY int32_t dlog_setlevel(int32_t moduleId, int32_t level, int32_t enableEvent);
54 :
55 : /**
56 : * @brief check module level enable or not
57 : * @param [in] moduleId module id, eg: CCE
58 : * @param [in] logLevel log level, eg: DLOG_ERROR/DLOG_WARN/DLOG_INFO/DLOG_DEBUG
59 : * @return 1:enable, 0:disable
60 : */
61 : LOG_FUNC_VISIBILITY int32_t CheckLogLevel(int32_t moduleId, int32_t logLevel);
62 :
63 : /**
64 : * @brief set log attr, default pid is 0, default device id is 0, default process type is APPLICATION
65 : * @param [in] logAttrInfo attr info, include pid(must be larger than 0), process type and device id(chip ID)
66 : * @return 0: SUCCEED, others: FAILED
67 : */
68 : LOG_FUNC_VISIBILITY int32_t DlogSetAttr(LogAttr logAttrInfo);
69 :
70 : /**
71 : * @brief print log, need va_list variable, exec CheckLogLevel() before call this function
72 : * @param[in] moduleId module id, eg: CCE
73 : * @param[in] level log level, eg: DLOG_ERROR/DLOG_WARN/DLOG_INFO/DLOG_DEBUG
74 : * @param[in] fmt log content
75 : * @param[in] list variable list of log content
76 : * @return NA
77 : */
78 : LOG_FUNC_VISIBILITY void DlogVaList(int32_t moduleId, int32_t level, const char *fmt, va_list list);
79 :
80 : /**
81 : * @brief flush log buffer to file
82 : * @return NA
83 : */
84 : LOG_FUNC_VISIBILITY void DlogFlush(void);
85 :
86 : /**
87 : * @brief print error log
88 : * @param [in] moduleId module id, eg: CCE
89 : * @param [in] fmt log content
90 : * @return NA
91 : */
92 : #define dlog_error(moduleId, fmt, ...) \
93 : do { \
94 : DlogRecord(moduleId, DLOG_ERROR, "[%s:%d]" fmt, DLOG_FILE_NAME, __LINE__, ##__VA_ARGS__); \
95 : } while (0)
96 :
97 : /**
98 : * @brief print warning log
99 : * call CheckLogLevel in advance to optimize performance, call interface with fmt input take time
100 : * @param [in] moduleId module id, eg: CCE
101 : * @param [in] fmt log content
102 : * @return NA
103 : */
104 : #define dlog_warn(moduleId, fmt, ...) \
105 : do { \
106 : if (CheckLogLevel(moduleId, DLOG_WARN) == 1) { \
107 : DlogRecord(moduleId, DLOG_WARN, "[%s:%d]" fmt, DLOG_FILE_NAME, __LINE__, ##__VA_ARGS__); \
108 : } \
109 : } while (0)
110 :
111 : /**
112 : * @brief print info log
113 : * call CheckLogLevel in advance to optimize performance, call interface with fmt input take time
114 : * @param [in] moduleId module id, eg: CCE
115 : * @param [in] fmt log content
116 : * @return NA
117 : */
118 : #define dlog_info(moduleId, fmt, ...) \
119 : do { \
120 : if (CheckLogLevel(moduleId, DLOG_INFO) == 1) { \
121 : DlogRecord(moduleId, DLOG_INFO, "[%s:%d]" fmt, DLOG_FILE_NAME, __LINE__, ##__VA_ARGS__); \
122 : } \
123 : } while (0)
124 :
125 : /**
126 : * @brief print debug log
127 : * call CheckLogLevel in advance to optimize performance, call interface with fmt input take time
128 : * @param [in] moduleId module id, eg: CCE
129 : * @param [in] fmt log content
130 : * @return NA
131 : */
132 : #define dlog_debug(moduleId, fmt, ...) \
133 : do { \
134 : if (CheckLogLevel(moduleId, DLOG_DEBUG) == 1) { \
135 : DlogRecord(moduleId, DLOG_DEBUG, "[%s:%d]" fmt, DLOG_FILE_NAME, __LINE__, ##__VA_ARGS__); \
136 : } \
137 : } while (0)
138 :
139 : /**
140 : * @brief print log, need caller to specify level
141 : * call CheckLogLevel in advance to optimize performance, call interface with fmt input take time
142 : * @param [in] moduleId module id, eg: CCE
143 : * @param [in] level log level, eg: DLOG_ERROR/DLOG_WARN/DLOG_INFO/DLOG_DEBUG
144 : * @param [in] fmt log content
145 : * @return NA
146 : */
147 : #define Dlog(moduleId, level, fmt, ...) \
148 : do { \
149 : if (CheckLogLevel(moduleId, level) == 1) { \
150 : DlogRecord(moduleId, level, "[%s:%d]" fmt, DLOG_FILE_NAME, __LINE__, ##__VA_ARGS__); \
151 : } \
152 : } while (0)
153 :
154 : /**
155 : * @brief print log, need caller to specify level and submodule
156 : * call CheckLogLevel in advance to optimize performance, call interface with fmt input take time
157 : * @param [in] moduleId module id, eg: CCE
158 : * @param [in] submodule eg: engine
159 : * @param [in] level log level, eg: DLOG_ERROR/DLOG_WARN/DLOG_INFO/DLOG_DEBUG
160 : * @param [in] fmt log content
161 : * @return NA
162 : */
163 : #define DlogSub(moduleId, submodule, level, fmt, ...) \
164 : do { \
165 : if (CheckLogLevel(moduleId, level) == 1) { \
166 : DlogRecord(moduleId, level, "[%s:%d][%s]" fmt, DLOG_FILE_NAME, __LINE__, submodule, ##__VA_ARGS__); \
167 : } \
168 : } while (0)
169 :
170 : /**
171 : * @brief record log
172 : * @param [in] moduleId module id, eg: SLOG
173 : * @param [in] level log level, eg: DLOG_ERROR/DLOG_WARN/DLOG_INFO/DLOG_DEBUG
174 : * @param [in] fmt log content
175 : * @return: NA
176 : */
177 : LOG_FUNC_VISIBILITY void DlogRecord(int32_t moduleId, int32_t level, const char *fmt, ...) __attribute((weak));
178 :
179 : #ifdef __cplusplus
180 : #ifndef LOG_CPP
181 : }
182 : #endif // LOG_CPP
183 : #endif // __cplusplus
184 :
185 : #endif // DLOG_PUB_H_
|