JavaAI how to find error number

JavaAI how to find error number

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

Post Reply
Bla
Posts: 79
Joined: 25 Feb 2013, 14:44

JavaAI how to find error number

Post 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?
meriton
Posts: 18
Joined: 11 Jul 2009, 14:03

Re: JavaAI how to find error number

Post 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.
Bla
Posts: 79
Joined: 25 Feb 2013, 14:44

Re: JavaAI how to find error number

Post 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!
meriton
Posts: 18
Joined: 11 Jul 2009, 14:03

Re: JavaAI how to find error number

Post 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.
Bla
Posts: 79
Joined: 25 Feb 2013, 14:44

Re: JavaAI how to find error number

Post 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 :(
Bla
Posts: 79
Joined: 25 Feb 2013, 14:44

Re: JavaAI how to find error number

Post by Bla »

why does sendTextMessage(ex.getMessage()) always cause spring to crash?
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: JavaAI how to find error number

Post by abma »

Bla wrote:why does sendTextMessage(ex.getMessage()) always cause spring to crash?
can you provide more details? infolog.txt could be useful...
Bla
Posts: 79
Joined: 25 Feb 2013, 14:44

Re: JavaAI how to find error number

Post 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.
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: JavaAI how to find error number

Post by abma »

try set LogFlush = 1 (should be default)...
Post Reply

Return to “AI”