import RT-Thread@9217865c without bsp, libcpu and components/net
This commit is contained in:
commit
e2376a3709
1414 changed files with 390370 additions and 0 deletions
262
components/utilities/ulog/syslog/syslog.c
Normal file
262
components/utilities/ulog/syslog/syslog.c
Normal file
|
@ -0,0 +1,262 @@
|
|||
/*
|
||||
* 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()->parent.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 and string end sign */
|
||||
if (log_len + newline_len + sizeof('\0') > ULOG_LINE_BUF_SIZE)
|
||||
{
|
||||
/* using max length */
|
||||
log_len = ULOG_LINE_BUF_SIZE;
|
||||
/* reserve some space for newline sign */
|
||||
log_len -= newline_len;
|
||||
/* reserve some space for string end sign */
|
||||
log_len -= sizeof('\0');
|
||||
}
|
||||
|
||||
/* package newline sign */
|
||||
if (newline)
|
||||
{
|
||||
log_len += ulog_strcpy(log_len, log_buf + log_len, ULOG_NEWLINE_SIGN);
|
||||
}
|
||||
|
||||
/* add string end sign */
|
||||
log_buf[log_len] = '\0';
|
||||
|
||||
return log_len;
|
||||
}
|
||||
|
||||
#endif /* ULOG_USING_SYSLOG */
|
100
components/utilities/ulog/syslog/syslog.h
Normal file
100
components/utilities/ulog/syslog/syslog.h
Normal file
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-09-07 armink the first version
|
||||
*/
|
||||
|
||||
#ifndef _SYSLOG_H_
|
||||
#define _SYSLOG_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* priorities/facilities are encoded into a single 32-bit quantity, where the
|
||||
* bottom 3 bits are the priority (0-7) and the top 28 bits are the facility
|
||||
* (0-big number). Both the priorities and the facilities map roughly
|
||||
* one-to-one to strings in the syslogd(8) source code. This mapping is
|
||||
* included in this file.
|
||||
*
|
||||
* priorities (these are ordered)
|
||||
*/
|
||||
#define LOG_EMERG 0 /* system is unusable */
|
||||
#define LOG_ALERT 1 /* action must be taken immediately */
|
||||
#define LOG_CRIT 2 /* critical conditions */
|
||||
#define LOG_ERR 3 /* error conditions */
|
||||
#define LOG_WARNING 4 /* warning conditions */
|
||||
#define LOG_NOTICE 5 /* normal but significant condition */
|
||||
#define LOG_INFO 6 /* informational */
|
||||
#define LOG_DEBUG 7 /* debug-level messages */
|
||||
|
||||
#define LOG_PRIMASK 0x07
|
||||
|
||||
#define LOG_PRI(p) ((p) & LOG_PRIMASK)
|
||||
#define LOG_MAKEPRI(fac, pri) (((fac) << 3) | (pri))
|
||||
|
||||
/* facility codes */
|
||||
#define LOG_KERN (0<<3) /* kernel messages */
|
||||
#define LOG_USER (1<<3) /* random user-level messages */
|
||||
#define LOG_MAIL (2<<3) /* mail system */
|
||||
#define LOG_DAEMON (3<<3) /* system daemons */
|
||||
#define LOG_AUTH (4<<3) /* security/authorization messages */
|
||||
#define LOG_SYSLOG (5<<3) /* messages generated internally by syslogd */
|
||||
#define LOG_LPR (6<<3) /* line printer subsystem */
|
||||
#define LOG_NEWS (7<<3) /* network news subsystem */
|
||||
#define LOG_UUCP (8<<3) /* UUCP subsystem */
|
||||
#define LOG_CRON (9<<3) /* clock daemon */
|
||||
#define LOG_AUTHPRIV (10<<3) /* security/authorization messages (private) */
|
||||
|
||||
/* other codes through 15 reserved for system use */
|
||||
#define LOG_LOCAL0 (16<<3) /* reserved for local use */
|
||||
#define LOG_LOCAL1 (17<<3) /* reserved for local use */
|
||||
#define LOG_LOCAL2 (18<<3) /* reserved for local use */
|
||||
#define LOG_LOCAL3 (19<<3) /* reserved for local use */
|
||||
#define LOG_LOCAL4 (20<<3) /* reserved for local use */
|
||||
#define LOG_LOCAL5 (21<<3) /* reserved for local use */
|
||||
#define LOG_LOCAL6 (22<<3) /* reserved for local use */
|
||||
#define LOG_LOCAL7 (23<<3) /* reserved for local use */
|
||||
|
||||
#define LOG_NFACILITIES 24 /* current number of facilities */
|
||||
#define LOG_FACMASK 0x03f8 /* mask to extract facility part */
|
||||
/* facility of pri */
|
||||
#define LOG_FAC(p) (((p) & LOG_FACMASK) >> 3)
|
||||
|
||||
/*
|
||||
* arguments to setlogmask.
|
||||
*/
|
||||
#define LOG_MASK(pri) (1 << (pri)) /* mask for one priority */
|
||||
#define LOG_UPTO(pri) ((1 << ((pri)+1)) - 1) /* all priorities through pri */
|
||||
|
||||
/*
|
||||
* Option flags for openlog.
|
||||
*
|
||||
* LOG_ODELAY no longer does anything.
|
||||
* LOG_NDELAY is the inverse of what it used to be.
|
||||
*/
|
||||
#define LOG_PID 0x01 /* log the pid with each message */
|
||||
#define LOG_CONS 0x02 /* log on the console if errors in sending */
|
||||
#define LOG_ODELAY 0x04 /* delay open until first syslog() (default) */
|
||||
#define LOG_NDELAY 0x08 /* don't delay open */
|
||||
#define LOG_NOWAIT 0x10 /* don't wait for console forks: DEPRECATED */
|
||||
#define LOG_PERROR 0x20 /* log to stderr as well */
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
void closelog(void);
|
||||
void openlog(const char *ident, int option, int facility);
|
||||
int setlogmask(int mask);
|
||||
void syslog(int priority, const char *format, ...);
|
||||
void vsyslog(int priority, const char *format, va_list args);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _SYSLOG_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue