android_sink.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #ifdef __ANDROID__
  5. #include <spdlog/details/fmt_helper.h>
  6. #include <spdlog/details/null_mutex.h>
  7. #include <spdlog/details/os.h>
  8. #include <spdlog/details/synchronous_factory.h>
  9. #include <spdlog/sinks/base_sink.h>
  10. #include <android/log.h>
  11. #include <chrono>
  12. #include <mutex>
  13. #include <string>
  14. #include <thread>
  15. #include <type_traits>
  16. #if !defined(SPDLOG_ANDROID_RETRIES)
  17. #define SPDLOG_ANDROID_RETRIES 2
  18. #endif
  19. namespace spdlog {
  20. namespace sinks {
  21. /*
  22. * Android sink
  23. * (logging using __android_log_write or __android_log_buf_write depending on the specified
  24. * BufferID)
  25. */
  26. template <typename Mutex, int BufferID = log_id::LOG_ID_MAIN>
  27. class android_sink final : public base_sink<Mutex> {
  28. public:
  29. explicit android_sink(std::string tag = "spdlog", bool use_raw_msg = false)
  30. : tag_(std::move(tag)),
  31. use_raw_msg_(use_raw_msg) {}
  32. protected:
  33. void sink_it_(const details::log_msg &msg) override {
  34. const android_LogPriority priority = convert_to_android_(msg.level);
  35. memory_buf_t formatted;
  36. if (use_raw_msg_) {
  37. details::fmt_helper::append_string_view(msg.payload, formatted);
  38. } else {
  39. base_sink<Mutex>::formatter_->format(msg, formatted);
  40. }
  41. formatted.push_back('\0');
  42. const char *msg_output = formatted.data();
  43. // See system/core/liblog/logger_write.c for explanation of return value
  44. int ret = android_log(priority, tag_.c_str(), msg_output);
  45. if (ret == -EPERM) {
  46. return; // !__android_log_is_loggable
  47. }
  48. int retry_count = 0;
  49. while ((ret == -11 /*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES)) {
  50. details::os::sleep_for_millis(5);
  51. ret = android_log(priority, tag_.c_str(), msg_output);
  52. retry_count++;
  53. }
  54. if (ret < 0) {
  55. throw_spdlog_ex("logging to Android failed", ret);
  56. }
  57. }
  58. void flush_() override {}
  59. private:
  60. // There might be liblog versions used, that do not support __android_log_buf_write. So we only
  61. // compile and link against
  62. // __android_log_buf_write, if user explicitly provides a non-default log buffer. Otherwise,
  63. // when using the default log buffer, always log via __android_log_write.
  64. template <int ID = BufferID>
  65. typename std::enable_if<ID == static_cast<int>(log_id::LOG_ID_MAIN), int>::type android_log(
  66. int prio, const char *tag, const char *text) {
  67. return __android_log_write(prio, tag, text);
  68. }
  69. template <int ID = BufferID>
  70. typename std::enable_if<ID != static_cast<int>(log_id::LOG_ID_MAIN), int>::type android_log(
  71. int prio, const char *tag, const char *text) {
  72. return __android_log_buf_write(ID, prio, tag, text);
  73. }
  74. static android_LogPriority convert_to_android_(spdlog::level::level_enum level) {
  75. switch (level) {
  76. case spdlog::level::trace:
  77. return ANDROID_LOG_VERBOSE;
  78. case spdlog::level::debug:
  79. return ANDROID_LOG_DEBUG;
  80. case spdlog::level::info:
  81. return ANDROID_LOG_INFO;
  82. case spdlog::level::warn:
  83. return ANDROID_LOG_WARN;
  84. case spdlog::level::err:
  85. return ANDROID_LOG_ERROR;
  86. case spdlog::level::critical:
  87. return ANDROID_LOG_FATAL;
  88. default:
  89. return ANDROID_LOG_DEFAULT;
  90. }
  91. }
  92. std::string tag_;
  93. bool use_raw_msg_;
  94. };
  95. using android_sink_mt = android_sink<std::mutex>;
  96. using android_sink_st = android_sink<details::null_mutex>;
  97. template <int BufferId = log_id::LOG_ID_MAIN>
  98. using android_sink_buf_mt = android_sink<std::mutex, BufferId>;
  99. template <int BufferId = log_id::LOG_ID_MAIN>
  100. using android_sink_buf_st = android_sink<details::null_mutex, BufferId>;
  101. } // namespace sinks
  102. // Create and register android syslog logger
  103. template <typename Factory = spdlog::synchronous_factory>
  104. inline std::shared_ptr<logger> android_logger_mt(const std::string &logger_name,
  105. const std::string &tag = "spdlog") {
  106. return Factory::template create<sinks::android_sink_mt>(logger_name, tag);
  107. }
  108. template <typename Factory = spdlog::synchronous_factory>
  109. inline std::shared_ptr<logger> android_logger_st(const std::string &logger_name,
  110. const std::string &tag = "spdlog") {
  111. return Factory::template create<sinks::android_sink_st>(logger_name, tag);
  112. }
  113. } // namespace spdlog
  114. #endif // __ANDROID__