用C++实现一个Log系统
提要
近期在写一些C++的图形代码,在调试和測试过程中都会须要在终端打印一些信息出来。
之前的做法是直接用
std::cout<<"Some Word"<<std::endl;
这样做事实上非常的麻烦,每次都要打非常多的字母还有特殊符号,除去我要打印的内容。还须要按下28下键盘,简直不能忍!
參考Unity里面的打log的方式
Debug.Log("Some Word");
或者Qt中的处理方式
qDebug() << "Some Word";
这两种都方便太多。
今天要实现的Log系统须要满足的特性有:
1.非常方便地在终端打印各种类型数据信息;
2.能够区分Log等级;
3.打印信息的同一时候能够提供打印语句的文件,函数名,行号
类说明
简单地画了下UML,主要分为以下几个类
简单说一下类的作用
MessageLogContext
记录Log的上下文,也就是Log处在的文件。函数名,行号。
MessageLogger
基本的Log类,提供了上次调用的一些接口。注意一下这个宏比較有意思
qDebug MessageLogger(__FILE__, __FUNCTION__, __LINE__).debug
这样当使用
qDebug()
的时候,
宏替换就直接转换成了MessageLogger的构造函数
MessageLogger(__FILE__, __FUNCTION__, __LINE__).debug()
等于是先构造MessageLogger,然后调用这个对象的debug()方法。
Debug
详细处理Debug信息的类。
用了一个内部Stream结构体来记录Debug信息,记得在使用前要new。析构的时候delete掉。
重构了非常多的<<方法,就是为了能处理多种数据类型,包含自己定义的类。还能够通过模板来打印stl里面的东西。
LogToConsole是将log打印信息到终端的函数。在析构函数中会被调用。假设想要实现更加炫酷的打印log方式(各种颜色),扩展这个函数就好了。
整个Log的流程例如以下图
測试代码
void DebugTest()
{
Vector2 v = Vector2(1, 1);
Vector2 v2 = Vector2(2, 1);
Vector3 v3 = Vector3(0, 2, 1);
Vector3 v4 = Vector3(0, 2, 1);
Vector3 v5 = Vector3(23, 112, 22);
Vector3 v6 = Vector3(23, 112, 22);
std::vector<Vector3> vec;
vec.push_back(v3);
vec.push_back(v4);
vec.push_back(v5);
vec.push_back(v6);
vec.push_back(v6);
vec.push_back(v6);
vec.push_back(v6);
vec.push_back(v6);
std::string testStr = "vector Test";
qDebug() << "Hello Debug";
qDebug() <<""<< v << v2<< v3;
qDebug() << v3;
qWarning() << vec;
}
执行结果
代码清单
#pragma once
#include <string> class MessageLogContext
{
public:
MessageLogContext() : line(0), file(0), function(0) {}
MessageLogContext(const char *fileName, const char *functionName, int lineNumber)
: file(fileName), function(functionName), line(lineNumber) {} int line;
const char *file;
const char *function;
void copy(const MessageLogContext &logContext)
{
this->file = logContext.file;
this->line = logContext.line;
this->function = logContext.function;
} private:
friend class MessageLogger;
friend class Debug;
};
#pragma once
#define qDebug MessageLogger(__FILE__, __FUNCTION__, __LINE__).debug
#define qInfo MessageLogger(__FILE__, __FUNCTION__, __LINE__).info
#define qWarning MessageLogger(__FILE__, __FUNCTION__, __LINE__).warning
#define qCritical MessageLogger(__FILE__, __FUNCTION__, __LINE__).critical
#define qFatal MessageLogger(__FILE__, __FUNCTION__, __LINE__).fatal
#include "Debug.h"
#include "MessageLogContext.h" class MessageLogger
{
public:
MessageLogger() : context(){}
MessageLogger(const char *fileName, const char *functionName, int lineNumber)
: context(fileName, functionName, lineNumber) {} Debug info() const;
Debug warning() const;
Debug critical() const;
Debug debug() const; protected:
private:
MessageLogContext context;
};
#include "Log.h" Debug MessageLogger::debug() const
{
std::string debug = "debug";
Debug dbg = Debug(&debug);
MessageLogContext &ctxt = dbg.stream->context;
ctxt.copy(context);
dbg.stream->logType = Info;
return dbg;
} Debug MessageLogger::info() const
{
Debug dbg = Debug();
MessageLogContext &ctxt = dbg.stream->context;
ctxt.copy(context);
dbg.stream->logType = Info;
return dbg;
} Debug MessageLogger::warning() const
{
Debug dbg = Debug();
MessageLogContext &ctxt = dbg.stream->context;
ctxt.copy(context);
dbg.stream->logType = Warning;
return dbg;
} Debug MessageLogger::critical() const
{
Debug dbg = Debug();
MessageLogContext &ctxt = dbg.stream->context;
ctxt.copy(context);
dbg.stream->logType = Error;
return dbg;
}
#pragma once
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdint.h>
#include <sstream>
#include "Math/Vector2.h"
#include "Math/Vector3.h"
#include <vector>
//#include "Log.h"
#include "MessageLogContext.h"
enum LogType
{
Info,
Warning,
Error,
Default,
};
class Debug
{
public:
struct Stream {
Stream():ss(), space(true), context() {}
Stream(std::string *s) :ss(*s), space(true), context(){}
std::ostringstream ss;
bool space;
MessageLogContext context;
LogType logType;
} *stream;
Debug() : stream(new Stream()) {}
inline Debug(std::string *s) : stream(new Stream(s)) {}
~Debug();
inline Debug &operator<<(bool t) { stream->ss<<(t ? "true" : "false"); return maybeSpace(); }
inline Debug &operator<<(char t) { stream->ss<< t; return maybeSpace(); }
inline Debug &operator<<(signed short t) { stream->ss << t; return maybeSpace(); }
inline Debug &operator<<(unsigned short t) { stream->ss << t; return maybeSpace(); }
inline Debug &operator<<(std::string s) { stream->ss << s; return maybeSpace(); }
inline Debug &operator<<(const char* c) { stream->ss << c; return maybeSpace(); }
inline Debug &operator<<(Vector2 vec) { stream->ss << "(" << vec.x <<","<< vec.y<<")"; return maybeSpace(); }
inline Debug &operator<<(Vector3 vec) { stream->ss << "(" << vec.x << "," << vec.y <<"," << vec.z << ")"; return maybeSpace(); }
inline Debug &space() { stream->space = true; stream->ss << ' '; return *this; }
inline Debug &nospace() { stream->space = false; return *this; }
inline Debug &maybeSpace() { if (stream->space) stream->ss << ' '; return *this; }
template <typename T>
inline Debug &operator<<(const std::vector<T> &vec)
{
stream->ss << '(';
for (int i = 0; i < vec.size(); ++i) {
stream->ss << vec.at(i);
stream->ss << ", ";
}
stream->ss << ')';
return maybeSpace();
}
void LogToConsole(LogType type, const MessageLogContext &context, std::string logBuffer);
private:
static Debug* _instance;
};
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdint.h>
#include <sstream>
#include "Math/Vector2.h"
#include "Math/Vector3.h"
#include <vector>
//#include "Log.h"
#include "MessageLogContext.h" enum LogType
{
Info,
Warning,
Error,
Default,
}; class Debug
{
public:
struct Stream {
Stream():ss(), space(true), context() {}
Stream(std::string *s) :ss(*s), space(true), context(){}
std::ostringstream ss;
bool space;
MessageLogContext context;
LogType logType;
} *stream; Debug() : stream(new Stream()) {}
inline Debug(std::string *s) : stream(new Stream(s)) {}
~Debug();
inline Debug &operator<<(bool t) { stream->ss<<(t ? "true" : "false"); return maybeSpace(); }
inline Debug &operator<<(char t) { stream->ss<< t; return maybeSpace(); }
inline Debug &operator<<(signed short t) { stream->ss << t; return maybeSpace(); }
inline Debug &operator<<(unsigned short t) { stream->ss << t; return maybeSpace(); }
inline Debug &operator<<(std::string s) { stream->ss << s; return maybeSpace(); }
inline Debug &operator<<(const char* c) { stream->ss << c; return maybeSpace(); }
inline Debug &operator<<(Vector2 vec) { stream->ss << "(" << vec.x <<","<< vec.y<<")"; return maybeSpace(); }
inline Debug &operator<<(Vector3 vec) { stream->ss << "(" << vec.x << "," << vec.y <<"," << vec.z << ")"; return maybeSpace(); }
inline Debug &space() { stream->space = true; stream->ss << ' '; return *this; }
inline Debug &nospace() { stream->space = false; return *this; }
inline Debug &maybeSpace() { if (stream->space) stream->ss << ' '; return *this; } template <typename T>
inline Debug &operator<<(const std::vector<T> &vec)
{
stream->ss << '(';
for (int i = 0; i < vec.size(); ++i) {
stream->ss << vec.at(i);
stream->ss << ", ";
}
stream->ss << ')';
return maybeSpace();
} void LogToConsole(LogType type, const MessageLogContext &context, std::string logBuffer); private:
static Debug* _instance;
};
#include "Debug.h"
Debug::~Debug()
{
LogToConsole(stream->logType, stream->context, stream->ss.str());
delete stream;
}
void Debug::LogToConsole(LogType type, const MessageLogContext &context, std::string logBuffer)
{
std::string logString;
switch (type)
{
case Error:
logString.append("Error! ");
break;
case Info:
//logString.append("");
break;
case Warning:
logString.append("Warning! ");
break;
default:
break;
}
logString.append(logBuffer);
logString.append("......");
logString.append(context.file);
logString.append(" ");
logString.append(context.function);
logString.append("()");
std::cout << logString <<" line: " << context.line << " " << std::endl;
//logString.append(context.line);
}
{
LogToConsole(stream->logType, stream->context, stream->ss.str());
delete stream;
} void Debug::LogToConsole(LogType type, const MessageLogContext &context, std::string logBuffer)
{
std::string logString;
switch (type)
{
case Error:
logString.append("Error! ");
break;
case Info:
//logString.append("");
break;
case Warning:
logString.append("Warning! ");
break;
default:
break;
}
logString.append(logBuffer);
logString.append("......"); logString.append(context.file);
logString.append(" ");
logString.append(context.function);
logString.append("()"); std::cout << logString <<" line: " << context.line << " " << std::endl; //logString.append(context.line);
}
參考
Qt source code
Qt Documentation http://doc.qt.io/qt-4.8/qdebug.html
http://www.cplusplus.com/
用C++实现一个Log系统的更多相关文章
- 自己封装一个Log模块
Unity自己有log系统,为什么要自己封装一个 1.不好用,只能在pc上记录log文件,移动平台是没有的 2.在开发时期的log,不想在正式版里面出现.没有一个统一的开关来控制是不是要显示log,要 ...
- 使用monit搭建一个监控系统
上周用monit搭建或者说定制了一个监控系统,来监控服务器发生事情.当然了主要是监控异常,因为我们的产品属于服务器类型,很多进程都daemon,要不停的运行.我们搭建监控目的不过是出现问题能够及时的知 ...
- 超强教程:如何搭建一个 iOS 系统的视频直播 App?
现今,直播市场热火朝天,不少人喜欢在手机端安装各类直播 App,便于随时随地观看直播或者自己当主播.作为开发者来说,搭建一个稳定性强.延迟率低.可用性强的直播平台,需要考虑到部署视频源.搭建聊天室.优 ...
- Java核心知识点学习----线程中如何创建锁和使用锁 Lock,设计一个缓存系统
理论知识很枯燥,但这些都是基本功,学完可能会忘,但等用的时候,会发觉之前的学习是非常有意义的,学习线程就是这样子的. 1.如何创建锁? Lock lock = new ReentrantLock(); ...
- [km] 如何判断一个直播系统是否使用的是RTMP
如何判断一个直播系统是否使用的是RTMP from: http://peiqiang.net/2016/03/21/how-to-judge-whether-rtmp-is-used-by-a-liv ...
- log4j:ERROR setFile(null,true) call failed.java.io.FileNotFoundException: ..\logs\2010-1-19.log (系统找不到指定的路径。)
log4j:ERROR setFile(null,true) call failed.java.io.FileNotFoundException: ..\logs\2010-1-19.log (系统找 ...
- 如何设计一个RPC系统
版权声明:本文由韩伟原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/162 来源:腾云阁 https://www.qclou ...
- petshop4.0 具体解释之中的一个(系统架构设计)
前言:PetShop是一个范例,微软用它来展示.Net企业系统开发的能力.业界有很多.Net与J2EE之争,很多数据是从微软的PetShop和Sun的PetStore而来.这样的争论不可避免带有浓厚的 ...
- 架构漫谈:自己开发一个Log框架
前言 在日常开发中我们常常都会用到写日志的功能,现在网上的写Log的框架有很多,但是对于我个人而言,过于庞大:我们往往只为了使用框架中的某一个功能就不得不引用整个框架. 所以,我们今天就来自己动手开发 ...
随机推荐
- 【编码】【转发】enca 转换编码
enca用法如下: enca -L zh_CN file 检查文件的编码 enca -L zh_CN -x UTF-8 file 将文件编码转换为"UTF-8"编码 enca -L ...
- 数据结构( Pyhon 语言描述 ) — —第10章:树
树的概览 树是层级式的集合 树中最顶端的节点叫做根 个或多个后继(子节点). 没有子节点的节点叫做叶子节点 拥有子节点的节点叫做内部节点 ,其子节点位于层级1,依次类推.一个空树的层级为 -1 树的术 ...
- cs231n课程索引
课程资源 课程官网 课程视频-youtube 课程视频-字幕版 官方笔记 官方笔记-中文版 课程作业参考答案
- python之动态参数 *args,**kwargs(聚合,打散)
一.函数的动态参数 *args,**kwargs, 形参的顺序1.你的函数,为了拓展,对于传入的实参数量应该是不固定,所以就需要用到万能参数,动态参数,*args, **kwargs 1,*args ...
- android sdk 下载
不知道是因为最近kaihui还是怎么的,打开android sdk官方网站特别的慢,想下载最新版本的platform几乎变成不可能完成的任务,不知道为什么Google不像Apache那样在各国设立镜像 ...
- java 中 image 和 byte[] 相互转换
java 中 image 和 byte[] 相互转换可恶的…………其实也挺好的 只是把好不容易写出来的东西记下来,怕忘了…… 下面,我来介绍一个简单的 byte[] to image, 我们只需要 ...
- 关于面试总结-app测试面试题
前言 现在面试个测试岗位,都是要求全能的,web.接口.app啥都要会测,那么APP测试一般需要哪些技能呢? 面试app测试岗位会被问到哪些问题,怎样让面试管觉得你对APP测试很精通的样子? 本篇总结 ...
- Course Machine Learning Note
Machine Learning Note Introduction Introduction What is Machine Learning? Two definitions of Machine ...
- nginx反向代理,负载均衡,动静分离,rewrite地址重写介绍
一.rewrite地址重写 地址转发后客户端浏览器地址栏中的地址显示是不变的,而地址重写后地址栏中的地址会变成正确的地址. 在一次地址转发过程中只会产生一次网络请求,而一次地址重写产生两次请求. 地址 ...
- Tomcat基础配置(一)
详情请看散尽浮华的tomcat相关配置技巧梳理 本次只用于自己的查看,谢谢作者的谅解. tomcat常用架构:1)nginx+tomcat:即前端放一台nginx,然后通过nginx反向代理到tomc ...