Logcat logging

This commit is contained in:
artdeell 2020-09-10 17:35:58 +03:00
parent c450afe458
commit 16550344e7
7 changed files with 77 additions and 37 deletions

View File

@ -99,7 +99,7 @@ public class InstallModActivity extends AppCompatActivity
JREUtils.setJavaEnvironment(this);
JREUtils.redirectStdio(false);
//JREUtils.redirectStdio(false);
JREUtils.setJavaEnvironment(this);
JREUtils.initJavaRuntime();
JREUtils.chdir(Tools.MAIN_PATH);

View File

@ -24,17 +24,7 @@ public class JREUtils
dlopen("libgl04es.so");
}
}
public static File redirectStdio(boolean current) throws ErrnoException {
File logFile = new File(current ? Tools.datapath : Tools.MAIN_PATH, (current ? "current" : "latest") + "log.txt");
FileDescriptor fd = Os.open(logFile.getAbsolutePath(), OsConstants.O_WRONLY | OsConstants.O_CREAT | OsConstants.O_TRUNC, 0666);
Os.dup2(fd, OsConstants.STDERR_FILENO);
Os.dup2(fd, OsConstants.STDOUT_FILENO);
return logFile;
}
public static native void redirectLogcat();
public static void setJavaEnvironment(Context ctx) throws IOException, ErrnoException {
String libName = System.getProperty("os.arch").contains("64") ? "lib64" : "lib";
String ldLibraryPath = (

View File

@ -17,6 +17,7 @@ import com.kdt.glsupport.*;
import com.kdt.pointer.*;
import dalvik.system.*;
import java.io.*;
import java.lang.Process;
import java.lang.reflect.*;
import java.security.*;
import java.util.*;
@ -840,6 +841,7 @@ public class MainActivity extends AppCompatActivity implements OnTouchListener,
private FileObserver mLogObserver;
private void runCraft() throws Throwable {
/* Old logger
if (Tools.LAUNCH_TYPE != Tools.LTYPE_PROCESS) {
currLogFile = JREUtils.redirectStdio(true);
// DEPRECATED constructor (String) api 29
@ -848,7 +850,7 @@ public class MainActivity extends AppCompatActivity implements OnTouchListener,
public void onEvent(int event, String file) {
try {
if (event == FileObserver.MODIFY && currLogFile.length() > 0l) {
appendToLog(Tools.read(currLogFile.getAbsolutePath()));
System.out.println(Tools.read(currLogFile.getAbsolutePath()));
Tools.write(currLogFile.getAbsolutePath(), "");
}
} catch (Throwable th) {
@ -859,7 +861,26 @@ public class MainActivity extends AppCompatActivity implements OnTouchListener,
};
mLogObserver.startWatching();
}
*/
JREUtils.redirectLogcat();
Log.v("jrelog","Log starts here");
Thread t = new Thread(() -> {
try {
Log.i("jrelog-logcat","Clearing logcat");
new ProcessBuilder().command("logcat","-c").redirectErrorStream(true).start();
Log.i("jrelog-logcat","Starting logcat");
Process p = new ProcessBuilder().command("logcat","-v","brief","*:S").redirectErrorStream(true).start();
byte[] buf = new byte[512];
int len;
while ((len = p.getInputStream().read(buf)) != -1) {
appendToLog(new String(buf, 0, len));
}
}catch (IOException e) {
e.printStackTrace();
}
});
t.start();
Log.i("jrelog-logcat","Logcat thread started");
Tools.launchMinecraft(this, mProfile, mVersionInfo);
}

View File

@ -642,16 +642,13 @@ public final class Tools
public static String convertStream(InputStream inputStream, Charset charset) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
String line = null;
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
String out = "";
int len;
byte[] buf = new byte[512];
while((len = inputStream.read(buf))!=-1) {
out += new String(buf,0,len,charset);
}
return stringBuilder.toString();
return out;
}
// Current Useless below but keep it for future usage.
@ -687,19 +684,16 @@ public final class Tools
return choice;
}
public static byte[] getByteArray(String filePath) throws Exception
{
return getByteArray(new FileInputStream(filePath));
}
public static byte[] getByteArray(InputStream stream) throws IOException {
return IOUtils.toByteArray(stream);
}
public static String read(InputStream is) throws Exception {
byte[] byteArr = getByteArray(is);
return new String(byteArr, 0, byteArr.length);
String out = "";
int len;
byte[] buf = new byte[512];
while((len = is.read(buf))!=-1) {
out += new String(buf,0,len);
}
return out;
}
public static String read(String path) throws Exception {

View File

@ -23,5 +23,4 @@ JNIEXPORT void JNICALL Java_java_awt_TextField_initIDs(JNIEnv *env, jclass cls)
JNIEXPORT void JNICALL Java_java_awt_Dialog_initIDs(JNIEnv *env, jclass cls) {}
JNIEXPORT void JNICALL Java_java_awt_KeyboardFocusManager_initIDs(JNIEnv *env, jclass cls) {}
JNIEXPORT void JNICALL Java_java_awt_TrayIcon_initIDs(JNIEnv *env, jclass cls) {}
JNIEXPORT void JNICALL Java_sun_awt_X11_XWindow_initIDs(JNIEnv *env, jclass cls) {}
JNIEXPORT void JNICALL Java_sun_awt_X11_XWindow_initIDs(JNIEnv *env, jclass cls) {}

View File

@ -27,6 +27,8 @@
#include <stdio.h>
#include <android/log.h>
#include <dlfcn.h>
#include <pthread.h>
#include <unistd.h>
// Boardwalk: missing include
#include <string.h>
@ -194,5 +196,39 @@ JNIEXPORT jint JNICALL Java_com_oracle_dalvik_VMLauncher_launchJVM(JNIEnv *env,
return res;
}
static int pfd[2];
static pthread_t logger;
static const char* tag = "jrelog";
static void *logger_thread() {
ssize_t rsize;
char buf[512];
while((rsize = read(pfd[0], buf, sizeof(buf)-1)) > 0) {
if(buf[rsize-1]=='\n') {
rsize=rsize-1;
}
buf[rsize]=0x00;
__android_log_write(ANDROID_LOG_SILENT,tag,buf);
}
}
JNIEXPORT void JNICALL
Java_net_kdt_pojavlaunch_JREUtils_redirectLogcat(JNIEnv *env, jclass clazz) {
// TODO: implement redirectLogcat()
setvbuf(stdout, 0, _IOLBF, 0); // make stdout line-buffered
setvbuf(stderr, 0, _IONBF, 0); // make stderr unbuffered
/* create the pipe and redirect stdout and stderr */
pipe(pfd);
dup2(pfd[1], 1);
dup2(pfd[1], 2);
/* spawn the logging thread */
if(pthread_create(&logger, 0, logger_thread, 0) == -1) {
__android_log_write(ANDROID_LOG_ERROR,tag,"Error while spawning logging thread. JRE output won't be logged.");
}
pthread_detach(logger);
__android_log_write(ANDROID_LOG_INFO,tag,"Starting logging STDIO as jrelog:V");
}

View File

@ -8,7 +8,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
// NOTE: Do not place your application dependencies here; they belong
// NOTE: Do not place syour application dependencies here; they belong
// in the individual module build.gradle files
}
}