实时log记录管理

 
#include <stdio.h>
#include <stdlib.h>
#include <list>
#include <stdarg.h>
#include "ext-log.h"

using namespace std;

//typedef void (*ext_ringlog_func_t)(int, const char*);

typedef struct pair_num{
    int level;
    char* format;
}pair_num;



typedef struct ext_ringlog{
    unsigned max_lognum;
    unsigned perlog_max;
    ext_ringlog_func_t func;
    list<pair_num*> loglist;
}ext_ringlog;

typedef list<pair_num*>::iterator iter;

//define buffer.
ext_ringlog* ext_ringlog_init(unsigned count, unsigned per_max_size){
        ext_ringlog *buff=new ext_ringlog;
        if(buff!=NULL&&per_max_size>0){
            buff->max_lognum=count;
            buff->perlog_max=per_max_size;
            printf("the define of buffer is successed\n");//show the log
        }
        else{
            printf("the buff is NULL\n");//show the log
        }
        return buff;
}
//release the buffer of log
void ext_ringlog_done(ext_ringlog* ringlog){
    if(ringlog!=NULL){
        iter firNum=ringlog->loglist.begin();
        while(firNum!=ringlog->loglist.end()){
            delete []((*firNum)->format);
            delete *firNum;
            ++firNum;
        }
        ringlog->loglist.erase(ringlog->loglist.begin(),ringlog->loglist.end());
        delete ringlog;
    }
}
//output log to file
void ext_ringlog_regist_callback(ext_ringlog *ringlog, ext_ringlog_func_t f){
    if(ringlog!=NULL && f!=NULL){
        ringlog->func = f;
    }

}
//recall function of file of user
void ext_ringlog_log(ext_ringlog* ringlog,int level,const char* format, ...){

    if(ringlog!=NULL){
        iter it1;
        unsigned log_longth=ringlog->perlog_max;
        char *lon_buff=new char[log_longth];
        va_list args;
        va_start(args,format);
        //if your longth of log is longer than the rule,
        //the longth of log_ongth can input to the buffer
        vsnprintf(lon_buff,log_longth,format,args);
        perror(lon_buff);//show status of log information
        va_end(args);

        pair_num *le_fo=new pair_num;
        le_fo->level=level;
        le_fo->format=lon_buff;

        if(ringlog->loglist.size()<ringlog->max_lognum){
            ringlog->loglist.push_back(le_fo);
        }
        else{
            it1=ringlog->loglist.begin();
            delete []((*it1)->format);
            delete *it1;
            ringlog->loglist.erase(it1);
            ringlog->loglist.push_back(le_fo);
        }
    }
}
//output log to file
void ext_ringlog_flush(ext_ringlog* ringlog){
    if(ringlog!=NULL){
        // list iterator
        list<pair_num*>::iterator it;
        pair_num *mid_it;
        it=ringlog->loglist.begin();
        while(it!=ringlog->loglist.end()){
            mid_it=*it;
            ringlog->func(mid_it->level,mid_it->format);
            printf("input file success\n");//show the log
            ++it;
        }
    }

}

 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值