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