头文件
成都创新互联公司:2013年至今为各行业开拓出企业自己的“网站建设”服务,为近千家公司企业提供了专业的网站设计、网站建设、网页设计和网站推广服务, 按需网站制作由设计师亲自精心设计,设计的效果完全按照客户的要求,并适当的提出合理的建议,拥有的视觉效果,策划师分析客户的同行竞争对手,根据客户的实际情况给出合理的网站构架,制作客户同行业具有领先地位的。#pragma once
#include
#include
#include
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< 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< 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); } 另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
名称栏目:c++日志接口等级-创新互联
本文网址:http://kswsj.cn/article/ijecj.html