工具类Log和Trace

代码地址:https://github.com/hanxi/Log

/*=============================================================================
#     FileName: log.h
#         Desc: 日志记录,只支持单线程,支持网页格式
#       Author: hanxi
#        Email: hanxi.com@gmail.com
#     HomePage: http://hanxi.cnblogs.com
#      Version: 0.0.1
#   LastChange: 2013-03-07 18:24:21
#      History:
=============================================================================*/

#ifndef __LOG_H_
#define __LOG_H_

//STD
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>

#define HTML_LOG   1     //输出为html格式
#define TXT_LOG    2     //输出为txt格式

class Log
{
private:
    static std::ofstream     ms_file;          //log文件流
    static const char       *ms_path;          //log文件路径
    static int               ms_logLevel;      //小于logLevel的log将被打印出来
    static int               ms_logFileType;   //log文件类型

    int                      m_nowLevel;
    std::string             *m_theFunctionName;//进入的函数名

public:
    static void s_init(const char *i_path, int i_logLevel, int i_fileType);
    static void s_stop();
    static const char *endl;

    Log(const char *funcName, int logLevel);
    ~Log();

    template<typename Type>
    void debug(Type msg);
    template<typename Type>
    Log& operator<<(Type msg);
};
// 初始化静态成员变量
std::ofstream     Log::ms_file;
const char       *Log::ms_path = NULL;       //log文件路径
int               Log::ms_logLevel = 0;      //小于logLevel的log将被打印出来
int               Log::ms_logFileType = 0;   //log文件类型
const char       *Log::endl = "\n";
/*=============================================================================
#     FileName: trace.h
#         Desc: 调试类
#       Author: hanxi
#        Email: hanxi.com@gmail.com
#     HomePage: http://hanxi.cnblogs.com
#      Version: 0.0.1
#   LastChange: 2013-03-08 13:05:03
#      History:
=============================================================================*/
#ifndef __TRACE_H_ 
#define __TRACE_H_ 

#include <iostream>
using namespace std;

class Trace {
public:
    Trace (const char *name);
    ~Trace ();
    void debug (const string &msg);

    static bool traceIsActive;

private:
    string *theFunctionName;
};
bool Trace::traceIsActive = false;

 

posted @ 2013-03-08 15:46 涵曦 阅读(...) 评论(...) 编辑 收藏