From 278761c7c02d2a9f6c3079b738f47841e20c2ddc Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sat, 5 Oct 2024 19:16:23 +0200 Subject: [PATCH] android cout hack --- src/main.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 1e66ce6e..e72fc003 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,42 @@ #include #include +#ifdef __ANDROID__ +#include + +// logger hack based on https://stackoverflow.com/questions/8870174/is-stdcout-usable-in-android-ndk +class AndroidBuf : public std::streambuf { + static constexpr size_t bufsize = 128; + char buffer[bufsize]; + public: + AndroidBuf(void) { + setp(buffer, buffer + bufsize - 1); + } + + private: + int overflow(int c) { + if (c == traits_type::eof()) { + *pptr() = traits_type::to_char_type(c); + sbumpc(); + } + return sync() ? traits_type::eof() : traits_type::not_eof(c); + } + + int sync(void) { + int rc = 0; + if (pbase() != pptr()) { + char writebuf[bufsize+1]; + memcpy(writebuf, pbase(), pptr() - pbase()); + writebuf[pptr() - pbase()] = '\0'; + + rc = __android_log_write(ANDROID_LOG_INFO, "std", writebuf) > 0; + setp(buffer, buffer + bufsize - 1); + } + return rc; + } +}; +#endif + int main(int argc, char** argv) { // better args std::vector args; @@ -31,6 +67,9 @@ int main(int argc, char** argv) { SDL_SetAppMetadata("tomato", "0.0.0-wip", nullptr); #ifdef __ANDROID__ + std::cout.rdbuf(new AndroidBuf); // log hack + std::cerr.rdbuf(new AndroidBuf); // log hack + // change current working dir to internal storage std::filesystem::current_path(SDL_GetAndroidInternalStoragePath()); #endif