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

c++日志记录模块的更多相关文章

  1. python爬虫学习之日志记录模块

    这次的代码就是一个日志记录模块,代码很容易懂,注释很详细,也不需要安装什么库.提供的功能是日志可以显示在屏幕上并且保存在日志文件中.调用的方式也很简单,测试代码里面有. 源代码: #encoding= ...

  2. Python开发之日志记录模块:logging

    1 引言 最近在开发一个应用软件,为方便调试和后期维护,在代码中添加了日志,用的是Python内置的logging模块,看了许多博主的博文,颇有所得.不得不说,有许多博主大牛总结得确实很好.似乎我再写 ...

  3. 基于AOP和ThreadLocal实现的一个简单Http API日志记录模块

    Log4a 基于AOP和ThreadLocal实现的一个简单Http API日志记录模块 github地址 : https://github.com/EalenXie/log4a 在API每次被请求时 ...

  4. [ Python入门教程 ] Python中日志记录模块logging使用实例

    python中的logging模块用于记录日志.用户可以根据程序实现需要自定义日志输出位置.日志级别以及日志格式. 将日志内容输出到屏幕 一个最简单的logging模块使用样例,直接打印显示日志内容到 ...

  5. 日志记录模块logging

    在python中,日志记录显示有两种方式,一种是保存在文件和打印屏幕上,一种保存在文件中. 第一种,直接保存在文件中. import logging #日志模块,方便记录日志 # 下面是配置日志记录格 ...

  6. python基础语法13 内置模块 subprocess,re模块,logging日志记录模块,防止导入模块时自动执行测试功能,包的理论

    subprocess模块: - 可以通过python代码给操作系统终端发送命令, 并且可以返回结果. sub: 子    process: 进程 import subprocess while Tru ...

  7. nodejs之log4js日志记录模块简单配置使用

    在我的一个node express项目中,使用了log4js来生成日志并且保存到文件里,生成的文件如下: 文件名字叫:access.log 如果在配置log4js的时候允许了同时存在多个备份log文件 ...

  8. python3 logging 日志记录模块

    #coding:utf-8 import logginglogging.basicConfig(filename='log1.log', format='%(asctime)s -%(name)s-% ...

  9. Elmah 日志记录组件

    http://www.cnblogs.com/jys509/p/4571298.html 简介 ELMAH(Error Logging Modules and Handlers)错误日志记录模块和处理 ...

随机推荐

  1. Oracle 数据库 简单查询

    select DISTINCT dept_id from s_emp; desc s_emp; ; --给入职3年以上员工发10万元年终奖 ; --列出职位是仓库管理员的名字和工资 select la ...

  2. ajax status 错误

    status **:未被始化 status **:请求收到,继续处理 status **:操作成功收到,分析.接受 status **:完成此请求必须进一步处理 status **:请求包含一个错误语 ...

  3. python 多进程 Event的使用

    Event事件  多进程的使用 通俗点儿讲  就是 1.  Event().wait()    插入在进程中插入一个标记(flag)  默认为 false  然后flag为false时  程序会停止运 ...

  4. [2018HN省队集训D9T1] circle

    [2018HN省队集训D9T1] circle 题意 给定一个 \(n\) 个点的竞赛图并在其中钦定了 \(k\) 个点, 数据保证删去钦定的 \(k\) 个点后这个图没有环. 问在不删去钦定的这 \ ...

  5. dns bind配置教程

    实验环境 三台centos7虚拟机,一台ip为192.168.52.130,一台为192.168.52.131,最后一台为192.168.52.132 安装bind 使用yum -y insall b ...

  6. 网站如何接入第三方登录,微信登录和QQ登录:注册认证篇

    第三方登录平台接入 (QQ\微信登录) QQ登录接入 第一步成为QQ应用开发者,审核期限七天 一.所需材料 1.公司注册相关信息 2.营业执照扫描件 微信登录接入 第一步成为微信开发平台开发者,认证费 ...

  7. 7、Django的模型层(1)

    第1节:ORM简介 ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极 ...

  8. JS中 map, filter, some, every, forEach, for in, for of 用法总结

    本文转载自:http://blog.csdn.net/gis_swb/article/details/52297343 1.map 有返回值,返回一个新的数组,每个元素为调用func的结果. let ...

  9. ip 报文头

  10. Mac svn使用学习-4-客户端cli命令详解

    客户端cli的使用 WC:Working Copy 你的工作区 将文件或目录版本化,这样下一次提交到存储库的时候,他们就都会被提交上去.能实现版本化的命令有: add 1.import 是否访问存储库 ...