C/C++知识点之c++日志记录模块
小标 2018-12-26 来源 : 阅读 1428 评论 0

摘要:本文主要向大家介绍了 C/C++知识点之c++日志记录模块,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了 C/C++知识点之c++日志记录模块,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

C++ 日志记录模块

该模块从实际项目中产生,通过extern声明的方式,可在代码不同模块中生成日志,日志文件名称为随机码加用户指定名称,采用随机码是为了避免日志文件可能被覆盖的问题。
愿意的话你也能自己构建个人的日志记录模块,本次分享的模块实现方法比较简单,可能有些地方没考虑清楚。
源码:
//
// Created by jerry on 2/12/16.
//

#include 
#include 
#include 
#include 
#include 
#include 

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
        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

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved