游戏log格式的优化<<代替%d、%s

游戏log是非常关键的,对于追踪bug和查看一些信息,所以游戏里面是大量的log输出,sprintf的格式非常难用,而且对格式有较高的要求。
下面一种优化方案如下:

// MVC.cpp : 定义控制台应用程序的入口点。
//

// MVC.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class EndLogOut{};
class LogOut
{
public:
    LogOut(int type)
    {
        m_type = type;
    }

    ~LogOut()
    {
        logString = ss.str();
        cout<<logString<<endl;
    }

    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)
    {
        logString = ss.str();
        return logString;
    }

    std::string operator<<(EndLogOut)
    {
        logString = ss.str();
        cout<<logString<<endl;
        return logString;
    }

    std::string GetString()
    {
        logString = ss.str();
        return logString;
    }

private:
    ostringstream ss;
    std::string logString;
    int m_type;
};

#define ELOGOUT LogOut(1)
#define ENGLOG EndLogOut()
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;
    //cout<<log.GetString()<<endl;
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
解释下面这段cmd文件的作用:CLS REM The following is required in all INSTALL.CMD files if exist c:\system.sav\util\SetVariables.cmd Call c:\system.sav\util\SetVariables.cmd set version=1.05 Set block=%~dp0 set errcodeinstallinstallinstallinstallinstallinstallinstall=0 CD /D "%block%" set Log_Folder=%~d0\programdata\HP\logs if not exist "%Log_Folder%" md "%Log_Folder%" set Install_Log=%Log_Folder%\HotkeyInstall.log REM Remove the REM from the next line if your component does not support Silent Install (Application Recovery) REM Erase /F /Q *.CVA REM Add the command-line to have your component to be installed properly Pushd src if exist "%~dp0src\Uninstall.cmd" ( call "%~dp0src\Uninstall.cmd" ) if %errorlevel% NEQ 0 ( echo. >> "%Install_log%" echo *exit /b %errcodeinstall% >> "%Install_log%" echo. >> "%Install_log%" echo ^<^< %~f0 >> "%Install_log%" echo ^<^< %date% %time% >> "%Install_log%" echo. >> "%Install_log%" goto :END ) if exist "%~dp0src\InstallFusion.cmd" ( call "%~dp0src\InstallFusion.cmd" ) if %errorlevel% NEQ 0 ( echo. >> "%Install_log%" echo *exit /b %errcodefusion% >> "%Install_log%" echo. >> "%Install_log%" echo ^<^< %~f0 >> "%Install_log%" echo ^<^< %date% %time% >> "%Install_log%" echo. >> "%Install_log%" goto :END ) if exist "%~dp0src\InstallDriver.cmd" ( call "%~dp0src\InstallDriver.cmd" ) if %errorlevel% NEQ 0 ( echo. >> "%Install_log%" echo *exit /b %errcodedriver% >> "%Install_log%" echo. >> "%Install_log%" echo ^<^< %~f0 >> "%Install_log%" echo ^<^< %date% %time% >> "%Install_log%" echo. >> "%Install_log%"goto :END ) if exist "%~dp0src\InstallApp.cmd" ( call "%~dp0src\InstallApp.cmd" ) if %errorlevel% NEQ 0 ( echo. >> "%Install_log%" echo *exit /b %errcodeapp% >> "%Install_log%" echo. >> "%Install_log%" echo ^<^< %~f0 >> "%Install_log%" echo ^<^< %date% %time% >> "%Install_log%" echo. >> "%Install_log%" goto :END ) :END Popd REM Erase failure flag file when install succeeded. Most applications return zero to indicate success. ECHO %ERRORLEVEL% >> FAILURE.FLG IF %ERRORLEVEL% EQU 0 ERASE /F /Q FAILURE.FLG IF %ERRORLEVEL% EQU 3010 ERASE /F /Q FAILURE.FLG echo %date% %time% "Fusion=" %errcodefusion% >> %install_log% echo %date% %time% "Driver=" %errcodedriver% >> %install_log% echo %date% %time% "App=" %errcodeapp% >> %install_log% EXIT /B %ERRORLEVEL%
07-20
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值