<thead id="kqoxr"></thead>
<blockquote id="kqoxr"></blockquote>
<legend id="kqoxr"><li id="kqoxr"></li></legend>
    1. <sub id="kqoxr"></sub>
      1. <blockquote id="kqoxr"><i id="kqoxr"><noscript id="kqoxr"></noscript></i></blockquote>
        <pre id="kqoxr"></pre>

        91午夜福利在线观看精品,亚洲综合色婷婷中文字幕,亚洲日本欧洲二区精品,竹菊影视欧美日韩一区二区三区四区五区,亚洲色在线V中文字幕,国产精品毛片av999999,精品视频不卡免费观看,亚洲全乱码精品一区二区

        用C++實現一個Log系統 -電腦資料

        電腦資料 時間:2019-01-01
        【m.r9876.cn - 電腦資料】

           

        提要

            最近在寫一些C++的圖形代碼,在調試和測試過程中都會需要在終端打印一些信息出來,

        用C++實現一個Log系統

        。之前的做法是直接用

        std::cout<<some pre=""></p><p>    這樣做其實非常的麻煩,每次都要打很多的字母還有特殊符號,除去我要打印的內容,還需要按下28下鍵盤,簡直不能忍!</p><p>    參考Unity里面的打log的方式</p>

            或者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;}</vector3>

            運行結果

           

        代碼清單

        MessageLogContext.h

           

        #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;};</string>

            Log.h

        #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.hclass 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;};

            Log.cpp

           

        #include Log.hDebug 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;}

            Debug.h

        #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.henum 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;};</t></typename></vector></sstream></stdint.h></cstdlib></string></fstream></iomanip></iostream>

            Debug.cpp

        #include Debug.hDebug::~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);}

           

        最新文章
        主站蜘蛛池模板: 无码免费大香伊蕉在人线国产| 一本色道精品久久一区二区三区| 人妻丰满熟妇AV无码区APP| www.jizzjizz| 国产精品中文一区二区| 精品国产一区av天美传媒| 中文字幕制服丝袜| 黄梅县| 999国产精品永久免费视频精品久久| 亚洲肥老太bbw中国熟女| 国产午夜福利高清在线观看| 国产不卡一区二区精品| 亚洲国产成人av| 婷婷久久综合九色综合97最多收藏| 污网站在线免费观看| 国产JJIZZ女人多水喷水| 日韩有码中文字幕av| 国产成人久久| 精产国品一二三产品蜜桃| 福利导航网| 久热这里只有精品视频6| 亚洲区中文字幕| 华人在线亚洲欧美精品| 天堂一区二区三区av| 久草网视频在线观看| 亚洲AV日韩AV综合在线观看| 国产精品久久这里只有精品| 色窝窝无码一区二区三区色欲| 中文字幕人妻熟女人妻a片| 国产精品黄色大片在线看| bt天堂新版中文在线| 欧美日韩在线精品一区二区三区| 人妻日韩精品中文字幕| 加勒比一区二区三区精品| 久久久99久久久国产精品| 国产成人亚洲精品狼色在线 | 99视频在线精品免费观看6| 久操香蕉| 天堂国产一区二区三区四区不卡 | 国产欧美日本| 人妻在线无码一区二区三区|