您的位置:首页 > 娱乐 > 八卦 > c++ 简单的日志类 CCLog

c++ 简单的日志类 CCLog

2024/12/24 20:22:56 来源:https://blog.csdn.net/cxsjabcabc/article/details/7294969  浏览:    关键词:c++ 简单的日志类 CCLog

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

代码如下:

/**  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:unsigned	write(const CCString & str);unsigned	writeEndl();void	clearAllData();	    // if a file is opened, all contents of the file will be cleared, the file will be opened a second time.public:void	setWriteToConsole();void	setWriteToFile();public:LOG_TYPE	getLogType() const { return _logType; }CCString	getLogFileName() const { return _fileName; }private:    void	clearConsole()	    // 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	    // CCLogCCLog * 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
}


微风不燥,阳光正好,你就像风一样经过这里,愿你停留的片刻温暖舒心。

我是程序员小迷(致力于C、C++、Java、Kotlin、Android、Shell、JavaScript、TypeScript、Python等编程技术的技巧经验分享),若作品对您有帮助,请关注、分享、点赞、收藏、在看、喜欢,您的支持是我们为您提供帮助的最大动力。

欢迎关注。助您在编程路上越走越好!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com