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
20
components/utilities/ulog/SConscript
Normal file
20
components/utilities/ulog/SConscript
Normal file
|
@ -0,0 +1,20 @@
|
|||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c')
|
||||
path = [cwd]
|
||||
|
||||
if GetDepend('ULOG_BACKEND_USING_CONSOLE'):
|
||||
src += ['backend/console_be.c']
|
||||
|
||||
if GetDepend('ULOG_BACKEND_USING_FILE'):
|
||||
path += [cwd + '/backend']
|
||||
src += ['backend/file_be.c']
|
||||
|
||||
if GetDepend('ULOG_USING_SYSLOG'):
|
||||
path += [cwd + '/syslog']
|
||||
src += Glob('syslog/*.c')
|
||||
|
||||
group = DefineGroup('Utilities', src, depend = ['RT_USING_ULOG'], CPPPATH = path)
|
||||
|
||||
Return('group')
|
53
components/utilities/ulog/backend/console_be.c
Normal file
53
components/utilities/ulog/backend/console_be.c
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-09-04 armink the first version
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <ulog.h>
|
||||
|
||||
#ifdef ULOG_BACKEND_USING_CONSOLE
|
||||
|
||||
#if defined(ULOG_ASYNC_OUTPUT_BY_THREAD) && ULOG_ASYNC_OUTPUT_THREAD_STACK < 384
|
||||
#error "The thread stack size must more than 384 when using async output by thread (ULOG_ASYNC_OUTPUT_BY_THREAD)"
|
||||
#endif
|
||||
|
||||
static struct ulog_backend console = { 0 };
|
||||
|
||||
void ulog_console_backend_output(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw,
|
||||
const char *log, rt_size_t len)
|
||||
{
|
||||
#ifdef RT_USING_DEVICE
|
||||
rt_device_t dev = rt_console_get_device();
|
||||
|
||||
if (dev == RT_NULL)
|
||||
{
|
||||
rt_hw_console_output(log);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_device_write(dev, 0, log, len);
|
||||
}
|
||||
#else
|
||||
rt_hw_console_output(log);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
int ulog_console_backend_init(void)
|
||||
{
|
||||
ulog_init();
|
||||
console.output = ulog_console_backend_output;
|
||||
|
||||
ulog_backend_register(&console, "console", RT_TRUE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
INIT_PREV_EXPORT(ulog_console_backend_init);
|
||||
|
||||
#endif /* ULOG_BACKEND_USING_CONSOLE */
|
226
components/utilities/ulog/backend/file_be.c
Normal file
226
components/utilities/ulog/backend/file_be.c
Normal file
|
@ -0,0 +1,226 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2021-01-07 ChenYong first version
|
||||
* 2021-12-20 armink add multi-instance version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <dfs_file.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <ulog.h>
|
||||
#include <ulog_be.h>
|
||||
|
||||
#ifdef ULOG_BACKEND_USING_FILE
|
||||
|
||||
#if defined(ULOG_ASYNC_OUTPUT_THREAD_STACK) && (ULOG_ASYNC_OUTPUT_THREAD_STACK < 2048)
|
||||
#error "The value of ULOG_ASYNC_OUTPUT_THREAD_STACK must be greater than 2048."
|
||||
#endif
|
||||
|
||||
/* rotate the log file xxx_n-1.log => xxx_n.log, and xxx.log => xxx_0.log */
|
||||
static rt_bool_t ulog_file_rotate(struct ulog_file_be *be)
|
||||
{
|
||||
#define SUFFIX_LEN 10
|
||||
/* mv xxx_n-1.log => xxx_n.log, and xxx.log => xxx_0.log */
|
||||
static char old_path[ULOG_FILE_PATH_LEN], new_path[ULOG_FILE_PATH_LEN];
|
||||
int index = 0, err = 0, file_fd = 0;
|
||||
rt_bool_t result = RT_FALSE;
|
||||
size_t base_len = 0;
|
||||
|
||||
rt_snprintf(old_path, ULOG_FILE_PATH_LEN, "%s/%s", be->cur_log_dir_path, be->parent.name);
|
||||
rt_snprintf(new_path, ULOG_FILE_PATH_LEN, "%s/%s", be->cur_log_dir_path, be->parent.name);
|
||||
base_len = rt_strlen(be->cur_log_dir_path) + rt_strlen(be->parent.name) + 1;
|
||||
|
||||
if (be->cur_log_file_fd >= 0)
|
||||
{
|
||||
close(be->cur_log_file_fd);
|
||||
}
|
||||
|
||||
for (index = be->file_max_num - 2; index >= 0; --index)
|
||||
{
|
||||
rt_snprintf(old_path + base_len, SUFFIX_LEN, index ? "_%d.log" : ".log", index - 1);
|
||||
rt_snprintf(new_path + base_len, SUFFIX_LEN, "_%d.log", index);
|
||||
/* remove the old file */
|
||||
if ((file_fd = open(new_path, O_RDONLY)) >= 0)
|
||||
{
|
||||
close(file_fd);
|
||||
unlink(new_path);
|
||||
}
|
||||
/* change the new log file to old file name */
|
||||
if ((file_fd = open(old_path , O_RDONLY)) >= 0)
|
||||
{
|
||||
close(file_fd);
|
||||
err = dfs_file_rename(old_path, new_path);
|
||||
}
|
||||
|
||||
if (err < 0)
|
||||
{
|
||||
result = RT_FALSE;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
result = RT_TRUE;
|
||||
}
|
||||
|
||||
__exit:
|
||||
/* reopen the file */
|
||||
be->cur_log_file_fd = open(be->cur_log_file_path, O_CREAT | O_RDWR | O_APPEND);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void ulog_file_backend_flush_with_buf(struct ulog_backend *backend)
|
||||
{
|
||||
struct ulog_file_be *be = (struct ulog_file_be *) backend;
|
||||
rt_size_t file_size = 0, write_size = 0;
|
||||
|
||||
if (be->enable == RT_FALSE || be->buf_ptr_now == be->file_buf)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (be->cur_log_file_fd < 0)
|
||||
{
|
||||
/* check log file directory */
|
||||
if (access(be->cur_log_dir_path, F_OK) < 0)
|
||||
{
|
||||
mkdir(be->cur_log_dir_path, 0);
|
||||
}
|
||||
/* open file */
|
||||
rt_snprintf(be->cur_log_file_path, ULOG_FILE_PATH_LEN, "%s/%s.log", be->cur_log_dir_path, be->parent.name);
|
||||
be->cur_log_file_fd = open(be->cur_log_file_path, O_CREAT | O_RDWR | O_APPEND);
|
||||
if (be->cur_log_file_fd < 0)
|
||||
{
|
||||
rt_kprintf("ulog file(%s) open failed.", be->cur_log_file_path);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
file_size = lseek(be->cur_log_file_fd, 0, SEEK_END);
|
||||
if (file_size >= (be->file_max_size - be->buf_size * 2))
|
||||
{
|
||||
if (!ulog_file_rotate(be))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
write_size = (rt_size_t)(be->buf_ptr_now - be->file_buf);
|
||||
/* write to the file */
|
||||
if (write(be->cur_log_file_fd, be->file_buf, write_size) != write_size)
|
||||
{
|
||||
return;
|
||||
}
|
||||
/* flush file cache */
|
||||
fsync(be->cur_log_file_fd);
|
||||
|
||||
/* point be->buf_ptr_now at the head of be->file_buf[be->buf_size] */
|
||||
be->buf_ptr_now = be->file_buf;
|
||||
}
|
||||
|
||||
static void ulog_file_backend_output_with_buf(struct ulog_backend *backend, rt_uint32_t level,
|
||||
const char *tag, rt_bool_t is_raw, const char *log, rt_size_t len)
|
||||
{
|
||||
struct ulog_file_be *be = (struct ulog_file_be *)backend;
|
||||
rt_size_t copy_len = 0, free_len = 0;
|
||||
const unsigned char *buf_ptr_end = be->file_buf + be->buf_size;
|
||||
|
||||
while (len)
|
||||
{
|
||||
/* free space length */
|
||||
free_len = buf_ptr_end - be->buf_ptr_now;
|
||||
/* copy the log to the mem buffer */
|
||||
if (len > free_len)
|
||||
{
|
||||
copy_len = free_len;
|
||||
}
|
||||
else
|
||||
{
|
||||
copy_len = len;
|
||||
}
|
||||
rt_memcpy(be->buf_ptr_now, log, copy_len);
|
||||
/* update data pos */
|
||||
be->buf_ptr_now += copy_len;
|
||||
len -= copy_len;
|
||||
log += copy_len;
|
||||
|
||||
RT_ASSERT(be->buf_ptr_now <= buf_ptr_end);
|
||||
/* check the log buffer remain size */
|
||||
if (buf_ptr_end == be->buf_ptr_now)
|
||||
{
|
||||
ulog_file_backend_flush_with_buf(backend);
|
||||
if (buf_ptr_end == be->buf_ptr_now)
|
||||
{
|
||||
/* There is no space, indicating that the data cannot be refreshed
|
||||
to the back end of the file Discard data and exit directly */
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* initialize the ulog file backend */
|
||||
int ulog_file_backend_init(struct ulog_file_be *be, const char *name, const char *dir_path, rt_size_t max_num,
|
||||
rt_size_t max_size, rt_size_t buf_size)
|
||||
{
|
||||
be->file_buf = rt_calloc(1, buf_size);
|
||||
if (!be->file_buf)
|
||||
{
|
||||
rt_kprintf("Warning: NO MEMORY for %s file backend\n", name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
/* temporarily store the start address of the ulog file buffer */
|
||||
be->buf_ptr_now = be->file_buf;
|
||||
be->cur_log_file_fd = -1;
|
||||
be->file_max_num = max_num;
|
||||
be->file_max_size = max_size;
|
||||
be->buf_size = buf_size;
|
||||
be->enable = RT_FALSE;
|
||||
rt_strncpy(be->cur_log_dir_path, dir_path, ULOG_FILE_PATH_LEN);
|
||||
/* the buffer length MUST less than file size */
|
||||
RT_ASSERT(be->buf_size < be->file_max_size);
|
||||
|
||||
be->parent.output = ulog_file_backend_output_with_buf;
|
||||
be->parent.flush = ulog_file_backend_flush_with_buf;
|
||||
ulog_backend_register((ulog_backend_t) be, name, RT_FALSE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* uninitialize the ulog file backend */
|
||||
int ulog_file_backend_deinit(struct ulog_file_be *be)
|
||||
{
|
||||
if (be->cur_log_file_fd >= 0)
|
||||
{
|
||||
/* flush log to file */
|
||||
ulog_file_backend_flush_with_buf((ulog_backend_t)be);
|
||||
/* close */
|
||||
close(be->cur_log_file_fd);
|
||||
be->cur_log_file_fd = -1;
|
||||
}
|
||||
|
||||
if (!be->file_buf)
|
||||
{
|
||||
rt_free(be->file_buf);
|
||||
be->file_buf = RT_NULL;
|
||||
}
|
||||
|
||||
ulog_backend_unregister((ulog_backend_t)be);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ulog_file_backend_enable(struct ulog_file_be *be)
|
||||
{
|
||||
be->enable = RT_TRUE;
|
||||
}
|
||||
|
||||
void ulog_file_backend_disable(struct ulog_file_be *be)
|
||||
{
|
||||
be->enable = RT_FALSE;
|
||||
}
|
||||
|
||||
#endif /* ULOG_BACKEND_USING_FILE */
|
44
components/utilities/ulog/backend/ulog_be.h
Normal file
44
components/utilities/ulog/backend/ulog_be.h
Normal file
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2021-01-07 ChenYong first version
|
||||
* 2021-12-20 armink add multi-instance version
|
||||
*/
|
||||
|
||||
#ifndef _ULOG_BE_H_
|
||||
#define _ULOG_BE_H_
|
||||
|
||||
#include <ulog.h>
|
||||
|
||||
#ifndef ULOG_FILE_PATH_LEN
|
||||
#define ULOG_FILE_PATH_LEN 128
|
||||
#endif
|
||||
|
||||
struct ulog_file_be
|
||||
{
|
||||
struct ulog_backend parent;
|
||||
int cur_log_file_fd;
|
||||
rt_size_t file_max_num;
|
||||
rt_size_t file_max_size;
|
||||
rt_size_t buf_size;
|
||||
rt_bool_t enable;
|
||||
|
||||
rt_uint8_t *file_buf;
|
||||
rt_uint8_t *buf_ptr_now;
|
||||
|
||||
char cur_log_file_path[ULOG_FILE_PATH_LEN];
|
||||
char cur_log_dir_path[ULOG_FILE_PATH_LEN];
|
||||
};
|
||||
|
||||
/* ulog file backend api */
|
||||
int ulog_file_backend_init(struct ulog_file_be *be, const char *name, const char *dir_path, rt_size_t max_num,
|
||||
rt_size_t max_size, rt_size_t buf_size);
|
||||
int ulog_file_backend_deinit(struct ulog_file_be *be);
|
||||
void ulog_file_backend_enable(struct ulog_file_be *be);
|
||||
void ulog_file_backend_disable(struct ulog_file_be *be);
|
||||
|
||||
#endif /* _ULOG_BE_H_ */
|
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_ */
|
1553
components/utilities/ulog/ulog.c
Normal file
1553
components/utilities/ulog/ulog.c
Normal file
File diff suppressed because it is too large
Load diff
102
components/utilities/ulog/ulog.h
Normal file
102
components/utilities/ulog/ulog.h
Normal file
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-08-25 armink the first version
|
||||
*/
|
||||
|
||||
#ifndef _ULOG_H_
|
||||
#define _ULOG_H_
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "ulog_def.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* ulog init and deint
|
||||
*/
|
||||
int ulog_init(void);
|
||||
int ulog_async_init(void);
|
||||
void ulog_output_lock_enabled(rt_bool_t enabled);
|
||||
void ulog_deinit(void);
|
||||
|
||||
/*
|
||||
* output different level log by LOG_X API
|
||||
*
|
||||
* NOTE: The `LOG_TAG` and `LOG_LVL` must be defined before including the <ulog.h> when you want to use LOG_X API.
|
||||
*
|
||||
* #define LOG_TAG "example"
|
||||
* #define LOG_LVL LOG_LVL_DBG
|
||||
* #include <ulog.h>
|
||||
*
|
||||
* Then you can using LOG_X API to output log
|
||||
*
|
||||
* LOG_D("this is a debug log!");
|
||||
* LOG_E("this is a error log!");
|
||||
*/
|
||||
#define LOG_E(...) ulog_e(LOG_TAG, __VA_ARGS__)
|
||||
#define LOG_W(...) ulog_w(LOG_TAG, __VA_ARGS__)
|
||||
#define LOG_I(...) ulog_i(LOG_TAG, __VA_ARGS__)
|
||||
#define LOG_D(...) ulog_d(LOG_TAG, __VA_ARGS__)
|
||||
#define LOG_RAW(...) ulog_raw(__VA_ARGS__)
|
||||
#define LOG_HEX(name, width, buf, size) ulog_hex(name, width, buf, size)
|
||||
|
||||
/*
|
||||
* backend register and unregister
|
||||
*/
|
||||
rt_err_t ulog_backend_register(ulog_backend_t backend, const char *name, rt_bool_t support_color);
|
||||
rt_err_t ulog_backend_unregister(ulog_backend_t backend);
|
||||
rt_err_t ulog_backend_set_filter(ulog_backend_t backend, ulog_backend_filter_t filter);
|
||||
ulog_backend_t ulog_backend_find(const char *name);
|
||||
|
||||
#ifdef ULOG_USING_FILTER
|
||||
/*
|
||||
* log filter setting
|
||||
*/
|
||||
int ulog_tag_lvl_filter_set(const char *tag, rt_uint32_t level);
|
||||
rt_uint32_t ulog_tag_lvl_filter_get(const char *tag);
|
||||
rt_slist_t *ulog_tag_lvl_list_get(void);
|
||||
void ulog_global_filter_lvl_set(rt_uint32_t level);
|
||||
rt_uint32_t ulog_global_filter_lvl_get(void);
|
||||
void ulog_global_filter_tag_set(const char *tag);
|
||||
const char *ulog_global_filter_tag_get(void);
|
||||
void ulog_global_filter_kw_set(const char *keyword);
|
||||
const char *ulog_global_filter_kw_get(void);
|
||||
#endif /* ULOG_USING_FILTER */
|
||||
|
||||
/*
|
||||
* flush all backends's log
|
||||
*/
|
||||
void ulog_flush(void);
|
||||
|
||||
#ifdef ULOG_USING_ASYNC_OUTPUT
|
||||
/*
|
||||
* asynchronous output API
|
||||
*/
|
||||
void ulog_async_output(void);
|
||||
void ulog_async_output_enabled(rt_bool_t enabled);
|
||||
rt_err_t ulog_async_waiting_log(rt_int32_t time);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* dump the hex format data to log
|
||||
*/
|
||||
void ulog_hexdump(const char *tag, rt_size_t width, const rt_uint8_t *buf, rt_size_t size, ...);
|
||||
|
||||
/*
|
||||
* Another log output API. This API is more difficult to use than LOG_X API.
|
||||
*/
|
||||
void ulog_output(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, ...);
|
||||
void ulog_raw(const char *format, ...);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _ULOG_H_ */
|
222
components/utilities/ulog/ulog_def.h
Normal file
222
components/utilities/ulog/ulog_def.h
Normal file
|
@ -0,0 +1,222 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-08-25 armink the first version
|
||||
*/
|
||||
|
||||
#ifndef _ULOG_DEF_H_
|
||||
#define _ULOG_DEF_H_
|
||||
|
||||
#include <rtdef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* logger level, the number is compatible for syslog */
|
||||
#define LOG_LVL_ASSERT 0
|
||||
#define LOG_LVL_ERROR 3
|
||||
#define LOG_LVL_WARNING 4
|
||||
#define LOG_LVL_INFO 6
|
||||
#define LOG_LVL_DBG 7
|
||||
|
||||
/* the output silent level and all level for filter setting */
|
||||
#ifndef ULOG_USING_SYSLOG
|
||||
#define LOG_FILTER_LVL_SILENT 0
|
||||
#define LOG_FILTER_LVL_ALL 7
|
||||
#else
|
||||
#define LOG_FILTER_LVL_SILENT 1
|
||||
#define LOG_FILTER_LVL_ALL 255
|
||||
#endif /* ULOG_USING_SYSLOG */
|
||||
|
||||
/* compatible for rtdbg */
|
||||
#undef LOG_D
|
||||
#undef LOG_I
|
||||
#undef LOG_W
|
||||
#undef LOG_E
|
||||
#undef LOG_RAW
|
||||
#undef DBG_ERROR
|
||||
#undef DBG_WARNING
|
||||
#undef DBG_INFO
|
||||
#undef DBG_LOG
|
||||
#undef dbg_log
|
||||
#define DBG_ERROR LOG_LVL_ERROR
|
||||
#define DBG_WARNING LOG_LVL_WARNING
|
||||
#define DBG_INFO LOG_LVL_INFO
|
||||
#define DBG_LOG LOG_LVL_DBG
|
||||
#define dbg_log(level, ...) \
|
||||
if ((level) <= LOG_LVL) \
|
||||
{ \
|
||||
ulog_output(level, LOG_TAG, RT_FALSE, __VA_ARGS__);\
|
||||
}
|
||||
|
||||
#if !defined(LOG_TAG)
|
||||
/* compatible for rtdbg */
|
||||
#if defined(DBG_TAG)
|
||||
#define LOG_TAG DBG_TAG
|
||||
#elif defined(DBG_SECTION_NAME)
|
||||
#define LOG_TAG DBG_SECTION_NAME
|
||||
#else
|
||||
#define LOG_TAG "NO_TAG"
|
||||
#endif
|
||||
#endif /* !defined(LOG_TAG) */
|
||||
|
||||
#if !defined(LOG_LVL)
|
||||
/* compatible for rtdbg */
|
||||
#if defined(DBG_LVL)
|
||||
#define LOG_LVL DBG_LVL
|
||||
#elif defined(DBG_LEVEL)
|
||||
#define LOG_LVL DBG_LEVEL
|
||||
#else
|
||||
#define LOG_LVL LOG_LVL_DBG
|
||||
#endif
|
||||
#endif /* !defined(LOG_LVL) */
|
||||
|
||||
#if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG)
|
||||
#define ulog_d(TAG, ...) ulog_output(LOG_LVL_DBG, TAG, RT_TRUE, __VA_ARGS__)
|
||||
#else
|
||||
#define ulog_d(TAG, ...)
|
||||
#endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */
|
||||
|
||||
#if (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO)
|
||||
#define ulog_i(TAG, ...) ulog_output(LOG_LVL_INFO, TAG, RT_TRUE, __VA_ARGS__)
|
||||
#else
|
||||
#define ulog_i(TAG, ...)
|
||||
#endif /* (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) */
|
||||
|
||||
#if (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING)
|
||||
#define ulog_w(TAG, ...) ulog_output(LOG_LVL_WARNING, TAG, RT_TRUE, __VA_ARGS__)
|
||||
#else
|
||||
#define ulog_w(TAG, ...)
|
||||
#endif /* (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) */
|
||||
|
||||
#if (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR)
|
||||
#define ulog_e(TAG, ...) ulog_output(LOG_LVL_ERROR, TAG, RT_TRUE, __VA_ARGS__)
|
||||
#else
|
||||
#define ulog_e(TAG, ...)
|
||||
#endif /* (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) */
|
||||
|
||||
#if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG)
|
||||
#define ulog_hex(TAG, width, buf, size) ulog_hexdump(TAG, width, buf, size)
|
||||
#else
|
||||
#define ulog_hex(TAG, width, buf, size)
|
||||
#endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */
|
||||
|
||||
/* assert for developer. */
|
||||
#ifdef ULOG_ASSERT_ENABLE
|
||||
#define ULOG_ASSERT(EXPR) \
|
||||
if (!(EXPR)) \
|
||||
{ \
|
||||
ulog_output(LOG_LVL_ASSERT, LOG_TAG, RT_TRUE, "(%s) has assert failed at %s:%ld.", #EXPR, __FUNCTION__, __LINE__); \
|
||||
ulog_flush(); \
|
||||
while (1); \
|
||||
}
|
||||
#else
|
||||
#define ULOG_ASSERT(EXPR)
|
||||
#endif
|
||||
|
||||
/* ASSERT API definition */
|
||||
#if !defined(ASSERT)
|
||||
#define ASSERT ULOG_ASSERT
|
||||
#endif
|
||||
|
||||
/* compatible for elog */
|
||||
#undef assert
|
||||
#undef log_e
|
||||
#undef log_w
|
||||
#undef log_i
|
||||
#undef log_d
|
||||
#undef log_v
|
||||
#undef ELOG_LVL_ASSERT
|
||||
#undef ELOG_LVL_ERROR
|
||||
#undef ELOG_LVL_WARN
|
||||
#undef ELOG_LVL_INFO
|
||||
#undef ELOG_LVL_DEBUG
|
||||
#undef ELOG_LVL_VERBOSE
|
||||
#define assert ASSERT
|
||||
#define log_e LOG_E
|
||||
#define log_w LOG_W
|
||||
#define log_i LOG_I
|
||||
#define log_d LOG_D
|
||||
#define log_v LOG_D
|
||||
#define log_raw LOG_RAW
|
||||
#define log_hex LOG_HEX
|
||||
#define ELOG_LVL_ASSERT LOG_LVL_ASSERT
|
||||
#define ELOG_LVL_ERROR LOG_LVL_ERROR
|
||||
#define ELOG_LVL_WARN LOG_LVL_WARNING
|
||||
#define ELOG_LVL_INFO LOG_LVL_INFO
|
||||
#define ELOG_LVL_DEBUG LOG_LVL_DBG
|
||||
#define ELOG_LVL_VERBOSE LOG_LVL_DBG
|
||||
|
||||
/* setting static output log level */
|
||||
#ifndef ULOG_OUTPUT_LVL
|
||||
#define ULOG_OUTPUT_LVL LOG_LVL_DBG
|
||||
#endif
|
||||
|
||||
/* buffer size for every line's log */
|
||||
#ifndef ULOG_LINE_BUF_SIZE
|
||||
#define ULOG_LINE_BUF_SIZE 128
|
||||
#endif
|
||||
|
||||
/* output filter's tag max length */
|
||||
#ifndef ULOG_FILTER_TAG_MAX_LEN
|
||||
#define ULOG_FILTER_TAG_MAX_LEN 23
|
||||
#endif
|
||||
|
||||
/* output filter's keyword max length */
|
||||
#ifndef ULOG_FILTER_KW_MAX_LEN
|
||||
#define ULOG_FILTER_KW_MAX_LEN 15
|
||||
#endif
|
||||
|
||||
#ifndef ULOG_NEWLINE_SIGN
|
||||
#define ULOG_NEWLINE_SIGN "\r\n"
|
||||
#endif
|
||||
|
||||
#define ULOG_FRAME_MAGIC 0x10
|
||||
|
||||
/* tag's level filter */
|
||||
struct ulog_tag_lvl_filter
|
||||
{
|
||||
char tag[ULOG_FILTER_TAG_MAX_LEN + 1];
|
||||
rt_uint32_t level;
|
||||
rt_slist_t list;
|
||||
};
|
||||
typedef struct ulog_tag_lvl_filter *ulog_tag_lvl_filter_t;
|
||||
|
||||
struct ulog_frame
|
||||
{
|
||||
/* magic word is 0x10 ('lo') */
|
||||
rt_uint32_t magic:8;
|
||||
rt_uint32_t is_raw:1;
|
||||
rt_uint32_t log_len:23;
|
||||
rt_uint32_t level;
|
||||
const char *log;
|
||||
const char *tag;
|
||||
};
|
||||
typedef struct ulog_frame *ulog_frame_t;
|
||||
|
||||
struct ulog_backend
|
||||
{
|
||||
char name[RT_NAME_MAX];
|
||||
rt_bool_t support_color;
|
||||
rt_uint32_t out_level;
|
||||
void (*init) (struct ulog_backend *backend);
|
||||
void (*output)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, rt_size_t len);
|
||||
void (*flush) (struct ulog_backend *backend);
|
||||
void (*deinit)(struct ulog_backend *backend);
|
||||
/* The filter will be call before output. It will return TRUE when the filter condition is math. */
|
||||
rt_bool_t (*filter)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, rt_size_t len);
|
||||
rt_slist_t list;
|
||||
};
|
||||
typedef struct ulog_backend *ulog_backend_t;
|
||||
typedef rt_bool_t (*ulog_backend_filter_t)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, rt_size_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _ULOG_DEF_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue