Logs in my code do not display when getting logcat programmatically - android

My android code contains Log.e. When getting logcat programmatically, those logs are not displayed. My code for logcat is
String command = "logcat MyApp:V";
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder result = new StringBuilder();
String currentLine;
while ((currentLine = reader.readLine()) != null) {
result.append(currentLine);
result.append("\n");
}
Log.e(TAG, "printLog: " + result.toString());

Dont use result.toString.. Instead use resultLog.e(TAG, "printLog: " + result);

Related

How to send Command prompt output to android

I am creating an app in which it will send some command to server and i want to get the output of that command on client (android).
Basically i am sending command "systeminfo" and the output is too big to handle, so is there any way to get that big output on android as text view or anything else?
Code is as below
Process process = Runtime.getRuntime().exec("systeminfo");
and for get the output i have used
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
I am confused how to use it as too much of string. Any reference material would be appreciated.
I'd recommend using a StringBuilder to reconstruct each line from the BufferedReader
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
builder.append(line).append("\n"); // Remove the \n if you don't want newlines
}
final String execOutput = builder.toString();

Logcat is only showing my PID

I am trying to get the logcat programmatically from my application. There are plenty of tutorials online on how to do this and I am doing it in one of my fragments as such:
Process process = Runtime.getRuntime().exec("logcat -v time -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");
}
bufferedReader.close();
This works great; the only problem is that what is returned are only logcat entries from my PID. I would like all logcat entries. I do have READ_LOGS permission. Any ideas what is going on here?

How to read and write logcat logs problematically

I know there are several similar questions however, I couldn't figure out what is wrong with my code. I am trying to read the program logs and write them in an activity. I basically wanted to write only my tagged Debug and Error messages to show but it does still show logs I don't want.
Here it is my code:
String[] command=new String[]{"logcat", "-d","-s","*:D"};
Process process = Runtime.getRuntime().exec(command);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append("\n"+line);
}
TextView txtLogs = (TextView)findViewById(R.id.txtLogs);
txtLogs.setMovementMethod(new ScrollingMovementMethod());
txtLogs.setText(log.toString());
and i have tags like ;
Log.e(TAG, "Error onCreate() " + ex.getMessage());
and I am getting result like
D/ScanFragment(18675): message
Why do I get a log like 'message' ?
thank you

Programmatically get log cat data

i want to get logcat data programmatically i used the below code
StringBuilder debuglog=new StringBuilder();
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
debuglog.append(line);
debuglog.append("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
Above code is working fine
This line "logcat -d" shows -d,-w,-i messages
But,my problem is i want to show -e messages
To get only ERROR use this:
Process process = Runtime.getRuntime().exec("logcat -d *:E");

Android TCP read several line

Does Android TCP Socket Client read one more line the response??
inputStreamReader = new InputStreamReader(socket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
response = bufferedReader.readLine();
response = bufferedReader.readLine();
Log.i(TAG, "Response :: " + response);
I cannot read two line. Because my server will response
200 OK \n
Content.......
And the content will stream to the client every seconds, I don't wanna connect the socket every times. Can sbd help??
An example that will continue to read until an empty new line is found:
inputStreamReader = new InputStreamReader(socket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
String line = bufferedReader.readLine(); // add first line
while (line != "")
{
response += line;
line = bufferedReader.readLine();
}
Log.i(TAG, "Response :: " + response);

Categories

Resources