• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
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 /**
17  ****************************************************************************************
18  *
19  * @file sonata_log.h
20  *
21  * @brief header file - log
22  *
23  ****************************************************************************************
24  */
25 
26 #ifndef _SONATA_LOG_H_
27 #define _SONATA_LOG_H_
28 /*
29  * INCLUDE FILES
30  ****************************************************************************************
31  */
32 #include <stdarg.h>
33 
34 /*
35  * ENUM DEFINITIONS
36  ****************************************************************************************
37  */
38 /**
39  * @brief Log level
40  *
41  */
42 typedef enum {
43     SONATA_LOG_NONE,       /* !< No log output */
44     SONATA_LOG_ERROR,      /* !< Critical errors, software module can not recover on its own */
45     SONATA_LOG_WARN,       /* !< Error conditions from which recovery measures have been taken */
46     SONATA_LOG_INFO,       /* !< Information messages which describe normal flow of events */
47     SONATA_LOG_DEBUG,      /* !< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
48     SONATA_LOG_VERBOSE     /* !< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */
49 } sonata_log_level;
50 
51 /*
52  * MACRO DEFINES
53  ****************************************************************************************
54  */
55 /*!
56  * @brief Defalut log level
57  */
58 #define LOG_LOCAL_LEVEL  SONATA_LOG_VERBOSE
59 
60 /*!
61  * @brief vprintf
62  */
63 typedef int (*vprintf_like_t)(const char *, va_list);
64 
65 #ifdef CFG_SONATA_LOG
66 #define SONATA_LOGT_FORMAT_SIMPLE(format)  "%s: " format "\r\n"
67 #define SONATA_LOGT_LEVEL_SIMPLE(level, tag, format, ...) do {                     \
68         if (level==SONATA_LOG_ERROR)          { sonata_logt_write(SONATA_LOG_ERROR,      tag, \
69                                                                   SONATA_LOGT_FORMAT_SIMPLE(format), \
70                                                                   tag, ##__VA_ARGS__); } \
71         else if (level == SONATA_LOG_WARN)    { sonata_logt_write(SONATA_LOG_WARN,       tag, \
72                                                                   SONATA_LOGT_FORMAT_SIMPLE(format), tag, \
73                                                                   ##__VA_ARGS__); } \
74         else if (level == SONATA_LOG_DEBUG)   { sonata_logt_write(SONATA_LOG_DEBUG,      tag, \
75                                                                   SONATA_LOGT_FORMAT_SIMPLE(format), tag, \
76                                                                   ##__VA_ARGS__); } \
77         else if (level == SONATA_LOG_VERBOSE) { sonata_logt_write(SONATA_LOG_VERBOSE,    tag, \
78                                                                   SONATA_LOGT_FORMAT_SIMPLE(format), tag, \
79                                                                   ##__VA_ARGS__); } \
80         else                                  { sonata_logt_write(SONATA_LOG_INFO,       tag, \
81                                                                   SONATA_LOGT_FORMAT_SIMPLE(format), tag, \
82                                                                   ##__VA_ARGS__); } \
83     } while (0)
84 #define SONATA_LOGT_LEVEL_LOCAL_SIMPLE(level, tag, format, ...) do {               \
85         if (LOG_LOCAL_LEVEL >= level) SONATA_LOGT_LEVEL_SIMPLE(level, tag, format, ##__VA_ARGS__); \
86     } while (0)
87 /*!
88  * @brief  Log function With TAG
89  * @example LOGTE("TagE","A Simple logE: %02X, %s", 0,__FUNCTION__);
90  * @note  LOGTX function will auto add linefeed for every log string.
91  */
92 #define LOGTE(tag, format, ...) SONATA_LOGT_LEVEL_LOCAL_SIMPLE(SONATA_LOG_ERROR,   tag, format, ##__VA_ARGS__)
93 #define LOGTW(tag, format, ...) SONATA_LOGT_LEVEL_LOCAL_SIMPLE(SONATA_LOG_WARN,    tag, format, ##__VA_ARGS__)
94 #define LOGTI(tag, format, ...) SONATA_LOGT_LEVEL_LOCAL_SIMPLE(SONATA_LOG_INFO,    tag, format, ##__VA_ARGS__)
95 #define LOGTD(tag, format, ...) SONATA_LOGT_LEVEL_LOCAL_SIMPLE(SONATA_LOG_DEBUG,   tag, format, ##__VA_ARGS__)
96 #define LOGTV(tag, format, ...) SONATA_LOGT_LEVEL_LOCAL_SIMPLE(SONATA_LOG_VERBOSE, tag, format, ##__VA_ARGS__)
97 
98 /********************************************* Log function for no TAG ***********************************************************/
99 
100 #define SONATA_LOG_FORMAT_SIMPLE(format)  "" format ""
101 #define SONATA_LOG_LEVEL_SIMPLE(level, format, ...) do {                     \
102         if (level==SONATA_LOG_ERROR)           { sonata_logt_write(SONATA_LOG_ERROR,     NULL, \
103                                                                    SONATA_LOG_FORMAT_SIMPLE(format), \
104                                                                    ##__VA_ARGS__); } \
105         else if (level==SONATA_LOG_WARN)       { sonata_logt_write(SONATA_LOG_WARN,      NULL, \
106                                                                    SONATA_LOG_FORMAT_SIMPLE(format), \
107                                                                    ##__VA_ARGS__); } \
108         else if (level==SONATA_LOG_DEBUG)      { sonata_logt_write(SONATA_LOG_DEBUG,     NULL, \
109                                                                    SONATA_LOG_FORMAT_SIMPLE(format), \
110                                                                    ##__VA_ARGS__); } \
111         else if (level==SONATA_LOG_VERBOSE)    { sonata_logt_write(SONATA_LOG_VERBOSE,   NULL, \
112                                                                    SONATA_LOG_FORMAT_SIMPLE(format), \
113                                                                    ##__VA_ARGS__); } \
114         else                                    { sonata_logt_write(SONATA_LOG_INFO,      NULL, \
115                                                                     SONATA_LOG_FORMAT_SIMPLE(format), \
116                                                                     ##__VA_ARGS__); } \
117     } while (0)
118 #define SONATA_LOG_LEVEL_LOCAL_SIMPLE(level, format, ...) do {               \
119         if (LOG_LOCAL_LEVEL >= level) SONATA_LOG_LEVEL_SIMPLE(level, format, ##__VA_ARGS__); \
120     } while (0)
121 
122 /*!
123  * @brief  Log function without TAG
124  * @example "LOGE("A Simple logE: %02X, %s r n", 0,__FUNCTION__);"
125  * @note  user should add linefeed for every log string if necessary
126  */
127 #define LOGE(format, ...) SONATA_LOG_LEVEL_LOCAL_SIMPLE(SONATA_LOG_ERROR,   format, ##__VA_ARGS__)
128 #define LOGW(format, ...) SONATA_LOG_LEVEL_LOCAL_SIMPLE(SONATA_LOG_WARN,    format, ##__VA_ARGS__)
129 #define LOGI(format, ...) SONATA_LOG_LEVEL_LOCAL_SIMPLE(SONATA_LOG_INFO,    format, ##__VA_ARGS__)
130 #define LOGD(format, ...) SONATA_LOG_LEVEL_LOCAL_SIMPLE(SONATA_LOG_DEBUG,   format, ##__VA_ARGS__)
131 #define LOGV(format, ...) SONATA_LOG_LEVEL_LOCAL_SIMPLE(SONATA_LOG_VERBOSE, format, ##__VA_ARGS__)
132 
133 #else // CFG_SONATA_LOG
134 
135 #define LOGTE(tag, format, ...)
136 #define LOGTW(tag, format, ...)
137 #define LOGTI(tag, format, ...)
138 #define LOGTD(tag, format, ...)
139 #define LOGTV(tag, format, ...)
140 #define LOGE(format, ...)
141 #define LOGW(format, ...)
142 #define LOGI(format, ...)
143 #define LOGD(format, ...)
144 #define LOGV(format, ...)
145 
146 #endif // CFG_SONATA_LOG
147 
148 /*
149 * FUNCTION DEFINITIONS
150 ****************************************************************************************
151 */
152 
153 /*!
154  * @brief Set custom log function. If not set, will use default print log function
155  * @param func functions with vprintf format
156  * @return
157  */
158 vprintf_like_t sonata_log_set_vprintf(vprintf_like_t func);
159 /*!
160  * @brief Set log level
161  * @param level @see sonata_log_level
162  */
163 void sonata_log_level_set(sonata_log_level level);
164 
165 // Internal use
166 void sonata_logt_write(sonata_log_level level, const char *tag, const char *format, ...);
167 
168 #endif // _SONATA_LOG_H_
169