头文件
#pragma once
#include <Windows.h>
#include <fstream>
#include<direct.h>
using namespace std;
#define FATAL_LOG 0x0000
#define ERROR_LOG 0x0001
#define TRACE_LOG 0x0010
#define INFOR_LOG 0x0100
#define DEBUG_LOG 0x1000
#define MAX_NAME (256)
#define LOGSYNCTIME (1000)
void SaveLog(char *funName, int returnValue, char *Msg);
void setTraceLevel(int type);
void log_data(char* fname,char* errmsg,char *errvalue);
void log_fatal(char* fname,char* errmsg,char * format,...);
void log_error(char* fname,char* errmsg,char * format,...);
void log_infor(char* fname,char* errmsg,char * format,...);
void log_trace(char* fname,char* errmsg,char * format,...);
void log_debug(char* fname,char* errmsg,char * format,...);
原文件:
#include "stdafx.h"
#include "log.h"
long m_tracelevel = INFOR_LOG | ERROR_LOG | DEBUG_LOG | TRACE_LOG;
#define DATAMAXLEN 10240
#define FUNMAXLEN 256
void setTraceLevel(int type)
{
m_tracelevel = type;
m_tracelevel |= (INFOR_LOG | ERROR_LOG);
}
void SaveLog(char *funName, int returnValue, char *Msg)
{
char sFileName[FUNMAXLEN];
char sLog[DATAMAXLEN + FUNMAXLEN*2];
SYSTEMTIME CurTime;
GetLocalTime(&CurTime);
_mkdir("D:\\driver");
char temp[MAX_PATH] = {};
sprintf(temp,"D:\\driver\\%04d%02d",CurTime.wYear,CurTime.wMonth);
_mkdir(temp);
sprintf(sFileName,"%s\\log%04d%02d%02d.txt",temp,CurTime.wYear,CurTime.wMonth,CurTime.wDay,CurTime.wHour);
sprintf(sLog,"[%4d-%02d-%02d %02d:%02d:%02d:%003d][funName:%s][ret:%d][msg:%s]",
CurTime.wYear,CurTime.wMonth,CurTime.wDay,CurTime.wHour,CurTime.wMinute,CurTime.wSecond,CurTime.wMilliseconds,funName,returnValue,Msg);
ofstream f(sFileName,ios::out|ios::app);
if (f.good())
{
f<<sLog<<endl;
f.close();
}
}
void log_data(char* fname,char* errmsg,char *errvalue)
{
char sFileName[FUNMAXLEN];
char sLog[DATAMAXLEN + FUNMAXLEN*2];
SYSTEMTIME CurTime;
GetLocalTime(&CurTime);
_mkdir("D:\\driver");
char temp[MAX_PATH] = {};
sprintf(temp,"D:\\driver\\%04d%02d",CurTime.wYear,CurTime.wMonth);
_mkdir(temp);
sprintf(sFileName,"%s\\log%04d%02d%02d.txt",temp,CurTime.wYear,CurTime.wMonth,CurTime.wDay,CurTime.wHour);
sprintf(sLog,"[%4d-%02d-%02d %02d:%02d:%02d:%003d][%s][%s][%s]",
CurTime.wYear,CurTime.wMonth,CurTime.wDay,CurTime.wHour,CurTime.wMinute,CurTime.wSecond,CurTime.wMilliseconds,fname,errmsg,errvalue);
ofstream f(sFileName,ios::out|ios::app);
if (f.good())
{
f<<sLog<<endl;
f.close();
}
}
void log_infor(char* fname,char* errmsg,char *format,...)
{
if(m_tracelevel & INFOR_LOG)
{
char tmpbuf[FUNMAXLEN];
char errvalue[DATAMAXLEN + FUNMAXLEN*2];
sprintf(tmpbuf,"infor in %s",fname);
memset(errvalue,0x0,sizeof(errvalue));
va_list ap;
va_start(ap,format);
_vsnprintf(errvalue,DATAMAXLEN - 1,format,ap);
va_end(ap);
log_data(tmpbuf,errmsg,errvalue);
}
}
void log_error(char* fname,char* errmsg,char *format,...)
{
if(m_tracelevel & ERROR_LOG)
{
char tmpbuf[FUNMAXLEN];
char errvalue[DATAMAXLEN + FUNMAXLEN*2];
sprintf(tmpbuf,"error in %s",fname);
memset(errvalue,0x0,sizeof(errvalue));
va_list ap;
va_start(ap,format);
_vsnprintf(errvalue,DATAMAXLEN - 1,format,ap);
va_end(ap);
log_data(tmpbuf,errmsg,errvalue);
}
}
void log_debug(char* fname,char* errmsg,char *format,...)
{
if(m_tracelevel & DEBUG_LOG)
{
char tmpbuf[FUNMAXLEN];
char errvalue[DATAMAXLEN + FUNMAXLEN*2];
sprintf(tmpbuf,"debug in %s",fname);
memset(errvalue,0x0,sizeof(errvalue));
va_list ap;
va_start(ap,format);
_vsnprintf(errvalue,DATAMAXLEN - 1,format,ap);
va_end(ap);
log_data(tmpbuf,errmsg,errvalue);
}
}
void log_trace(char* fname,char* errmsg,char *format,...)
{
if(m_tracelevel & TRACE_LOG)
{
char tmpbuf[FUNMAXLEN];
char errvalue[DATAMAXLEN + FUNMAXLEN*2];
sprintf(tmpbuf,"trace in %s",fname);
memset(errvalue,0x0,sizeof(errvalue));
va_list ap;
va_start(ap,format);
_vsnprintf(errvalue,DATAMAXLEN - 1,format,ap);
va_end(ap);
log_data(tmpbuf,errmsg,errvalue);
}
}
void log_fatal(char* fname,char* errmsg,char *format,...)
{
char tmpbuf[FUNMAXLEN];
char errvalue[DATAMAXLEN + FUNMAXLEN*2];
sprintf(tmpbuf,"fatal error in %s",fname);
memset(errvalue,0x0,sizeof(errvalue));
va_list ap;
va_start(ap,format);
_vsnprintf(errvalue,DATAMAXLEN - 1,format,ap);
va_end(ap);
log_data(tmpbuf,errmsg,errvalue);
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。