Redirect logs to logcat on android

This commit is contained in:
loonycyborg 2023-06-12 23:23:18 +03:00
parent c47c0ebf42
commit 7900333d4b
No known key found for this signature in database
GPG key ID: 6E8233FAB8F26D61

View file

@ -36,6 +36,10 @@
#include <iostream>
#include <iomanip>
#ifdef __ANDROID__
#include <android/log.h>
#endif
static lg::log_domain log_setup("logsetup");
#define ERR_LS LOG_STREAM(err, log_setup)
#define WRN_LS LOG_STREAM(warn, log_setup)
@ -51,6 +55,31 @@ public:
null_streambuf() {}
};
#ifdef __ANDROID__
class android_log_buf : public std::streambuf
{
std::string output;
void write() {
auto newline { output.find_first_of("\n") };
if(newline != std::string::npos) {
__android_log_write(ANDROID_LOG_INFO, "wesnoth", output.substr(0, newline).c_str());
output = output.substr(newline+1);
}
}
protected:
virtual std::streamsize xsputn(const char_type* s, std::streamsize n) override {
output.append(s, n);
write();
return n;
}
virtual int_type overflow(int_type ch) override {
output.push_back(ch);
write();
return 1;
}
};
#endif
} // end anonymous namespace
static std::ostream null_ostream(new null_streambuf);
@ -64,6 +93,10 @@ static std::ostream *output_stream_ = nullptr;
static std::ostream& output()
{
#ifdef __ANDROID__
static std::ostream android_ostream(new android_log_buf);
return android_ostream;
#endif
if(output_stream_) {
return *output_stream_;
}