spdlog/include/spdlog/sinks/android_sink.h

75 lines
1.7 KiB
C
Raw Normal View History

2016-04-20 16:57:49 +08:00
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#if defined(__ANDROID__)
#include <spdlog/sinks/sink.h>
2016-04-20 16:57:49 +08:00
#include <mutex>
#include <string>
#include <android/log.h>
2016-04-20 16:57:49 +08:00
namespace spdlog
{
namespace sinks
{
2016-04-20 16:57:49 +08:00
/*
* Android sink (logging using __android_log_write)
* __android_log_write is thread-safe. No lock is needed.
2016-04-20 16:57:49 +08:00
*/
class android_sink : public sink
2016-04-20 16:57:49 +08:00
{
public:
explicit android_sink(const std::string& tag = "spdlog"): _tag(tag) {}
2016-04-20 16:57:49 +08:00
void log(const details::log_msg& msg) override
2016-04-20 16:57:49 +08:00
{
const android_LogPriority priority = convert_to_android(msg.level);
// See system/core/liblog/logger_write.c for explanation of return value
const int ret = __android_log_write(
2016-04-20 16:57:49 +08:00
priority, _tag.c_str(), msg.formatted.c_str()
);
if (ret < 0) {
throw spdlog_ex("__android_log_write() failed", ret);
2016-04-20 16:57:49 +08:00
}
}
void flush() override
{
}
2016-04-20 16:57:49 +08:00
private:
static android_LogPriority convert_to_android(spdlog::level::level_enum level)
{
switch(level)
{
case spdlog::level::trace:
return ANDROID_LOG_VERBOSE;
case spdlog::level::debug:
return ANDROID_LOG_DEBUG;
case spdlog::level::info:
return ANDROID_LOG_INFO;
case spdlog::level::warn:
return ANDROID_LOG_WARN;
case spdlog::level::err:
return ANDROID_LOG_ERROR;
case spdlog::level::critical:
return ANDROID_LOG_FATAL;
default:
return ANDROID_LOG_DEFAULT;
2016-04-20 16:57:49 +08:00
}
}
std::string _tag;
};
}
}
#endif