Skip to content

Commit

Permalink
android cout hack
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Sky committed Oct 5, 2024
1 parent 70bc3a4 commit 278761c
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,42 @@
#include <thread>
#include <chrono>

#ifdef __ANDROID__
#include <log.h>

// 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<std::string_view> args;
Expand All @@ -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
Expand Down

0 comments on commit 278761c

Please sign in to comment.