c++日志记录模块
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++日志记录模块的更多相关文章
- python爬虫学习之日志记录模块
这次的代码就是一个日志记录模块,代码很容易懂,注释很详细,也不需要安装什么库.提供的功能是日志可以显示在屏幕上并且保存在日志文件中.调用的方式也很简单,测试代码里面有. 源代码: #encoding= ...
- Python开发之日志记录模块:logging
1 引言 最近在开发一个应用软件,为方便调试和后期维护,在代码中添加了日志,用的是Python内置的logging模块,看了许多博主的博文,颇有所得.不得不说,有许多博主大牛总结得确实很好.似乎我再写 ...
- 基于AOP和ThreadLocal实现的一个简单Http API日志记录模块
Log4a 基于AOP和ThreadLocal实现的一个简单Http API日志记录模块 github地址 : https://github.com/EalenXie/log4a 在API每次被请求时 ...
- [ Python入门教程 ] Python中日志记录模块logging使用实例
python中的logging模块用于记录日志.用户可以根据程序实现需要自定义日志输出位置.日志级别以及日志格式. 将日志内容输出到屏幕 一个最简单的logging模块使用样例,直接打印显示日志内容到 ...
- 日志记录模块logging
在python中,日志记录显示有两种方式,一种是保存在文件和打印屏幕上,一种保存在文件中. 第一种,直接保存在文件中. import logging #日志模块,方便记录日志 # 下面是配置日志记录格 ...
- python基础语法13 内置模块 subprocess,re模块,logging日志记录模块,防止导入模块时自动执行测试功能,包的理论
subprocess模块: - 可以通过python代码给操作系统终端发送命令, 并且可以返回结果. sub: 子 process: 进程 import subprocess while Tru ...
- nodejs之log4js日志记录模块简单配置使用
在我的一个node express项目中,使用了log4js来生成日志并且保存到文件里,生成的文件如下: 文件名字叫:access.log 如果在配置log4js的时候允许了同时存在多个备份log文件 ...
- python3 logging 日志记录模块
#coding:utf-8 import logginglogging.basicConfig(filename='log1.log', format='%(asctime)s -%(name)s-% ...
- Elmah 日志记录组件
http://www.cnblogs.com/jys509/p/4571298.html 简介 ELMAH(Error Logging Modules and Handlers)错误日志记录模块和处理 ...
随机推荐
- [Python_5] Python 线程
0. 说明 Python 线程笔记 1. 低级 API # -*-coding:utf-8-*- """ 线程 """ "&quo ...
- redis 配置文件示例
# redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位,# 通常的格式就是 1k 5gb 4m 等酱紫:## 1k => 1000 bytes# 1kb =& ...
- PyQt5--QColorDiaglog
# -*- coding:utf-8 -*- ''' Created on Sep 17, 2018 @author: SaShuangYiBing Comment: ''' import sys f ...
- [python]pip 版本9.0.1升级到10.0.1故障解决办法
问题背景: 在做android自动化时使用到第三方库uiautomator时,提示要安装,但安装该uiautomator库时提示当前的pip版本偏低,需要安装10.0.1版本方可.但在升级到升级到该版 ...
- 【Ansible 文档】【译文】Windows 支持
see also:List of Windows Modules Windows Support Windows 支持 Windows: How Does It Work Windows:如何工作 正 ...
- BZOJ5102:[POI2018]Prawnicy(贪心,堆)
Description 定义一个区间(l,r)的长度为r-l,空区间的长度为0. 给定数轴上n个区间,请选择其中恰好k个区间,使得交集的长度最大. Input 第一行包含两个正整数n,k(1<= ...
- ES6标准简介之Babel转码器解说
ES6是ECMAScript 6的简称,是JavaScript语言的下一代标准,现在基于jquery库的前端开发js所使用的标准是ES5(ECMAScript 5).ES6已于2015年6月正式发布. ...
- 晚上打开eclipse时碰到这个问题 :Workspace in use or cannot be created, choose a different one.
晚上打开eclipse时碰到这个问题 :Workspace in use or cannot be created, choose a different one. 网上看到这方面的解决方式: 原因: ...
- leetcode25—Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Python2.7-struct模块
struct模块 处理二进制数据,与C语言交互,可以较为方便的对C语言的struct类型和python中的数据进行转换 主要是用于将int,char之类的C语言中基础数据pack至一个二进制流的字符串 ...