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	    // 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
}


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

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

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

相关推荐

  1. c++ 简单日志 CCLog

    2024-06-08 11:42:04       7 阅读
  2. C++简单日志系统

    2024-06-08 11:42:04       15 阅读
  3. C++日期实现

    2024-06-08 11:42:04       8 阅读
  4. c++日期实现

    2024-06-08 11:42:04       4 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-08 11:42:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-08 11:42:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-08 11:42:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-08 11:42:04       18 阅读

热门阅读

  1. CAB203 Special Topics Assignment

    2024-06-08 11:42:04       8 阅读
  2. 智能合约中未授权访问

    2024-06-08 11:42:04       7 阅读
  3. Visual Studio的快捷按键

    2024-06-08 11:42:04       9 阅读
  4. Docker面试整理-如何管理Docker容器的安全?

    2024-06-08 11:42:04       11 阅读
  5. 52.Fork & Join线程池

    2024-06-08 11:42:04       7 阅读