1 /**
2  * @file ulog/ulog.h
3  * @copyright Copyright (C) 2015-2019 Alibaba Group Holding Limited
4  */
5 
6 #ifndef ULOG_H
7 #define ULOG_H
8 
9 #include <string.h>
10 #include "ulog_config.h"
11 /** @addtogroup aos_ulog ulog
12  *  Optional policy of log, which can output into default direction, virtual file
13  *  system, mqtt channel and syslog udp.
14  *
15  *  @{
16  */
17 
18 /**
19  * Important!!! Switch on NDEBUG will make no log produced(except use API LOG),
20  * it doesn't work even you try to increase log level.
21  */
22 #ifdef NDEBUG
23 #define CONFIG_LOGMACRO_SILENT
24 #endif
25 
26 #if defined(CONFIG_DEBUG) && CONFIG_DEBUG
27 #define DEBUG
28 #endif
29 
30 #define ULOG_TAG __FILE__, __LINE__
31 
32 #define LOG_EMERG   0 /* system is unusable */
33 #define LOG_ALERT   1 /* action must be taken immediately */
34 #define LOG_CRIT    2 /* critical conditions */
35 #define LOG_ERR     3 /* error conditions */
36 #define LOG_WARNING 4 /* warning conditions */
37 #define LOG_NOTICE  5 /* normal, but significant, condition */
38 #define LOG_INFO    6 /* informational message */
39 #define LOG_DEBUG   7 /* debug-level message */
40 #define LOG_NONE    8 /* used in stop filter, all log will pop out */
41 
42 typedef enum {
43     AOS_LL_NONE  = LOG_EMERG,   /* disable log */
44     AOS_LL_FATAL = LOG_CRIT,    /* fatal log will output */
45     AOS_LL_ERROR = LOG_ERR,     /* fatal + error log will output */
46     AOS_LL_WARN  = LOG_WARNING, /* fatal + warn + error log will output(default level) */
47     AOS_LL_INFO  = LOG_INFO,    /* info + warn + error log will output */
48     AOS_LL_DEBUG = LOG_DEBUG,   /* debug + info + warn + error + fatal log will output */
49 } aos_log_level_t;
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 /**
56  * Function prototype for syncronized log text, Recommed using below brief API LOGX instead of this.
57  *
58  * @param[in]  s         Serverity of Log
59  * @param[in]  mod       Module name
60  * @param[in]  f         Usually File name
61  * @param[in]  l         Usually Line number of comment
62  * @param[in]  fmt, ...  Variable Parameter, support format print to log
63  *
64  * @return  0 on success, negative error on failure.
65  */
66 int ulog(const unsigned char s, const char *mod, const char *f,
67          const unsigned long l, const char *fmt, ...);
68 
69 /**
70  * Log at the alert level, brief using of ulog.
71  *
72  * @param[in]  ...  same as printf() usage.
73  *
74  * @return  0 on success, negative error on failure.
75  */
76 #define LOG(...) ulog(LOG_ALERT, "AOS", ULOG_TAG, __VA_ARGS__)
77 
78 #ifdef CONFIG_LOGMACRO_SILENT
79 #define LOGF(mod, ...)
80 #define LOGE(mod, ...)
81 #define LOGW(mod, ...)
82 #define LOGI(mod, ...)
83 #define LOGD(mod, ...)
84 
85 #else /* !CONFIG_LOGMACRO_SILENT */
86 /**
87  * Log at fatal level, brief using of ulog.
88  *
89  * @param[in]  mod  string description of module.
90  * @param[in]  ...  same as printf() usage.
91  *
92  * @return  0 on success, negative error on failure.
93  */
94 #define LOGF(mod, ...) ulog(LOG_CRIT, mod, ULOG_TAG, __VA_ARGS__)
95 
96 /**
97  * Log at error level, brief using of ulog.
98  *
99  * @param[in]  mod  string description of module.
100  * @param[in]  ...  same as printf() usage.
101  *
102  * @return  0 on success, negative error on failure.
103  */
104 #define LOGE(mod, ...) ulog(LOG_ERR, mod, ULOG_TAG, __VA_ARGS__)
105 
106 /**
107  * Log at warning level, brief using of ulog.
108  *
109  * @param[in]  mod  string description of module.
110  * @param[in]  ...  same as printf() usage.
111  *
112  * @return  0 on success, negative error on failure.
113  */
114 #define LOGW(mod, ...) ulog(LOG_WARNING, mod, ULOG_TAG, __VA_ARGS__)
115 
116 /**
117  * Log at info level, brief using of ulog.
118  *
119  * @param[in]  mod  string description of module.
120  * @param[in]  ...  same as printf() usage.
121  *
122  * @return  0 on success, negative error on failure.
123  */
124 #define LOGI(mod, ...) ulog(LOG_INFO, mod, ULOG_TAG, __VA_ARGS__)
125 
126 /**
127  * Log at debug level, brief using of ulog.
128  *
129  * @note: This Log API take effect only the switcher 'DEBUG' is switch on.
130  * @param[in]  mod  string description of module.
131  * @param[in]  ...  same as printf() usage.
132  *
133  * @return  0 on success, negative error on failure.
134  */
135 #ifdef DEBUG
136 #define LOGD(mod, ...) ulog(LOG_DEBUG, mod, ULOG_TAG,  __VA_ARGS__)
137 #else
138 #define LOGD(mod, ...)
139 #endif
140 
141 #endif /* CONFIG_LOGMACRO_SILENT */
142 
143 /**
144  * Set the log level.
145  *
146  * @param[in]  log_level  level to be set,must be one of AOS_LL_NONE,AOS_LL_FATAL,
147  * AOS_LL_ERROR,AOS_LL_WARN,AOS_LL_INFO or AOS_LL_DEBUG.
148  *
149  * @return  0 on success, negative error on failure.
150  */
151 int aos_set_log_level(aos_log_level_t log_level);
152 
153 /**
154  * Set the log terminal output func.
155  *
156  * @param[in]  void (*output_func)(const char* fmt, ...)
157  * function to output log for terminal in different platform
158  *
159  * @return  0 on success, negative error on failure.
160  */
161 int aos_set_log_output(void (*output_func)(const char* fmt, ...));
162 
163 #if ULOG_POP_CLOUD_ENABLE
164 int aos_set_popcloud_log_level(aos_log_level_t log_level);
165 #endif
166 
167 #if ULOG_POP_FS_ENABLE
168 int ulog_fs_log_file_size(unsigned int filesize);
169 int ulog_fs_log_file_path(char *filepath);
170 int aos_set_popfs_log_level(aos_log_level_t log_level);
171 #endif
172 
173 #if ULOG_POP_UDP_ENABLE
174 int aos_set_popudp_log_level(aos_log_level_t log_level);
175 #endif
176 
177 /**
178  * Function prototype for log init. This is called in system level automatically,
179  * it is not necessary calling it in application.
180  *
181  */
182 void ulog_init(void);
183 
184 /**
185  * Function prototype for ulog management.
186  *
187  * @param[in] cmd_str command string, user fill the command string to manage ulog, format
188  *            shall as below:
189  *            "tcpip service=1" or "tcpip service=0" to notice the tcpip feature is install or not,
190  *            which have impact on the ulog pop out via udp session;
191  *            "listen ip=16777343" to nofice the syslog listener's address is 127.0.0.1;
192  *            "fspause=1" or "fspause=0" to command pause/resume into file system or not;
193  *            "ulog file up=1" to notify upload log file 1 via http.
194  *
195  * @return  0 on Success, -EINVAL: invalid parameter, -EPERM: ulog module not initied yet,
196  *          -EACCES: this function not work as there is no ASYNC method using. Other fail
197  *          reason may come from kernel message queue.
198  */
199 int ulog_man(const char *cmd_str);
200 
201 /**
202  * Function prototype for get ulog list recorded in file system.
203  * Precondition of use this function is switch on ULOG_POP_FS_ENABLE
204  * The result is usually like below:
205  * {"time":"   4.500",list:[{"idx":1,"start":"   0.000","end":""}]}
206  * context of time is system time when called, list show the start time and end time of special index.
207  * If value of end time is empty, means this log file is logging.
208  *
209  * @param[in] buf  buffer received the context of file list
210  * @param[in] len  length of buffer
211  *
212  * @return  0 sucessfully, -EINVAL param illegal, -ENOMEM not enough room to receive file
213  *          list, -EIO system error.
214  */
215 int aos_get_ulog_list(char *buf, const unsigned short len);
216 
217 /**
218  * Dump buffer by hex.
219  * @param[in]  tag  tag
220  * @param[in]  buffer buffer
221  * @param[in]  len buffer len
222  * @returns zero on success.  On error, yoc_err is returned
223  */
224 int aos_log_hexdump(const char* tag, char *buffer, int len);
225 
226 #ifdef __cplusplus
227 }
228 #endif
229 
230 /** @} */
231 
232 #endif /* ULOG_H */
233 
234