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