• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 IOT_LOG_H
17 #define IOT_LOG_H
18 
19 #include <stdio.h>
20 
21 /*
22  * @brief:this defines for the log module, and
23  * IOT_LOG_TRACE/IOT_LOG_DEBUG will not participate the compile in the release version
24  * EN_IOT_LOG_LEVEL_TRACE:this is used as the trace function,like the function enter and function out
25  * EN_IOT_LOG_LEVEL_DEBUG:this is used as the debug, you could add any debug as you wish
26  * EN_IOT_LOG_LEVEL_INFO:which means it is import message, and you should known
27  * EN_IOT_LOG_LEVEL_WARN:this is used as the executed result,
28  * which means the status is not what we expected,but could accept
29  * EN_IOT_LOG_LEVEL_ERROR:this is used as the executed result,
30  * which means the status is not what we expected,could not accepta
31  * EN_IOT_LOG_LEVEL_FATAL:this is used as the parameters input for the api interface, which could not accepted
32  */
33 typedef enum {
34     EN_IOT_LOG_LEVEL_TRACE = 0,
35     EN_IOT_LOG_LEVEL_DEBUG,
36     EN_IOT_LOG_LEVEL_INFO,
37     EN_IOT_LOG_LEVEL_WARN,
38     EN_IOT_LOG_LEVEL_ERROR,
39     EN_IOT_LOG_LEVEL_FATAL,
40     EN_IOT_LOG_LEVEL_MAX,
41 }EnIotLogLevelT;
42 
43 /*
44  * @brief:use this function to get the current output log
45  * @return: the current output mask log, defined by en_iot_log_level_t
46  */
47 EnIotLogLevelT IoTLogLevelGet(void);
48 
49 /*
50  * @brief: use this function to get the debug level name
51  * @parameter[in]:level, the level to get
52  * @return: the mapped level name
53  */
54 const char *IoTLogLevelGetName(EnIotLogLevelT logLevel);
55 /*
56  * @brief:use this function to set the current output log
57  * @parameter[in] level:defined by en_iot_log_level_t
58  * @return: 0 success while -1 failed
59  */
60 int IoTLogLevelSet(EnIotLogLevelT level);
61 
62 /*
63  * @brief: this is a weak function ,and you could rewrite one
64  * @param fmt: same use as the fmt for printf
65  * @param unfixed: same use for printf
66  * @return: don't care about it
67  * @attention: and the components should not call this function directly, you'd better
68  * call IOT_LOG groups
69  */
70 
71 #ifndef IOT_PRINT
72 #define IOT_PRINT(fmt, ...) \
73     do \
74     { \
75         printf(fmt, ##__VA_ARGS__); \
76     } while (0)
77 #endif
78 
79 #ifdef CONFIG_LINKLOG_ENABLE
80 
81 #define IOT_LOG(level, fmt, ...) \
82     do \
83     { \
84         IOT_PRINT("[%s][%s] " fmt "\r\n", \
85         IoTLogLevelGetName((level)), __FUNCTION__, ##__VA_ARGS__); \
86     } while (0)
87 
88 #define IOT_LOG_TRACE(fmt, ...) \
89     do \
90     { \
91         if ((EN_IOT_LOG_LEVEL_TRACE) >= IoTLogLevelGet()) \
92         { \
93             IOT_LOG(EN_IOT_LOG_LEVEL_TRACE, fmt, ##__VA_ARGS__); \
94         } \
95     } while (0)
96 
97 #define IOT_LOG_DEBUG(fmt, ...) \
98     do \
99     { \
100         if ((EN_IOT_LOG_LEVEL_DEBUG) >= IoTLogLevelGet()) \
101         { \
102             IOT_LOG(EN_IOT_LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__); \
103         } \
104     } while (0)
105 
106 #else
107 
108 #define IOT_LOG(level, fmt, ...)
109 #define IOT_LOG_TRACE(fmt, ...)
110 #define IOT_LOG_DEBUG(fmt, ...)
111 
112 #endif
113 
114 #define IOT_LOG_TRACE(fmt, ...)   IOT_LOG(EN_IOT_LOG_LEVEL_TRACE, fmt, ##__VA_ARGS__)
115 #define IOT_LOG_INFO(fmt, ...)   IOT_LOG(EN_IOT_LOG_LEVEL_INFO, fmt, ##__VA_ARGS__)
116 #define IOT_LOG_WARN(fmt, ...)   IOT_LOG(EN_IOT_LOG_LEVEL_WARN, fmt, ##__VA_ARGS__)
117 #define IOT_LOG_ERROR(fmt, ...)  IOT_LOG(EN_IOT_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
118 #define IOT_LOG_FATAL(fmt, ...)  IOT_LOG(EN_IOT_LOG_LEVEL_FATAL, fmt, ##__VA_ARGS__)
119 
120 
121 #endif /* IOT_LOG_H_ */