一個c++給程序打log的單例模式類


開發過程中需要給程序打log. 所以照着網上寫了個單例模式的log類

 1 #ifndef MISCLOGWRITER_H_
 2 #define MISCLOGWRITER_H_
 3 
 4 #include <iostream>
 5 #include <fstream>
 6 #include <string>
 7 #include <vector>
 8 
 9 namespace miscfactory
10 {
11 class MiscLogWriter
12 {
13     public:
14         ~MiscLogWriter();
15         //獲取本類的對象  
16         static MiscLogWriter* getInstance();  

//刪除本類的對象
void del();
17    //往log文件寫東西 18 bool writeLog(const char* logInfo); 20 bool writeLog(const std::string& logInfo); 22 bool writeLog(std::vector<char*>& vectorLogInfo); 24 bool writeLog(std::vector<std::string>& vectorLogInfo); 25      //清空文件內容 26 static bool clearFile(); 27 //重新設置文件路徑 28 static bool setLogLocation(const char* logPath); 29 30 private: 31 MiscLogWriter(); //之所以把構造函數弄成私有是因為:不允許外界直接定義這個log類的對象,只能我這個類通過getInstance自己定義對象 32      //打開log文件,關閉log文件 33 static std::ofstream* openFile(); 34 static bool closeFile(std::ofstream* ofsHandle); 35 36     //指定的log文件所在的路徑是否合法 37 static bool isAvailableLocation(const char* logPath); 38 39 static std::string m_filePath; //log's 文件路徑 40 static MiscLogWriter* m_miscLogWriter; //本類的一個對象指針,getInstance總是返回這個對象的指針. 弄成靜態的是因為可以讓靜態函數getInstance直接返回 41 }; 42 }//namespace miscfactory 43 44 #endif /*MISCLOGWRITER_H_*/

 

  1 #include "miscfactory/misclogwriter.h"
  2 #include <string.h>
  3 #include <iostream>
  4 #include <fstream>
  5 
  6 namespace miscfactory
  7 {
  8 
  9 using namespace std;
 10 
 11 MiscLogWriter* MiscLogWriter::m_miscLogWriter = NULL;
 12 std::string MiscLogWriter::m_filePath = string("./miscLog.log");   //靜態成員變量必須要初始化,而且初始化要放到cpp中不能放到.h中
 13 
 14 MiscLogWriter::MiscLogWriter()
 15 {
 16 
 17 }
 18 
 19 MiscLogWriter::~MiscLogWriter()
 20 {
/*
//在析構函數中是否需要刪除靜態成員變量m_miscLogWriter指向的對象.或者說,這個析構函數到底會不會被執行.如果有這幾行,成員函數del執行時候,是否有沖突??
    if (m_miscLogWriter != NULL)
{
if (m_miscLogWriter != NULL)
{
delete m_miscLogWriter;
m_miscLogWriter = NULL;
}
}
*/
26 } 27
void MiscLogWriter::del() //單例模式下,把靜態成員變量所指向的對象的刪除工作放在析構函數中是不合適的吧,是否應該提供一個del函數,如果是,這個函數是否應該是靜態 ????
{
if (m_miscLogWriter != NULL)
{
delete m_miscLogWriter;
m_miscLogWriter = NULL;
}
}
28 MiscLogWriter* MiscLogWriter::getInstance() 29 { 30 if (m_miscLogWriter != NULL ) 31 { 32 return m_miscLogWriter; 33 } 34 35 return m_miscLogWriter = new MiscLogWriter(); 36 } 37 38 bool MiscLogWriter::setLogLocation(const char* logPath) 39 { 40 if (!isAvailableLocation(logPath)) 41 { 42 cout<<"logLocation ["<<logPath<<"] is unAvailable"<<endl; 43 return false; 44 } 45 46 m_filePath = string(logPath); 47 return true; 48 } 49 50 bool MiscLogWriter::isAvailableLocation(const char* logLocation) 51 { 52 //TODO check whether logLocation is a correct path 53 return true; 54 } 55 bool MiscLogWriter::writeLog(const char* logInfo) 56 { 57 ofstream* ofsHander = openFile(); 58 if (ofsHander == NULL) 59 { 60 cout<<"fileOpenError"<<endl; 61 return false; 62 } 63 64 ofsHander->write( logInfo, strlen(logInfo) ); 65 ofsHander->write( "\n", strlen("\n") ); 66 closeFile(ofsHander); 67 return true; 68 } 69 70 bool MiscLogWriter::writeLog(const string& logInfo) 71 { 72 return writeLog(logInfo.data()); 73 } 74 75 bool MiscLogWriter::writeLog(vector<char*>& vectorLogInfo) 76 { 77 for (int i = 0; i < vectorLogInfo.size(); i++) 78 { 79 if (!writeLog(vectorLogInfo[i])) 80 { 81 return false; 82 } 83 } 84 85 return true; 86 } 87 88 bool MiscLogWriter::writeLog(vector<string>& vectorLogInfo) 89 { 90 for (int i = 0; i < vectorLogInfo.size(); i++) 91 { 92 if (!writeLog(vectorLogInfo[i].data())) 93 { 94 return false; 95 } 96 } 97 98 return true; 99 } 100 101 ofstream* MiscLogWriter::openFile() 102 { 103 if(!isAvailableLocation(m_filePath.data())) 104 { 105 return NULL; 106 } 107 108 ofstream* ofsHandle = new ofstream(m_filePath, ios::app); //追加方式打開文件 109 return ofsHandle; 110 } 111 112 bool MiscLogWriter::closeFile(ofstream* ofsHandle) 113 { 114 if (ofsHandle == NULL) 115 { 116 return true; 117 } 118 119 if (ofsHandle->is_open()) 120 { 121 ofsHandle->close(); 122 delete ofsHandle; 123 ofsHandle = NULL; 124 } 125 126 return true; 127 } 128 129 bool MiscLogWriter::clearFile() 130 { 131 if(!isAvailableLocation(m_filePath.data())) 132 { 133 return false; 134 } 135 136 ofstream* ofsHandle = new ofstream(m_filePath, ios::out); //清空方式打開文件 137 return closeFile(ofsHandle); 138 } 139 }//namespace miscfactory

調用

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

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

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

MiscLogWriter::getInstance().del();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM