MMKVLog.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2019 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "MMKVLog.h"
  21. MMKV_NAMESPACE_BEGIN
  22. #ifdef MMKV_DEBUG
  23. MMKVLogLevel g_currentLogLevel = MMKVLogDebug;
  24. #else
  25. MMKVLogLevel g_currentLogLevel = MMKVLogInfo;
  26. #endif
  27. mmkv::LogHandler g_logHandler = nullptr;
  28. #ifndef __FILE_NAME__
  29. const char *_getFileName(const char *path) {
  30. const char *ptr = strrchr(path, '/');
  31. if (!ptr) {
  32. ptr = strrchr(path, '\\');
  33. }
  34. if (ptr) {
  35. return ptr + 1;
  36. } else {
  37. return path;
  38. }
  39. }
  40. #endif
  41. MMKV_NAMESPACE_END
  42. #ifdef ENABLE_MMKV_LOG
  43. # include <cstdarg>
  44. # include <string>
  45. using namespace mmkv;
  46. # ifndef MMKV_ANDROID
  47. static const char *MMKVLogLevelDesc(MMKVLogLevel level) {
  48. switch (level) {
  49. case MMKVLogDebug:
  50. return "D";
  51. case MMKVLogInfo:
  52. return "I";
  53. case MMKVLogWarning:
  54. return "W";
  55. case MMKVLogError:
  56. return "E";
  57. default:
  58. return "N";
  59. }
  60. }
  61. # ifdef MMKV_APPLE
  62. void _MMKVLogWithLevel(MMKVLogLevel level, const char *filename, const char *func, int line, const char *format, ...) {
  63. if (level >= g_currentLogLevel) {
  64. NSString *nsFormat = [NSString stringWithUTF8String:format];
  65. va_list argList;
  66. va_start(argList, format);
  67. NSString *message = [[NSString alloc] initWithFormat:nsFormat arguments:argList];
  68. va_end(argList);
  69. if (g_logHandler) {
  70. g_logHandler(level, filename, line, func, message);
  71. } else {
  72. NSLog(@"[%s] <%s:%d::%s> %@", MMKVLogLevelDesc(level), filename, line, func, message);
  73. }
  74. }
  75. }
  76. # else
  77. void _MMKVLogWithLevel(MMKVLogLevel level, const char *filename, const char *func, int line, const char *format, ...) {
  78. if (level >= g_currentLogLevel) {
  79. std::string message;
  80. char buffer[16];
  81. va_list args;
  82. va_start(args, format);
  83. auto length = std::vsnprintf(buffer, sizeof(buffer), format, args);
  84. va_end(args);
  85. if (length < 0) { // something wrong
  86. message = {};
  87. } else if (length < sizeof(buffer)) {
  88. message = std::string(buffer, static_cast<unsigned long>(length));
  89. } else {
  90. message.resize(static_cast<unsigned long>(length), '\0');
  91. va_start(args, format);
  92. std::vsnprintf(const_cast<char *>(message.data()), static_cast<size_t>(length) + 1, format, args);
  93. va_end(args);
  94. }
  95. if (g_logHandler) {
  96. g_logHandler(level, filename, line, func, message);
  97. } else {
  98. printf("[%s] <%s:%d::%s> %s\n", MMKVLogLevelDesc(level), filename, line, func, message.c_str());
  99. //fflush(stdout);
  100. }
  101. }
  102. }
  103. # endif // MMKV_APPLE
  104. # endif // MMKV_ANDROID
  105. #endif // ENABLE_MMKV_LOG