简单的写日志工具类LogUtil
2014-01-08 23:23
375 查看
1、LogUtil.h
2、LogUtil.cpp
#ifndef __LOG_UTIL_H_ #define __LOG_UTIL_H_ #include <stdio.h> #include <stdlib.h> #include <iostream> #include <sstream> #include <fstream> #include <time.h> #include <windows.h> using namespace std; #define PATH_LOG_FILE ".\\LogFile.txt" class CLogUtil { public: typedef enum LOGTYPE { LOG_SYSTEM=0, LOG_CRASH, LOG_ERROR, LOG_WARNING, LOG_INFO, LOG_DEBUG } LOGTYPE; public: CLogUtil(const char *logname = PATH_LOG_FILE); ~CLogUtil(); public: void begin(); void clear(); void add_time(); void record(LOGTYPE lgtype, const char *filename,const char *module,const char *info); void add_separator(bool bHasTitle, const char *title_name); void end(); public: std::ofstream& operator << (const char *txt); std::ofstream& operator << (const unsigned *txt); std::ofstream& operator << (int txt); private: std::ofstream o; std::string log_name; std::string module_name; }; #endif // __FCC_LOG_H__
2、LogUtil.cpp
#include "stdafx.h" #include"logutil.h" #define TIME_LENGTH 50 /*class CLogUtil constructor*/ CLogUtil::CLogUtil(const char *logname) : o(logname, std::ios_base::out | std::ios_base::trunc) { log_name = logname; } /*class CLogUtil destructor*/ CLogUtil::~CLogUtil() { if (o.is_open()) o.close(); } void CLogUtil::begin() { if (!o.is_open()) { o.open(log_name.c_str(), std::ios_base::app|std::ios_base::out); } } /*clear log*/ void CLogUtil::clear() { o.flush(); } /*add time in the log*/ void CLogUtil::add_time() { if (!o.is_open()) return; SYSTEMTIME stCurTime; GetLocalTime(&stCurTime); char tmp_time[TIME_LENGTH] = {0}; char strTime[TIME_LENGTH] = {0}; sprintf(tmp_time, "%d-%d-%d ", stCurTime.wYear,stCurTime.wMonth,stCurTime.wDay); sprintf(strTime, "%d:%d:%d", stCurTime.wHour,stCurTime.wMinute,stCurTime.wSecond); strcat(tmp_time,strTime); o <<"Log Time: "<<tmp_time<<std::endl; } void CLogUtil::record(LOGTYPE lgtype, const char *filename,const char *module,const char *info) { if (!o.is_open()) { return; } module_name = module; o <<"/********************************************************************/"<<std::endl; add_time(); o <<"Log Level: "; switch (lgtype) { case LOG_SYSTEM: o << "SYSTEM"; break; case LOG_CRASH: o << "CRASH"; break; case LOG_ERROR: o << "ERROR"; break; case LOG_WARNING: o << "WARNING"; break; case LOG_INFO: o << "INFO"; break; case LOG_DEBUG: o << "DEBUG"; break; default: o << "OTHER"; break; }; o << std::endl; o << "Module: "<<filename<<" ["<<module_name<<"]"<<std::endl; o << "Log Event: "<<info << std::endl; o << "Result: "<<std::endl; o << "Description:"<<std::endl; o << "========================================================================"<<std::endl; } void CLogUtil::add_separator(bool bHasTitle, const char *title_name) { } void CLogUtil::end() { if (o.is_open()) o.close(); } std::ofstream& CLogUtil::operator << (const char *txt) { if (o.is_open()) o << txt; return o; } std::ofstream& CLogUtil::operator << (const unsigned *txt) { if (o.is_open()) o << txt; return o; } std::ofstream& CLogUtil::operator << (int txt) { if (o.is_open()) o << txt; return o; }
相关文章推荐
- 公钥安全机制与宫爆鸡丁的故事
- Java中线程的生命周期
- C#中的委托和事件
- PHP基础Mysql扩展库
- 【py网页】sitecopy代码
- jdk与jre的区别
- JSTL标签库____(2)I18N格式化标签库
- python之字符编码解码
- Linux学习笔记----(2)
- Linux学习笔记----(2)
- 安装Linux的注意点
- 网页扩展名由html改为shtml 301永久重定向伪静态方法
- ini文件访问工具类 WinCfgUtil
- man bootparam 内核启动参数文档
- FusionCharts 3D饼图
- 如何在PADS9.5中插入公司的LOGO(别的图片也一样)
- 第四十四天【java虐我千百遍,我待java如初恋】
- 【转】Log4cpp 封装
- VMware VMmark
- Microsoft100——12.求和