Skip to content

Instantly share code, notes, and snippets.

@dzhioev
Created August 1, 2013 02:33
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dzhioev/6127982 to your computer and use it in GitHub Desktop.
Save dzhioev/6127982 to your computer and use it in GitHub Desktop.
#include <android/log.h>
class androidbuf: public std::streambuf {
public:
enum { bufsize = 128 }; // ... or some other suitable buffer size
androidbuf() { this->setp(buffer, buffer + bufsize - 1); }
private:
int overflow(int c) {
if (c == traits_type::eof()) {
*this->pptr() = traits_type::to_char_type(c);
this->sbumpc();
}
return this->sync()? traits_type::eof(): traits_type::not_eof(c);
}
int sync() {
int rc = 0;
if (this->pbase() != this->pptr()) {
__android_log_print(ANDROID_LOG_INFO,
"Native",
"%s",
std::string(this->pbase(),
this->pptr() - this->pbase()).c_str());
rc = 0;
this->setp(buffer, buffer + bufsize - 1);
}
return rc;
}
char buffer[bufsize];
};
@Astragalus
Copy link

Thanks for this incredibly useful scrap of code! I am using in my project, Conform! I have credited you in the comments. Feel free to check it out, it is on github.

@qige023
Copy link

qige023 commented Apr 16, 2015

genius, wonderful code, save my time from my life.

@kittinunf
Copy link

rc is always 0 which makes overflow always return traits_type::not_eof(c). Is this intentional?

@KRMisha
Copy link

KRMisha commented Aug 18, 2017

Thank you!

@richoux
Copy link

richoux commented Jul 31, 2023

Still in 2023, this code is very helpful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment