#ifndef _X_LOG_VIEW_H_
#define _X_LOG_VIEW_H_
#include <cocos2d.h>
#include <vector>
#include <cstring>
#include <cstdarg>
USING_NS_CC;
namespace zdh
{
using std::vector;
using std::
string;
///滚动文本日志
/*
这是一个模板类,模板参数N主要是设置一行日志缓冲的字节数。
*/
template<
int N = 128>
class XLogView :
public CCLabelTTF
{
public:
XLogView()
:m_LogMaxLine(10)
{
}
//一组create,直接从CCLabelTTF复制过来
static XLogView * create(
const char *
string,
const char *fontName,
float fontSize)
{
return XLogView::create(
string, fontName, fontSize, CCSizeZero, kCCTextAlignmentCenter, kCCVerticalTextAlignmentTop);
}
static XLogView * create(
const char *
string,
const char *fontName,
float fontSize,
const CCSize& dimensions, CCTextAlignment hAlignment)
{
return XLogView::create(
string, fontName, fontSize, dimensions, hAlignment, kCCVerticalTextAlignmentTop);
}
static XLogView * create(
const char *
string,
const char *fontName,
float fontSize,
const CCSize& dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment)
{
XLogView *pRet =
new XLogView();
if (pRet && pRet->initWithString(
string, fontName, fontSize, dimensions, hAlignment, vAlignment))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
static XLogView * createWithFontDefinition(
const char *
string, ccFontDefinition &textDefinition)
{
XLogView *pRet =
new XLogView();
if (pRet && pRet->initWithStringAndTextDefinition(
string, textDefinition))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
//设置滚动日志的最大行数
void setLogMaxLine(
int param_max_line)
{
if (param_max_line < 1)
{
m_LogMaxLine = 1;
}
else if (param_max_line > 1024)
{
m_LogMaxLine = 1024;
}
else m_LogMaxLine = param_max_line
}
//取滚动日志的最大行数
int getLogMaxLine()
const
{
return m_LogMaxLine;
}
//清除日志
void Clear()
{
m_LogList.clear();
setString("");
}
//生成日志
void VLog(
const char * paramFormat, va_list param_argptr)
{
char sTemp[N];
vsnprintf(sTemp,
sizeof(sTemp), paramFormat, param_argptr);
//删除超出行数的日志
while ((
int)m_LogList.size() > m_LogMaxLine)
{
m_LogList.erase(m_LogList.begin());
}
m_LogList.push_back(sTemp);
//生成日志
m_LogTemp = "";
size_t iCount = m_LogList.size();
for (size_t i = 0; i < iCount; i++)
{
if (m_LogTemp.length() >0) m_LogTemp += "\n";
m_LogTemp += m_LogList[i];
}
setString(m_LogTemp.c_str());
//输出到控制台窗口
#ifdef _MSC_VER
OutputDebugStringA(sTemp);
#endif
}
void Log(
const char * paramFormat,
)
{
va_list argptr;
va_start(argptr, paramFormat);
VLog(sTemp,
sizeof(sTemp), paramFormat, argptr);
va_end(argptr);
}
private:
string m_LogTemp;
//生成日志的临时变量
int m_LogMaxLine;
//日志最大的行数
vector<
string> m_LogList;
//日志内容
};
}
#endif