Created
February 1, 2015 22:35
-
-
Save theojulienne/237ee696d1f650fe7278 to your computer and use it in GitHub Desktop.
C++ line-buffered output for Android (for usage in JNI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::cout.rdbuf(new androidbuf); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment