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
}