1 /*
2  * Copyright (c) 2006-2022, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2018-09-07     armink       the first version
9  */
10 
11 #include <stdarg.h>
12 #include <ulog.h>
13 #include <rthw.h>
14 #include <stdint.h>
15 #include "syslog.h"
16 
17 /*
18  * reference:
19  * http://pubs.opengroup.org/onlinepubs/7908799/xsh/syslog.h.html
20  * https://www.gnu.org/software/libc/manual/html_node/Submitting-Syslog-Messages.html
21  * http://man7.org/linux/man-pages/man3/syslog.3.html
22  */
23 
24 #ifdef ULOG_USING_SYSLOG
25 
26 #include <sys/time.h>
27 
28 #ifndef ULOG_SYSLOG_IDENT_MAX_LEN
29 #define ULOG_SYSLOG_IDENT_MAX_LEN      ULOG_FILTER_TAG_MAX_LEN
30 #endif
31 
32 static char local_ident[ULOG_SYSLOG_IDENT_MAX_LEN + 1];
33 static int local_facility = LOG_USER;
34 static int local_option = LOG_USER;
35 static rt_bool_t is_open = RT_FALSE;
36 
37 /**
38  * open connection to syslog
39  *
40  * @param ident is an arbitrary identification string which future syslog invocations will prefix to each message.
41  * @param option is not using on ulog.
42  * @param facility is the default facility code for this connection.
43  */
openlog(const char * ident,int option,int facility)44 void openlog(const char *ident, int option, int facility)
45 {
46     rt_base_t level;
47 
48     ulog_init();
49 
50     level = rt_hw_interrupt_disable();
51 
52     rt_memset(local_ident, 0, sizeof(local_ident));
53     if (ident)
54     {
55         rt_strncpy(local_ident, ident, ULOG_SYSLOG_IDENT_MAX_LEN);
56     }
57     else
58     {
59         rt_strncpy(local_ident, "rtt", ULOG_SYSLOG_IDENT_MAX_LEN);
60     }
61 
62     local_option = option;
63 
64     if (facility)
65     {
66         local_facility = facility;
67     }
68     else
69     {
70         /* default facility is LOG_USER */
71         local_facility = LOG_USER;
72     }
73     /* output all level log */
74     setlogmask(LOG_UPTO(LOG_DEBUG));
75 
76     is_open = RT_TRUE;
77 
78     rt_hw_interrupt_enable(level);
79 
80 }
81 
82 /**
83  * This is functionally identical to syslog.
84  *
85  * @param priority log priority, can be generated by the macro LOG_MAKEPRI
86  * @param format log format
87  * @param args log arguments
88  */
vsyslog(int priority,const char * format,va_list args)89 void vsyslog(int priority, const char *format, va_list args)
90 {
91     if (LOG_FAC(priority) == 0)
92     {
93         /* using local facility */
94         priority |= local_facility;
95     }
96 
97     ulog_voutput(priority, local_ident, RT_TRUE, RT_NULL, 0, 0, 0, format, args);
98 }
99 
100 /**
101  * generates a log message
102  *
103  * @param priority log priority, can be generated by the macro LOG_MAKEPRI
104  * @param format log format, like printf()
105  */
syslog(int priority,const char * format,...)106 void syslog(int priority, const char *format, ...)
107 {
108     va_list args;
109 
110     if (!is_open)
111     {
112         openlog(0, 0, 0);
113     }
114     /* args point to the first variable parameter */
115     va_start(args, format);
116 
117     vsyslog(priority, format, args);
118 
119     va_end(args);
120 }
121 
122 /**
123  * close the syslog
124  */
closelog(void)125 void closelog(void)
126 {
127     ulog_deinit();
128 
129     is_open = RT_FALSE;
130 }
131 
132 /**
133  * set log priority mask
134  *
135  * @param mask The log priority mask which generate by macro LOG_MASK and LOG_UPTO.
136  *
137  * @return This function returns the previous log priority mask.
138  */
setlogmask(int mask)139 int setlogmask(int mask)
140 {
141     static int old_mask = 0;
142     int return_mask = old_mask;
143 
144     ulog_tag_lvl_filter_set(local_ident, mask);
145 
146     old_mask = mask;
147 
148     return return_mask;
149 }
150 
get_month_str(uint8_t month)151 static const char *get_month_str(uint8_t month)
152 {
153     switch(month)
154     {
155     case 1: return "Jan";
156     case 2: return "Feb";
157     case 3: return "Mar";
158     case 4: return "Apr";
159     case 5: return "May";
160     case 6: return "June";
161     case 7: return "July";
162     case 8: return "Aug";
163     case 9: return "Sept";
164     case 10: return "Oct";
165     case 11: return "Nov";
166     case 12: return "Dec";
167     default: return "Unknown";
168     }
169 }
170 
syslog_formater(char * log_buf,int level,const char * tag,rt_bool_t newline,const char * format,va_list args)171 rt_weak rt_size_t syslog_formater(char *log_buf, int level, const char *tag, rt_bool_t newline, const char *format, va_list args)
172 {
173     extern rt_size_t ulog_strcpy(rt_size_t cur_len, char *dst, const char *src);
174 
175     rt_size_t log_len = 0, newline_len = rt_strlen(ULOG_NEWLINE_SIGN);
176     int fmt_result;
177 
178     RT_ASSERT(log_buf);
179     RT_ASSERT(LOG_PRI(level) <= LOG_DEBUG);
180     RT_ASSERT(tag);
181     RT_ASSERT(format);
182 
183     /* add time and priority (level) info */
184     {
185         time_t now = time(RT_NULL);
186         struct tm *tm, tm_tmp;
187 
188         tm = gmtime_r(&now, &tm_tmp);
189 
190 #ifdef ULOG_OUTPUT_LEVEL
191         rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, "<%d>%s%3d %02d:%02d:%02d", level,
192                 get_month_str(tm->tm_mon + 1), tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
193 #else
194         rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, "%s%3d %02d:%02d:%02d",
195                 get_month_str(tm->tm_mon + 1), tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
196 #endif /* ULOG_OUTPUT_LEVEL */
197 
198         log_len += rt_strlen(log_buf + log_len);
199     }
200 
201 #ifdef ULOG_OUTPUT_TAG
202     /* add identification (tag) info */
203     {
204         log_len += ulog_strcpy(log_len, log_buf + log_len, " ");
205         log_len += ulog_strcpy(log_len, log_buf + log_len, tag);
206     }
207 #endif /* ULOG_OUTPUT_TAG */
208 
209 #ifdef ULOG_OUTPUT_THREAD_NAME
210     /* add thread info */
211     {
212         log_len += ulog_strcpy(log_len, log_buf + log_len, " ");
213         /* is not in interrupt context */
214         if (rt_interrupt_get_nest() == 0)
215         {
216             log_len += ulog_strcpy(log_len, log_buf + log_len, rt_thread_self()->parent.name);
217         }
218         else
219         {
220             log_len += ulog_strcpy(log_len, log_buf + log_len, "ISR");
221         }
222     }
223 #endif /* ULOG_OUTPUT_THREAD_NAME */
224 
225     log_len += ulog_strcpy(log_len, log_buf + log_len, ": ");
226     fmt_result = rt_vsnprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, format, args);
227 
228     /* calculate log length */
229     if ((log_len + fmt_result <= ULOG_LINE_BUF_SIZE) && (fmt_result > -1))
230     {
231         log_len += fmt_result;
232     }
233     else
234     {
235         /* using max length */
236         log_len = ULOG_LINE_BUF_SIZE;
237     }
238 
239     /* overflow check and reserve some space for newline sign and string end sign */
240     if (log_len + newline_len + sizeof('\0') > ULOG_LINE_BUF_SIZE)
241     {
242         /* using max length */
243         log_len = ULOG_LINE_BUF_SIZE;
244         /* reserve some space for newline sign */
245         log_len -= newline_len;
246         /* reserve some space for string end sign */
247         log_len -= sizeof('\0');
248     }
249 
250     /* package newline sign */
251     if (newline)
252     {
253         log_len += ulog_strcpy(log_len, log_buf + log_len, ULOG_NEWLINE_SIGN);
254     }
255 
256     /* add string end sign */
257     log_buf[log_len] = '\0';
258 
259     return log_len;
260 }
261 
262 #endif /* ULOG_USING_SYSLOG */
263