当前位置:文档之家› 任务五实现步骤

任务五实现步骤

//Level的名称
char ICLevelName[5][10] = { "NOLOG", "DEBUG", "INFO", "WARNING", "ERROR" };
static int MyCAST_GetCurTime(char* strTime)
{
struct tm*tmTime = NULL;
int fd;
time_t now;
time(&now);
struct tm *local;
local = localtime(&now);
printf("%04d-%02d-%02d %02d:%02d:%02d %s\n", local->tm_year+1900, local->tm_mon,
struct stat buf;
//初始化
memset(str, 0, ITCAST_MAX_STRING_LEN);
memset(tmpStr, 0, 64);
//加入LOG发生行数
tmpStrLen = sprintf(str + strLen, " [%d]", line);
strLen += tmpStrLen;
size_ttimeLen = 0;
time_ttTime = 0;
tTime = time(RT_NULL);
tmTime = localtime(&tTime);
timeLen = strftime(strTime, 33, "%Y.%m.%d %H:%M:%S", tmTime);
return timeLen;
void MyCAST_LOG(int line, const char *fmt);
#define ITCAST_DEBUG_FILE_"/dir_test/log.csv"
#define ITCAST_MAX_STRING_LEN1024
//Level类别
#define IC_NO_LOG_LEVEL0
任务五实现步骤:
1、准备环境:
Qemu+winimage
2、程序流程:
创建写日志接口
第一次日志是存储打印信息,所以在ksevice.c中的rt_kprintf里添加写入日志
3、效果
4、C源码
#include <rtthread.h>
#include <time.h>
#include <dfs_posix.h>
{
rt_kprintf("open file for append write failed\n");
return;
}
size = write(fd, buffer, sizeof(buffer));
if (size != sizeof(buffer))
{
rt_kprintf("append write data failed\n");
//加入LOG时间
//tmpStrLen = MyCAST_GetCurTime(tmpStr);
//tmpStrLen = sprintf(str, "[%s] ", tmpStr);
//strLen = tmpStrLen;
//加入LOG信息
tmpStrLen = sprintf(str,"%s",fmt);
local->tm_mday, local->tm_hour, local->tm_min, local->tm_sec,
wzLog);
/*打开/text.txt作写入,如果该文件不存在则建立该文件*/
fd = open("log.csv",O_WRONLY | O_CREAT);
if (fd < 0)
local->tm_mday, local->tm_hour, local->tm_min, local->tm_sec,
wzLog);
sprintf(buffer,"%04d-%02d-%02d %02d:%02d:%02d %s\n", local->tm_year+1900, local->tm_mon,
close(fd);
return;
}
/*关闭文件*/
close(fd);
//syslog(LOGnt rv = -3;
MyCAST_LOG(__LINE__,"func cltSocketRev2: (buflen == NULL) [%d]");
//system("pause");
#endif
}
/*导出到msh命令列表中*/
MSH_CMD_EXPORT(addlog_sample, add log.csv to SD);
#define IC_DEBUG_LEVEL1
#define IC_INFO_LEVEL2
#define IC_WARNING_LEVEL3
#define IC_ERROR_LEVEL4
int LogLevel[5] = { IC_NO_LOG_LEVEL, IC_DEBUG_LEVEL, IC_INFO_LEVEL, IC_WARNING_LEVEL, IC_ERROR_LEVEL };
/* Move to end of the file to append data */
res = lseek(pf,buf.st_size,SEEK_END);
//写入LOG文件
size = write(pf, str, strLen);
//关闭文件
close(pf);
return;
}
void MyCAST_LOG(int line,const char *fmt)
{
MyCAST_Core(line,fmt);
return;
}
static int addlog_sample(int argc, char** argv)
{
#if 0//test
char wzLog[1024] = {0};
char buffer[1024] = {0};
int size = 0;
#include "sys/types.h"
#include <sys/time.h>
/************************************************************************/
//实际使用的Level
extern int LogLevel[5];
strLen += tmpStrLen;
//打开LOG文件
pf =open(ITCAST_DEBUG_FILE_, O_WRONLY | O_CREAT);
if (pf<0)
{
//rt_kprintf("open file for append write failed\n");
return;
}
stat("/log.csv", &buf);
}
static void MyCAST_Core(int line,const char *fmt)
{
char str[ITCAST_MAX_STRING_LEN];
intstrLen = 0;
char tmpStr[64];
inttmpStrLen = 0;
int pf;
int size;
off_t res;
相关主题