mirror of
https://github.com/gabime/spdlog.git
synced 2025-10-02 11:29:01 +08:00
Refactored sink interface and base_sink
This commit is contained in:
@@ -5,10 +5,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
|
||||
#include "spdlog/details/os.h"
|
||||
#include "spdlog/sinks/sink.h"
|
||||
#include "spdlog/sinks/base_sink.h"
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include "spdlog/details/fmt_helper.h"
|
||||
|
||||
#include <android/log.h>
|
||||
#include <chrono>
|
||||
@@ -25,9 +25,9 @@ namespace sinks {
|
||||
|
||||
/*
|
||||
* Android sink (logging using __android_log_write)
|
||||
* __android_log_write is thread-safe. No lock is needed.
|
||||
*/
|
||||
class android_sink : public sink
|
||||
template<typename Mutex>
|
||||
class android_sink SPDLOG_FINAL: public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
explicit android_sink(const std::string &tag = "spdlog", bool use_raw_msg = false)
|
||||
@@ -36,13 +36,14 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
void log(const details::log_msg &msg) override
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
const android_LogPriority priority = convert_to_android_(msg.level);
|
||||
fmt::memory_buffer formatted;
|
||||
if (use_raw_msg_)
|
||||
{
|
||||
formatted.append(msg.raw.data(), msg.raw.data() + msg.raw.size());
|
||||
fmt_helper::append_buf(msg.raw, formatted);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -67,7 +68,9 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void flush() override {}
|
||||
void flush_() override
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
static android_LogPriority convert_to_android_(spdlog::level::level_enum level)
|
||||
@@ -95,16 +98,24 @@ private:
|
||||
bool use_raw_msg_;
|
||||
};
|
||||
|
||||
using android_sink_mt = android_sink<std::mutex>;
|
||||
using android_sink_st = android_sink<details::null_mutex>;
|
||||
} // namespace sinks
|
||||
|
||||
// Create and register android syslog logger
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
inline std::shared_ptr<logger> android_logger(const std::string &logger_name, const std::string &tag = "spdlog")
|
||||
inline std::shared_ptr<logger> android_logger_mt(const std::string &logger_name, const std::string &tag = "spdlog")
|
||||
{
|
||||
return Factory::template create<sinks::android_sink>(logger_name, tag);
|
||||
return Factory::template create<sinks::android_sink_mt>(logger_name, tag);
|
||||
}
|
||||
|
||||
template<typename Factory = default_factory>
|
||||
inline std::shared_ptr<logger> android_logger_st(const std::string &logger_name, const std::string &tag = "spdlog")
|
||||
{
|
||||
return Factory::template create<sinks::android_sink_st>(logger_name, tag);
|
||||
}
|
||||
|
||||
} // namespace spdlog
|
||||
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user