开发过程中需要给程序打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. [AngularJS] Extract predicate methods into filters for ng-if and ng-show

    Leaking logic in controllers is not an option, filters are a way to refactor your code and are compa ...

  2. UVA10518 - How Many Calls?(矩阵高速幂)

    UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...

  3. 管理http服务的脚本

    因为老是须要又一次安装系统,重一次都要又一次设置http服务的启动脚本.麻烦,所以这一次就把脚本备份出来. httpd for Ubuntu system: nginx + php-fpm #! /b ...

  4. java se 6在solaris的可观察性特征分析

        java平台标准版(java se)6,代码名为"mustang",是最新的java se发行版本(正在开发中).java se 6源码和二进制代码都可以在www.java ...

  5. Choosing a Linux Tracer ------Brendan Gregg's Blog

    home Choosing a Linux Tracer (2015) 08 Jul 2015 Linux Tracing is Magic! A tracer is an advanced perf ...

  6. 基于VMware为CentOS 6.5配置两个网卡

    为CentOS 6.5配置两块网卡,一块是eth0,一块是eth1,下面以master为例 1.选择“master”-->“编辑虚拟机设置”,如下所示 2.单击“添加”,如下 3.选择“网络适配 ...

  7. Sqlserver中实现oralce 数据库的rownumber

    引用自:http://cai555.javaeye.com/blog/466033 方法1: with temp as ( select row_number() over(order by city ...

  8. Caused by: java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC

    Caused by: java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC 缺少com/sun/tools/inte ...

  9. (转) linux虚拟机中和主机三种网络连接方式的区别

    在介绍网络模式之前,关于网络的几个简单命令的使用 ifup eth0   //启动网卡eth0 ifdown eth0 //关闭网卡eth0 /etc/network/interfaces  //网络 ...

  10. Java 之文件IO编程 之写入

    package com.sun; /* * 操作对文件IO的写 * 2014-08-10 */ import java.io.*; public class File_Write { public s ...