Merge pull request #2358 from tiolan/topic/android_buffer_v1_x

V1: Allow modifying the used Android buffer
This commit is contained in:
Gabi Melman 2022-05-07 00:27:55 +03:00 committed by GitHub
commit a9347017db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,6 +16,7 @@
# include <mutex> # include <mutex>
# include <string> # include <string>
# include <thread> # include <thread>
# include <type_traits>
# if !defined(SPDLOG_ANDROID_RETRIES) # if !defined(SPDLOG_ANDROID_RETRIES)
# define SPDLOG_ANDROID_RETRIES 2 # define SPDLOG_ANDROID_RETRIES 2
@ -25,9 +26,10 @@ namespace spdlog {
namespace sinks { namespace sinks {
/* /*
* Android sink (logging using __android_log_write) * Android sink
* (logging using __android_log_write or __android_log_buf_write depending on the specified BufferID)
*/ */
template<typename Mutex> template<typename Mutex, int BufferID = log_id::LOG_ID_MAIN>
class android_sink final : public base_sink<Mutex> class android_sink final : public base_sink<Mutex>
{ {
public: public:
@ -53,24 +55,39 @@ protected:
const char *msg_output = formatted.data(); const char *msg_output = formatted.data();
// See system/core/liblog/logger_write.c for explanation of return value // See system/core/liblog/logger_write.c for explanation of return value
int ret = __android_log_write(priority, tag_.c_str(), msg_output); int ret = android_log(priority, tag_.c_str(), msg_output);
int retry_count = 0; int retry_count = 0;
while ((ret == -11 /*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES)) while ((ret == -11 /*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES))
{ {
details::os::sleep_for_millis(5); details::os::sleep_for_millis(5);
ret = __android_log_write(priority, tag_.c_str(), msg_output); ret = android_log(priority, tag_.c_str(), msg_output);
retry_count++; retry_count++;
} }
if (ret < 0) if (ret < 0)
{ {
throw_spdlog_ex("__android_log_write() failed", ret); throw_spdlog_ex("logging to Android failed", ret);
} }
} }
void flush_() override {} void flush_() override {}
private: private:
// There might be liblog versions used, that do not support __android_log_buf_write. So we only compile and link against
// __android_log_buf_write, if user explicitely provides a non-default log buffer. Otherwise, when using the default log buffer, always
// log via __android_log_write.
template<int ID = BufferID>
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)
{
return __android_log_write(prio, tag, text);
}
template<int ID = BufferID>
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)
{
return __android_log_buf_write(ID, prio, tag, text);
}
static android_LogPriority convert_to_android_(spdlog::level::level_enum level) static android_LogPriority convert_to_android_(spdlog::level::level_enum level)
{ {
switch (level) switch (level)
@ -98,6 +115,12 @@ private:
using android_sink_mt = android_sink<std::mutex>; using android_sink_mt = android_sink<std::mutex>;
using android_sink_st = android_sink<details::null_mutex>; using android_sink_st = android_sink<details::null_mutex>;
template<int BufferId = log_id::LOG_ID_MAIN>
using android_sink_buf_mt = android_sink<std::mutex, BufferId>;
template<int BufferId = log_id::LOG_ID_MAIN>
using android_sink_buf_st = android_sink<details::null_mutex, BufferId>;
} // namespace sinks } // namespace sinks
// Create and register android syslog logger // Create and register android syslog logger