现在的位置: 首页 > 综合 > 正文

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

2014年04月13日 ⁄ 综合 ⁄ 共 2044字 ⁄ 字号 评论关闭
 简单实用的日志系统,打开文件方式后面用 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);
}

【上篇】
【下篇】

抱歉!评论已关闭.