一个c++给程序打log的单例模式类
开发过程中需要给程序打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的单例模式类的更多相关文章
- 手把手教你写一个RN小程序!
时间过得真快,眨眼已经快3年了! 1.我的第一个App 还记得我14年初写的第一个iOS小程序,当时是给别人写的一个单机的相册,也是我开发的第一个完整的app,虽然功能挺少,但是耐不住心中的激动啊,现 ...
- fir.im Weekly - 如何做一个出色的程序员
做一个出色的程序员,困难而高尚.本期 fir.im Weekly 精选了一些实用的 iOS,Android 开发工具和源码分享,还有一些关于程序员的成长 Tips 和有意思有质量的线下活动~ How ...
- 封装一个简单好用的打印Log的工具类And快速开发系列 10个常用工具类
快速开发系列 10个常用工具类 http://blog.csdn.net/lmj623565791/article/details/38965311 ------------------------- ...
- Spring MVC框架下的第一个Hello World程序
本程序是一个maven程序,使用maven方便管理jar包和程序,简化了操作步骤.本程序的目的是通过一个简单的程序,了解Spring MVC框架的基本工作流程,由简入繁的学习Spring MVC框架, ...
- 3.第一个Node.js程序:Hello World!
转自:http://www.runoob.com/nodejs/nodejs-tutorial.html 以下是我们的第一个Node.js程序: console.log("Hello Wor ...
- 撸了一个微信小程序项目
学会一项开发技能最快的步骤就是:准备,开火,瞄准.最慢的就是:准备,瞄准,瞄准,瞄准-- 因为微信小程序比较简单,直接开撸就行,千万别瞄准. 于是乎,趁着今天上午空气质量不错,撸了一个小程序,放在了男 ...
- 【做中学】第一个 Go 语言程序:漫画下载器
原文地址: 第一个 Go 语言程序:漫画下载器: https://schaepher.github.io/2020/04/11/golang-first-comic-downloader 之前学了点 ...
- 用c-free 5写一个入门的程序
本文记录了在windows系统中使用C-FREE 5新建一个Hello HoverTree程序的步骤. 安装好C-Free 5之后,打开.新建一个工程: 附C-Free 5下载:http://hove ...
- 字符串混淆技术应用 设计一个字符串混淆程序 可混淆.NET程序集中的字符串
关于字符串的研究,目前已经有两篇. 原理篇:字符串混淆技术在.NET程序保护中的应用及如何解密被混淆的字符串 实践篇:字符串反混淆实战 Dotfuscator 4.9 字符串加密技术应对策略 今天来 ...
随机推荐
- GXT之旅:第三章:表单和窗口(4)——表单的提交和RPC
表单使用HTTP提交 表单有两种提交方式,第一种就是传统的HTTP提交. 最直接的步骤就是: 使用FormPanel的setAction()方法,去定义submit的URL 使用FormPanel的i ...
- Cocos2d-x游戏中默认的AndroidManifest.xml的解析
直接上代码说明: <?xml version="1.0" encoding="utf-8"? > <!-- xmlns:android=&qu ...
- Messages的例子
13.33 13.36 13.37 Message.h #ifndef MESSAGE_H #define MESSAGE_H #include<iostream> #include< ...
- register_globals(全局变量注册开关)
register_globals,是php.ini文件里面的一个配置选项,接下来,我们可以通过例程来分析一下,当register_globals = on 与 register_globals = o ...
- 【Android】知晓当前是哪一个活动
首先需要新建一个 BaseActivity 继承自Activity,然后在 BaseActivity 中重写 onCreate()方法,如下所示:public class BaseActivity e ...
- ASP.NET+ashx+jQuery动态添加删除表格
aspx: <script src="../script/jquery-1.4.4.min.js" type="text/javascript" lang ...
- Python实战:Python爬虫学习教程,获取电影排行榜
Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...
- CCProcxy代理服务器的配置使用
资源准备及设置 1.资源:http://www.ccproxy.com/ 下载官方正式版本. 2.解压之后打开,界面如下: 打开“设置”,如图做设置,点击确定: 打开“账号”: 点击新建,在ip地址/ ...
- AutoResetEvent 详解
AutoResetEvent 允许线程通过发信号互相通信.通常,此通信涉及线程需要独占访问的资源. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号.如果 AutoRese ...
- iOS设备、Icon、LaunchImage、图片分辨率
iOS设备 iOS设备的屏幕的大小.分辨率以及比例因数(Scale Factor)[1]. iPhone 设备 宽(inch) 高(inch) 对角线(inch) 逻辑分辨率(point) Scale ...