首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> C++ 入門

Cocos2d-x 增強:優化 CCPlatformMacros.h 日誌輸出格式

     編輯:C++ 入門

Cocos2d-x 增強:優化 CCPlatformMacros.h 日誌輸出格式

修改 cocos2dx/platform/CCPlatformMacros.h,為 CCLOG 系列宏添加日誌級別標籤,便於過濾與調試。

修改內容

錯誤日誌

// 第 218 行左右
#define CCLOGERROR(format, ...) cocos2d::CCLog("[error]" format, ##__VA_ARGS__)

資訊日誌(新增)

// 第 224 行左右
#define CCLOGERROR(format, ...) cocos2d::CCLog("[error]" format, ##__VA_ARGS__)
#define CCLOGINFO(format, ...) cocos2d::CCLog("[info]" format, ##__VA_ARGS__)

使用示例

CCLOGERROR("Failed to load resource: %s", filename);
CCLOGINFO("Player entered level %d", level);

輸出效果

[error] Failed to load resource: hero.png
[info] Player entered level 3

優勢

  • 日誌級別一目瞭然,支援 grep/IDE 過濾。
  • 格式統一,便於日誌分析工具解析。
  • 零性能開銷,僅編譯期字符串拼接。

擴展建議

  • 添加 [warn], [debug] 等級別。
  • 配合 __FILE__, __LINE__, __FUNCTION__ 宏輸出位置信息。
  • 發布版通過宏控制關閉 CCLOGINFO/CCLOGDEBUG。

微小的宏改動能大幅提升團隊協作開發中的日誌可讀性與調試效率,是引擎層面的「低成本高收益」優化。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved