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下wxpython程序国际化的实践(中文英文切换)
一.什么是python的国际化(I18N) 有关I18N,百度上解释一大堆,个人比较喜欢这个说法. i18n是 Internationalization 这个英文的简写,因为International ...
- MySQL二进制日志文件Binlog的三种格式以及对应的主从复制中三种技术
二进制日志文件Binlog的格式主要有三种: 1.Statement:基于SQL语句级别的Binlog,每条修改数据的SQL都会保存到Binlog里面. 2.ROW:基于行级别,每一行数据的变化都会记 ...
- Azure IoT 预配置解决方案
Azure IoT 预配置解决方案 Sangyu Li © 2018 一.什么是Azure IoT 预配置解决方案? 如图,这就是Azure IoT Suite中 Provision solutio ...
- app的描述-软件的描述
app的描述=需求文档+接口文档+程序架构+工程结构. 程序架构:类结构图: 需求文档:业务逻辑-->时序图.
- Articulate Presenter文字乱码的排除
Articulate Presenter乱码的问题如何设置? 字体乱码的设置: 1.首先如果ppt中有中文内容,肯定需要将Articulate Presenter的Character Set设置为No ...
- BZOJ4735:你的生命已如风中残烛(组合数学)
Description 众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习.但是今天六花酱不想做数学题,于是他们开始打牌. 现在他们手上有m张不同的牌,牌有两种:普通牌和功能牌.功能牌一 ...
- [CTSC2018]混合果汁
题目连接:https://www.luogu.org/problemnew/show/P4602 因为题中说是让最小值最大,所以自然想到二分答案.对于每一个二分的值,判断是否合法,若合法,在右区间二分 ...
- rest_framework源码分析
CBV&APIView '''原生django as_view方法''' class View(object): http_method_names = ['get', 'post', 'pu ...
- Docker技术入门与实战 第二版-学习笔记-5-容器-命令及限制内存与cpu资源
1.启动容器 启动容器有两种方式: 基于镜像新建一个容器并启动 将在终止状态(stopped)的容器重新启动 1)新建并启动——docker run 比如在启动ubuntu:14.04容器,并输出“H ...
- OpenCV——使用多边形包围轮廓