From 2f205a6dbc7959403f071c4b610dc811bbda3769 Mon Sep 17 00:00:00 2001 From: Alexander Zilberkant Date: Sun, 30 Apr 2017 23:23:17 +0300 Subject: [PATCH] android sink - add retry mechanism - in some cases subsequent calls to __android_log_write() may result with -EAGAIN error code. in such cases spdlog will sleep and try again for number of times defined by SPDLOG_ANDROID_LOG_NUM_OF_RETRIES - defeult SPDLOG_ANDROID_LOG_NUM_OF_RETRIES set to 2 - can be overridden at build time --- include/spdlog/sinks/android_sink.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/include/spdlog/sinks/android_sink.h b/include/spdlog/sinks/android_sink.h index 0a1bd297..afc7a547 100644 --- a/include/spdlog/sinks/android_sink.h +++ b/include/spdlog/sinks/android_sink.h @@ -12,6 +12,12 @@ #include #include #include +#include +#include + +#if !defined(SPDLOG_ANDROID_LOG_NUM_OF_RETRIES) +define SPDLOG_ANDROID_LOG_NUM_OF_RETRIES 2 +#endif namespace spdlog { @@ -31,10 +37,17 @@ public: { const android_LogPriority priority = convert_to_android(msg.level); const char *msg_output = (_use_raw_msg ? msg.raw.c_str() : msg.formatted.c_str()); + // See system/core/liblog/logger_write.c for explanation of return value - const int ret = __android_log_write( - priority, _tag.c_str(), msg_output - ); + int ret = __android_log_write(priority, _tag.c_str(), msg_output); + int retry_count = 1; + while ((ret == -11/*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_LOG_NUM_OF_RETRIES)) + { + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + ret = __android_log_write(priority, _tag.c_str(), msg_output); + retry_count++; + } + if (ret < 0) { throw spdlog_ex("__android_log_write() failed", ret);