Skip to content

Instantly share code, notes, and snippets.

@theojulienne
Created February 1, 2015 22:35
Show Gist options
  • Save theojulienne/237ee696d1f650fe7278 to your computer and use it in GitHub Desktop.
Save theojulienne/237ee696d1f650fe7278 to your computer and use it in GitHub Desktop.
C++ line-buffered output for Android (for usage in JNI)
class androidbuf: public std::streambuf {
public:
enum { bufsize = 1024 };
androidbuf() { this->setp(0, 0); }
private:
int overflow(int c) {
if (c == traits_type::eof() || c == '\n' || idx == bufsize - 1) {
buffer[idx] = '\0';
idx++;
__android_log_print(ANDROID_LOG_INFO, "yourlibrary", "%s", buffer);
idx = 0;
}
if (!(c == traits_type::eof() || c == '\n')) {
buffer[idx] = c;
idx++;
}
return c;
}
char buffer[bufsize];
int idx = 0;
};
std::cout.rdbuf(new androidbuf);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment