• 一个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( " ", strlen(" ") ); 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();

  • 相关阅读:
    关于C 语言的字符串常量拼接
    网络处理器简介
    杨先生的博客目录(持续更新......)
    搭建json-server服务
    Spring boot + Mybatis + SQLite 搭建blog API
    使用json-server POST 数据结果只有id
    解决python查询数据库字段为decimal类型的数据结果为科学计数法的问题
    Maven仓库安装配置及使用
    Jekins发送Allure测试报告邮件
    Jenkins发送邮件配置
  • 原文地址:https://www.cnblogs.com/silentNight/p/5515301.html
Copyright © 2020-2023  润新知