8 #if defined(__ANDROID__) 10 #include "../details/os.h" 13 #include <android/log.h> 19 #if !defined(SPDLOG_ANDROID_RETRIES) 20 #define SPDLOG_ANDROID_RETRIES 2 30 class android_sink :
public sink
33 explicit android_sink(
const std::string &tag =
"spdlog",
bool use_raw_msg =
false)
35 , _use_raw_msg(use_raw_msg)
39 void log(
const details::log_msg &msg)
override 41 const android_LogPriority priority = convert_to_android(msg.level);
42 const char *msg_output = (_use_raw_msg ? msg.raw.c_str() : msg.formatted.c_str());
45 int ret = __android_log_write(priority, _tag.c_str(), msg_output);
47 while ((ret == -11 ) && (retry_count < SPDLOG_ANDROID_RETRIES))
49 details::os::sleep_for_millis(5);
50 ret = __android_log_write(priority, _tag.c_str(), msg_output);
56 throw spdlog_ex(
"__android_log_write() failed", ret);
60 void flush()
override {}
63 static android_LogPriority convert_to_android(spdlog::level::level_enum level)
67 case spdlog::level::trace:
68 return ANDROID_LOG_VERBOSE;
69 case spdlog::level::debug:
70 return ANDROID_LOG_DEBUG;
71 case spdlog::level::info:
72 return ANDROID_LOG_INFO;
73 case spdlog::level::warn:
74 return ANDROID_LOG_WARN;
75 case spdlog::level::err:
76 return ANDROID_LOG_ERROR;
77 case spdlog::level::critical:
78 return ANDROID_LOG_FATAL;
80 return ANDROID_LOG_DEFAULT;
Definition: async_logger.h:26