偶尔会用到c语言打印log写文件本地.这里分享一个比较好的demo.
先看log效果:
main函数:
void main(int argc,char argv[])
{
int value = 2;
double dvalue = 0.5;
char* str = "string test";
hdslog("hdstestttt");
hdslog("hdstestttt:%d",value);
hdslog("hdstestttt:%f",dvalue);
hdslog("hdstestttt:%s",str);
}
log结果:
[2020.04.25 09:42:05] :log.c:118: hdstestttt
[2020.04.25 09:42:05] :log.c:119: hdstestttt:2
[2020.04.25 09:42:05] :log.c:120: hdstestttt:0.500000
[2020.04.25 09:42:05] :log.c:121: hdstestttt:string test
打印添加了时间,文件名,行号
代码在linux下运行成功过.如果要在window下运行,估计需要修改下:LJQ_DEBUG_FILE_log保存的路径,和修改头文件.
具体代码如下:
头文件log.h:
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#ifndef _LJQ_LOG_H_
#define _LJQ_LOG_H_
#define HDS_DEBUG_ON //hds open
#ifdef __cplusplus
extern "C" {
#endif
#ifdef HDS_DEBUG_ON
void C_LJQ_LOG2(const char *file, int line,const char *fmt, ...);
#define hdslog(...) C_LJQ_LOG2(__FILE__, __LINE__,__VA_ARGS__)
#else
#define hdslog(...)
#endif
#define hdslog_anyway(...) C_LJQ_LOG2(__FILE__, __LINE__,__VA_ARGS__)
#ifdef __cplusplus
}
#endif
#endif
源文件log.c:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "log.h"
#define LJQ_DEBUG_FILE_ "/home/hds/桌面/log.txt"
#define LJQ_MAX_STRING_LEN 3000
//Level类别
static int C_LJQ_Error_GetCurTime(char* strTime)
{
struct tm* tmTime = NULL;
size_t timeLen = 0;
time_t tTime = 0;
tTime = time(NULL);
tmTime = localtime(&tTime);
//timeLen = strftime(strTime, 33, "%Y(Y)%m(M)%d(D)%H(H)%M(M)%S(S)", tmTime);
timeLen = strftime(strTime, 33, "%Y.%m.%d %H:%M:%S", tmTime);
return timeLen;
}
static int C_LJQ_Error_OpenFile(int* pf)
{
char fileName[1024];
memset(fileName, 0, sizeof(fileName));
strcat(fileName,LJQ_DEBUG_FILE_);
// sprintf(fileName, LJQ_DEBUG_FILE_);
*pf = open(fileName, O_WRONLY | O_CREAT | O_APPEND, 0666);
if (*pf < 0)
{
return -1;
}
return 0;
}
static void C_LJQ_Error_Core2(const char *file, int line,const char *fmt, va_list args)
{
char str[LJQ_MAX_STRING_LEN];
int strLen = 0;
char tmpStr[64];
int tmpStrLen = 0;
int pf = 0;
//初始化
memset(str, 0, LJQ_MAX_STRING_LEN);
memset(tmpStr, 0, 64);
//加入LOG时间
tmpStrLen = C_LJQ_Error_GetCurTime(tmpStr);
tmpStrLen = sprintf(str, "[%s] ", tmpStr);
strLen = tmpStrLen;
//加入LOG发生文件
tmpStrLen = sprintf(str + strLen, ":%s:", file);
strLen += tmpStrLen;
//加入LOG发生行数
tmpStrLen = sprintf(str + strLen, "%d:\t", line);
strLen += tmpStrLen;
//加入LOG信息
tmpStrLen = vsprintf(str + strLen, fmt, args);
strLen += tmpStrLen;
//加入LOG发生行数
tmpStrLen = sprintf(str + strLen, "\n");
strLen += tmpStrLen;
//打开LOG文件
if (C_LJQ_Error_OpenFile(&pf))
{
return;
}
//写入LOG文件
printf(str);
write(pf, str, strLen);
//IC_Log_Error_WriteFile(str);
//关闭文件
close(pf);
return;
}
void C_LJQ_LOG2(const char *file, int line,const char *fmt, ...)
{
va_list args;
//调用核心的写LOG函数
va_start(args, fmt);
C_LJQ_Error_Core2(file, line,fmt, args);
va_end(args);
return;
}
void main(int argc,char argv[])
{
int value = 2;
double dvalue = 0.5;
char* str = "string test";
hdslog("hdstestttt");
hdslog("hdstestttt:%d\n",value);
hdslog("hdstestttt:%f\n",dvalue);
hdslog("hdstestttt:%s\n",str);
}