2023-06-17 10:26:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2023-06-16 murmur the first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <..\rt-thread\components\utilities\ulog\backend\ulog_be.h>
|
2023-08-29 11:29:58 +00:00
|
|
|
#include "usrcfg.h"
|
2023-06-17 10:26:40 +00:00
|
|
|
/*
|
|
|
|
* 后端注册表
|
|
|
|
*/
|
|
|
|
struct _log_file
|
|
|
|
{
|
2023-08-27 04:31:54 +00:00
|
|
|
char *name; //文件名
|
2023-06-17 10:26:40 +00:00
|
|
|
ulog_backend_t backend;
|
|
|
|
struct ulog_file_be *file_be;
|
|
|
|
const char *dir_path; //保存路径
|
|
|
|
rt_size_t max_num; //保存最大文件数量
|
|
|
|
rt_size_t max_size; //保存最大文件大小
|
|
|
|
rt_size_t buf_size; //文件保存缓存大小
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
* 文件后端标识
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
console_id,
|
|
|
|
sys_id,
|
|
|
|
motion_id,
|
|
|
|
}ulog_file_be_name;
|
|
|
|
|
2023-08-29 11:29:58 +00:00
|
|
|
#define ROOT_PATH ROOT_PATH_DEBUG //设置保存路径
|
2023-09-03 08:54:23 +00:00
|
|
|
#define FILE_SIZE 100*1024 //设置单个文件大小
|
2023-09-01 08:59:49 +00:00
|
|
|
#define BUFF_SIZE 1*1024 //设备缓存区大小
|
2023-06-17 10:26:40 +00:00
|
|
|
|
|
|
|
static struct ulog_backend sys_log_backend;
|
|
|
|
static struct ulog_file_be sys_log_file;
|
2023-09-03 08:54:23 +00:00
|
|
|
|
2023-08-27 04:31:54 +00:00
|
|
|
extern char *getTimestmp(char *str);
|
2023-06-17 10:26:40 +00:00
|
|
|
|
|
|
|
static struct _log_file table[] =
|
|
|
|
{
|
2023-06-19 07:56:45 +00:00
|
|
|
{"motion" ,&sys_log_backend,&sys_log_file,ROOT_PATH,5,FILE_SIZE,BUFF_SIZE},
|
2023-08-27 04:31:54 +00:00
|
|
|
{"sys" ,&sys_log_backend,&sys_log_file,ROOT_PATH,10,FILE_SIZE,BUFF_SIZE},
|
2023-06-17 10:26:40 +00:00
|
|
|
};
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
2023-09-01 08:59:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief 系统日志文件后端滤波器设置.
|
|
|
|
* @param None.
|
|
|
|
* @retval The filter will be call before output. It will return TRUE when the filter condition is math.
|
|
|
|
* @note None.
|
|
|
|
*/
|
|
|
|
static rt_bool_t sys_log_file_backend_filter(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw,
|
|
|
|
const char *log, rt_size_t len)
|
|
|
|
{
|
|
|
|
//设置滤波规则,可以自己编写代码实现不同滤波规则
|
|
|
|
// if (rt_strncmp(tag,"MOVE", sizeof(MOTION_TAG)) == 0)//排除带有"MOVE"标签输出
|
|
|
|
// return RT_FALSE;
|
|
|
|
// else
|
|
|
|
// return RT_TRUE;
|
|
|
|
|
|
|
|
if (level < LOG_LVL_DBG)//"DEBUG"
|
|
|
|
return RT_TRUE;
|
|
|
|
else
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
|
|
|
|
2023-06-17 10:26:40 +00:00
|
|
|
/**
|
|
|
|
* @brief 系统日志文件后端初始化.
|
|
|
|
* @param None.
|
|
|
|
* @retval None.
|
|
|
|
* @note None.
|
|
|
|
*/
|
|
|
|
void sys_log_file_backend_init(void)
|
|
|
|
{
|
2023-08-31 08:12:44 +00:00
|
|
|
static struct ulog_file_be *file_be = &sys_log_file;
|
2023-06-17 10:26:40 +00:00
|
|
|
uint8_t id = sys_id;
|
|
|
|
file_be->parent = sys_log_backend;
|
|
|
|
|
2023-09-03 08:54:23 +00:00
|
|
|
char logfilename[30];// = "2023-08-27/2023-08-27#11-";
|
|
|
|
char tmstr[50];//定义时间前缀
|
2023-09-01 08:59:49 +00:00
|
|
|
|
2023-09-03 08:54:23 +00:00
|
|
|
getTimestmp(tmstr);// 2023-08-27 11-44-42-65
|
|
|
|
tmstr[10]='#';//2023-08-27#11-
|
2023-08-27 04:31:54 +00:00
|
|
|
tmstr[14]='\0';
|
2023-09-03 08:54:23 +00:00
|
|
|
|
|
|
|
//以天为独立文件夹
|
|
|
|
strncpy(logfilename,tmstr,10);
|
|
|
|
char dir[30]=ROOT_PATH;
|
|
|
|
strcat(dir,"/");
|
|
|
|
strcat(dir,logfilename);
|
|
|
|
mkdir(dir, 0);
|
|
|
|
strcat(logfilename,"/");
|
|
|
|
|
|
|
|
//总路径不能太长
|
2023-09-03 10:12:55 +00:00
|
|
|
strcat(logfilename,tmstr+11);//sd/debug/2023-09-03/11-
|
2023-09-03 08:54:23 +00:00
|
|
|
|
2023-09-03 10:12:55 +00:00
|
|
|
// ulog_file_backend_deinit(file_be);
|
2023-08-27 04:31:54 +00:00
|
|
|
|
2023-06-17 10:26:40 +00:00
|
|
|
ulog_file_backend_init( file_be,
|
2023-09-03 08:54:23 +00:00
|
|
|
strcat(logfilename,table[id].name),
|
2023-06-17 10:26:40 +00:00
|
|
|
table[id].dir_path,
|
|
|
|
table[id].max_num,
|
|
|
|
table[id].max_size,
|
|
|
|
table[id].buf_size);
|
|
|
|
|
|
|
|
ulog_file_backend_enable(file_be); //必须使能才能有效
|
2023-09-01 08:59:49 +00:00
|
|
|
|
|
|
|
//新增代码
|
|
|
|
ulog_backend_filter_t filter = sys_log_file_backend_filter;
|
2023-09-03 08:54:23 +00:00
|
|
|
// ulog_backend_set_filter(&file_be->parent,filter);
|
2023-06-17 10:26:40 +00:00
|
|
|
}
|
2023-08-27 04:31:54 +00:00
|
|
|
|
2023-08-31 08:12:44 +00:00
|
|
|
void updateLogFileBackend()
|
|
|
|
{
|
|
|
|
ulog_file_backend_deinit(&sys_log_file);
|
|
|
|
sys_log_file_backend_init();
|
|
|
|
}
|
2023-08-27 04:31:54 +00:00
|
|
|
|
2023-06-17 10:26:40 +00:00
|
|
|
MSH_CMD_EXPORT(sys_log_file_backend_init,log2file);
|
2023-09-01 08:59:49 +00:00
|
|
|
INIT_APP_EXPORT(sys_log_file_backend_init);
|