[原创]linux c项目log日志系统

 简单实用的日志系统,打开文件方式后面用 w时,每次系统运行会清空以前的,部分实现在前一篇文掌的cofig里面

log.h
/*
* File:   log.h:日志系统的头文件
* Author: netpet
* Flower net server
* 本程序是为一体化web server产品专用设计,具有部分代码为具体产品优化而不代表普遍通用性的特性
* 程序在linux 2.46下调试通过,编辑工具netbeans 6.1 for c
* 联系方式:Email:netpetboy@163.com QQ:51977431
* Created on 2008年5月26日, 下午4:18
*/

#include "config.h"


#ifndef _LOG_H
#define    _LOG_H

#ifdef    __cplusplus
extern "C" {
#endif

   
#define Error(fmt, ...) /
             vlog_debug(config->log.log_log, "[%s %d] 错误: "fmt, __FILE__, __LINE__, __VA_ARGS__)

#define Warning(fmt, ...) /
             vlog_debug(config->log.log_log, "[%s %d] 警告: "fmt, __FILE__, __LINE__, __VA_ARGS__)
#define Record(fmt, ...) /
             vlog_debug(config->log.access_log, "[%s %d] 记录: "fmt, __FILE__, __LINE__, __VA_ARGS__)

#define Debug(fmt, ...) /
             log_debug(__FILE__, __LINE__, fmt, __VA_ARGS__)
   

extern void log_debug(const char *file, const int line, const char *format,...);
extern void vlog_debug(FILE *file, const char *format,...);

#ifdef    __cplusplus
}
#endif

#endif    /* _LOG_H */

log.c
/*
* File:   log.c:日志系统的实现
* Author: netpet
* Flower net server
* 本程序是为一体化web server产品专用设计,具有部分代码为具体产品优化而不代表普遍通用性的特性
* 程序在linux 2.46下调试通过,编辑工具netbeans 6.1 for c
* 联系方式:Email:netpetboy@163.com QQ:51977431
* Created on 2008年5月26日, 下午4:18
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include "config.h"
/*
*功能:调试输出函数
*/
void log_debug(const char *file, const int line, const char *format,...)
{
    if(!config->log.DebugModel)
        return;   
    char new_format[2048];
   
    va_list va;
    va_start(va, format);
   
    snprintf(new_format, 8191 , "/n[%s] | [%s %d] %s",ServerTime(), file, line, format);

    if(vfprintf(config->log.debug_log, new_format, va) < 0)
        fprintf(stderr, "警告. [%s %d] %s/n", __FILE__, __LINE__, strerror(errno));

    va_end(va);
}
/*
*功能:警告或者错误输出函数
*
*/
void vlog_debug(FILE *file, const char *format,...)
{

    if(!file)
        return;

    static char new_format[2048];
   
    va_list va;
    va_start(va, format);
   
    snprintf(new_format,8191 , "/n[%s] | %s",ServerTime(), format);

    if(vfprintf(file, new_format, va) < 0)
        fprintf(stderr, "警告 [%s %d] %s/n", __FILE__, __LINE__, strerror(errno));

    va_end(va);
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值