Skip to content

Instantly share code, notes, and snippets.

@dodola
Forked from dzhioev/gist:6127982
Created October 24, 2018 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dodola/0b78eb36e6d84955dfcb6ccac601fd20 to your computer and use it in GitHub Desktop.
Save dodola/0b78eb36e6d84955dfcb6ccac601fd20 to your computer and use it in GitHub Desktop.
android cout 重定向
#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];
};
@dodola
Copy link
Author

dodola commented Oct 24, 2018

    std::cout.rdbuf(new androidbuf);

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