一个c++给程序打log的单例模式类

时间:2022-07-19 16:42:44

开发过程中需要给程序打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();