Page 1 of 1

JavaAI how to find error number

Posted: 20 May 2014, 04:38
by Bla
so if I have

try{
//random stuff
}
catch(Exception ex){
CallbackHelper.say("Error in (insert meathod name here)");
CallbackHelper.say(ex.toString());
}

where CallbackHelper is a static class with the callback and say is a method that does getGame().sendTextMessage(string, 0), is there a way to get a more detailed error report than, say, java.lang.nullPointerException?

Like I want to at least see a line number. Is there a way?

Re: JavaAI how to find error number

Posted: 20 May 2014, 13:29
by meriton
Of course there is :-)

Here's how I do it:

Code: Select all

public class Logger {

	private static final PrintWriter writer;
	
	static {
		try {
			String filename = "merai-" + MerAI.callback.getSkirmishAI().getTeamId() + ".log";
			String path = MerAI.callback.getDataDirs().allocatePath(filename, true, true, false, false);
			writer = new PrintWriter(new FileOutputStream(path));
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static void log(String msg) {
		writer.println(msg);
	}
	
	public static void log(Throwable t) {
		t.printStackTrace(writer);
	}
	
	public static void shutdown() {
		writer.close();
	}
}
and then

Code: Select all

	@Override
	public int unitIdle(Unit unit) {
		try {
                        // code to handle the event goes here
			return 0;
		} catch (Throwable t) {
			Logger.log(t);
			return -1;
		}
	}
will write something like
java.lang.RuntimeException: no site found!
at merai.Builder$BuildAction.perform(Builder.java:59)
at merai.MerAI.unitIdle(MerAI.java:40)
at merai.MerAI.unitFinished(MerAI.java:51)
at com.springrts.ai.oo.OOAI.unitFinished(OOAI.java:116)
to the log file. This is known as a stack trace. See http://stackoverflow.com/questions/1268 ... tack-trace on how to interpret them.

Re: JavaAI how to find error number

Posted: 21 May 2014, 00:10
by Bla
Ahh.. I was kind of hoping there was an easier way to find just the line number and print it onto the Spring log as I wouldn't have to worry about a separate log file, but thanks!

Re: JavaAI how to find error number

Posted: 22 May 2014, 22:40
by meriton
You can of course write it to the spring log file instead:

Code: Select all

    } catch (Throwable t) {
        StringWriter sw = new StringWriter();
        t.printStackTrace(sw);
        writeToConsole(sw.toString);
   }
The reason I write to a separate file is that I also want to emit other diagnostic information, making the log quite large. Writing to the spring log is both somewhat slower, and spams the ingame console, distracting the player.

Re: JavaAI how to find error number

Posted: 23 May 2014, 02:50
by Bla
meriton wrote:You can of course write it to the spring log file instead:

Code: Select all

    } catch (Throwable t) {
        StringWriter sw = new StringWriter();
        t.printStackTrace(sw);
        writeToConsole(sw.toString);
   }
The reason I write to a separate file is that I also want to emit other diagnostic information, making the log quite large. Writing to the spring log is both somewhat slower, and spams the ingame console, distracting the player.
I get an error saying that stringwriter can't be converted to printwriter :(

Re: JavaAI how to find error number

Posted: 23 May 2014, 03:49
by Bla
why does sendTextMessage(ex.getMessage()) always cause spring to crash?

Re: JavaAI how to find error number

Posted: 26 May 2014, 00:22
by abma
Bla wrote:why does sendTextMessage(ex.getMessage()) always cause spring to crash?
can you provide more details? infolog.txt could be useful...

Re: JavaAI how to find error number

Posted: 16 Jun 2014, 21:20
by Bla
abma wrote:
Bla wrote:why does sendTextMessage(ex.getMessage()) always cause spring to crash?
can you provide more details? infolog.txt could be useful...
Hm kind of late reply, as I have not ran into many bugs, but in the infolog, it just cuts out. It just ends there.

Re: JavaAI how to find error number

Posted: 17 Jun 2014, 12:54
by abma
try set LogFlush = 1 (should be default)...