8#if defined(__ANDROID__)
14#include <android/log.h>
18#if !defined(SPDLOG_ANDROID_RETRIES)
19#define SPDLOG_ANDROID_RETRIES 2
28
29
30
31class android_sink :
public sink
34 explicit android_sink(
const std::string& tag =
"spdlog",
bool use_raw_msg =
false): _tag(tag), _use_raw_msg(use_raw_msg) {}
36 void log(
const details::log_msg& msg) override
38 const android_LogPriority priority = convert_to_android(msg.level);
39 const char *msg_output = (_use_raw_msg ? msg.raw.c_str() : msg.formatted.c_str());
42 int ret = __android_log_write(priority, _tag.c_str(), msg_output);
44 while ((ret == -11) && (retry_count < SPDLOG_ANDROID_RETRIES))
46 std::this_thread::sleep_for(std::chrono::milliseconds(5));
47 ret = __android_log_write(priority, _tag.c_str(), msg_output);
53 throw spdlog_ex(
"__android_log_write() failed", ret);
62 static android_LogPriority convert_to_android(spdlog::level::level_enum level)
66 case spdlog::level::trace:
67 return ANDROID_LOG_VERBOSE;
68 case spdlog::level::debug:
69 return ANDROID_LOG_DEBUG;
70 case spdlog::level::info:
71 return ANDROID_LOG_INFO;
72 case spdlog::level::warn:
73 return ANDROID_LOG_WARN;
74 case spdlog::level::err:
75 return ANDROID_LOG_ERROR;
76 case spdlog::level::critical:
77 return ANDROID_LOG_FATAL;
79 return ANDROID_LOG_DEFAULT;