#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; } } }
实时log记录管理
最新推荐文章于 2024-05-23 09:50:59 发布