开发过程中需要给程序打log. 所以照着网上写了个单例模式的log类

 #ifndef MISCLOGWRITER_H_
#define MISCLOGWRITER_H_ #include <iostream>
#include <fstream>
#include <string>
#include <vector> namespace miscfactory
{
class MiscLogWriter
{
public:
~MiscLogWriter();
//获取本类的对象
static MiscLogWriter* getInstance(); //删除本类的对象
void del();
   //往log文件写东西
bool writeLog(const char* logInfo);
bool writeLog(const std::string& logInfo);
bool writeLog(std::vector<char*>& vectorLogInfo);
bool writeLog(std::vector<std::string>& vectorLogInfo);
     //清空文件内容
static bool clearFile();
//重新设置文件路径
static bool setLogLocation(const char* logPath); private:
MiscLogWriter(); //之所以把构造函数弄成私有是因为:不允许外界直接定义这个log类的对象,只能我这个类通过getInstance自己定义对象
     //打开log文件,关闭log文件
static std::ofstream* openFile();
static bool closeFile(std::ofstream* ofsHandle);     //指定的log文件所在的路径是否合法
static bool isAvailableLocation(const char* logPath); static std::string m_filePath; //log's 文件路径
static MiscLogWriter* m_miscLogWriter; //本类的一个对象指针,getInstance总是返回这个对象的指针. 弄成静态的是因为可以让静态函数getInstance直接返回
};
}//namespace miscfactory #endif /*MISCLOGWRITER_H_*/
 #include "miscfactory/misclogwriter.h"
#include <string.h>
#include <iostream>
#include <fstream> namespace miscfactory
{ using namespace std; MiscLogWriter* MiscLogWriter::m_miscLogWriter = NULL;
std::string MiscLogWriter::m_filePath = string("./miscLog.log"); //静态成员变量必须要初始化,而且初始化要放到cpp中不能放到.h中 MiscLogWriter::MiscLogWriter()
{ } MiscLogWriter::~MiscLogWriter()
{
/*//在析构函数中是否需要删除静态成员变量m_miscLogWriter指向的对象.或者说,这个析构函数到底会不会被执行.如果有这几行,成员函数del执行时候,是否有冲突??
    if (m_miscLogWriter != NULL)
{
if (m_miscLogWriter != NULL)
{
delete m_miscLogWriter;
m_miscLogWriter = NULL;
}
}
*/
}

void MiscLogWriter::del() //单例模式下,把静态成员变量所指向的对象的删除工作放在析构函数中是不合适的吧,是否应该提供一个del函数,如果是,这个函数是否应该是静态 ????
{
if (m_miscLogWriter != NULL)
{
delete m_miscLogWriter;
m_miscLogWriter = NULL;
}
}
MiscLogWriter* MiscLogWriter::getInstance()
{
if (m_miscLogWriter != NULL )
{
return m_miscLogWriter;
} return m_miscLogWriter = new MiscLogWriter();
} bool MiscLogWriter::setLogLocation(const char* logPath)
{
if (!isAvailableLocation(logPath))
{
cout<<"logLocation ["<<logPath<<"] is unAvailable"<<endl;
return false;
} m_filePath = string(logPath);
return true;
} bool MiscLogWriter::isAvailableLocation(const char* logLocation)
{
//TODO check whether logLocation is a correct path
return true;
}
bool MiscLogWriter::writeLog(const char* logInfo)
{
ofstream* ofsHander = openFile();
if (ofsHander == NULL)
{
cout<<"fileOpenError"<<endl;
return false;
} ofsHander->write( logInfo, strlen(logInfo) );
ofsHander->write( "\n", strlen("\n") );
closeFile(ofsHander);
return true;
} bool MiscLogWriter::writeLog(const string& logInfo)
{
return writeLog(logInfo.data());
} bool MiscLogWriter::writeLog(vector<char*>& vectorLogInfo)
{
for (int i = ; i < vectorLogInfo.size(); i++)
{
if (!writeLog(vectorLogInfo[i]))
{
return false;
}
} return true;
} bool MiscLogWriter::writeLog(vector<string>& vectorLogInfo)
{
for (int i = ; i < vectorLogInfo.size(); i++)
{
if (!writeLog(vectorLogInfo[i].data()))
{
return false;
}
} return true;
} ofstream* MiscLogWriter::openFile()
{
if(!isAvailableLocation(m_filePath.data()))
{
return NULL;
} ofstream* ofsHandle = new ofstream(m_filePath, ios::app); //追加方式打开文件
return ofsHandle;
} bool MiscLogWriter::closeFile(ofstream* ofsHandle)
{
if (ofsHandle == NULL)
{
return true;
} if (ofsHandle->is_open())
{
ofsHandle->close();
delete ofsHandle;
ofsHandle = NULL;
} return true;
} bool MiscLogWriter::clearFile()
{
if(!isAvailableLocation(m_filePath.data()))
{
return false;
} ofstream* ofsHandle = new ofstream(m_filePath, ios::out); //清空方式打开文件
return closeFile(ofsHandle);
}
}//namespace miscfactory

调用

MiscLogWriter::setLogLocation("./miscLog.log");
MiscLogWriter::clearFile();

MiscLogWriter::getInstance().WriterLog("abc");

MiscLogWriter::getInstance().WriterLog("123");

MiscLogWriter::getInstance().del();

一个c++给程序打log的单例模式类的更多相关文章

  1. 手把手教你写一个RN小程序!

    时间过得真快,眨眼已经快3年了! 1.我的第一个App 还记得我14年初写的第一个iOS小程序,当时是给别人写的一个单机的相册,也是我开发的第一个完整的app,虽然功能挺少,但是耐不住心中的激动啊,现 ...

  2. fir.im Weekly - 如何做一个出色的程序员

    做一个出色的程序员,困难而高尚.本期 fir.im Weekly 精选了一些实用的 iOS,Android 开发工具和源码分享,还有一些关于程序员的成长 Tips 和有意思有质量的线下活动~ How ...

  3. 封装一个简单好用的打印Log的工具类And快速开发系列 10个常用工具类

    快速开发系列 10个常用工具类 http://blog.csdn.net/lmj623565791/article/details/38965311 ------------------------- ...

  4. Spring MVC框架下的第一个Hello World程序

    本程序是一个maven程序,使用maven方便管理jar包和程序,简化了操作步骤.本程序的目的是通过一个简单的程序,了解Spring MVC框架的基本工作流程,由简入繁的学习Spring MVC框架, ...

  5. 3.第一个Node.js程序:Hello World!

    转自:http://www.runoob.com/nodejs/nodejs-tutorial.html 以下是我们的第一个Node.js程序: console.log("Hello Wor ...

  6. 撸了一个微信小程序项目

    学会一项开发技能最快的步骤就是:准备,开火,瞄准.最慢的就是:准备,瞄准,瞄准,瞄准-- 因为微信小程序比较简单,直接开撸就行,千万别瞄准. 于是乎,趁着今天上午空气质量不错,撸了一个小程序,放在了男 ...

  7. 【做中学】第一个 Go 语言程序:漫画下载器

    原文地址: 第一个 Go 语言程序:漫画下载器: https://schaepher.github.io/2020/04/11/golang-first-comic-downloader 之前学了点 ...

  8. 用c-free 5写一个入门的程序

    本文记录了在windows系统中使用C-FREE 5新建一个Hello HoverTree程序的步骤. 安装好C-Free 5之后,打开.新建一个工程: 附C-Free 5下载:http://hove ...

  9. 字符串混淆技术应用 设计一个字符串混淆程序 可混淆.NET程序集中的字符串

    关于字符串的研究,目前已经有两篇. 原理篇:字符串混淆技术在.NET程序保护中的应用及如何解密被混淆的字符串  实践篇:字符串反混淆实战 Dotfuscator 4.9 字符串加密技术应对策略 今天来 ...

随机推荐

  1. Jakarta-Commons- BeanUtils学习笔记:

    http://www.cnblogs.com/zhangyi85/archive/2009/04/22/1441341.html 1.什么是BeanUtils: BeanUtils主要提供了对于Jav ...

  2. .jar是什么文件?(转载)

    JAR(Java ARchive,Java 归档)是一种与平台无关的文件格式,可将多个文件合成一个文件.用户可将多个 Java applet 及其所需组件(.class 文件.图像和声音)绑定到 JA ...

  3. JavaScript中七种函数调用方式及对应 this 的含义

    this 在 JavaScript 开发中占有相当重要的地位,不过很多人对this这个东西都感觉到琢磨不透.要真正理解JavaScript的函数机制,就非常有必要搞清楚this到底是怎么回事. 函数调 ...

  4. android开发的学习路线

    参考资料:千锋3G学院--课程大纲    http://www.mobiletrain.org 看了专业的培训机构的课程大纲,才知道,自己学习android的路途才刚刚开始!特此整理分享一下,希望能帮 ...

  5. 阿里云SQL Server 2008 客户端导入数据库教程

    一.适用场景   源端数据库是SQL Server 2005 及以上.(SQL Server 2000未测试.) 数据文件总大小在10G以内. 可以在低峰期停应用.   二.导出步骤   1.软件准备 ...

  6. CentOS7上Nginx的使用

    Nginx 的启动 指定配置文件的方式启动nginx # nginx -c /etc/nginx/nginx.conf 对于yum安装的nginx,使用systemctl命令启动 # systemct ...

  7. http实现方式概念学习笔记

    web概念:web1.0:静态页面为主,门户新闻.企业宣传web2.0:动态页面为主,用户参与,bbs,blog,sns,微博            web3.0:web2.0基础上,智能化,人性化, ...

  8. skynet网络库socket-server

    最近在读大神云风的开源服务器架构skynet,其中的网络库,云风已经单独开来,可以独立使用. 开源地址: https://github.com/cloudwu/socket-server 网络库已经封 ...

  9. codeforces 505C Mr. Kitayuta, the Treasure Hunter(dp)

    题意:有30001个岛,在一条线上,从左到右编号一次为0到30000.某些岛屿上有些宝石.初始的时候有个人在岛屿0,他将跳到岛屿d,他跳跃的距离为d.如果当前他跳跃的距离为L,他下一次跳跃的距离只能为 ...

  10. 绝对定位的DIV绝对居中显示

    绝对居中:即在客户端上任何分辨率下纵横方向均居中 紫色的正方形为绝对定位的div position:absolute; top: 50%; left: 50%; 只能把div定位在以红色圈为起点的位置 ...