Useful stack trace in Mojo

Monkey Targets Forums/Android/Useful stack trace in Mojo

Samah(Posted 2011) [#1]
1) Open MonkeyPro\modules\mojo\native\mojo.android.java
2) Find this line in the Die method:
if( t.getMessage().length()==0 ) System.exit( 0 );

3) Add this after it:
android.util.Log.e("[Monkey]", "Error:", t);


It should look like something this:
void Die( Throwable t ){
	dead=true;
	audio.OnDestroy();
	
	if( t.getMessage().length()==0 ) System.exit( 0 );
	android.util.Log.e("[Monkey]", "Error:", t);
	
	AlertDialog.Builder db=new AlertDialog.Builder( MonkeyGame.activity );
	db.setMessage( t.getMessage()+"\n"+bb_std_lang.stackTrace() );
	AlertDialog dlg=db.show();
}


As long as you're viewing your device in DDMS (Eclipse, batch file, whatever), you'll see a proper stack trace when Monkey dies.

Example:
07-07 10:43:47.750: ERROR/Monkey(4702): java.lang.NumberFormatException: unable to parse '' as integer
07-07 10:43:47.750: ERROR/Monkey(4702):     at java.lang.Integer.parseInt(Integer.java:362)
07-07 10:43:47.750: ERROR/Monkey(4702):     at java.lang.Integer.parseInt(Integer.java:332)
...
07-07 10:43:47.750: ERROR/Monkey(4702):     at com.monkey.bb_app_AppDevice.OnCreate(MonkeyGame.java:1832)
07-07 10:43:47.750: ERROR/Monkey(4702):     at com.monkey.gxtkApp.InvokeOnRender(MonkeyGame.java:532)
07-07 10:43:47.750: ERROR/Monkey(4702):     at com.monkey.gxtkApp.onDrawFrame(MonkeyGame.java:462)
07-07 10:43:47.750: ERROR/Monkey(4702):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1363)
07-07 10:43:47.750: ERROR/Monkey(4702):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)


Edit: moved the line so that Error("") won't print a stack trace.


DGuy(Posted 2011) [#2]
Thanks, will give this a try ... :)


therevills(Posted 2011) [#3]
This would have saved me hours of head banging the desk!!!


AdamRedwoods(Posted 2011) [#4]
This is excellent info. Why can't info like this be more prominent?

So to use DDMS, just keep your device connected and run it from the command line. Add these lines and you'll at least get SOME feedback on why your app dies on android.

Especially useful when only android dies, but the other targets (GLFW, Flash) it works fine on.

One question: How do I relay information from my app to this debugger from monkey, so I can find out WHERE it's dying?


anawiki(Posted 2011) [#5]
Not sure if this is what you're asking for, but can't you do:

adb logcat >mylog.txt

and then read the file when the app dies?


AdamRedwoods(Posted 2011) [#6]
That works as well, thanks.
I also discovered that the PRINT command does come through the DDMS, you have to look for it.


Samah(Posted 2011) [#7]
Not sure if this is what you're asking for, but can't you do:

adb logcat >mylog.txt

and then read the file when the app dies?

The problem is that in certain situations, Android is unable to display a popup message showing the stack trace (due to a blocked UI thread, or similar). This means that when trying to bring up mojo's default stack trace window, it throws ANOTHER exception, which hides the first one. This change will print the original stack trace before trying to display that window so that you won't lose it.


Aman(Posted 2011) [#8]
I was just reading about how to use logcat flags to show only what I need to know. This is useful specially when you run logcat while your testing the game to see exactly what error you get when it crashes.


Samah(Posted 2011) [#9]
I always debug through Eclipse anyway, so I can choose the level of logging there. It also means I can profile stuff to find out what's killing framerate.