1

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?

Garima Tiwari
  • 1,490
  • 6
  • 28
  • 46
  • check out [this link](http://stackoverflow.com/a/16562770/2345913) – CRUSADER May 27 '13 at 06:16
  • I am looking for something similar to what you talk about in your link, only I want to be able to record the stack trace previous to what caused the exeption and not the exception alone. Also is there is some way I can save it to a text file in the SD card or be able to upload the error report ? ( in reference to your answer in the link as you mentioned that ) – Garima Tiwari May 27 '13 at 08:03
  • you must have observed many people answer the questions in SOF just by looking at stack trace that caused the problem, like NumberFormatException in line no. XX inside someMethod(). So, you get clear idea where exactly the exception has occured, you dont require stack traces of before exception occurs... – CRUSADER May 27 '13 at 13:46
  • And ofcorse you can save the error log in in your sqllite db, or any text file and then upload to your server whenever, the user is in range to upload without users intervention. – CRUSADER May 27 '13 at 13:48
  • I didn't quite understand the link of how i'm going to be able to collect the errors from all over my application and condense them into a single place. – Garima Tiwari May 28 '13 at 12:19
  • @CRUSADER, your link helped, if you could post the same as an answer I'll be happy to accept it! – Garima Tiwari May 30 '13 at 06:39
  • if you like the answer in provided in link then go ahead and up-vote in answer provided in link, It doesn't make sense providing same answer on different threads... Well, happy to know it benefited you.. – CRUSADER Jun 01 '13 at 04:47
  • @CRUSADER, but I need to close the question! – Garima Tiwari Jun 03 '13 at 09:03
  • Alright, posted in answer section.... – CRUSADER Jun 04 '13 at 07:10

2 Answers2

0

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.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • The link is helful in being able to point out the errors and show it, but unfortunately for my case my users will have no clue about it whatsoever. I simply want to be able to record the errors and put them in a text file that can be saved in the SD card or send an error report to some id. – Garima Tiwari May 27 '13 at 08:09
  • @GarimaTiwari, Check this link for how to log your errors to a textfile. http://stackoverflow.com/questions/6757179/how-to-write-exception-in-log-file-using-android – Shankar Narayana Damodaran May 27 '13 at 08:31
  • Updating my question to show you what I've already tried out. – Garima Tiwari May 27 '13 at 08:36
  • @GarimaTiwari, You need to put the code in the try block inside the catch statement. What i mean is , you have the variable `error=e.getMessage();` right ? Log that `error` variable in the text file. – Shankar Narayana Damodaran May 27 '13 at 10:15
  • I want a solution that will be able to collect the errors from ANYWHERE in the application and collect them in a text file or something. – Garima Tiwari May 28 '13 at 12:21
  • @GarimaTiwari, have a look here at http://www.bugsense.com/ . I hope this will help you out and its free ! – Shankar Narayana Damodaran May 29 '13 at 05:16
0

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...

CRUSADER
  • 5,486
  • 3
  • 28
  • 64