I thought I would share a piece of code I used recently in debugging an application on a specific device. It did help solve the problem, but it wasn't pretty. I'm sure it could be more robust, but it worked for me. Just a note, I'm big on arrays and not using vectors for anything when I code j2me. The device I was debugging on was already close to memory limits so this code uses arrays. The screen was very small as well and 5 lines was all I could fit.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| Font msgFont; String errorLines[];
public yourConstructor() { msgFont = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_SMALL); errorLines = new String[5]; }
public void addLine(String st) { int i; for (i=1;i<errorLines.length;i++) { errorLines[i-1] = errorLines[i]; } errorLines[errorLines.length-1] = st;
System.out.println("added line = "+st); this.repaint(); }
public void paint(Graphics g) {
System.out.println("ErrorLines.len="+errorLines.length); int top = getHeight()-(errorLines.length*msgFont.getHeight()); int i; for (i=0;i<errorLines.length;i++) { if (errorLines[i]!=null) { g.drawString(errorLines[i],2,top,Graphics.LEFT | Graphics.TOP); } top+=msgFont.getHeight(); }
}
public void codeToTest() { addLine("This is a test"); } |
I spent some time originally trying to catch System.out so I could just see the messages I was already puting out in the emulator, but security restrictions caused that idea to fail.
Hope this helps someone out there.
Wood