android_sink.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/sinks/base_sink.h>
  9. # include <spdlog/details/synchronous_factory.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 BufferID)
  24. */
  25. template<typename Mutex, int BufferID = log_id::LOG_ID_MAIN>
  26. class android_sink final : public base_sink<Mutex>
  27. {
  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. {}
  33. protected:
  34. void sink_it_(const details::log_msg &msg) override
  35. {
  36. const android_LogPriority priority = convert_to_android_(msg.level);
  37. memory_buf_t formatted;
  38. if (use_raw_msg_)
  39. {
  40. details::fmt_helper::append_string_view(msg.payload, formatted);
  41. }
  42. else
  43. {
  44. base_sink<Mutex>::formatter_->format(msg, formatted);
  45. }
  46. formatted.push_back('\0');
  47. const char *msg_output = formatted.data();
  48. // See system/core/liblog/logger_write.c for explanation of return value
  49. int ret = android_log(priority, tag_.c_str(), msg_output);
  50. int retry_count = 0;
  51. while ((ret == -11 /*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES))
  52. {
  53. details::os::sleep_for_millis(5);
  54. ret = android_log(priority, tag_.c_str(), msg_output);
  55. retry_count++;
  56. }
  57. if (ret < 0)
  58. {
  59. throw_spdlog_ex("logging to Android failed", ret);
  60. }
  61. }
  62. void flush_() override {}
  63. private:
  64. // There might be liblog versions used, that do not support __android_log_buf_write. So we only compile and link against
  65. // __android_log_buf_write, if user explicitely provides a non-default log buffer. Otherwise, when using the default log buffer, always
  66. // log via __android_log_write.
  67. template<int ID = BufferID>
  68. typename std::enable_if<ID == static_cast<int>(log_id::LOG_ID_MAIN), int>::type android_log(int prio, const char *tag, const char *text)
  69. {
  70. return __android_log_write(prio, tag, text);
  71. }
  72. template<int ID = BufferID>
  73. typename std::enable_if<ID != static_cast<int>(log_id::LOG_ID_MAIN), int>::type android_log(int prio, const char *tag, const char *text)
  74. {
  75. return __android_log_buf_write(ID, prio, tag, text);
  76. }
  77. static android_LogPriority convert_to_android_(spdlog::level::level_enum level)
  78. {
  79. switch (level)
  80. {
  81. case spdlog::level::trace:
  82. return ANDROID_LOG_VERBOSE;
  83. case spdlog::level::debug:
  84. return ANDROID_LOG_DEBUG;
  85. case spdlog::level::info:
  86. return ANDROID_LOG_INFO;
  87. case spdlog::level::warn:
  88. return ANDROID_LOG_WARN;
  89. case spdlog::level::err:
  90. return ANDROID_LOG_ERROR;
  91. case spdlog::level::critical:
  92. return ANDROID_LOG_FATAL;
  93. default:
  94. return ANDROID_LOG_DEFAULT;
  95. }
  96. }
  97. std::string tag_;
  98. bool use_raw_msg_;
  99. };
  100. using android_sink_mt = android_sink<std::mutex>;
  101. using android_sink_st = android_sink<details::null_mutex>;
  102. template<int BufferId = log_id::LOG_ID_MAIN>
  103. using android_sink_buf_mt = android_sink<std::mutex, BufferId>;
  104. template<int BufferId = log_id::LOG_ID_MAIN>
  105. using android_sink_buf_st = android_sink<details::null_mutex, BufferId>;
  106. } // namespace sinks
  107. // Create and register android syslog logger
  108. template<typename Factory = spdlog::synchronous_factory>
  109. inline std::shared_ptr<logger> android_logger_mt(const std::string &logger_name, const std::string &tag = "spdlog")
  110. {
  111. return Factory::template create<sinks::android_sink_mt>(logger_name, tag);
  112. }
  113. template<typename Factory = spdlog::synchronous_factory>
  114. inline std::shared_ptr<logger> android_logger_st(const std::string &logger_name, const std::string &tag = "spdlog")
  115. {
  116. return Factory::template create<sinks::android_sink_st>(logger_name, tag);
  117. }
  118. } // namespace spdlog
  119. #endif // __ANDROID__