diff --git a/chat/src/main/java/rtgre/ChatLauncher.java b/chat/src/main/java/rtgre/ChatLauncher.java index 052ba74..c7dd978 100644 --- a/chat/src/main/java/rtgre/ChatLauncher.java +++ b/chat/src/main/java/rtgre/ChatLauncher.java @@ -3,6 +3,10 @@ package rtgre; import rtgre.chat.ChatApplication; import rtgre.server.ChatServer; +import java.util.logging.Level; + +import static rtgre.chat.ChatApplication.LOGGER; + /** * Application pour lancer soit ChatServer, soit ChatApplication @@ -14,19 +18,19 @@ public class ChatLauncher { */ public static void main(String[] args) { for (String arg : args) { - System.out.println(arg); + LOGGER.log(Level.FINEST, arg); } try { if ("server".equals(args[0])) { - System.out.println("test1"); + LOGGER.log(Level.INFO, "Lancement du serveur..."); ChatServer.main(args); } else { + LOGGER.log(Level.INFO, "Lancement du client..."); ChatApplication.main(args); } } catch (Exception e) { - System.out.println(e.getMessage()); - e.printStackTrace(); - System.out.println("test2"); + LOGGER.log(Level.FINEST, e.getMessage(), e); + LOGGER.log(Level.INFO, "Lancement du client..."); ChatApplication.main(args); } diff --git a/chat/src/main/java/rtgre/chat/ChatController.java b/chat/src/main/java/rtgre/chat/ChatController.java index efff122..7a14321 100644 --- a/chat/src/main/java/rtgre/chat/ChatController.java +++ b/chat/src/main/java/rtgre/chat/ChatController.java @@ -34,6 +34,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.*; +import java.util.logging.Level; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -126,7 +127,7 @@ public class ChatController implements Initializable { try { InputStream in = ChatController.class.getResourceAsStream("config.properties"); - System.out.println(ChatController.class.getResource("config.properties").getPath()); + LOGGER.finest("Chemin du fichier config.properties: " + ChatController.class.getResource("config.properties").getPath()); properties.load(in); if (contact != null) { this.contact.setAvatar(Contact.base64ToImage(properties.getProperty("avatar"))); @@ -145,14 +146,9 @@ public class ChatController implements Initializable { exchangeSplitPane.setDividerPositions(Double.parseDouble(properties.getProperty("split2"))); } - } catch (IOException e) { + } catch (IOException | NullPointerException e) { LOGGER.warning("Impossible de charger le fichier de configuration! Configuration par défaut chargée"); - System.out.println(e.getMessage()); - e.printStackTrace(); - } catch (NullPointerException e) { - LOGGER.warning("Impossible de charger le fichier de configuration! Configuration par défaut chargée"); - System.out.println(e.getMessage()); - e.printStackTrace(); + LOGGER.log(Level.SEVERE, e.getMessage(), e); } Thread dateTimeLoop = new Thread(this::dateTimeLoop); @@ -291,7 +287,8 @@ public class ChatController implements Initializable { } } catch (IOException e) { LOGGER.warning("Impossible d'ouvrir la fenêtre de dialogue: fxml introuvable \n" + e.getMessage()); - e.printStackTrace(); + LOGGER.log(Level.FINEST, e.getMessage(), e); + } } @@ -659,9 +656,9 @@ public class ChatController implements Initializable { */ private void handlePostEvent(JSONObject content) { - System.out.println("Selected: " + roomsListView.getSelectionModel().getSelectedItem()); - System.out.println("From: " + content.getString("from")); - System.out.println("To: " + content.getString("to")); + LOGGER.info("Selected: " + roomsListView.getSelectionModel().getSelectedItem()); + LOGGER.info("From: " + content.getString("from")); + LOGGER.info("To: " + content.getString("to")); try { if (!content.getString("to").contains("#")) { @@ -743,17 +740,17 @@ public class ChatController implements Initializable { if (!content.getString("avatar").isEmpty()) { avatar = Contact.base64ToImage(content.getString("avatar")); } - System.out.println(avatar); if (contact != null) { LOGGER.info(contactMap.toString()); contactMap.getContact(content.getString("login")).setConnected(content.getBoolean("connected")); if (avatar != null) { + LOGGER.log(Level.FINEST, avatar.toString()); contactMap.getContact(content.getString("login")).setAvatar(avatar); } contactsListView.refresh(); LOGGER.info(contactMap.toString()); } else { - System.out.println(content); + LOGGER.log(Level.FINEST, content.toString()); LOGGER.info(contactMap.toString()); Contact user = Contact.fromJSON( content, diff --git a/chat/src/main/java/rtgre/chat/net/ChatClient.java b/chat/src/main/java/rtgre/chat/net/ChatClient.java index 5f8cd5a..6f7b05b 100644 --- a/chat/src/main/java/rtgre/chat/net/ChatClient.java +++ b/chat/src/main/java/rtgre/chat/net/ChatClient.java @@ -9,6 +9,7 @@ import rtgre.modeles.Message; import rtgre.modeles.Post; import java.io.IOException; +import java.util.logging.Level; import java.util.logging.Logger; import static rtgre.chat.ChatApplication.LOGGER; @@ -48,13 +49,13 @@ public class ChatClient extends ClientTCP { if (message == null) { // fin du flux stdIn message = END_MESSAGE; } - System.out.println(BLUE + "Envoi: " + message + RST); + LOGGER.log(Level.FINE, BLUE + "Envoi: " + message + RST); this.send(message); if (END_MESSAGE.equals(message)) { connected = false; } } catch (IOException e) { - LOGGER.severe(e.toString()); + LOGGER.log(Level.SEVERE, e.getMessage(), e); connected = false; } } @@ -110,10 +111,10 @@ public class ChatClient extends ClientTCP { try { while (connected) { String message = this.receive(); - LOGGER.info(RED + "Réception: " + message + RST); + LOGGER.finest(RED + "Réception: " + message + RST); LOGGER.info(RED + message + RST); if (listener != null) { - System.out.println(message); + LOGGER.log(Level.FINE, message); Platform.runLater(() -> listener.handleEvent(Event.fromJson(message))); } } diff --git a/chat/src/main/java/rtgre/chat/net/ClientTCP.java b/chat/src/main/java/rtgre/chat/net/ClientTCP.java index bbe9818..5de0a16 100644 --- a/chat/src/main/java/rtgre/chat/net/ClientTCP.java +++ b/chat/src/main/java/rtgre/chat/net/ClientTCP.java @@ -4,6 +4,8 @@ package rtgre.chat.net; import java.io.*; import java.net.Socket; import java.nio.charset.StandardCharsets; +import java.util.logging.Level; + import static rtgre.chat.ChatApplication.LOGGER; /** @@ -70,7 +72,7 @@ public class ClientTCP { * @throws IOException si la connexion échoue ou si les flux ne sont pas récupérables */ public ClientTCP(String host, int port) throws IOException { - System.out.printf("Connexion à [%s:%d]%n", host, port); + LOGGER.info("Connexion à [%s:%d]".formatted(host, port)); sock = new Socket(host, port); ipPort = "%s:%d".formatted(sock.getLocalAddress().getHostAddress(), sock.getLocalPort()); LOGGER.info("[%s] Connexion établie vers [%s:%d]".formatted(ipPort, host, port)); @@ -149,24 +151,24 @@ public class ClientTCP { * Boucle d'envoi de messages */ public void sendLoop() { - System.out.println(BLUE + "Boucle d'envoi de messages..." + RST); + LOGGER.log(Level.FINE, BLUE + "Boucle d'envoi de messages..." + RST); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); connected = true; try { while (connected) { - System.out.println(BLUE + "Votre message (\"fin\" pour terminer) : " + RST); + LOGGER.log(Level.FINE, BLUE + "Votre message (\"fin\" pour terminer) : " + RST); String message = stdIn.readLine(); if (message == null) { // fin du flux stdIn message = END_MESSAGE; } - System.out.println(BLUE + "Envoi: " + message + RST); + LOGGER.log(Level.FINE, BLUE + "Envoi: " + message + RST); this.send(message); if (END_MESSAGE.equals(message)) { connected = false; } } } catch (IOException e) { - LOGGER.severe(e.toString()); + LOGGER.log(Level.WARNING, e.getMessage(), e); connected = false; } } @@ -175,12 +177,12 @@ public class ClientTCP { * Boucle de réception de messages */ public void receiveLoop() { - System.out.println(RED + "Boucle de réception de messages..." + RST); + LOGGER.log(Level.FINE, RED + "Boucle de réception de messages..." + RST); connected = true; try { while (connected) { String message = this.receive(); - System.out.println(RED + "Réception: " + message + RST); + LOGGER.log(Level.FINE, RED + "Réception: " + message + RST); } } catch (IOException e) { LOGGER.severe("[%s] %s".formatted(ipPort, e)); diff --git a/chat/src/main/java/rtgre/modeles/Contact.java b/chat/src/main/java/rtgre/modeles/Contact.java index f9293c3..93fa6e6 100644 --- a/chat/src/main/java/rtgre/modeles/Contact.java +++ b/chat/src/main/java/rtgre/modeles/Contact.java @@ -11,6 +11,7 @@ import java.io.File; import java.io.IOException; import java.util.Base64; import java.util.Objects; +import java.util.logging.Level; import static rtgre.chat.ChatApplication.LOGGER; @@ -201,7 +202,7 @@ public class Contact { try { this.avatar = avatarFromLogin(file, this.login); } catch (IOException e) { - System.out.println("Erreur : " + e.getMessage()); + LOGGER.log(Level.WARNING, e.getMessage(), e); } } @@ -230,7 +231,6 @@ public class Contact { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ImageIO.write(img, "png", bos); - System.out.println(Base64.getEncoder().encodeToString(bos.toByteArray())); return Base64.getEncoder().encodeToString(bos.toByteArray()); } catch (IOException e) { LOGGER.severe("Impossible de convertir l'image en base64"); diff --git a/chat/src/main/java/rtgre/modeles/DatabaseApi.java b/chat/src/main/java/rtgre/modeles/DatabaseApi.java index f1002c8..60b8e2c 100644 --- a/chat/src/main/java/rtgre/modeles/DatabaseApi.java +++ b/chat/src/main/java/rtgre/modeles/DatabaseApi.java @@ -2,6 +2,8 @@ package rtgre.modeles; import java.sql.*; import java.util.UUID; +import java.util.logging.Level; + import org.sqlite.JDBC; import static rtgre.chat.ChatApplication.LOGGER; @@ -25,8 +27,8 @@ public class DatabaseApi { initDB(con); LOGGER.info("Database connected!"); } catch (SQLException e) { - LOGGER.severe("Can't connect to database! \n" + e.getMessage()); - e.printStackTrace(); + LOGGER.severe("Can't connect to database! \n"); + LOGGER.log(Level.FINE, e.getMessage(), e); } } @@ -47,8 +49,7 @@ public class DatabaseApi { stmt.execute(sql); } catch (SQLException e) { LOGGER.severe("Cannot initialize database!"); - System.out.println(e.getMessage()); - e.printStackTrace(); + LOGGER.log(Level.FINE, e.getMessage(), e); } } @@ -63,6 +64,7 @@ public class DatabaseApi { return stmt.executeQuery(query); } catch (SQLException e) { LOGGER.severe("Can't get post by id!"); + LOGGER.log(Level.FINE, e.getMessage(), e); return null; } } @@ -78,6 +80,7 @@ public class DatabaseApi { return stmt.executeQuery(query); } catch (SQLException e) { LOGGER.severe("Can't get post since " + timestamp + "!"); + LOGGER.log(Level.FINE, e.getMessage(), e); return null; } } @@ -133,8 +136,7 @@ public class DatabaseApi { return true; } catch (SQLException e) { LOGGER.severe("Cannot remove post!"); - System.out.println(e.getMessage()); - e.printStackTrace();; + LOGGER.log(Level.FINE, e.getMessage(), e); return false; } } @@ -146,6 +148,7 @@ public class DatabaseApi { try { con.close(); } catch (SQLException e) { + LOGGER.log(Level.FINE, e.getMessage(), e); LOGGER.severe("Can't close database connection! Is database connected ?"); } } diff --git a/chat/src/main/java/rtgre/server/ChatServer.java b/chat/src/main/java/rtgre/server/ChatServer.java index 1b6dbd8..0623856 100644 --- a/chat/src/main/java/rtgre/server/ChatServer.java +++ b/chat/src/main/java/rtgre/server/ChatServer.java @@ -443,11 +443,11 @@ public class ChatServer { private void doListPost(JSONObject content) throws JSONException, IllegalStateException { if (contactMap.getContact(user.getLogin()).isConnected()) { if (!contactMap.containsKey(content.getString("select")) && !roomMap.containsKey(content.getString("select"))) { - System.out.println("!select"); + LOGGER.log(Level.FINEST, "!select"); throw new IllegalStateException(); } if (!content.getString("select").contains("#")) { - System.out.println("!#"); + LOGGER.log(Level.FINEST, "!#"); for (Post post : postVector.getPostsSince(content.getLong("since"))) { if (post.getTo().equals(content.getString("select")) || post.getFrom().equals(content.getString("select"))) { @@ -455,7 +455,7 @@ public class ChatServer { } } } else if (user.getCurrentRoom().equals(content.getString("select"))) { - System.out.println("#"); + LOGGER.log(Level.FINEST, "#"); for (Post post: postVector.getPostsSince(content.getLong("since"))) { if (post.getTo().equals(content.getString("select"))) { sendEventToContact(contactMap.getContact(user.getLogin()), new Event(Event.POST, post.toJsonObject())); @@ -551,7 +551,7 @@ public class ChatServer { LOGGER.info("Connexion de " + login); contactMap.getContact(login).setConnected(true); this.user = contactMap.getContact(login); - System.out.println(user.isConnected()); + LOGGER.log(Level.FINEST, String.valueOf(user.isConnected())); sendAllOtherClients( findClient(contactMap.getContact(login)), new Event("CONT", user.toJsonObject()).toJson()