kingBook

导航

Cocos2d-x 打印日志、CCLOG格式符号表

cocos2d-x-3.17.2 Window 10:

  1. CCLog(const char * pszFormat, ...): 位于 CCDeprecated.h下,此函数已被标记为弃用,不应该再用.

  2. log(const char * format, ...): 位于 CCConsole.h 下.

    log("hello");
    log("number: %d", 1997)
    
    // 输出:
    //hello
    //number: 1997
    
  3. CCLOG(format, ...): 位于 CCPlatformMacros.h 下, 注意所有字母都是大写.

    CCLOG("hello");
    CCLOG("number: %d", 1997)
    CCLOG("decimals: %ld", 650000L);
    CCLOG("Preceding with blanks: %10d", 1977);
    CCLOG("Preceding with zeros: %010d", 1977);
    CCLOG("Some different radixes: %d %x %o %#x %#o", 100, 100, 100, 100, 100);
    CCLOG("Floats: %4.2f %.0e %E", 3.1416, 3.1416, 3.1416);
    
    // 输出:
    //hello
    //number: 1997
    //decimals: 650000
    //Preceding with blanks:       1977
    //Preceding with zeros: 0000001977
    //Some different radixes: 100 64 144 0x64 0144
    //Floats: 3.14 3e+00 3.141600E+00
    
  4. 使用c++标准库中的打印日志函数。

    // stdio.h 如果已引入 cocos2d.h 则不必再引入
    std::printf(const char * _Format, ...)
    
    // #include <iostream>
    std::cout
    

    注意:Visual Studio 中需要打开 cmd 命令窗口才能看到输出.
    Visual Studio 中打开 cmd 命令窗口的方法:
    方法一:
    Visual Studio 中生成解决方案后,项目 -> 属性 -> 配置属性 -> 生成事件 -> 预先生成事件后期生成事件 -> 命令行,输入以内容即可:

     editbin /SUBSYSTEM:CONSOLE $(OUTDIR)\$(TargetFileName)
    

    方法二:
    在使用 printf 或 cout 前执行以下代码:
    代码1:

    // 别忘了加头文件
    // #include <tchar.h>
    
    _tsetlocale(LC_ALL,_T(""));
    ::AllocConsole();
    ::freopen("conout$","w",stdout);
    

    代码2:

    AllocConsole();
    freopen("CONIN$", "r", stdin);
    freopen("CONOUT$", "w", stdout);
    freopen("CONOUT$", "w", stderr);
    

    以上两份代码都可以在 Windows 下打开 cmd 命令窗口,例如在 AppDelegate.cppbool AppDelegate::applicationDidFinishLaunching() 函数的开头添加以上代码,Windows系统也可以在 {项目目录}\proj.win32\main.cpp 添加以上代码.也可以定义一个宏再加入代码方便开关,不用的时候的宏定义注释掉就可以了:

    #define USE_WIN32_CONSOLE
    
    #ifdef USE_WIN32_CONSOLE
    // enter code
    #endif
    

    printf、cout 使用示例:

    // 使用 printf 时,不会自动换行,需要加 "\n"
    printf("hello\n");
    printf("number: %d\n", 1997)
    
    std::cout << "hello cout" << std::endl;
    

    最后需要注意的是在 Visual Studio 中,如果不调试直接执行(Ctrl+F5),在输出面板是看不见输出的,此时用上述方法打开 cmd 命令窗口,在 cmd 命令窗口可以看到输出。

    附上 CCLOG、log、printf 使用的格式符号表
    image

posted on 2015-06-25 11:21  kingBook  阅读(421)  评论(0编辑  收藏  举报