c++ log

UtilLog.h

#pragma once


#include <fstream>
#include "StringTools.h"
#include "GameTools.h"

#include "cocos2d.h"
USING_NS_CC;


class UtilLog
{
public:
	UtilLog(std::string strLog);
	~UtilLog();

	static void Log(const char *format, ...);
	static void LogGBK(const char *format, ...);

	static void AddLog(std::string strLog);
	static void PrintLog();
private:
	std::string m_strLog;
	long long m_llStartTime;

	static std::string m_strLogBuff;
};

#if COCOS2D_DEBUG > 0
#define UtilLog_Start(X) UtilLog infoLog(X);

#define UtilLog_Log(X) {\
	std::stringstream ss;\
	ss << #X << ":" << X << " ";\
	UtilLog::Log(ss.str().c_str()); }

#define UtilLog_Error(X) {\
	std::stringstream ss;\
	ss << "!!!Error!!! :" << X;\
	UtilLog::Log(ss.str().c_str()); }

#define UtilLog_Success {\
	UtilLog::Log("Success!"); }

#define UtilLog_Add(X) {\
	std::stringstream ss;\
	ss << #X << ":" << X << " ";\
	UtilLog::AddLog(ss.str()); }

#define UtilLog_Print  UtilLog::PrintLog();

#else
#define UtilLog_Start(X) UtilLog infoLog(X);
#define UtilLog_Log(X)
#define UtilLog_Error(X)
#define UtilLog_Success
#define UtilLog_Add(X)
#define UtilLog_Print
#endif
	

UtilLog.cpp

#include "UtilLog.h"

std::string UtilLog::m_strLogBuff = "";

UtilLog::UtilLog(std::string strLog)
{
#if COCOS2D_DEBUG > 0
	m_strLog = StringTools::getInstance()->GBKToUTF8(strLog.c_str());
	std::string strOut = "\n--start-- " + m_strLog;
	CCLOG(strOut.c_str());

	m_llStartTime = GameTools::getInstance()->millisecondNow();
	m_strLogBuff = "";
#endif
}

UtilLog::~UtilLog()
{
#if COCOS2D_DEBUG > 0
	int nUseTime = GameTools::getInstance()->millisecondNow() - m_llStartTime;
	std::string strUseTime = StringTools::getInstance()->toString(nUseTime);
	std::string strOut = "--end-- Use time:" + strUseTime + "ms";
	CCLOG(strOut.c_str());
#endif
}

void UtilLog::Log(const char *format, ...)
{
#if COCOS2D_DEBUG > 0
	va_list args;
	va_start(args, format);
	char buf[1024] = { 0 };
	vsnprintf(buf, 1024, format, args);


	std::string strOut = std::string("\t") + buf;
	CCLOG(strOut.c_str());
#endif
}

void UtilLog::LogGBK(const char *format, ...)
{
#if COCOS2D_DEBUG > 0
	va_list args;
	va_start(args, format);
	char buf[1024] = { 0 };
	vsnprintf(buf, 1024, format, args);
	va_end(args);

	std::string strOut = std::string("\t") + StringTools::getInstance()->GBKToUTF8(buf);
	CCLOG(strOut.c_str());
#endif
}

void UtilLog::AddLog(std::string strLog)
{
#if COCOS2D_DEBUG > 0
	m_strLogBuff += strLog;
#endif
}

void UtilLog::PrintLog()
{
#if COCOS2D_DEBUG > 0
	Log(m_strLogBuff.c_str());
	m_strLogBuff = "";
#endif
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值