Lesson: Exercise: Using Logging

Overview: 
Explore adding logging of debugging information to a file on the EV3 and then pulling that file back to the PC for examination.
Objectives: 

Add the provided logging utility source code to your project and then understand how to use logging in your programs.

 

Content: 

We are now going to take a look at logging (also called tracing) as a tool to debug our robot programs. Logging is recording useful information from our robot program to a disk file on the EV3 controller. We can then download that file to the development PC and examine it. It can be very useful to record information while your robot is running during a match so you can look at it afterwards and see what took place. If you have not read the general lesson on Logging, you should do that now then return here.

To get started with logging, we will install the Logging class provided with this course. In Eclipse, select the src node in the package explorer and add a new package called ev3.exercises.library. We call it library because we will put classes here that can be used as resources by your robot program classes. You could call it utility instead of library. The idea is to organize the support classes shared by your robot programs.

In that new package, create a class called Logging. Click here to open the Logging code for this class. Copy all of the code paste it into the Logging.java class in Eclipse overwriting the original code. 

Now we are going to take the last exercise, DriveCircle and outfit it with logging. Create a new class called DriveCircleLogging in the ev3.exercises package. Copy the code below into that class.

Lets examine what is different. At the top of the program we added an import for all members of our library package. Then we add a call to Logging.setup() to get logging started. Note setup must be wrapped in a try/catch block. We pass in the name of the class we are working with as shown. Once we have done that we can use the Logging.log() method to write whatever information we need to the log file. You can see several examples of that in the code. One example shows using Java format specifiers and variable parameters to include data values in the log message. You can read more about formatting here.

The information we log is written to a disk file called Logging.txt on the EV3. You can see that file appear on the EV3 Programs list after running this new program. The file will be overwritten each time you run a program.

Connect to the EV3 with the EV3 Control Center and look at the Programs list. You will see Logging.txt on that list and you can download it to your PC and open it in Notepad. This what it will look like:

<1>02:21:53:990 DriveCircleLogging.main(DriveCircleLogging.java:25): Starting DriveCircleLogging
<1>02:21:57:348 DriveCircleLogging.main(DriveCircleLogging.java:42): waiting for key press
<1>02:22:01:360 DriveCircleLogging.main(DriveCircleLogging.java:46): started
<1>02:22:04:947 DriveCircleLogging.main(DriveCircleLogging.java:59): sensor touched
<1>02:22:05:107 DriveCircleLogging.main(DriveCircleLogging.java:76): s=This is a test, i=2, d=6.750000
<1>02:22:05:146 DriveCircleLogging.main(DriveCircleLogging.java:78): done

You can use this logging scheme if you wish or explore other ways to log information on your own.

 

Navigation: