android_sink.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. if (ret == -EPERM)
  51. {
  52. return; // !__android_log_is_loggable
  53. }
  54. int retry_count = 0;
  55. while ((ret == -11 /*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES))
  56. {
  57. details::os::sleep_for_millis(5);
  58. ret = android_log(priority, tag_.c_str(), msg_output);
  59. retry_count++;
  60. }
  61. if (ret < 0)
  62. {
  63. throw_spdlog_ex("logging to Android failed", ret);
  64. }
  65. }
  66. void flush_() override {}
  67. private:
  68. // There might be liblog versions used, that do not support __android_log_buf_write. So we only compile and link against
  69. // __android_log_buf_write, if user explicitly provides a non-default log buffer. Otherwise, when using the default log buffer, always
  70. // log via __android_log_write.
  71. template<int ID = BufferID>
  72. 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)
  73. {
  74. return __android_log_write(prio, tag, text);
  75. }
  76. template<int ID = BufferID>
  77. 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)
  78. {
  79. return __android_log_buf_write(ID, prio, tag, text);
  80. }
  81. static android_LogPriority convert_to_android_(spdlog::level::level_enum level)
  82. {
  83. switch (level)
  84. {
  85. case spdlog::level::trace:
  86. return ANDROID_LOG_VERBOSE;
  87. case spdlog::level::debug:
  88. return ANDROID_LOG_DEBUG;
  89. case spdlog::level::info:
  90. return ANDROID_LOG_INFO;
  91. case spdlog::level::warn:
  92. return ANDROID_LOG_WARN;
  93. case spdlog::level::err:
  94. return ANDROID_LOG_ERROR;
  95. case spdlog::level::critical:
  96. return ANDROID_LOG_FATAL;
  97. default:
  98. return ANDROID_LOG_DEFAULT;
  99. }
  100. }
  101. std::string tag_;
  102. bool use_raw_msg_;
  103. };
  104. using android_sink_mt = android_sink<std::mutex>;
  105. using android_sink_st = android_sink<details::null_mutex>;
  106. template<int BufferId = log_id::LOG_ID_MAIN>
  107. using android_sink_buf_mt = android_sink<std::mutex, BufferId>;
  108. template<int BufferId = log_id::LOG_ID_MAIN>
  109. using android_sink_buf_st = android_sink<details::null_mutex, BufferId>;
  110. } // namespace sinks
  111. // Create and register android syslog logger
  112. template<typename Factory = spdlog::synchronous_factory>
  113. inline std::shared_ptr<logger> android_logger_mt(const std::string &logger_name, const std::string &tag = "spdlog")
  114. {
  115. return Factory::template create<sinks::android_sink_mt>(logger_name, tag);
  116. }
  117. template<typename Factory = spdlog::synchronous_factory>
  118. inline std::shared_ptr<logger> android_logger_st(const std::string &logger_name, const std::string &tag = "spdlog")
  119. {
  120. return Factory::template create<sinks::android_sink_st>(logger_name, tag);
  121. }
  122. } // namespace spdlog
  123. #endif // __ANDROID__