You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spdlog/include/spdlog/sinks/android_sink.h

78 lines
1.8 KiB
C

10 years ago
//
// 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>
10 years ago
#include <mutex>
#include <string>
#include <android/log.h>
10 years ago
namespace spdlog
{
namespace sinks
{
10 years ago
/*
* Android sink (logging using __android_log_write)
* __android_log_write is thread-safe. No lock is needed.
10 years ago
*/
class android_sink : public sink
10 years ago
{
public:
explicit android_sink(const std::string& tag = "spdlog", bool use_raw_msg = false): _tag(tag), _use_raw_msg(use_raw_msg){}
10 years ago
void log(const details::log_msg& msg) override
10 years ago
{
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
9 years ago
);
if (ret < 0)
{
throw spdlog_ex("__android_log_write() failed", ret);
10 years ago
}
}
void flush() override
{
}
10 years ago
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;
10 years ago
}
}
std::string _tag;
bool _use_raw_msg;
10 years ago
};
}
}
#endif