I read the other posts and can't figure out the "trick".
I looked at Log Collector but can't use a separate APK. I'm basically using the same approach and I consistently get nothing back on the processes inputstream.
I have READ_LOGS in the manifest.
From within my default activity, I'm able to get the log, but if I move the logic to another activity or utilize an asynctask, no output is returned.
this code is from my default activity... inline, i dump it to the log
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
Log.d(LOGTAG, "Logcat: " +log.toString());
} catch (IOException e) {}
if i wrap it in an asynctask or just inline it in another activity, it returns nothing
ArrayList<String> commandLine = new ArrayList<String>();
//terminate on completion and suppress everything except the filter
commandLine.add("logcat -d -s");
...
//replace asynctask with inline (could not get log in asynctask)
showProgressDialog(getString(R.string.acquiring_log_progress_dialog_message));
final StringBuilder log = new StringBuilder();
BufferedReader bufferedReader = null;
try{
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null){
log.append(line);
log.append(MangoApp.LINE_SEPARATOR);
}
sendIntent.putExtra(Intent.EXTRA_TEXT, log.toString());
startActivity(Intent.createChooser(sendIntent, getString(R.string.chooser_title)));
dismissProgressDialog();
dismissMainDialog();
finish();
}
catch (IOException e){
dismissProgressDialog();
showErrorDialog(getString(R.string.failed_to_get_log_message));
Log.e(LOGTAG, "Log collection failed: ", e);//$NON-NLS-1$
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ignore) {}
}
}
Can anyone spot the diff or explain the magic? I'm pretty sure the commandline is right in the second version so scratching my head. I'm using 2.1 SDK 7 on the emulator.
Thanks
Hope this will be helpful, you don't have to create file by your self just execute the below command, to get the error info.
Runtime.getRuntime().exec("logcat -v time -r 100 -f /sdcard/log.txt *:E");
Logcat parameters options:
-r <size in kilobytes> -> for specifying the size of file
-f <filename> -> file to which you want to write the logs.
Can you try it without the ArrayList. Just pass the command String
I have implemented it in the following way (without the ArrayList). It works for me.
String baseCommand = "logcat -v time";
baseCommand += " MyApp:I "; // Info for my app
baseCommand += " *:S "; // Silence others
ServicesController.logReaderProcess = Runtime.getRuntime().exec(baseCommand);
Related
I am creating an app for personal use only, and for a while now I have been trying to figure out how to connect to a third-party's app's database without sharing content user ID. Note: I have root, so all root options are welcome too!
I have been using this:
protected String RefreshMessages() {
try {
String line;
String result = "";
Process process = Runtime.getRuntime().exec("su");
OutputStream stdin = process.getOutputStream();
InputStream stdout = process.getInputStream();
stdin.write("su -c 'sqlite3 \"/data/data/<app>/databases/database.db\" \"" + REFRESH_QUERY + "\"'\n".getBytes());
stdin.write("exit\n".getBytes());
stdin.flush();
stdin.close();
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while((line = br.readLine()) != null){
result = result + line;
}
Log.d("[Output]", result);
br.close();
process.waitFor();
process.destroy();
return result;
} catch (Exception ex) {
Log.d("ERR-RFRSH-MSG", ex.toString());
return "0";
}
}
And spamming it in a service as fast as I can to get updates, but seriously.. there has to be a better option available... especially as a root user. Something that allows me to connect properly and get live information from the database. I have literally Google'd for months and this is the best I could find.
I also found stuff about copying the database into a TEMP folder within my app's domain and then reading contents, but I need to read information quick. Constantly copying over the whole database is too slow.
Please tell me I have better options, Android wizards!
I have an application and I'd like to collect the LogCat messages of a specified level and tag.
Can I somehow get the accumulated messages at some point? I don't want to collect the messages one by one, it should be the sum of them like when I use adb to read the actual log. Is this possible?
Try this: Note that in Android 4 you will only see the log messages that were written by your own app unless you have root access.
public static String getLog(Context c) {
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append("\n");
}
return log.toString();
} catch (IOException e) {
return null;
}
}
Why not just write them to a file instead? LogCat is really for real-time logs. There are lots of good quality logging packages that can log to a file if that's what you want to do.
Just as an example:
How to write logs in text file when using java.util.logging.Logger
I want get complete system log. Currently, I'm using below code to get system log
try {
final Process process = Runtime.getRuntime().exec("logcat -d -v threadtime *:I");
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
lineList.add(line);
}
} catch (final IOException e) {
Log.e(e.toString());
}
I'm able to get system log successfully using above code. It gives only last 30 min log. But, I want complete system log from my application launch. Is it possible to write system log to our private file?
To write continuously to a file, try this:
... exec( "logcat -f cachedirectory/logfile.txt -v threadtime" );
See the -f option in http://developer.android.com/tools/help/logcat.html.
I am trying to read from the logcat output in my app. I am able to read in correctly, but it goes on reading it in endless loop. Somehow there seems no way to detect the end of stream.
Not sure what I am doing wrong.
Here is my code:
String baseCommand = "logcat -v time MyTag:D *:S";
Process process = null;
try {
process = Runtime.getRuntime().exec(baseCommand);
InputStreamReader reader = new InputStreamReader(process.getInputStream());
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
Log.d("SomeOtherTag", line); //This line executes endlessly
}
} catch (IOException e) {
Log.e(DEBUG_TAG, "error in logging");
e.printStackTrace();
}
Logcat doesn't exit so the buffer is blocked.
Use 'logcat -d' in order to dump the log and then exit.
Hope this still helps, Yaron
Not positive but I believe you need to pass the logcat call if it has args in a String[] so it would be something like
String[] baseCommand = {"logcat", "-v", "time", "MyTag:D", "*:S"};
then the rest of your code.
The single string call is just the program name, not the args.
I wrote some code that runs an android command line and collect the output.
it is executing "ls" correctly but when I put the command "top -n 1" it shows nothing.
is it a manifest issue? the phone is not rooted and when using "terminal emulator" I can see "top" output.
here is the code:
// ** execute command line and gather the output **//
final StringBuilder log = new StringBuilder();
try{
ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("top");
commandLine.add("-n1");
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null){
log.append(line);
log.append(", \n");
}
log.append(", \n");
}
catch (IOException e){
}
thanks,
A.
You might want to show some of your code. Generally, commands you run using Runtime are not executed in a shell, so you might want to try something like "sh -c top -n 1" as the prog parameter.