【C语言】日志库的使用

转至Github上一个开源的日志框架:https://github.com/vbirds/logRecord

字符编码的相关问题的解决方案:https://blog.csdn.net/xionglifei2014/article/details/80284686

logrecord.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 "logrecord.h"

#define LR_DEBUG_FILE_	"lrdebug.log"
#define LR_MAX_STRING_LEN 		10240

/*Level类别*/
#define LR_NO_LOG_LEVEL			0
#define LR_DEBUG_LEVEL			1
#define LR_INFO_LEVEL			2
#define LR_WARNING_LEVEL		3
#define LR_ERROR_LEVEL			4

int  LogLevel[5] = {LR_NO_LOG_LEVEL, LR_DEBUG_LEVEL, LR_INFO_LEVEL, LR_WARNING_LEVEL, LR_ERROR_LEVEL};

/*Level的名称*/
char LRLevelName[5][10] = {"NOLOG", "DEBUG", "INFO", "WARNING", "ERROR"};

static int LR_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 LR_Error_OpenFile(int* pf)
{
	char	fileName[1024];
	
	memset(fileName, 0, sizeof(fileName));
#ifdef WIN32
	sprintf(fileName, "c:\\lrlog\\%s",LR_DEBUG_FILE_);
#else
	sprintf(fileName, "%s/log/%s", getenv("HOME"), LR_DEBUG_FILE_);
#endif
    
    *pf = open(fileName, O_WRONLY|O_CREAT|O_APPEND, 0666);
    if(*pf < 0)
    {
        return -1;
    }
	
	return 0;
}

static void LR_Error_Core(const char *file, int line, int level, int status, const char *fmt, va_list args)
{
    char str[LR_MAX_STRING_LEN];
    int	 strLen = 0;
    char tmpStr[64];
    int	 tmpStrLen = 0;
    int  pf = 0;
    
    /*初始化*/
    memset(str, 0, LR_MAX_STRING_LEN);
    memset(tmpStr, 0, 64);
    
    /*加入LOG时间*/
    tmpStrLen = LR_Error_GetCurTime(tmpStr);
    tmpStrLen = sprintf(str, "[%s] ", tmpStr);
    strLen = tmpStrLen;

    /*加入LOG等级*/
    tmpStrLen = sprintf(str+strLen, "[%s] ", LRLevelName[level]);
    strLen += tmpStrLen;
    
    /*加入LOG状态*/
    if (status != 0) 
    {
        tmpStrLen = sprintf(str+strLen, "[ERRNO is %d] ", status);
    }
    else
    {
    	tmpStrLen = sprintf(str+strLen, "[SUCCESS] ");
    }
    strLen += tmpStrLen;

    /*加入LOG信息*/
    tmpStrLen = vsprintf(str+strLen, fmt, args);
    strLen += tmpStrLen;

    /*加入LOG发生文件*/
    tmpStrLen = sprintf(str+strLen, " [%s]", file);
    strLen += tmpStrLen;

    /*加入LOG发生行数*/
    tmpStrLen = sprintf(str+strLen, " [%d]\n", line);
    strLen += tmpStrLen;
    
    /*打开LOG文件*/
    if(LR_Error_OpenFile(&pf))
	{
		return ;
	}
	
    /*写入LOG文件*/
    write(pf, str, strLen);
    /*LR_Log_Error_WriteFile(str);*/
    
    /*关闭文件*/
    close(pf);
    
    return ;
}

void LR_LOG(const char *file, int line, int level, int status, const char *fmt, ...)
{
    va_list args;
	
	/*判断是否需要写LOG*/
	/*if(level!=LR_DEBUG_LEVEL && level!=LR_INFO_LEVEL && level!=LR_WARNING_LEVEL && level!=LR_ERROR_LEVEL)*/
	if(level == LR_NO_LOG_LEVEL)
	{
		return ;
	}
	
	/*调用核心的写LOG函数*/
    va_start(args, fmt);
    LR_Error_Core(file, line, level, status, fmt, args);
    va_end(args);
    
    return ;
}

 

logrecord.h:


#ifndef _LOG_RECORD_H_
#define _LOG_RECORD_H_

/*
#define LR_NO_LOG_LEVEL			0
#define LR_DEBUG_LEVEL			1
#define LR_INFO_LEVEL			2
#define LR_WARNING_LEVEL		3
#define LR_ERROR_LEVEL			4
*/

/************************************************************************/
/* 
const char *file:文件名称
int line:文件行号
int level:错误级别
		0 -- 没有日志
		1 -- debug级别
		2 -- info级别
		3 -- warning级别
		4 -- err级别
int status:错误码
const char *fmt:可变参数
*/
/************************************************************************/

/*实际使用的Level*/
extern int  LogLevel[5];
void LR_LOG(const char *file, int line, int level, int status, const char *fmt, ...);

#endif

 

test.c:


#include <stdio.h>
#include "logrecord.h"

int main(void)
{
	int i = -2;

	if (i < 0 )
	{
		LR_LOG(__FILE__, __LINE__, 4, -1, "i 小于0");
	}

	return 0;
}

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值