14 # include <android/log.h>
20 # if !defined(SPDLOG_ANDROID_RETRIES)
21 # define SPDLOG_ANDROID_RETRIES 2
30 template<
typename Mutex>
31 class android_sink final :
public base_sink<Mutex>
34 explicit android_sink(std::string tag =
"spdlog",
bool use_raw_msg =
false)
35 : tag_(
std::move(tag))
36 , use_raw_msg_(use_raw_msg)
40 void sink_it_(
const details::log_msg &msg)
override
42 const android_LogPriority priority = convert_to_android_(msg.level);
52 formatted.push_back(
'\0');
53 const char *msg_output = formatted.data();
56 int ret = __android_log_write(priority, tag_.c_str(), msg_output);
58 while ((ret == -11 ) && (retry_count < SPDLOG_ANDROID_RETRIES))
61 ret = __android_log_write(priority, tag_.c_str(), msg_output);
71 void flush_()
override {}
79 return ANDROID_LOG_VERBOSE;
81 return ANDROID_LOG_DEBUG;
83 return ANDROID_LOG_INFO;
85 return ANDROID_LOG_WARN;
87 return ANDROID_LOG_ERROR;
89 return ANDROID_LOG_FATAL;
91 return ANDROID_LOG_DEFAULT;
99 using android_sink_mt = android_sink<std::mutex>;
100 using android_sink_st = android_sink<details::null_mutex>;
105 template<
typename Factory = spdlog::synchronous_factory>
106 inline std::shared_ptr<logger> android_logger_mt(
const std::string &logger_name,
const std::string &tag =
"spdlog")
108 return Factory::template create<sinks::android_sink_mt>(logger_name, tag);
111 template<
typename Factory = spdlog::synchronous_factory>
112 inline std::shared_ptr<logger> android_logger_st(
const std::string &logger_name,
const std::string &tag =
"spdlog")
114 return Factory::template create<sinks::android_sink_st>(logger_name, tag);
119 #endif // __ANDROID__