Cocos2dx开发日志2-番外篇

由于采用了控制台显示log就有两个问题要解决

1. log的保存

由于没有找到比较好的重定向方案(既要显示到控制台又要保存到文件)

所以改用关闭时保存来实现(这样便不能处理异常关闭时的情况了不完美)

修改main.cpp的相应部分

#ifdef USE_WIN32_CONSOLE    CHAR_INFO *chiBuffer=(CHAR_INFO*)malloc(160*9999*sizeof(CHAR_INFO));    COORD coordBufSize={0};    COORD coordBufCoord={0};     SMALL_RECT srctReadRect={0};    coordBufSize.Y = 9999;     coordBufSize.X = 160;     srctReadRect.Bottom = 9999;    srctReadRect.Right = 160;     ReadConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE),chiBuffer,coordBufSize,coordBufCoord,&srctReadRect);    FILE *out=fopen("out.log","wb");int cur=0;int s=0;char buff[3];for(int i=0;i<160*9999;i++){        wchar_t c=chiBuffer[i].Char.UnicodeChar;if(c==0){break;}if(c<127){            s=1;            buff[0]=c;            fwrite(&buff,sizeof(char),1,out);        }elseif(c > 0x07FF){            s=2;            buff[0]=0xE0 | ((c >> 12) & 0x0F);            buff[1]=0x80 | ((c >> 6) & 0x3F);            buff[2]=0x80 | ((c >> 0) & 0x3F);            fwrite(&buff,sizeof(char),3,out);        }else{            s=2;            buff[0]=0x80 | ((c >> 6) & 0x1F);            buff[1]=0x80 | ((c >> 0) & 0x3F);            fwrite(&buff,sizeof(char),2,out);        }        cur+=s;if(cur>159){            fprintf(out,"\n");            cur=0;        }    }    fflush(out);    fclose(out);    FreeConsole();#endif

2. log的过滤

由于log本身不是采用我们的控件之类的来实现的所以只能做到颜色标记这步了

在AppDelegate.cpp中添加绑定的注册

绑定的代码

#include "cocos2d_specifics.hpp"#ifdef WIN32#include <windows.h>#ifdef USE_WIN32_CONSOLE#define SETCOLOR(color) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color)
#else#define SETCOLOR(color)
#endif#else#define NONE "\033[m"
#define RED "\033[0;32;31m"
#define LIGHT_RED "\033[1;31m"
#define GREEN "\033[0;32;32m"
#define LIGHT_GREEN "\033[1;32m"
#define BLUE "\033[0;32;34m"
#define LIGHT_BLUE "\033[1;34m"
#define DARY_GRAY "\033[1;30m"
#define CYAN "\033[0;36m"
#define LIGHT_CYAN "\033[1;36m"
#define PURPLE "\033[0;35m"
#define LIGHT_PURPLE "\033[1;35m"
#define BROWN "\033[0;33m"
#define YELLOW "\033[1;33m"
#define LIGHT_GRAY "\033[0;37m"
#define WHITE "\033[1;37m"
#endifchar *_cy_js_log_buf = NULL;void _cy_js_log(constchar *format, ...) {if (_cy_js_log_buf == NULL) {        _cy_js_log_buf = (char *)calloc(sizeof(char), kMaxLogLen+1);    }    va_list vl;    va_start(vl, format);int len = vsnprintf(_cy_js_log_buf, kMaxLogLen, format, vl);    va_end(vl);if (len) {// 增加颜色控制if(strstr(_cy_js_log_buf,"@")==_cy_js_log_buf){#ifdef WIN32if(strstr(_cy_js_log_buf,"@RED@")!=NULL){                SETCOLOR(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | BACKGROUND_RED);                    CCLOG("JS: %s", _cy_js_log_buf+5);            }elseif(strstr(_cy_js_log_buf,"@YELLOW@")!=NULL){                SETCOLOR(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | BACKGROUND_RED | BACKGROUND_GREEN);                    CCLOG("JS: %s", _cy_js_log_buf+8);            }elseif(strstr(_cy_js_log_buf,"@GREEN@")!=NULL){                SETCOLOR(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_GREEN);                    CCLOG("JS: %s", _cy_js_log_buf+7);            }            SETCOLOR(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);#elseif(strstr(_cy_js_log_buf,"@RED@")!=NULL){                CCLOG("JS: %s%s%s",RED,_cy_js_log_buf,NONE);             }elseif(strstr(_cy_js_log_buf,"@YELLOW@")!=NULL){                CCLOG("JS: %s%s%s",RED,_cy_js_log_buf,NONE);             }elseif(strstr(_cy_js_log_buf,"@GREEN@")!=NULL){                CCLOG("JS: %s%s%s",RED,_cy_js_log_buf,NONE);             }#endif}else{            CCLOG("JS: %s", _cy_js_log_buf);        }    }}JSBool _cy_log(JSContext* cx, uint32_t argc, jsval *vp){if (argc > 0) {        JSString *string = NULL;        JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "S", &string);if (string) {            JSStringWrapper wrapper(string);            _cy_js_log((char *)wrapper);        }    }return JS_TRUE;}void register_all_jsb_customs(JSContext* cx, JSObject* global){    JS_DefineFunction(cx, global, "log", _cy_log, 0, JSPROP_READONLY | JSPROP_PERMANENT);}


其中的颜色部分可以自行定义

对于android(logcat)部分可以忽略此设定

你可能感兴趣的:(style,开发,控制台,定向,cccccc)