android: allow stdout/stderr capture using extra field on intent

This allows launching the Panda apk from termux and getting command-line output back to termux.
This commit is contained in:
rdb 2018-02-06 22:42:40 +01:00
parent ae0f82911b
commit d269f7c6c3
6 changed files with 35 additions and 4 deletions

View File

@ -92,6 +92,7 @@ overflow(int ch) {
*/
void AndroidLogStream::AndroidLogStreamBuf::
write_char(char c) {
nout.put(c);
if (c == '\n') {
// Write a line to the log file.
__android_log_write(_priority, _tag.c_str(), _data.c_str());

View File

@ -335,9 +335,8 @@ assert_failure(const char *expression, int line,
#ifdef ANDROID
__android_log_assert("assert", "Panda3D", "Assertion failed: %s", message.c_str());
#else
nout << "Assertion failed: " << message << "\n";
#endif
nout << "Assertion failed: " << message << "\n";
// This is redefined here, shadowing the defining in config_prc.h, so we can
// guarantee it has already been constructed.

View File

@ -64,7 +64,11 @@ out(NotifySeverity severity, bool prefix) const {
// logging system. We use a special type of stream that redirects it to
// Android's log system.
if (prefix) {
return AndroidLogStream::out(severity) << *this << ": ";
if (severity == NS_info) {
return AndroidLogStream::out(severity) << *this << ": ";
} else {
return AndroidLogStream::out(severity) << *this << "(" << severity << "): ";
}
} else {
return AndroidLogStream::out(severity);
}

View File

@ -61,6 +61,11 @@ public class PandaActivity extends NativeActivity {
return path;
}
public String getIntentOutputPath() {
Intent intent = getIntent();
return intent.getStringExtra("org.panda3d.OUTPUT_PATH");
}
public String getCacheDirString() {
return getCacheDir().toString();
}

View File

@ -158,6 +158,28 @@ void android_main(struct android_app* app) {
}
}
// Were we given an optional location to write the stdout/stderr streams?
methodID = env->GetMethodID(activity_class, "getIntentOutputPath", "()Ljava/lang/String;");
jstring joutput_path = (jstring) env->CallObjectMethod(activity->clazz, methodID);
if (joutput_path != nullptr) {
const char *output_path = env->GetStringUTFChars(joutput_path, nullptr);
if (output_path != nullptr && output_path[0] != 0) {
int fd = open(output_path, O_CREAT | O_TRUNC | O_WRONLY);
if (fd != -1) {
android_cat.info()
<< "Writing standard output to file " << output_path << "\n";
dup2(fd, 1);
dup2(fd, 2);
} else {
android_cat.error()
<< "Failed to open output path " << output_path << "\n";
}
env->ReleaseStringUTFChars(joutput_path, output_path);
}
}
// Create bogus argc and argv for calling the main function.
const char *argv[] = {"pview", nullptr, nullptr};
int argc = 1;

View File

@ -78,7 +78,7 @@ is_regular_file(const Filename &file) const {
AAsset* asset;
asset = AAssetManager_open(_asset_mgr, file.c_str(), AASSET_MODE_UNKNOWN);
express_cat.error() << "is_regular_file " << file << " - " << asset << "\n";
//express_cat.error() << "is_regular_file " << file << " - " << asset << "\n";
if (asset == NULL) {
return false;