| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- /****************************************************************************
- Copyright (c) 2013-2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- #include "base/CCLog.h"
- #include <stdio.h>
- #include <new>
- #include <algorithm>
- #include <string.h>
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- #include <io.h>
- #include <WS2tcpip.h>
- #include <Winsock2.h>
- #endif // (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- /** private functions */
- namespace
- {
- const size_t SEND_BUFSIZ = 512;
- const int MAX_LOG_LENGTH = 16*1024;
- //
- // Free functions to log
- //
-
- void _log(const char *format, va_list args)
- {
- int bufferSize = MAX_LOG_LENGTH;
- char* buf = nullptr;
-
- do
- {
- buf = new (std::nothrow) char[bufferSize];
- if (buf == nullptr)
- return; // not enough memory
-
- int ret = vsnprintf(buf, bufferSize - 3, format, args);
- if (ret < 0)
- {
- bufferSize *= 2;
-
- delete [] buf;
- }
- else
- break;
-
- } while (true);
-
- strcat(buf, "\n");
-
- #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
- #include <android/log.h>
- __android_log_print(ANDROID_LOG_DEBUG, "debug info", "%s", buf);
- #elif CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
- int pos = 0;
- int len = strlen(buf);
- char tempBuf[MAX_LOG_LENGTH + 1] = { 0 };
- WCHAR wszBuf[MAX_LOG_LENGTH + 1] = { 0 };
-
- do
- {
- std::copy(buf + pos, buf + pos + MAX_LOG_LENGTH, tempBuf);
-
- tempBuf[MAX_LOG_LENGTH] = 0;
-
- MultiByteToWideChar(CP_UTF8, 0, tempBuf, -1, wszBuf, sizeof(wszBuf));
- OutputDebugStringW(wszBuf);
- WideCharToMultiByte(CP_ACP, 0, wszBuf, -1, tempBuf, sizeof(tempBuf), nullptr, FALSE);
- printf("%s", tempBuf);
-
- pos += MAX_LOG_LENGTH;
-
- } while (pos < len);
- fflush(stdout);
- #else
- // Linux, Mac, iOS, etc
- fprintf(stdout, "%s", buf);
- fflush(stdout);
- #endif
-
- //cocos2d::log(buf);
- delete [] buf;
- }
- }
- namespace cocos2d
- {
- void log(const char * format, ...)
- {
- va_list args;
- va_start(args, format);
- _log(format, args);
- va_end(args);
- }
- }
|