Skip to content

Commit

Permalink
Refactored logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian-Wollersberger committed Jun 13, 2018
1 parent 7a84705 commit e3a71ed
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 20 deletions.
4 changes: 3 additions & 1 deletion AI/src/at/dropical/wolliAI/AiMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ public class AiMain {
public static void main(String[] args) throws InterruptedException {
new AiMain(new ServerAdapter()).loop();
}
/** Called from Server. */
/** Called from Server.
* May be wise to execute in other thread. */
public static void newAIconnection(String gameID) throws InterruptedException {
new AiMain(new ServerAdapter(gameID)).loop();
}


private AI ai;

/** The AI type is fixed. */
public AiMain(ServerAdapter adapter) {
ai = new BestPossibilityAI(adapter);
}
Expand Down
8 changes: 4 additions & 4 deletions server/src/at/dropical/server/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void endlesslyAcceptConnections() {
}
// The ServerSocket is died. No hope :'(
} catch(IOException e) {
Server.log(Level.SEVERE, "IOException, ServerSocket probably faulty.");
Server.logger().log(Level.SEVERE, "IOException, ServerSocket probably faulty.");
}
}

Expand All @@ -64,9 +64,9 @@ private void waitForJoinRequestAndJoin(Socket connection) {
joinGame(request.getGameID(), request.getPlayerName(), request.wantsToPlayAgainsAI(), trans);

} catch(ClassCastException e) {
Server.log(Level.WARNING, "ClassCastException -- class received is not a subclass of Request");
Server.logger().log(Level.WARNING, "ClassCastException -- class received is not a subclass of Request");
} catch(IOException | RuntimeException e) {
Server.log(Level.WARNING, e.getLocalizedMessage());
Server.logger().log(Level.WARNING, e.getLocalizedMessage());
}
}

Expand Down Expand Up @@ -109,7 +109,7 @@ private Game autoJoinOrCreateGame(String playerName, Transmitter trans) {
}

game[0].addPlayerAndStart(playerName, trans);
Server.log(Level.INFO, "Game "+ name +" added");
Server.logger().log(Level.INFO, "Game "+ name +" added");
return game[0];
}

Expand Down
12 changes: 6 additions & 6 deletions server/src/at/dropical/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void main(String[] args) {
serverInstance.manager.endlesslyAcceptConnections();

} catch(IOException e) {
log(Level.SEVERE, e.toString());
logger().log(Level.SEVERE, e.toString());
} finally {
instance().executor.shutdownNow();
}
Expand All @@ -60,10 +60,10 @@ public static Server instance() {
return serverInstance;
}

/** Log in a file and sout.
* fixme This is always used as the stack location in the message. Not useful. */
public static void log(Level level, String msg) {
instance().logger.log(level, msg);
/** I tried to have a static Method Server.log() before,
* but then the stack trace always just shows that method. Not helpful. */
public static Logger logger() {
return instance().logger;
}

/** Run some code concurrenty in the global Thread pool. */
Expand All @@ -82,7 +82,7 @@ static void startLocalAI(String gameID) {
try {
AiMain.newAIconnection(gameID);
} catch(InterruptedException e) {
log(Level.INFO, "AI was interrupted.");
logger().log(Level.INFO, "AI was interrupted.");
}
});

Expand Down
4 changes: 2 additions & 2 deletions server/src/at/dropical/server/WebInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ private void loadTemplate() {
stringBuilder.append("\r\n");
}
} catch (FileNotFoundException e) {
Server.log(Level.WARNING, "Template.html not found");
Server.logger().log(Level.WARNING, "Template.html not found");
} catch (IOException e) {
Server.log(Level.WARNING, "Template.html -- io error");
Server.logger().log(Level.WARNING, "Template.html -- io error");
}
}

Expand Down
8 changes: 4 additions & 4 deletions server/src/at/dropical/server/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void run() {
Thread.sleep(10);
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
Server.log(Level.INFO, "Game was terminated");
Server.logger().log(Level.INFO, "Game was terminated");
}
}
updateClients();
Expand Down Expand Up @@ -83,7 +83,7 @@ private void gameLoop() {
}
} catch (GameOverException e) {
setCurrentGameState(new GameOverState(this,e.getLooserName()));
Server.log(Level.INFO,"Player "+e.getLooserName()+" lost his game.");
Server.logger().log(Level.INFO, "Player "+e.getLooserName()+" lost his game.");
}
}

Expand All @@ -99,7 +99,7 @@ public void addPlayerAndStart(String playerName, Transmitter transmitter) {
playerLock.lock();

if (games.size() < necessaryPlayers) {
Server.log(Level.INFO,"Player "+playerName+" added");
Server.logger().log(Level.INFO, "Player "+playerName+" added");

players.add(new ServerToClientAdapter(transmitter));
games.put(playerName,new OnePlayer(playerName));
Expand All @@ -121,7 +121,7 @@ public void addViewer(ServerToClientAdapter transmitter) {
}

public void setCurrentGameState(at.dropical.server.gamestates.State currentGameState) {
Server.log(Level.INFO,"State changed to "+currentGameState.getClass());
Server.logger().log(Level.INFO, "State changed to "+currentGameState.getClass());
this.currentGameState = currentGameState;

updateClientsNextTime = true;
Expand Down
5 changes: 3 additions & 2 deletions server/src/at/dropical/server/gamefield/TetrisArena.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ public boolean checkTetromino(Tetromino tetromino, int h, int w, boolean overTop
public void placeTetromino(Tetromino tetromino, int h, int w) throws GameOverException {
// Can it be placed here?
if(!checkTetromino(tetromino, h, w, false)) {
Server.log(Level.WARNING, "The Tetronimo is in an invalid Position. This should never happen." +
"Bad luck for Player "+ player); // See addLines()
// See addLines()
Server.logger().log(Level.WARNING, "The Tetronimo is in an invalid Position. This should never happen." +
"Bad luck for Player "+ player);
throw new GameOverException(player);
}

Expand Down
2 changes: 2 additions & 0 deletions server/src/at/dropical/server/logging/LoggerSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ static public void setup() throws IOException {
formatterHTML = new HtmlLogFormatter();
fileHTML.setFormatter(formatterHTML);
logger.addHandler(fileHTML);

System.out.println("Logger setup finished.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void writeRequest(SendableItem r) {
outputStream.writeObject(r);
} catch (IOException e) {

Server.log(Level.SEVERE,"Couldn't send SendableItem " + r.getClass());
Server.logger().log(Level.SEVERE, "Couldn't send SendableItem " + r.getClass());
}
}

Expand Down

0 comments on commit e3a71ed

Please sign in to comment.