2 Star 5 Fork 1

icedbeer/LinuxMediaCaptureRtsp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
log.cpp 3.66 KB
一键复制 编辑 原始数据 按行查看 历史
icedbeer 提交于 2016-11-30 23:35 . commit source code
#include "log.h"
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
CDebugLog *CDebugLog::pDebugLog = NULL;
CDebugLog::CDebugLog(DEBUGLEVEL level)
{
pthread_mutex_init(&m_Mutex, NULL);
filename = NULL;
debugLevel = level;
fp = NULL;
SetLogFileName("camera.log");
}
int CDebugLog::SetLogFileName(const char *logFileName)
{
if(NULL == logFileName)
return -1;
if(NULL != filename)
{
free(filename);
filename = NULL;
}
char *tmpBuf = get_current_dir_name();
filename = (char *)malloc(strlen(logFileName) + strlen(tmpBuf) + 1);
memset(filename, 0, strlen(logFileName) + strlen(tmpBuf) + 1);
memcpy(filename, tmpBuf, strlen(tmpBuf));
memcpy(filename, "/", 1);
memcpy(filename + strlen(tmpBuf) + 1, logFileName, strlen(logFileName));
if(access(filename, F_OK) < 0)
{
if(mkdir(filename, 0777 ) < 0 )
return -1;
}
memset(filename, 0, strlen(logFileName) + 1);
memcpy(filename, logFileName, strlen(logFileName));
if(NULL != fp)
{
fclose(fp);
fp = NULL;
}
fp = fopen(filename, "a+");
if(fp && fseek(fp, 0, SEEK_END)>-1)
{
if(ftell(fp) >= 100*1024*1024) //文件大于100M时删除此文件
{
fclose(fp);
fp = NULL;
fp = fopen(filename, "w+");
DebugLog(debugLevel, "The log file is too big, clear it!");
}
DebugLog(debugLevel, "The Player version is 2014-04-02");
}
return 0;
}
CDebugLog *CDebugLog::Instance()
{
if (NULL == pDebugLog)
pDebugLog = new CDebugLog(EDEBUG_LEVEL_MESSAGE);
return pDebugLog;
}
CDebugLog::~CDebugLog()
{
if(0 == pthread_mutex_trylock(&m_Mutex))
{
pthread_mutex_lock(&m_Mutex);
if(NULL != fp)
{
fclose(fp);
fp = NULL;
}
if(NULL != filename)
{
free(filename);
filename = NULL;
}
pthread_mutex_unlock(&m_Mutex);
pthread_mutex_destroy(&m_Mutex);
}
}
void CDebugLog::DebugLog(DEBUGLEVEL level, const char *arg, ...)
{
pthread_mutex_lock(&m_Mutex);
memset(time_buf, 0, LOG_STR_MAX_LEN);
memset(str_buf, 0, LOG_STR_MAX_LEN);
time_t tm;
time(&tm);
struct tm *timeinfo = localtime(&tm);
strcpy(str_buf, asctime(timeinfo));
strcpy(str_buf + strlen(str_buf) - 1, " : ");
va_list list;
va_start(list, arg);
vsprintf(time_buf, arg, list);
va_end(list);
strcpy(str_buf + strlen(str_buf) - 1, time_buf);
if(fp && debugLevel>=level) //写入日志文件
{
printf("%s\n", str_buf);
fflush(stdout);
strcat(str_buf, "\n");
fwrite(str_buf, 1, strlen(str_buf), fp);
fflush(fp);
}
pthread_mutex_unlock(&m_Mutex);
}
void CDebugLog::DebugLog(const char *arg, ...)
{
pthread_mutex_lock(&m_Mutex);
memset(time_buf, 0, LOG_STR_MAX_LEN);
memset(str_buf, 0, LOG_STR_MAX_LEN);
time_t tm;
time(&tm);
struct tm *timeinfo = localtime(&tm);
strcpy(str_buf, asctime(timeinfo));
strcpy(str_buf + strlen(str_buf) - 1, " : ");
va_list list;
va_start(list, arg);
vsprintf(time_buf, arg, list);
va_end(list);
strcpy(str_buf + strlen(str_buf) - 1, time_buf);
if(fp) //写入日志文件
{
printf("%s\n", str_buf);
fflush(stdout);
strcat(str_buf, "\n");
fwrite(str_buf, 1, strlen(str_buf), fp);
fflush(fp);
}
pthread_mutex_unlock(&m_Mutex);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/icedbeer/LinuxMediaCaptureRtsp.git
git@gitee.com:icedbeer/LinuxMediaCaptureRtsp.git
icedbeer
LinuxMediaCaptureRtsp
LinuxMediaCaptureRtsp
master

搜索帮助