Check if device is landscape via ADB - android

Is it possible to check a devices orientation via ADB?
Not by installing any software, calling any existing software, just through ADB. Would guess there is a status file somewhere in /proc, but could not find it yet.

This can be done through the following command:
adb shell dumpsys | grep 'SurfaceOrientation' | awk '{ print $2 }'
The output will be an integer ranging from 0 to 3 for each of the four possible orientations. 0 and 2 are landscapes while 1 and 3 are portraits. As the dumpsys output is very large, the command might take a few seconds to complete.
Update: dgmltn's modification is likely much faster:
adb shell dumpsys input | grep 'SurfaceOrientation' | awk '{ print $2 }'

Simpler solution:
adb shell dumpsys window | grep -i surfaceorientation | awk '{ print $2 }'

I found this method content query --uri content://settings/system --projection name:value --where "name='user_rotation'" after opening adb shell . Although doesn't seem to work if entered without opening a shell first.

In addition to the previous answers, I would like to point out that the return value of adb shell dumpsys | grep 'SurfaceOrientation' does not always follow a specific rule across devices ("0 and 2 are landscapes while 1 and 3 are portraits" is wrong).
I improved the method by an additional query, which advises how to interpret the return value, namely
dumpsys window | grep 'mLandscapeRotation'
value 0 means: 0 and 2 are landscapes, 1 and 3 are portraits
value 1 means the opposite situation

I am not sure if something has changed since the last answers were written, or if it is because I am emulating a different device.
but neither SurfaceOrientation nor mLandscapeRotation showed up in dumpsys for me.
After diffing the outputs of dumpsys between rotations I found
mPredictedRotation
#Normal
$./adb shell dumpsys window displays | grep 'mPredictedRotation'
mPredictedRotation=0
#Right
$./adb shell dumpsys window displays | grep 'mPredictedRotation'
mPredictedRotation=3
#Inverted
$./adb shell dumpsys window displays | grep 'mPredictedRotation'
mPredictedRotation=2
$Left
#./adb shell dumpsys window displays | grep 'mPredictedRotation'
mPredictedRotation=1

Related

Get IMEI via ADB only for old Huawei models

I need a bit of assistance. I have a Huawei g6-l11 (with Android 4.3) from which I am trying to extract the IMEI via ADB. I know that this device is ancient, but this is one of my tasks. So far I had tried everything I could find on the internet, like:
1) adb shell getprop | grep "<IMEI>"
2) adb shell service call iphonesubinfo N | grep "<IMEI>" - Where N is a number between 1 and 50
3) adb shell settings get secure android_id
4) adb shell content query --uri content://settings/secure | grep "<IMEI>"
5) adb shell content query --uri content://settings/system | grep "<IMEI>"
6) adb shell content query --uri content://settings/global | grep "<IMEI>"
7) adb shell dumpsys | grep "<IMEI>"
So I had made an Android app and run this piece of code on the smartphone:
val tm = this.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
Log.d("Emy_","The IMEI is ${tm.deviceId}")
That worked fine but it is an Android app when I need to do the same thing but only via ADB.
Also, I had found a fastboot command that would help me (like: fastboot oem get-psid). But the problem is that I need to reboot the phone into fast boot mode. Which is taking too long.
My questions are:
1) why is it different for Huawei models with the OS version below Marshmallow to extract the IMEI?
2) how could I replicate the function call done by the Java code to be done with the ADB in the terminal? Or in other words, what other commands would you recommend to me to try to extract the IMEI?
You could display it on screen:
adb am start -a android.intent.action.CALL -d tel:*%2306%23
If you just searching to know the IMEI, You can try this code : *#06#
Or you can try this : adb shell
service call iphonesubinfo 1 | toybox cut -d "'" -f2 | toybox grep -Eo '[0-9]' | toybox xargs | toybox sed 's/\ //g'
hop that help you !

find a pattern from grep in bash for computing average memory usage of android app

I want to get the average memory usage of some android app. All I want to do is to fetch only the memory usage. For example, below script provide instantaneous memory usage of mobile_cep application
adb shell dumpsys meminfo | grep mobile_cep
gives output as
233,328K: org.carleton.iot.mobile_cep (pid 27060 / activities)
While I am interested in getting 233,328K value. I want to repeat this process several times and get an average value for memory usage. I am using below script to print 233,328K value.
#!/bin/bash
counter=1
while [ $counter -le 10 ]
do
((counter++))
val1=$(adb shell dumpsys meminfo | grep mobile_cep)
val2=$($val1 | grep -i '\d\d\d,\d\d\dK')
echo $val2
done
echo done
however, I am not getting the desired result. What am I doing wrong?
Piping to sed:
adb shell dumpsys meminfo | grep mobile_cep | sed 's/:.*//'
It is also trivial with awk if you are able to execute an awk script you don't need grep either:
adb shell dumpsys meminfo | awk -F'[: ]' '/mobile_cep/ { print $1 }'

adb logcat specific app only [duplicate]

I observed that when i use Logcat with Eclipse with ADT for Android, I get messages from many other applications as well. Is there a way to filter this and show only messages from my own application only.
Linux and OS X
Use ps/grep/cut to grab the PID, then grep for logcat entries with that PID. Here's the command I use:
adb logcat | grep -F "`adb shell ps | grep com.asanayoga.asanarebel | tr -s [:space:] ' ' | cut -d' ' -f2`"
(You could improve the regex further to avoid the theoretical problem of unrelated log lines containing the same number, but it's never been an issue for me)
This also works when matching multiple processes.
Windows
On Windows you can do:
adb logcat | findstr com.example.package
Package names are guaranteed to be unique so you can use the Log function with the tag as your package name and then filter by package name:
NOTE: As of Build Tools 21.0.3 this will no longer work as TAGS are restricted to 23 characters or less.
Log.<log level>("<your package name>", "message");
adb -d logcat <your package name>:<log level> *:S
-d denotes an actual device and -e denotes an emulator. If there's more than 1 emulator running you can use -s emulator-<emulator number> (eg, -s emulator-5558)
Example: adb -d logcat com.example.example:I *:S
Or if you are using System.out.print to send messages to the log you can use adb -d logcat System.out:I *:S to show only calls to System.out.
You can find all the log levels and more info here: https://developer.android.com/studio/command-line/logcat.html
http://developer.android.com/reference/android/util/Log.html
EDIT: Looks like I jumped the gun a little and just realized you were asking about logcat in Eclipse. What I posted above is for using logcat through adb from the command line. I'm not sure if the same filters transfer over into Eclipse.
Since Android 7.0, logcat has --pid filter option, and pidof command is available, replace com.example.app to your package name.
(ubuntu terminal / Since Android 7.0)
adb logcat --pid=`adb shell pidof -s com.example.app`
or
adb logcat --pid=$(adb shell pidof -s com.example.app)
For more info about pidof command:
https://stackoverflow.com/a/15622698/7651532
Add filter
Specify names
Choose your filter.
This works for me with USB debugging:
The solution is to use your device's own logcat directly via shell.
Connect the device and use:
adb shell
Use logcat after the shell is set up:
logcat | grep com.yourapp.packagename
For me this works in mac Terminal
Got to the folder where you have adb then type below command in terminal
./adb logcat MyTAG:V AndroidRuntime:E *:S
Here it will filter all logs of MyTAG and AndroidRuntime
Update May 17
It's been a few years, and thing have changed. And Eclipse is no longer officially supported. So here's two more up-to-date approaches:
1. Android Studio
In the Android monitor toolbox, you can filter logcat per debuggable process. Normally, when you develop an application it is a debuggable process. Every once in a while I am having issues with this, and a do the following:
Tools -> Android -> Enable ADB Integration.
If it was already enabled, then toggle it off, and then back on
Unplug and replug your mobile device.
There are also options to filter via regex and the debug level
2. logcat-color
This is a nice python wrapper on top of adb logcat if you want to use a terminal based solution. The good thing about it is that you can save multiple configurations and simply reuse them. Filtering by tags is quite reliable. You can also filter by package to see logs of one or more apps only, but you start logcat-color right before launching your app.
Old Answer:
It seems that I can't comment to previous answers, so I will post a new one.
This is a comment to Tom Mulcahy's answer, that shows how the command should change so as to work on most devices, since adb shell ps PID column is variable.
NOTE: The command below works for the cases where you have connected many devices. So device id is needed. Otherwise, you can simply omit the brackets '[', ']'
1. To find out the column of pid, type:
adb [-s DEVICE_ID] shell ps | head -n 1
Now memorise the column number for the PID. Numbering starts from 1.
2. Then type the following:
adb [-s DEVICE_ID] logcat | grep $(adb [-s DEVICE_ID] shell ps \
| grep "com.example" | awk -F" " ' {print $PUT_COLUMN_HERE}')
Simply put the column you memorised in PUT_COLUMN_HERE, e.g. $5
Caveat
Each time you re-run your application, you have to re-run the 2nd command, because the application gets a new PID from the OS.
Ubuntu : adb logcat -b all -v color --pid=`adb shell pidof -s com.packagename` With color and continous log of app
This has been working for me in git bash:
$ pid=$(adb shell ps | grep <package name> | cut -c11-15) ; adb logcat | grep $pid
put this to applog.sh
#!/bin/sh
PACKAGE=$1
APPPID=`adb -d shell ps | grep "${PACKAGE}" | cut -c10-15 | sed -e 's/ //g'`
adb -d logcat -v long \
| tr -d '\r' | sed -e '/^\[.*\]/ {N; s/\n/ /}' | grep -v '^$' \
| grep " ${APPPID}:"
then:
applog.sh com.example.my.package
Using Windows command prompt: adb logcat -d | findstr <package>.
*This was first mentioned by jj_, but it took me ages to find it in the comments...
adb logcat -e "appname"
This works prefectly when filtering rows for one app only.
If you are using Android Studio you can select the process from which you want to receive logcats.
Here is the screenshot.
I wrote a shell script for filtering logcat by package name, which I think is more reliable than using
ps | grep com.example.package | cut -c10-15
It uses /proc/$pid/cmdline to find out the actual pid, then do a grep on logcat
https://gist.github.com/kevinxucs/7340e1b1dd2239a2b04a
Use -s !
You should use your own tag, look at:
http://developer.android.com/reference/android/util/Log.html
Like.
Log.d("AlexeysActivity","what you want to log");
And then when you want to read the log use>
adb logcat -s AlexeysActivity
That filters out everything that doesn't use the same tag.
Source
ADT v15 for Eclipse let you specify an application name (which is actually the package value in your androidmanifest.xml).
I love being able to filter by app, but the new logcat has a bug with the autoscroll. When you scroll up a little to look at previous logs, it automatically scrolls back to the bottom in a couple seconds. It seems scrolling 1/2 way up the log does keep it from jumping back to the bottom, but that's often useless.
EDIT: I tried specifying an app filter from the command-line -- but no luck. If someone figures this out OR how to stop the autoscroll, please let me know.
LogCat Application messages
As a variant you can use third party script PID Cat by Jake Wharton. This script has two major advantages:
shows log entries for processes from a specific application package
color logcat
From documentation:
During application development you often want to only display log messages coming from your app. Unfortunately, because the process ID changes every time you deploy to the phone it becomes a challenge to grep for the right thing.
This script solves that problem by filtering by application package.
An output looks like
In order to access the logcats you first need to install ADB command-line tool. ADB command-line tool is a part of android studio platform tools and can be downloaded from here. After this, you need to set the path/environment variable for adb tools. Now you can access logcat from eclipse terminal/ intellij terminal or mac terminal in case you are using a macbook.
adb logcat : To get entire logcat.
adb shell pidof 'com.example.debug' : To get the process id of your app.
adb logcat pid=<pid> : To get logcat specific to your app.
adb logcat pid=<pid>|grep 'sometext' : To filter logcat on basis of some text.
For more info about filtering logcats read this.
On Windows 10, using Ionic, what worked great to me was combine 'findstr' with the "INFO:CONSOLE" generated by all App messages.
So, my command in command line is:
adb logcat | findstr INFO:CONSOLE
I'm not sure there's a way to only see system messages regarding your app, but you can filter based on a string. If you're doing a log within the program, you can just include a certain unique keyword, and filter based on that word.
Try: Window -> Preferences -> Android -> LogCat. Change field "Show logcat view if ..." the value "VERBOSE". It helped me.
If you are using Eclipse, press the green + sign in the logCat window below and put your package name (com.example.yourappname) in the by Application Name box. Also, choose any name comfortable to you in Filter Name box and click ok. You will see only messages related to your application when the filter you just added is chosen from the left pane in the logCat.
Give your log a name. I called mine "wawa".
In Android Studio, go to Android-> Edit Filter Configurations
Then type in the name you gave the logs. In my case, it's called "wawa". Here are some examples of the types of filters you can do. You can filter by System.out, System.err, Logs, or package names:
This is probably the simplest solution.
On top of a solution from Tom Mulcahy, you can further simplify it like below:
alias logcat="adb logcat | grep `adb shell ps | egrep '\bcom.your.package.name\b' | cut -c10-15`"
Usage is easy as normal alias. Just type the command in your shell:
logcat
The alias setup makes it handy. And the regex makes it robust for multi-process apps, assuming you care about the main process only.
Of coz you can set more aliases for each process as you please. Or use hegazy's solution. :)
In addition, if you want to set logging levels, it is
alias logcat-w="adb logcat *:W | grep `adb shell ps | egrep '\bcom.your.package.name\b' | cut -c10-15`"
You can use below command to fetch verbose logs for your application package
adb logcat com.example.myapp:V *:S
Also if you have rolled out your app and you want to fetch error logs from released app, you can use below command.
adb logcat AndroidRuntime:E *:S
I am usually adding something in the log messages to make it distinct. Or for example unity app you can use "Unity" as matching string.
For mac :
adb logcat | grep "MyUniqueString"
for Windows (powershell ):
adb logcat | Select-String "MyUniqueString"
I have different approach, you can try access to local device's shell.
adb shell
and then follow by
logcat | grep com.package.name
This print all containing that package.
Alternatively, You can try flutter logs --verbose
Another way of getting logs of exact package name when you are inside the shell:
logcat --pid $(ps -ef | grep -E "com.example.app\$" | awk '{print $2}')
I tried to use Tom Mulcahy's answer but unfortunately it was not working for applications with multiple processes so I edit it to fit my needs.
#!/bin/bash
if [ "$#" -ne 1 ]; then echo "Illegal number of parameters"; exit 1; fi
echo "Lof for package name: $1"
PROCESSES=`adb shell ps | grep "$1" | cut -c10-15`
NUM_OF_PROCESSES=`echo "$PROCESSES" | wc -l`
if [ $NUM_OF_PROCESSES -eq 0 ]; then echo "The application is not running!"; exit 1; fi
COUNTER=1
for process in $PROCESSES; do
if [ $COUNTER -eq 1 ]; then GREP_TEXT="("; fi
GREP_TEXT+=$process
if [ $COUNTER -eq $NUM_OF_PROCESSES ]; then GREP_TEXT+=")"; else GREP_TEXT+="|"; fi
let COUNTER=COUNTER+1
if [ $COUNTER -gt $NUM_OF_PROCESSES ]; then break; fi
done
adb logcat | grep -E "$GREP_TEXT"
In addition to Tom Mulcahy's answer, if you want to filter by PID on Windows' console, you can create a little batch file like that:
#ECHO OFF
:: find the process id of our app (2nd token)
FOR /F "tokens=1-2" %%A IN ('adb shell ps ^| findstr com.example.my.package') DO SET PID=%%B
:: run logcat and filter the output by PID
adb logcat | findstr %PID%

adb: Find PID from the adb shell

I am trying to get the PID of the process INSIDE adb shell. So, I am doing adb shell which gets me to the android shell. Now, if I were to get the PID using a regular shell I would use
adb shell ps | grep android.process.acore | sed 's/\s\s*/ /g' | cut -d ' ' -f 2
OR
adb shell ps | grep android.process.acore | awk '{ print $2 }'
I get the PID (a numeric number - 2nd field of the ps | grep android.process.acore) output.
However, if I run the above commands inside android shell(after doing adb shell), I get /system/bin/sh: sed: not found and /system/bin/sh: awk: not found errors respectively. Which means, these commands are not available inside adb shell. However, grep works.
The output of the ps | grep android.process.acore inside adb shell is:
XXX_x21 11826 441 502296 39028 ffffffff 4010ff6c S android.process.acore
I am looking for the number 11826.
How can I extract it inside adb shell?
Also, please help if there is a direct way to get the PID inside the adb shell.
Regards,
Rumit
Android versions starting with 6.0 already include pidof utility:
usage: pidof [-s] [-o omitpid[,omitpid...]] [NAME]...
Print the PIDs of all processes with the given names.
-s single shot, only return one pid.
-o omit PID(s)
Not sure if you can get the PID directly however you can try the following
set `ps |grep android.process.acore`
echo $2
This has the affect of setting the output of the ps command into variables $1, $2, $3 etc. The PID value is in $2
I tried this one and it seems to work:
adb shell "set "ps | grep android.process.media"; kill -9 $2"
adb shell pidof [package name]
or
adb shell pidof -s [package name]
-s option is for single shot, returning only one pid.
I tried this one and it seems to work:
adb shell
ps -A | grep "android.process.acore"

Filter LogCat to get only the messages from My Application in Android?

I observed that when i use Logcat with Eclipse with ADT for Android, I get messages from many other applications as well. Is there a way to filter this and show only messages from my own application only.
Linux and OS X
Use ps/grep/cut to grab the PID, then grep for logcat entries with that PID. Here's the command I use:
adb logcat | grep -F "`adb shell ps | grep com.asanayoga.asanarebel | tr -s [:space:] ' ' | cut -d' ' -f2`"
(You could improve the regex further to avoid the theoretical problem of unrelated log lines containing the same number, but it's never been an issue for me)
This also works when matching multiple processes.
Windows
On Windows you can do:
adb logcat | findstr com.example.package
Package names are guaranteed to be unique so you can use the Log function with the tag as your package name and then filter by package name:
NOTE: As of Build Tools 21.0.3 this will no longer work as TAGS are restricted to 23 characters or less.
Log.<log level>("<your package name>", "message");
adb -d logcat <your package name>:<log level> *:S
-d denotes an actual device and -e denotes an emulator. If there's more than 1 emulator running you can use -s emulator-<emulator number> (eg, -s emulator-5558)
Example: adb -d logcat com.example.example:I *:S
Or if you are using System.out.print to send messages to the log you can use adb -d logcat System.out:I *:S to show only calls to System.out.
You can find all the log levels and more info here: https://developer.android.com/studio/command-line/logcat.html
http://developer.android.com/reference/android/util/Log.html
EDIT: Looks like I jumped the gun a little and just realized you were asking about logcat in Eclipse. What I posted above is for using logcat through adb from the command line. I'm not sure if the same filters transfer over into Eclipse.
Since Android 7.0, logcat has --pid filter option, and pidof command is available, replace com.example.app to your package name.
(ubuntu terminal / Since Android 7.0)
adb logcat --pid=`adb shell pidof -s com.example.app`
or
adb logcat --pid=$(adb shell pidof -s com.example.app)
For more info about pidof command:
https://stackoverflow.com/a/15622698/7651532
Add filter
Specify names
Choose your filter.
This works for me with USB debugging:
The solution is to use your device's own logcat directly via shell.
Connect the device and use:
adb shell
Use logcat after the shell is set up:
logcat | grep com.yourapp.packagename
For me this works in mac Terminal
Got to the folder where you have adb then type below command in terminal
./adb logcat MyTAG:V AndroidRuntime:E *:S
Here it will filter all logs of MyTAG and AndroidRuntime
Update May 17
It's been a few years, and thing have changed. And Eclipse is no longer officially supported. So here's two more up-to-date approaches:
1. Android Studio
In the Android monitor toolbox, you can filter logcat per debuggable process. Normally, when you develop an application it is a debuggable process. Every once in a while I am having issues with this, and a do the following:
Tools -> Android -> Enable ADB Integration.
If it was already enabled, then toggle it off, and then back on
Unplug and replug your mobile device.
There are also options to filter via regex and the debug level
2. logcat-color
This is a nice python wrapper on top of adb logcat if you want to use a terminal based solution. The good thing about it is that you can save multiple configurations and simply reuse them. Filtering by tags is quite reliable. You can also filter by package to see logs of one or more apps only, but you start logcat-color right before launching your app.
Old Answer:
It seems that I can't comment to previous answers, so I will post a new one.
This is a comment to Tom Mulcahy's answer, that shows how the command should change so as to work on most devices, since adb shell ps PID column is variable.
NOTE: The command below works for the cases where you have connected many devices. So device id is needed. Otherwise, you can simply omit the brackets '[', ']'
1. To find out the column of pid, type:
adb [-s DEVICE_ID] shell ps | head -n 1
Now memorise the column number for the PID. Numbering starts from 1.
2. Then type the following:
adb [-s DEVICE_ID] logcat | grep $(adb [-s DEVICE_ID] shell ps \
| grep "com.example" | awk -F" " ' {print $PUT_COLUMN_HERE}')
Simply put the column you memorised in PUT_COLUMN_HERE, e.g. $5
Caveat
Each time you re-run your application, you have to re-run the 2nd command, because the application gets a new PID from the OS.
Ubuntu : adb logcat -b all -v color --pid=`adb shell pidof -s com.packagename` With color and continous log of app
This has been working for me in git bash:
$ pid=$(adb shell ps | grep <package name> | cut -c11-15) ; adb logcat | grep $pid
put this to applog.sh
#!/bin/sh
PACKAGE=$1
APPPID=`adb -d shell ps | grep "${PACKAGE}" | cut -c10-15 | sed -e 's/ //g'`
adb -d logcat -v long \
| tr -d '\r' | sed -e '/^\[.*\]/ {N; s/\n/ /}' | grep -v '^$' \
| grep " ${APPPID}:"
then:
applog.sh com.example.my.package
Using Windows command prompt: adb logcat -d | findstr <package>.
*This was first mentioned by jj_, but it took me ages to find it in the comments...
adb logcat -e "appname"
This works prefectly when filtering rows for one app only.
If you are using Android Studio you can select the process from which you want to receive logcats.
Here is the screenshot.
I wrote a shell script for filtering logcat by package name, which I think is more reliable than using
ps | grep com.example.package | cut -c10-15
It uses /proc/$pid/cmdline to find out the actual pid, then do a grep on logcat
https://gist.github.com/kevinxucs/7340e1b1dd2239a2b04a
Use -s !
You should use your own tag, look at:
http://developer.android.com/reference/android/util/Log.html
Like.
Log.d("AlexeysActivity","what you want to log");
And then when you want to read the log use>
adb logcat -s AlexeysActivity
That filters out everything that doesn't use the same tag.
Source
ADT v15 for Eclipse let you specify an application name (which is actually the package value in your androidmanifest.xml).
I love being able to filter by app, but the new logcat has a bug with the autoscroll. When you scroll up a little to look at previous logs, it automatically scrolls back to the bottom in a couple seconds. It seems scrolling 1/2 way up the log does keep it from jumping back to the bottom, but that's often useless.
EDIT: I tried specifying an app filter from the command-line -- but no luck. If someone figures this out OR how to stop the autoscroll, please let me know.
LogCat Application messages
As a variant you can use third party script PID Cat by Jake Wharton. This script has two major advantages:
shows log entries for processes from a specific application package
color logcat
From documentation:
During application development you often want to only display log messages coming from your app. Unfortunately, because the process ID changes every time you deploy to the phone it becomes a challenge to grep for the right thing.
This script solves that problem by filtering by application package.
An output looks like
In order to access the logcats you first need to install ADB command-line tool. ADB command-line tool is a part of android studio platform tools and can be downloaded from here. After this, you need to set the path/environment variable for adb tools. Now you can access logcat from eclipse terminal/ intellij terminal or mac terminal in case you are using a macbook.
adb logcat : To get entire logcat.
adb shell pidof 'com.example.debug' : To get the process id of your app.
adb logcat pid=<pid> : To get logcat specific to your app.
adb logcat pid=<pid>|grep 'sometext' : To filter logcat on basis of some text.
For more info about filtering logcats read this.
On Windows 10, using Ionic, what worked great to me was combine 'findstr' with the "INFO:CONSOLE" generated by all App messages.
So, my command in command line is:
adb logcat | findstr INFO:CONSOLE
I'm not sure there's a way to only see system messages regarding your app, but you can filter based on a string. If you're doing a log within the program, you can just include a certain unique keyword, and filter based on that word.
Try: Window -> Preferences -> Android -> LogCat. Change field "Show logcat view if ..." the value "VERBOSE". It helped me.
If you are using Eclipse, press the green + sign in the logCat window below and put your package name (com.example.yourappname) in the by Application Name box. Also, choose any name comfortable to you in Filter Name box and click ok. You will see only messages related to your application when the filter you just added is chosen from the left pane in the logCat.
Give your log a name. I called mine "wawa".
In Android Studio, go to Android-> Edit Filter Configurations
Then type in the name you gave the logs. In my case, it's called "wawa". Here are some examples of the types of filters you can do. You can filter by System.out, System.err, Logs, or package names:
This is probably the simplest solution.
On top of a solution from Tom Mulcahy, you can further simplify it like below:
alias logcat="adb logcat | grep `adb shell ps | egrep '\bcom.your.package.name\b' | cut -c10-15`"
Usage is easy as normal alias. Just type the command in your shell:
logcat
The alias setup makes it handy. And the regex makes it robust for multi-process apps, assuming you care about the main process only.
Of coz you can set more aliases for each process as you please. Or use hegazy's solution. :)
In addition, if you want to set logging levels, it is
alias logcat-w="adb logcat *:W | grep `adb shell ps | egrep '\bcom.your.package.name\b' | cut -c10-15`"
You can use below command to fetch verbose logs for your application package
adb logcat com.example.myapp:V *:S
Also if you have rolled out your app and you want to fetch error logs from released app, you can use below command.
adb logcat AndroidRuntime:E *:S
I am usually adding something in the log messages to make it distinct. Or for example unity app you can use "Unity" as matching string.
For mac :
adb logcat | grep "MyUniqueString"
for Windows (powershell ):
adb logcat | Select-String "MyUniqueString"
I have different approach, you can try access to local device's shell.
adb shell
and then follow by
logcat | grep com.package.name
This print all containing that package.
Alternatively, You can try flutter logs --verbose
Another way of getting logs of exact package name when you are inside the shell:
logcat --pid $(ps -ef | grep -E "com.example.app\$" | awk '{print $2}')
I tried to use Tom Mulcahy's answer but unfortunately it was not working for applications with multiple processes so I edit it to fit my needs.
#!/bin/bash
if [ "$#" -ne 1 ]; then echo "Illegal number of parameters"; exit 1; fi
echo "Lof for package name: $1"
PROCESSES=`adb shell ps | grep "$1" | cut -c10-15`
NUM_OF_PROCESSES=`echo "$PROCESSES" | wc -l`
if [ $NUM_OF_PROCESSES -eq 0 ]; then echo "The application is not running!"; exit 1; fi
COUNTER=1
for process in $PROCESSES; do
if [ $COUNTER -eq 1 ]; then GREP_TEXT="("; fi
GREP_TEXT+=$process
if [ $COUNTER -eq $NUM_OF_PROCESSES ]; then GREP_TEXT+=")"; else GREP_TEXT+="|"; fi
let COUNTER=COUNTER+1
if [ $COUNTER -gt $NUM_OF_PROCESSES ]; then break; fi
done
adb logcat | grep -E "$GREP_TEXT"
In addition to Tom Mulcahy's answer, if you want to filter by PID on Windows' console, you can create a little batch file like that:
#ECHO OFF
:: find the process id of our app (2nd token)
FOR /F "tokens=1-2" %%A IN ('adb shell ps ^| findstr com.example.my.package') DO SET PID=%%B
:: run logcat and filter the output by PID
adb logcat | findstr %PID%

Categories

Resources