MMKVLog_Android.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #ifdef ENABLE_MMKV_LOG
  22. # ifdef MMKV_OHOS
  23. # include <hilog/log.h>
  24. # include <cstdarg>
  25. # include <string>
  26. using namespace std;
  27. constexpr auto APP_NAME = "MMKV";
  28. static LogLevel MMKVLogLevelDesc(MMKVLogLevel level) {
  29. switch (level) {
  30. case MMKVLogDebug:
  31. return LOG_DEBUG;
  32. case MMKVLogInfo:
  33. return LOG_INFO;
  34. case MMKVLogWarning:
  35. return LOG_WARN;
  36. case MMKVLogError:
  37. return LOG_ERROR;
  38. default:
  39. return LOG_INFO;
  40. }
  41. }
  42. void _MMKVLogWithLevel(MMKVLogLevel level, const char *filename, const char *func, int line, const char *format, ...) {
  43. if (level >= g_currentLogLevel) {
  44. string message;
  45. char buffer[16];
  46. va_list args;
  47. va_start(args, format);
  48. auto length = std::vsnprintf(buffer, sizeof(buffer), format, args);
  49. va_end(args);
  50. if (length < 0) { // something wrong
  51. message = {};
  52. } else if (length < sizeof(buffer)) {
  53. message = string(buffer, static_cast<unsigned long>(length));
  54. } else {
  55. message.resize(static_cast<unsigned long>(length), '\0');
  56. va_start(args, format);
  57. std::vsnprintf(const_cast<char *>(message.data()), static_cast<size_t>(length) + 1, format, args);
  58. va_end(args);
  59. }
  60. if (g_logHandler) {
  61. g_logHandler(level, filename, line, func, message);
  62. } else {
  63. auto desc = MMKVLogLevelDesc(level);
  64. OH_LOG_Print(LOG_APP, desc, 0, APP_NAME, "<%{public}s:%{public}d::%{public}s> %{public}s",
  65. filename, line, func, message.c_str());
  66. }
  67. }
  68. }
  69. # elif defined(MMKV_ANDROID)
  70. # include <android/log.h>
  71. # include <cstdarg>
  72. # include <string>
  73. using namespace std;
  74. constexpr auto APP_NAME = "MMKV";
  75. static android_LogPriority MMKVLogLevelDesc(MMKVLogLevel level) {
  76. switch (level) {
  77. case MMKVLogDebug:
  78. return ANDROID_LOG_DEBUG;
  79. case MMKVLogInfo:
  80. return ANDROID_LOG_INFO;
  81. case MMKVLogWarning:
  82. return ANDROID_LOG_WARN;
  83. case MMKVLogError:
  84. return ANDROID_LOG_ERROR;
  85. default:
  86. return ANDROID_LOG_UNKNOWN;
  87. }
  88. }
  89. void _MMKVLogWithLevel(MMKVLogLevel level, const char *filename, const char *func, int line, const char *format, ...) {
  90. if (level >= g_currentLogLevel) {
  91. string message;
  92. char buffer[16];
  93. va_list args;
  94. va_start(args, format);
  95. auto length = std::vsnprintf(buffer, sizeof(buffer), format, args);
  96. va_end(args);
  97. if (length < 0) { // something wrong
  98. message = {};
  99. } else if (length < sizeof(buffer)) {
  100. message = string(buffer, static_cast<unsigned long>(length));
  101. } else {
  102. message.resize(static_cast<unsigned long>(length), '\0');
  103. va_start(args, format);
  104. std::vsnprintf(const_cast<char *>(message.data()), static_cast<size_t>(length) + 1, format, args);
  105. va_end(args);
  106. }
  107. if (g_logHandler) {
  108. g_logHandler(level, filename, line, func, message);
  109. } else {
  110. auto desc = MMKVLogLevelDesc(level);
  111. __android_log_print(desc, APP_NAME, "<%s:%d::%s> %s", filename, line, func, message.c_str());
  112. }
  113. }
  114. }
  115. # endif // MMKV_ANDROID
  116. #endif // ENABLE_MMKV_LOG