14#include <android/log.h>
21#if !defined(SPDLOG_ANDROID_RETRIES)
22#define SPDLOG_ANDROID_RETRIES 2
34 template <
typename Mutex,
int BufferID = log_
id::LOG_ID_MAIN>
35 class android_sink final :
public base_sink<Mutex>
38 explicit android_sink(std::string tag =
"spdlog",
bool use_raw_msg =
false)
39 : tag_(
std::move(tag)), use_raw_msg_(use_raw_msg)
44 void sink_it_(
const details::log_msg& msg)
override
46 const android_LogPriority priority = convert_to_android_(msg.level);
50 details::fmt_helper::append_string_view(msg.payload, formatted);
54 base_sink<Mutex>::formatter_->format(msg, formatted);
56 formatted.push_back(
'\0');
57 const char* msg_output = formatted.data();
60 int ret = android_log(priority, tag_.c_str(), msg_output);
62 while ((ret == -11 ) && (retry_count < SPDLOG_ANDROID_RETRIES))
64 details::os::sleep_for_millis(5);
65 ret = android_log(priority, tag_.c_str(), msg_output);
75 void flush_()
override
83 template <
int ID = BufferID>
84 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)
86 return __android_log_write(prio, tag, text);
89 template <
int ID = BufferID>
90 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)
92 return __android_log_buf_write(ID, prio, tag, text);
100 return ANDROID_LOG_VERBOSE;
102 return ANDROID_LOG_DEBUG;
104 return ANDROID_LOG_INFO;
106 return ANDROID_LOG_WARN;
108 return ANDROID_LOG_ERROR;
110 return ANDROID_LOG_FATAL;
112 return ANDROID_LOG_DEFAULT;
120 using android_sink_mt = android_sink<std::mutex>;
121 using android_sink_st = android_sink<details::null_mutex>;
123 template <
int BufferId = log_
id::LOG_ID_MAIN>
124 using android_sink_buf_mt = android_sink<std::mutex, BufferId>;
125 template <
int BufferId = log_
id::LOG_ID_MAIN>
126 using android_sink_buf_st = android_sink<details::null_mutex, BufferId>;
132 template <
typename Factory = spdlog::synchronous_factory>
133 inline std::shared_ptr<logger> android_logger_mt(
const std::string& logger_name,
const std::string& tag =
"spdlog")
135 return Factory::template create<sinks::android_sink_mt>(logger_name, tag);
138 template <
typename Factory = spdlog::synchronous_factory>
139 inline std::shared_ptr<logger> android_logger_st(
const std::string& logger_name,
const std::string& tag =
"spdlog")
141 return Factory::template create<sinks::android_sink_st>(logger_name, tag);
type
Definition core.h:681
level_enum
Definition common.h:233
@ critical
Definition common.h:239
@ err
Definition common.h:238
@ warn
Definition common.h:237
@ info
Definition common.h:236
@ trace
Definition common.h:234
@ debug
Definition common.h:235
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition common-inl.h:75
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:173