Logcat displays nothing when used from Runtime.exec - android

I'm running the command
logcat -d AndroidRuntime:E *:S
When ran from adb on my computer, it displays all the things it should. When I try to run it from an Android application with
Runtime.getRuntime().exec("logcat -d AndroidRuntime:E *:S");
and print the output, it won't display anything except the headers.
How can I fix this?

First, this has never been supported.
Second, if you are running on Android 4.1 and later, you will only get any log messages that your own app logs, not messages from other apps, as you can no longer hold the READ_LOGS permission in an ordinary SDK app.

Related

How to get logcat crashes

A newbie logcat question here, I am sure this is well known but could not find the answer. Sorry.
I want to use logcat for the users of my app MyApp to email me crash reports that obviously I don't know where they will appear. I want to print Warning and above only for my app.
I read the the documentation, and understood that, to print logcat only for a package I should do something like:
logcat -d -v time MyApp:W *:S
However that does not work: that is just for tags, not for app names.
I read people suggesting something like
logcat -d -v time *:W | grep MyApp
but that does not work either: the error messages, exactly what I am interested in, do not print MyApp name in all the lines.
I also tried
logcat -d -v time --pid=$(pidof -s MyApp) *:W
but that does not work either since the app crashed already, so it is not running, and it will send me the log of the still running MyApp PID.
What I ended up doing was to print only the last 20 minutes of everything with:
long timesinceepoch = System.currentTimeMillis()/1000 - 1200;
Process process = Runtime.getRuntime().exec("logcat -d -v time -t "+timesinceepoch+".0 *:W");
but I still get lots of trash and big files, of course.
So how do I add a MyApp tag to ALL the logcat logs of MyApp?
EDIT: Actually, in Android Studio the MyApp appears in the logcat, and there is an option to show logcat for a single app. Isn't Android Studio logcat and adb logcat the same??
This is totally unnecessary since when an app calls logcat it only logs the calling app logs. See this (thanks #jake-lee !).

ADB logcat fails to clear the main log

When I connect to a device or an emulator using adb, I can clear logcat using adb logcat -c. The issue I am facing is that I often get the following message:
failed to clear the 'main' log
AFAIK, 'main' is a non-rooted buffer which means that I should be able to clear it without root. What can then be the reason for this message?
You might have your Android Studio opened, please close it and run again , it should able to clear the logs.
use
adb logcat -b all -c
it will clear all buffers. sometime adb logcat -c will not work because of many process using it like android studio.
Logcat messages are stored in buffer, you can try,
adb logcat -c
Clear (flush) the selected buffers and exit. The default buffer set is main, system and crash. To clear all of the buffers, use -b all -c.
source: logcat doc
For more logcat usage, check How do I get the logfile from an Android device?

Android AIDE, logcat scrolling on runtime error

On every runtime error in AIDE (when the application crashes) the logcat window is displaying all messages again (even from the last days) which needs long time to scroll to the end to find out what's wrong. I don't want always clear the logcat list before i compile the project, but i did not find any setting to avoid this behavior. Does anyone have a solution for this?
Actually you can have command line,use
Terminal IDE if your device is running on android 4.x
or lower,
Termux if Lollipop+,
launch your app within one of these Terminal Emulators with am command:
am start -a android.intent.action.Main -n com.your.package/.Ur_Main_Activity
And then use the logcat command to observe log, it will be a bit cumbersome though, use filters as you see fit. Enter ctrl+C to end Logcat dump. Hope this helps,

To get complete logs of 2 devices using adb

I am testing an app with 2 devices communicating using sockets and monitoring the timestamp values. I select the required device name from the DDMS perspective. But some initial logs are missing(as I have many Log.d statements in the code). I want to store the whole log file after I stop apps in both the phones. Can someone tell me how this can be done in command line using adb? I couldn't find any example for 2 devices.
Thanks
You can try with two console and get logcat separately for two devices
Get serial no for each devices using adb devices
and save logcat as text files
console 1: adb -s <device1serialNO> logcat -d > logcat1.txt
console 2: adb -s <device2serialNO> logcat -d > logcat2.txt
You can filter result for your given tag if needed
Before launching your application you can start the following command in your terminal:
adb logcat <your_application_log_tag>:V *:S > file.txt
<your_application_log_tag> is a log tag that you use in your application. *:S means that you suppress all log outputs from other components. > file.txt redirects the output of the command to file.txt.
The cause of the problem is that for logging Android has a buffer in RAM and if it becomes full it rewrites the most old entries (FIFO). The command I've provided will store the log on your computer.

Where does Android store shutdown logs?

I know that the boot up log can be obtained by pulling out contents of kmsg or dmesg through ADB.
But I'm not aware of how to retrieve the shutdown logs in Android as there's no /var folder in Android (place where most desktop linux distros generally store their shutdown logs).
So how can I obtain the shutdown logs in Android?
Look in some locations such as these:
/proc/last_kmsg
/data/tombstones/
/data/dontpanic/
/data/system/dropbox/
(This list isn't strictly kernel logs, including framework and application logs too, which are also sometimes of interest)
One work around I found for collecting shutdown logs in Android is to run adb pull /proc/kmsg C:\Logs.txt on the host PC and then switch off the device. You will get the logs till the USB communication between the host and the device snaps! I know this is only one case out of the numerous shutdown scenarios but I haven't found satisfactory answers for other cases!
TL;DR:
Run command through adb that copies logcat and proc/kmsg to a file and keep it running even when adb disconnects with nohup, disown or setsid. Probably needs busybox, needs root and adb root, too.
setsid cat proc/kmsg > /sdcard/kmsg.txt &
and
logcat -v long -f /sdcard/logcat.txt (somehow only works without setsid)
Or add normal copy commands to some startup script.
/TL;DR
You can constantly copy proc/kmsg and logcat to a file on your android device or a microSD card to get the logs even after adb disconnects.
You need root access and adb root access for this to work. For the latter, use the setting in the developer options if you have a custom rom or the adbd insecure app.
After using adb shell to get your android shell, type su to get superuser access.
Then you not only need to put an ampersand (&) after the command but also make sure that the command keeps running after adb disconnects. That is done by nohup, disown or setsid (see here for usage).
If it doesn't work because you don't have these commands, you need to install busybox.
See my question here.
See here for how to get logcat and kernel logs and print it to some file or merge it.
See developer.android.com/tools/help/logcat.html for parameters for the logcat command.
In the end you could have a command like setsid cat proc/kmsg > /sdcard/kmsg.txt & for the kernel messages.
For logcat you could have one of the following commands: logcat -v long -f /sdcard/logcat.txt or logcat -v long > /sdcard/logcat.txt
I don't know why, but sometimes it didn't work with setsid and just didn't copy continuously but stopped shortly after executing the command. In these situations, it also showed up when entering jobs, which it didn't otherwise. Then it just worked without setsid, it stayed alive after disconnecting and reconnecting. I guess you must just try when the file does keep getting larger. If someone figured out why it is behaving like it is... let me know and I'll edit the answer.
Probably adding the commands to a startup script could be a solution for some, too.
Hope this helps.
fightcookie
Newer phones do NOT use any of these locations so if you're reading this article then as of now
The kernel crash logs are now in /sys/fs/pstore instead of /proc/last_kmsg
I was looking for the same thing, and finally, I found the answer!
In android 8 all logs are located in \data\log\android_logs\... including apps and kernel logs. Kernel logs are called kmsgcat-log_timestamp_.gz
edit: Although this is a very old thread, I think the answer might be helpful.

Categories

Resources