#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <sys/statfs.h>
#include <stdarg.h>
#include <stdlib.h>
#include "log.h"
//日志配置全局结构体
static LogCfgMgrSt stLogCfgMgr;
static const char *StrLogLevel[7] = {"FAIL","ERROR","WARN","INFO","TRACE","DEBUG","ALL"};
static const char *g_LSLogPath = "../log/mqttlog";
//static char *g_LSLogFileName = "../LS_log/ls_log.log";
static char *g_LSLogFileName = "../log/mqttlog/mqttlog.log";
/***************************************************************************
function: GetCurrentTime()
caller: WriteLogFile
input:
output:
Author:
time:
Description:获取系统当前时间,并以字符串的形式输入到缓冲区中
****************************************************************************/
void GetCurrentTime(char *Timebuf, int len)
{
time_t CurTime;
struct tm *pLocalTime = NULL;
CurTime = time(NULL);
pLocalTime = localtime(&CurTime);
snprintf(Timebuf, (unsigned long)len - 1, "%04d%02d%02d-%02d:%02d:%02d",
1900 + pLocalTime->tm_year,
(1 + pLocalTime->tm_mon)%100,
pLocalTime->tm_mday%100,
pLocalTime->tm_hour%100,
pLocalTime->tm_min%100,
pLocalTime->tm_sec%100);
}
void GetLogInfo(int level, char *LogContent, const char *fmt, va_list args)
{
char LogInfo[1024*32];
char Curtime[32];
if(NULL == LogContent)
{
return;
}
/*获取当前时间字符串*/
memset(Curtime, 0, sizeof(Curtime));
GetCurrentTime(Curtime, sizeof(Curtime));
memset(LogInfo, 0, sizeof(LogInfo));
vsnprintf(LogInfo, sizeof(LogInfo), fmt, args);
/*格式化组织文件内容[日志生成时间][文件名][函数名][代码行][日志等级]日志具体信息*/
snprintf(LogContent, 1024*32+100,
"[%s][%s]%s",
Curtime,
StrLogLevel[level],
//"debug",
LogInfo);
}
/***************************************************************************
function: WriteLogFile
caller: LS_WriteLog
input:
output:
Author:
time:
Description:
****************************************************************************/
int WriteLogFile(char *LogContent, int len)
{
size_t Rcnt = 0;
if(NULL == stLogCfgMgr.LogFd)
{
printf("LogFile Fd is NULL !\n");
return LS_ERR;
}
pthread_mutex_lock(&stLogCfgMgr.lock);
Rcnt = fwrite((const char*)LogContent, sizeof(char), (unsigned long)len, stLogCfgMgr.LogFd);
pthread_mutex_unlock(&stLogCfgMgr.lock);
if(Rcnt != (unsigned long)len * sizeof(char))
{
printf("fail to write log to file !\n");
return LS_ERR;
}
fflush(stLogCfgMgr.LogFd);
return LS_OK;
}
void LS_WriteLog(int level,const char *fmt, ...)
{
char LogContent[1024*32];
//首先判断日志级别,提高效率
/*if(level > stLogCfgMgr.Level)
{
return;
}*/
//组织日志具体信息
memset(LogContent, 0, sizeof(LogContent));
va_list args;
va_start(args, fmt);
GetLogInfo(level, LogContent, fmt,args);
va_end(args);
WriteLogFile(LogContent, (int)strlen(LogContent));
}
/***************************************************************************
function: CreateDir()
caller: CreateLogFile
input:
output:
Author:
time:
Description: 创建多级目录
****************************************************************************/
int CreateDir(const char *LogPath)
{
DIR *Fd_Dir = NULL;
char DirName[256] = {0};
unsigned long StrLen = 0;
int i = 0;
if(NULL == LogPath)
{
return LS_ERR;
}
memset(DirName, 0, sizeof(DirName));
StrLen = strlen(LogPath);
if(StrLen >= sizeof(DirName) - 1)
{
return LS_ERR;
}
strcpy(DirName, LogPath);
if(DirName[StrLen-1] != '/')
{
DirName[StrLen] = '/';
StrLen += 1;
}
for(i = 1; i < (int)StrLen; i++)
{
if('/' == DirName[i])
{
DirName[i] = '\0';
Fd_Dir = opendir(DirName);
printf("DirName:%s\n", DirName);
if(NULL == Fd_Dir)
{
if(-1 == mkdir(DirName, 0755))
{
perror("mkdir error");
return LS_ERR;
}
}
else
{
closedir(Fd_Dir);
}
DirName[i] = '/';
}
}
return LS_OK;
}
/***************************************************************************
function: LSCreateDir()
caller: CreateLogFile
input:
output:
Author:
time:
Description: 创建多级目录
****************************************************************************/
int LSCreateDir(const char *LogPath)
{
DIR *Fd_Dir = NULL;
if(NULL == LogPath)
{
return LS_ERR;
}
Fd_Dir = opendir(LogPath);
if(NULL == Fd_Dir)
{
if(LS_OK != CreateDir(LogPath))
{
return LS_ERR;
}
}
closedir(Fd_Dir);
return LS_OK;
}
/***************************************************************************
function: CreateLogDir()
caller: logDataInit
input:
output:
Author:
time:
Description: 创建日志目录
****************************************************************************/
int CreateLogDir(const char *LogPath)
{
if(NULL == LogPath)
{
printf("Log Path is NULL!\n");
return LS_ERR;
}
if(LS_OK != LSCreateDir(LogPath))
{
printf("creat Log Dir failed!\n");
return LS_ERR;
}
return LS_OK;
}
/***************************************************************************
function: InsertLogFileToList()
caller:
input:
output:
Author:
time:
Description:
****************************************************************************/
void InsertLogFileToList(LogFileListSt **LogListHead, char *LogFilePath)
{
LogFileListSt *ptCur = NULL;
LogFileListSt *ptNew = NULL;
if(NULL == LogListHead || NULL == LogFilePath)
{
printf("input para NULL in InsertLogFileToList\n");
return;
}
ptNew = malloc(sizeof(LogFileListSt));
if(NULL == ptNew)
{
printf("malloc failed in InsertFileToList!\n");
return;
}
memset((char *)ptNew, 0, sizeof(LogFileListSt));
memcpy(ptNew->FilePath, LogFilePath, strlen(LogFilePath));
ptNew->next = NULL;
if(NULL == *LogListHead)
{
*LogListHead = ptNew;
return;
}
ptCur = *LogListHead;
while(NULL != ptCur->next)
{
ptCur = ptCur->next;
}
ptCur->next = ptNew;
ptNew->next = NULL; // 再次赋NULL,安全第一
}
/***************************************************************************
function: ReadLogFileList()
input:
output:
Description:获取目录文件list
****************************************************************************/
int ReadLogFileList(const char *basePath)
{
DIR *dir;
struct dirent *ptr;
char LogFilePath[LOG_FILE_MAX_LEN] = {0};
if(NULL == (dir = opendir(basePath)))
{
return LS_ERR;
}
while(NULL != (ptr = readdir(dir)))
{
if(4 == ptr->d_type) //dir
{
continue;
}
else if(8 == ptr->d_type) //file
{
memset(LogFilePath, 0, sizeof(LogFilePath));
if((strlen(ptr->d_name) + strlen(basePath) + 1) < sizeof(LogFilePath))
{
strcpy(LogFilePath, basePath);
strcat(LogFilePath,"/");
strcat(LogFilePath,ptr->d_name);
}
InsertLogFileToList(&stLogCfgMgr.LogListHead,LogFilePath);
stLogCfgMgr.FileCurCnt ++;
}
else if(10 == ptr->d_type) //link file
{
continue;
}
}
closedir(dir);
return LS_OK;
}
/*************************************************************************
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
mqttClient.rar (20个子文件)
mqttClient
log
log.c 19KB
log.h 1KB
mqttlog
mqttlog.log 39KB
bin
lib_x86
libtinyxml2.so.6 126KB
libpaho-mqtt3c.so.1 205KB
libsqlite3.so.0 736KB
lib_arm
libsqlite3.so.0.8.6 3.48MB
libpaho-mqtt3c.so 503KB
libpaho-mqtt3c.so.1.0 503KB
libpaho-mqtt3c.so.1 503KB
libtinyxml.so 158KB
mqttClient 34KB
main.c 1021B
mqttClient.pro.user 23KB
mqtt
mqttClientMgr.c 15KB
mqttClientMgr.h 2KB
include
list.h 7KB
mqttClient.pro 808B
queue
queue.c 3KB
queue.h 1KB
共 20 条
- 1
TARDIS_2020
- 粉丝: 606
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0