c++日志记录模块

9 篇文章 0 订阅
7 篇文章 0 订阅

C++ 日志记录模块

该模块从实际项目中产生,通过extern声明的方式,可在代码不同模块中生成日志,日志文件名称为随机码加用户指定名称,采用随机码是为了避免日志文件可能被覆盖的问题。

愿意的话你也能自己构建个人的日志记录模块,本次分享的模块实现方法比较简单,可能有些地方没考虑清楚。

源码:

//
// Created by jerry on 2/12/16.
//

#include <iostream>
#include <string>
#include <fstream>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>

namespace lg{
    class Log {
    private:
        std::string m_log_file_path;
        std::ofstream m_log_file;
        bool m_cout_flag;
    public:
        Log(const std::string file_path = "run.log", const bool cout_flag = true);
        ~Log();

        void close();
        template<class T>
        Log & operator << (T &log_data)
        {
            m_log_file << log_data;
            m_log_file << std::flush;
#if ((defined _RM_DEC_PRINT) && (defined _RM_DEC_LOG_PRINT))
            if(m_cout_flag)
                std::cout << log_data << std::flush;            
#endif      
            return *this;
        }
    };

    extern Log run_log;
}
//
// Created by jerry on 2/12/16.
//

#include "Log.h"

namespace lg{

    Log run_log;

    Log::Log(const std::string file_path, const bool cout_flag)
    {
        m_cout_flag = cout_flag;
        // system("mkdir $HOME/data/log");

        struct timeval tv;     
        gettimeofday(&tv,NULL);
        std::random_device rd;  
        std::default_random_engine e(rd());  
        std::uniform_int_distribution<> u(0,1000000);
        usleep(u(e));

        std::string home = getenv("HOME"); 

        std::string log_file_path = home + "/data/log/" +
                                std::to_string((tv.tv_sec * 1000) % 1000000 + tv.tv_usec / 1000) +
                                std::to_string(u(e)) +
                                file_path;
        m_log_file_path = log_file_path;
        m_log_file.open(m_log_file_path, std::ios::app);
    }

    Log::~Log()
    {
        m_log_file.close();
        std::cout << "-- Log Destructor successfully!" << std::endl;
    }

    void Log::close()
    {
        m_log_file.close();
    }

}

例程:

下述log_information为用户需要记录的日志信息。

//file test1.cpp
#include "Log.h"
//...   your code here
//...   your code here
lg::run_log << /***log_information1 here***/ << "\n";
//file test2.cpp
#include "Log.h"
//...   your code here
//...   your code here
lg::run_log << /***log_information2 here***/ << "\n";

最终日志输出为:

#file (rand_code)run.log
log_information1
log_information2
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
QT版本 //xxxx.h //log level #define LL_DEBUG 0 #define LL_INFO 1 #define LL_WARN 2 #define LL_ERROR 3 #define LL_ALARM 4 #define GetCurFileName (__FILE__) #define GetCurCodeLine (__LINE__) #define GetCurCodeFunctionName (__FUNCTION__) #define Write_Log(LEVEL, FMTLOG) WriteLog(LEVEL, FMTLOG, GetCurFileName, GetCurCodeLine, GetCurCodeFunctionName) void WriteLog(int, QString, QString, int, QString); //xxxx.cpp void WriteLog(int loglevel, QString strlogstr, QString strfilename, int iline, QString strfunname) { QString g_logdllpath(g_runPath + "/dependences/LoggingModeDLL.dll"); HINSTANCE hDll = LoadLibrary(g_logdllpath.toStdWString().data()); typedef int (__cdecl *MYFUNC)(int, char*, char*, int, char*); MYFUNC execfunc = (MYFUNC)GetProcAddress(hDll, "WriteLog"); execfunc(loglevel, const_cast<char*>(strlogstr.toLatin1().data()), const_cast<char*>(strfilename.toLatin1().data()), iline, const_cast<char*>(strfunname.toLatin1().data())); } MFC 版本 //xxxx.h //log level #define LL_DEBUG 0 #define LL_INFO 1 #define LL_WARN 2 #define LL_ERROR 3 #define LL_ALARM 4 #define GetCurFileName (__FILE__) #define GetCurCodeLine (__LINE__) #define GetCurCodeFunctionName (__FUNCTION__) #define Write_Log(LEVEL, FMTLOG) WriteLog(LEVEL, FMTLOG, GetCurFileName, GetCurCodeLine, GetCurCodeFunctionName) void WriteLog(int, CString, CString, int, CString); void WriteLog(int loglevel, CString strlogstr, CString strfilename, int iline, CString strfunname) { CString g_logdllpath(".\\LoggingModeDLL.dll"); HINSTANCE hDll = LoadLibrary(g_logdllpath.GetBuffer()); typedef int (__cdecl *MYFUNC)(int, char*, char*, int, char*); MYFUNC execfunc = (MYFUNC)GetProcAddress(hDll, "WriteLog"); execfunc(loglevel, const_cast<char*>(strlogstr.GetBuffer()), const_cast<char*>(strfilename.GetBuffer()), iline, const_cast<char*>(strfunname.GetBuffer())); }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值