Android is an open-source mobile phone operating system launched by Google based on the Linux platform. It has been launched to reach out to the favorite of the majority of programmers. Here, we will first have an in-depth understanding of the Android logcat application to facilitate our practical application.
Options and descriptions
- -S: sets the filter by default.
- -F file output to log file
- -C clear logs
- -D. Obtain logs.
- -G: Get the log size.
- For the-v format setting log, see the following format print format)
-V format and example
- brief W/tag ( 876): message
- process W( 876) message (tag)
- tag W/tag : message
- thread W( 876:0x37c) message
- raw message
- time 09-08 05:40:26.729 W/tag ( 876): message
- threadtime 09-08 05:40:26.729 876 892 W tag : message
- long [ 09-08 05:40:26.729 876:0x37c W/tag ] message
Code example:
AndroidManifest. xml: Add read permission
- < uses-permission android:name=
"android.permission.READ_LOGS" />
- < uses-permission android:name=
"android.permission.READ_LOGS" />
Clear logs
- try {
- Runtime.getRuntime().exec("logcat -c");
- } catch(Exception e) {
- try {
- Runtime.getRuntime().exec("logcat -c");
- } catch(Exception e) {
- }
Get logs
- try {
- ArrayList< String> commandLine = new ArrayList< String>();
- commandLine.add( "logcat");
- commandLine.add( "-d");
- commandLine.add( "-v");
- commandLine.add( "time");
- commandLine.add( "-s");
- commandLine.add( "tag:W");
- Process process = Runtime.getRuntime().exec
( commandLine.toArray( new String[commandLine.size()]));
- BufferedReader bufferedReader = new BufferedReader
( new InputStreamReader(process.getInputStream()), 1024);
- String line = bufferedReader.readLine();
- while ( line != null) {
- log.append(line);
- log.append("\n")
- }
- } catch ( IOException e) {
- }
- try {
- ArrayList< String> commandLine = new ArrayList< String>();
- commandLine.add( "logcat");
- commandLine.add( "-d");
- commandLine.add( "-v");
- commandLine.add( "time");
- commandLine.add( "-s");
- commandLine.add( "tag:W");
- Process process = Runtime.getRuntime().exec
( commandLine.toArray( new String[commandLine.size()]));
- BufferedReader bufferedReader = new BufferedReader
( new InputStreamReader(process.getInputStream()), 1024);
- String line = bufferedReader.readLine();
- while ( line != null) {
- log.append(line);
- log.append("\n")
- }
- } catch ( IOException e) {
- }
Result:
- 09-08 09:44:42.267 W/tag ( 754): message1
- 09-08 09:44:42.709 W/tag ( 754): message2
- 09-08 09:44:43.187 W/tag ( 754): message3
- 09-08 09:44:45.295 E/tag ( 754): message8