开发过程中需要给程序打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. java读取配置文件

    java 读取文件可以用字节流和字符流. 由于一个汉字占两个字节,所以如果配置文件中有汉字,用字节流读取,会出现乱码. 用字符流则不会出现乱码. 配置文件 b.properties 文件如下: fam ...

  2. in和exists的区别与SQL执行效率分析

    可总结为:当子查询表比主查询表大时,用Exists:当子查询表比主查询表小时,用in SQL中in可以分为三类: 1.形如select * from t1 where f1 in ('a','b'), ...

  3. 关于T-SQL重编译那点事,WITH RECOMPILE和OPTION(RECOMPILE)区别仅仅是存储过程级重编译和SQL语句级重编译吗

    本文出处:http://www.cnblogs.com/wy123/p/6262800.html   在考虑重编译T-SQL(或者存储过程)的时候,有两种方式可以实现强制重编译(前提是忽略导致重编译的 ...

  4. mysql 学习笔记5-- 数据库优化

    ext4:(rw,noatime,nodiratime,nobarrier,data=ordered)xfs: (rw,noatime,nodiratim,nobarrier,logbufs=8,lo ...

  5. linux shell pushd popd dirs命令

    1.dirs 1)功能显示当前目录栈中的所有记录(不带参数的dirs命令显示当前目录栈中的记录) 2)语法(1)格式:dirs  [-clpv]  [+n]  [-n](2)选项-c    删除目录栈 ...

  6. 【NodeJs】Ctrl+C在Linux平台和Windows平台下的TCP连接中的不同表现

    Linux平台:CentOS release 6.5 (Final) Windows平台:Windows 7 旗舰版 服务器端代码如下: var net = require('net'); var s ...

  7. mysql 导入excel 或 .csv

    第一步 导出excel 去掉列头,设置文本里面格式

  8. Android开发之UI的编程方式创建

    我们知道,android中一个activity对应一个xml的UI配置文件,除了用xml文件配置的方式创建用户界面外,还可以使用代码编程的方式来创建一个用户界面.如果用户界面需要在运行过程中动态生成的 ...

  9. GUI编程笔记(java)09:GUI控制文本框只能输入数字字符案例

    1.首先我们看看我的需求,如下: 控制文本框只能输入数字字符   2.源代码: package cn.itcast_07; import java.awt.FlowLayout; import jav ...

  10. javascript split() 正则表达式

    路由匹配 http.createServer(function(req, res) { var items = req.url.split('/'); if (items.length < 3 ...