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. EntityFramework Code-First 简易教程(二)-------Code First约定

    Code First 约定 在前一篇中,我们已经知道了EF Code-First怎样从模型类(domain classes)中创建数据库表,下面,我们开始学习默认的Code-First约定. 什么是约 ...

  2. MySQL索引与Index Condition Pushdown(employees示例)

    实验 先从一个简单的实验开始直观认识ICP的作用. 安装数据库 首先需要安装一个支持ICP的MariaDB或MySQL数据库.我使用的是MariaDB 5.5.34,如果是使用MySQL则需要5.6版 ...

  3. Mariadb配置Galera集群

    一.增加MariaDB源 cd /etc/yum.repos.d vi MariaDB.repo # MariaDB 10.1 CentOS repository list - created 201 ...

  4. 1.4环境的准备(四)之Pycharm的使用技巧

    返回总目录 目录: 1.快捷键的使用: 2.提示技巧: 3.其他技巧: (一)快捷键的使用: (1)Pycharm自带默认的快捷键 1.Ctrl + C 复制 2.Ctrl + V 粘贴 3.Ctrl ...

  5. win10子系统linux.ubuntu开发环境搭建

    移步新博客... win10子系统linux.ubuntu开发环境搭建

  6. October 29th, 2017 Week 44th Sunday

    There was another life that I might have had, but I am having this one. 我明明可以过另一种生活,但我却选择了这一种. Be re ...

  7. Spring Boot session与cookie的使用

    Session import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import ...

  8. XtraEditors五、SpinEdit、TimeEdit

    SpinEdit控件 此按钮控件是用来增加或减少在编辑的文本编辑区显示的数值, 该编辑值可以是一个整数或浮点数. 其 Text属性 用于设置编辑区的文本: 其 Value属性 用于获取编辑区的值: 示 ...

  9. MongoDB中_class字段的作用

    我们知道,如果你用Java的Sping Data 框架映射Pojo为MongoDB数据时,数据库中会自动给你添加一个_class字段,那这个字段是干嘛用的呢?我们可以不可以不要这个字段呢? 直接上结论 ...

  10. PyQt5--InputDiaglog

    # -*- coding:utf-8 -*- ''' Created on Sep 14, 2018 @author: SaShuangYiBing Comment: ''' import sys f ...