Question

I have created an application that extensively requires user inputs and interaction and even though I have made sure that I test and catch every possible case that might throw an error I want to be able to create a mechanism that traces the error in case my application crashes on the field.

I want to be able to record the entire flow right from a button click till whatever the user might be selecting or the navigation between the pages in a log file such that in case my application crashes I'm able to study the trace file later and know exactly where the error occurred.

I'm very new to this sort of programming and therefore any pointers on the above will be very helpful! Thank you in advance :]

PS: I'm not even sure whether what im referring to will be correctly called a "log trace" or not so any edit is welcome. :)

EDIT : I also want to be able to save the error report generated and send it to a particular id (similar to 'send an error report to xyz).

UPDATE :

 public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);


    try {
        File myFiles = new File("/sdcard/ScanApp");

        if(!myFiles.exists())
        {
            myFiles.mkdirs();
        }

        File myFile = new File("sdcard/ScanApp/log.txt");
        myFile.createNewFile();

        myFile.delete();

        myFile.createNewFile();

        String cmd = "logcat -d -v time -f "+myFile.getAbsolutePath()+ " -s ActivityManager:V";
        Runtime.getRuntime().exec(cmd);
        Logs.this.finish();
    } 
    catch (Exception e)
    {
        flag=1;
        error=e.getMessage();
    }

I used this in a previous application for recording any application activity and make a textfile and save it to the SD card, but the contents weren't exactly what I was looking for. Is the solution im looking for something along these lines?

Was it helpful?

Solution

Here, check for the link for reference.

In here you create a class say ExceptionHandler that implements java.lang.Thread.UncaughtExceptionHandler..

Inside this class you will do your life saving stuff like creating stacktrace and gettin ready to upload error report etc....

Now comes the important part i.e. How to catch that exception. Though it is very simple. Copy following line of code in your each Activity just after the call of super method in your overriden onCreate method.

Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));

Your Activity may look something like this…

public class ForceClose extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));

        setContentView(R.layout.main);
    }
}

Hope this helps...

OTHER TIPS

You need to look up on Exception Handling. That is when your application crashes or any other app level errors occur, the code in the exception block executes. So in that place, log that error in a text-file and which solves your "log trace" issue.

Refer the link for beautiful examples.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top