标签:游戏
游戏log是非常关键的,对于追踪bug和查看一些信息,所以游戏里面是大量的log输出,sprintf的格式非常难用,而且对格式有较高的要求。
下面一种优化方案如下:
// MVC.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class EndLogOut{};
class LogOut
{
public:
LogOut& operator<<(int a)
{
ss<<a;
return *this;
}
LogOut& operator<<(float a)
{
ss<<a;
return *this;
}
LogOut& operator<<(bool a)
{
ss<<a;
return *this;
}
LogOut& operator<<(char* a)
{
ss<<".."<<a<<"..";
return *this;
}
LogOut& operator<<(std::string a)
{
ss<<".."<<a<<"..";
return *this;
}
std::string operator<<(std::basic_ostream<char, char_traits<char> >& _Ostr)
{
ss>>logString;
return logString;
}
std::string operator<<(EndLogOut)
{
ss>>logString;
cout<<logString<<endl;
return logString;
}
std::string GetString()
{
ss>>logString;
return logString;
}
private:
stringstream ss;
std::string logString;
int type;
};
#define ELOGOUT LogOut()
int _tmain(int argc, _TCHAR* argv[])
{
LogOut log;
char* s = "ssss";
std::string ss = "oooo";
float ff = 1.09f;
bool bb = true;
log<<1<<"test"<<s<<ss<<ff<<bb;
ELOGOUT<<1<<"test"<<s<<ss<<ff<<bb<<EndLogOut();
//cout<<log.GetString()<<endl;
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:游戏
原文地址:http://blog.csdn.net/guxuxin/article/details/47347317