CCLog.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "base/CCLog.h"
  22. #include <stdio.h>
  23. #include <new>
  24. #include <algorithm>
  25. #include <string.h>
  26. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  27. #include <io.h>
  28. #include <WS2tcpip.h>
  29. #include <Winsock2.h>
  30. #endif // (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  31. /** private functions */
  32. namespace
  33. {
  34. const size_t SEND_BUFSIZ = 512;
  35. const int MAX_LOG_LENGTH = 16*1024;
  36. //
  37. // Free functions to log
  38. //
  39. void _log(const char *format, va_list args)
  40. {
  41. int bufferSize = MAX_LOG_LENGTH;
  42. char* buf = nullptr;
  43. do
  44. {
  45. buf = new (std::nothrow) char[bufferSize];
  46. if (buf == nullptr)
  47. return; // not enough memory
  48. int ret = vsnprintf(buf, bufferSize - 3, format, args);
  49. if (ret < 0)
  50. {
  51. bufferSize *= 2;
  52. delete [] buf;
  53. }
  54. else
  55. break;
  56. } while (true);
  57. strcat(buf, "\n");
  58. #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  59. #include <android/log.h>
  60. __android_log_print(ANDROID_LOG_DEBUG, "debug info", "%s", buf);
  61. #elif CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
  62. int pos = 0;
  63. int len = strlen(buf);
  64. char tempBuf[MAX_LOG_LENGTH + 1] = { 0 };
  65. WCHAR wszBuf[MAX_LOG_LENGTH + 1] = { 0 };
  66. do
  67. {
  68. std::copy(buf + pos, buf + pos + MAX_LOG_LENGTH, tempBuf);
  69. tempBuf[MAX_LOG_LENGTH] = 0;
  70. MultiByteToWideChar(CP_UTF8, 0, tempBuf, -1, wszBuf, sizeof(wszBuf));
  71. OutputDebugStringW(wszBuf);
  72. WideCharToMultiByte(CP_ACP, 0, wszBuf, -1, tempBuf, sizeof(tempBuf), nullptr, FALSE);
  73. printf("%s", tempBuf);
  74. pos += MAX_LOG_LENGTH;
  75. } while (pos < len);
  76. fflush(stdout);
  77. #else
  78. // Linux, Mac, iOS, etc
  79. fprintf(stdout, "%s", buf);
  80. fflush(stdout);
  81. #endif
  82. //cocos2d::log(buf);
  83. delete [] buf;
  84. }
  85. }
  86. namespace cocos2d
  87. {
  88. void log(const char * format, ...)
  89. {
  90. va_list args;
  91. va_start(args, format);
  92. _log(format, args);
  93. va_end(args);
  94. }
  95. }