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 /* 57 * @brief:use this function to set the current output log 58 * @parameter[in] level:defined by en_iot_log_level_t 59 * @return: 0 success while -1 failed 60 */ 61 int IoTLogLevelSet(EnIotLogLevelT level); 62 63 /* 64 * @brief: this is a weak function ,and you could rewrite one 65 * @param fmt: same use as the fmt for printf 66 * @param unfixed: same use for printf 67 * @return: don't care about it 68 * @attention: and the components should not call this function directly, you'd better 69 * call IOT_LOG groups 70 */ 71 72 #ifndef IOT_PRINT 73 #define IOT_PRINT(fmt, ...) \ 74 do \ 75 { \ 76 printf(fmt, ##__VA_ARGS__); \ 77 } while (0) 78 #endif 79 80 #ifdef CONFIG_LINKLOG_ENABLE 81 82 #define IOT_LOG(level, fmt, ...) \ 83 do \ 84 { \ 85 IOT_PRINT("[%s][%s] " fmt "\r\n", \ 86 IoTLogLevelGetName((level)), __FUNCTION__, ##__VA_ARGS__); \ 87 } while (0) 88 89 #define IOT_LOG_TRACE(fmt, ...) \ 90 do \ 91 { \ 92 if ((EN_IOT_LOG_LEVEL_TRACE) >= IoTLogLevelGet()) \ 93 { \ 94 IOT_LOG(EN_IOT_LOG_LEVEL_TRACE, fmt, ##__VA_ARGS__); \ 95 } \ 96 } while (0) 97 98 #define IOT_LOG_DEBUG(fmt, ...) \ 99 do \ 100 { \ 101 if ((EN_IOT_LOG_LEVEL_DEBUG) >= IoTLogLevelGet()) \ 102 { \ 103 IOT_LOG(EN_IOT_LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__); \ 104 } \ 105 } while (0) 106 107 #else 108 109 #define IOT_LOG(level, fmt, ...) 110 #define IOT_LOG_TRACE(fmt, ...) 111 #define IOT_LOG_DEBUG(fmt, ...) 112 113 #endif 114 115 #define IOT_LOG_TRACE(fmt, ...) IOT_LOG(EN_IOT_LOG_LEVEL_TRACE, fmt, ##__VA_ARGS__) 116 #define IOT_LOG_INFO(fmt, ...) IOT_LOG(EN_IOT_LOG_LEVEL_INFO, fmt, ##__VA_ARGS__) 117 #define IOT_LOG_WARN(fmt, ...) IOT_LOG(EN_IOT_LOG_LEVEL_WARN, fmt, ##__VA_ARGS__) 118 #define IOT_LOG_ERROR(fmt, ...) IOT_LOG(EN_IOT_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__) 119 #define IOT_LOG_FATAL(fmt, ...) IOT_LOG(EN_IOT_LOG_LEVEL_FATAL, fmt, ##__VA_ARGS__) 120 121 122 #endif /* IOT_LOG_H_ */