Wednesday, June 1, 2016

Debugging and Logging Basics in Android Studio

Basic Tools for Debugging

When creating your code in Android Studio, you may see indicators of syntax errors resulting from typos, missing punctuation, etc. that will cause your program to fail if you try to run it.

The most basic indicator is a red underline showing up both in the associated file names in the project window as well as in the file code itself in the main editor window where the error occurs.




If the error is something detectable a red dash line will show up to the right of the main editor window near the problem to offer a hint on what may need to be done. In the case below a missing semi-colon is expected at the location of the red underline




Fixing the errors will cause the code to automatically remove the red indicators




Orange colored indicators will represent notifications to be aware of, such as unused or unnecessary code in your program.


Using Logcat

 Another useful tool for debugging your programs is Logcat. By using Logcat to generate a log file of your program while it runs you can:

1)  Keep a record of error messages, notifications, and custom messages

2)  Understand your app's runtime behavior

3)  Filter and organize the log messages


How to Create a Custom Log message in Android Studio


First in the MainActivity.java file include the following line at the top of the file near the other "import" commands


import android.util.Log;

 




A local string variable needs to be created to identify the class where the log messages come from, add the following code in your MainActivity.java file somewhere below and inside the public class MainActivity.



private final static String LOG_TAG = MainActivity.class.getSimpleName ();
 
 
 

 
  
In this example a method called buttonPressed is run to create some display text when a button is pressed on the user interface, the custom log messages will be created here to follow along with the user and program actions




 There are 6 types of log messages or "log levels"


 1) Assert.a  - : Highest severity, if you filter log messages at this level no
                            other log level messages will be displayed
 

 2) Error -    .e  - : Second severity, if you filter log messages at this level only
                                        Assert and Error messages will be displayed


 3) Warn -   .w  - : Third severity, if you filter log messages at this level only
                            Assert, Error, and Warning messages will be displayed


 4) Info    -  .i  - :  Fourth severity, if you filter log messages at this level
                            Assert, Error, Warning, and Info messages display


 5) Debug.d  - : Fifth severity, if you filter log messages at this level all
                            other log level messages except Verbose will display


 6) Verbose.v  - : Sixth severity, if you filter log messages at this level all
                              other log level messages will be displayed



In the example, an Info level log text message is generated with the following code just after the user presses a button to log that the method buttonPressed has begun:



Log.i(LOG_TAG,"buttonPressed(View) Called");
 
 


The next log message is a verbose level and contains both a string of text and the value of a variable, by logging what the current value is of a variable during the program run you can check to make sure that the program is behaving as it should.

The code for this text + variable log message in this example looks like this:



Log.v(LOG_TAG, "text changed to:" + stringValue);



 
 
Finally, a debug level log message is added to the end of the buttonPressed method to log that the method has finished running. The code for this text log looks like this:
 
 
Log.d(LOG_TAG,"buttonPressed(View) finished");
 



How to filter Logcat messages in Android Studio


First you will run your code with your built-in custom log messages. In this example, a button is generated and when the user pushes it, some text is generated. When the program is launched the Android Monitor Window will display your log file.



You can filter the different log message levels by selecting the drop down box under the Logcat tab and selecting the level you wish to filter to. You can also search for keywords in your Logcat file using the search browser in the Logcat tab.




Tip:  It's a good idea to create log messages at the beginning and end of an important process in your code so that if you run it more than once you can easily track when and how many times that process ran

No comments:

Post a Comment