c++ 简单的日志类 CCLog

来源:互联网 发布:如何申请淘宝账号 编辑:程序博客网 时间:2024/06/09 20:26

   此日志类,简单地实现了向标准输出控制台和文件输出日志信息的功能,并能在这两者之间进行切换输出,

满足输出日志的不同需求。

代码如下:


 

/* *  CCLog.h *  c++_common_codes * *  Created by xichen on 12-1-12. *  Copyright 2012 cc_team. All rights reserved. **/#ifndef CC_LOG_H#define CC_LOG_H#include "ccString.h"#include <cstdio>typedef enum _LOG_TYPE{    LOG_TYPE_CONSOLE,    LOG_TYPE_FILE,    LOG_TYPE_MAX}LOG_TYPE;class CCLog{public:    CCLog(const char * fileName = NULL, const char * mode = "at+");    // by default, open file by "at+" mode    ~CCLog();public:    unsignedwrite(const CCString & str);    unsignedwriteEndl();    voidclearAllData();    // if a file is opened, all contents of the file will be cleared, the file will be opened a second time.public:    voidsetWriteToConsole();    voidsetWriteToFile();public:    LOG_TYPEgetLogType() const { return _logType; }    CCStringgetLogFileName() const { return _fileName; }private:        voidclearConsole()    // not coding ok    {    }private:    CCLog(const CCLog & log);    CCLog & operator=(const CCLog & log);private:    FILE*_file;    FILE*_backupFile;    CCString_fileName;    LOG_TYPE_logType;};#endif

/* *  CCLog.cpp *  c++_common_codes * *  Created by xichen on 12-1-12. *  Copyright 2012 cc_team. All rights reserved. **/#include "ccLog.h"CCLog::CCLog( const char * fileName /*= NULL*/, const char * mode /*= "at+"*/ ){    if(fileName == NULL)    {_file = _backupFile = NULL;_logType = LOG_TYPE_CONSOLE;return;    }    _logType = LOG_TYPE_FILE;    _file = fopen(fileName, mode);    _backupFile = _file;    if(_file == NULL)std::cerr << "Open file error" << std::endl;    else_fileName = CCString(fileName);}CCLog::~CCLog(){    if(_logType == LOG_TYPE_FILE)    {if(_file != NULL){    fclose(_file);    return;}if(_backupFile != NULL){    fclose(_backupFile);}    }}unsigned CCLog::write( const CCString & str ){    if(_logType == LOG_TYPE_CONSOLE)    {std::cout << str;return str.length();// it's not accurate. ????    }    fseek(_file, 0, SEEK_END);    return fwrite(str.c_str(), str.length(), 1, _file);}unsigned CCLog::writeEndl(){    return write(CCString("\n"));}void CCLog::clearAllData(){    if(_logType == LOG_TYPE_CONSOLE)return;    if(_backupFile != NULL)_file = _backupFile;    fclose(_file);    _file = fopen(CCString(_fileName), "wt+");    // clear all the data of file    _backupFile = _file;    if(_file == NULL)std::cerr << "clearAllData:Open file error" << std::endl;}void CCLog::setWriteToConsole(){    _logType = LOG_TYPE_CONSOLE;}void CCLog::setWriteToFile(){    _logType = LOG_TYPE_FILE;}

简单地测试代码如下:
void ccTestLog(){#if 1    // CCLog    CCLog * log = new CCLog(NULL);    log->write("hello");    log->write("\t1\n");    log->write("\txichen\n");    delete log;    log = new CCLog("d:\\test\\logtest.txt");    log->write("ab\t\n1");    delete log;    log = new CCLog("d:\\test\\logtest.txt");    log->write("xiche\t123");    log->clearAllData();    log->write("after clear");    log->setWriteToConsole();    log->write("the console info");    log->setWriteToFile();    log->write("the file content");    log->writeEndl();    log->write("the next line\nhehe");    delete log;#endif}


注:#include "ccString.h" 头文件见上一篇:c++ string的简单实现


原创粉丝点击