From c93f9fe2306c966d53f8526e22d98fc257968fb0 Mon Sep 17 00:00:00 2001 From: derharry333 Date: Wed, 27 Nov 2019 17:07:25 +0100 Subject: [PATCH] Replaced print statements with logger statements. --- src/main/java/envoy/client/Client.java | 10 ++++++--- src/main/java/envoy/client/LocalDB.java | 21 +++++++++++-------- src/main/java/envoy/client/ui/ChatWindow.java | 5 ++++- src/main/java/envoy/client/ui/Startup.java | 10 +++++++-- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index 522d44f..5b9a2b8 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -1,5 +1,7 @@ package envoy.client; +import java.util.logging.Logger; + import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; @@ -28,16 +30,18 @@ public class Client { private Config config; private User sender, recipient; + private static final Logger logger = Logger.getLogger(Client.class.getSimpleName()); + public Client(Config config, String username) { this.config = config; sender = getUser(username); - System.out.println("ID: " + sender.getID()); + + logger.info("ID: " + sender.getID()); } private R post(String uri, T body, Class responseBodyClass) { javax.ws.rs.client.Client client = ClientBuilder.newClient(); WebTarget target = client.target(uri); - Response response = target.request().post(Entity.entity(body, "application/xml")); R responseBody = response.readEntity(responseBodyClass); response.close(); @@ -92,7 +96,7 @@ public class Client { if (returnSenderSync.getUsers().size() == 1) { returnSender = returnSenderSync.getUsers().get(0); } else { - System.out.println("ERROR exiting..."); + logger.warning("ERROR exiting..."); } return returnSender; diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 142b687..8af3094 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -9,6 +9,7 @@ import java.io.ObjectOutputStream; import java.time.Instant; import java.util.ArrayList; import java.util.List; +import java.util.logging.Logger; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; @@ -36,6 +37,8 @@ public class LocalDB { private ObjectFactory objectFactory = new ObjectFactory(); private DatatypeFactory datatypeFactory; + private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName()); + /** * Constructs an empty local database. * @@ -80,13 +83,13 @@ public class LocalDB { localDB.createNewFile(); } catch (IOException e) { e.printStackTrace(); - System.err.println("unable to save the messages"); + logger.warning("unable to save the messages"); } try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) { out.writeObject(chats); } catch (IOException ex) { ex.printStackTrace(); - System.err.println("unable to save the messages"); + logger.warning("unable to save the messages"); } } @@ -144,7 +147,7 @@ public class LocalDB { } readMessages.getMessages().clear(); - System.out.println(sync.getMessages().size()); + logger.info(String.format("Filled sync with %d messages.", sync.getMessages().size())); return sync; } @@ -207,20 +210,20 @@ public class LocalDB { if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) { // Update local Messages to state READ - System.out.println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId() + logger.info("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId() + "was initialized to be set to READ in localDB."); for (int j = 0; j < getChats().size(); j++) { if (getChats().get(j) .getRecipient() .getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) { - System.out.println("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected."); + logger.info("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected."); for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) { if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync .getMessages() .get(i) .getMetadata() .getMessageId()) { - System.out.println("Message with ID: " + logger.info("Message with ID: " + getChats().get(j).getModel().get(k).getMetadata().getMessageId() + "was selected."); getChats().get(j) @@ -228,7 +231,7 @@ public class LocalDB { .get(k) .getMetadata() .setState(returnSync.getMessages().get(i).getMetadata().getState()); - System.out.println("Message State is now: " + logger.info("Message State is now: " + getChats().get(j).getModel().get(k).getMetadata().getState().toString()); } } @@ -244,7 +247,7 @@ public class LocalDB { if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) { getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus()); - System.out.println(getChats().get(k).getRecipient().getStatus().toString()); + logger.info(getChats().get(k).getRecipient().getStatus().toString()); } } } @@ -342,7 +345,7 @@ public class LocalDB { for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) { if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.WAITING) { // addMessageToSync(localDB.getChats().get(i).getModel().get(j)); - System.out.println("Got Waiting Message"); + logger.info("Got Waiting Message"); sync.getMessages().add(0, getChats().get(i).getModel().get(j)); } } diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 2a7ae11..c6fce27 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -10,6 +10,7 @@ import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.util.logging.Logger; import javax.swing.DefaultListModel; import javax.swing.JButton; @@ -56,6 +57,8 @@ public class ChatWindow extends JFrame { private Chat currentChat; private JTextArea messageEnterTextArea; + + private static final Logger logger = Logger.getLogger(ChatWindow.class.getSimpleName()); public ChatWindow(Client client, LocalDB localDB) { this.client = client; @@ -186,7 +189,7 @@ public class ChatWindow extends JFrame { SettingsScreen.open(localDB.getUser().getName()); } catch (Exception e) { SettingsScreen.open(); - System.err.println("An error occured while opening the settings screen: " + e); + logger.warning("An error occured while opening the settings screen: " + e); e.printStackTrace(); } }); diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index c3a3baa..0fdd76f 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -3,6 +3,8 @@ package envoy.client.ui; import java.awt.EventQueue; import java.io.IOException; import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.swing.JOptionPane; @@ -24,7 +26,11 @@ import envoy.exception.EnvoyException; */ public class Startup { + private static final Logger logger = Logger.getLogger(Client.class.getSimpleName()); + public static void main(String[] args) { + logger.setLevel(Level.ALL); + Config config = Config.getInstance(); if (args.length > 0) { config.load(args); @@ -40,13 +46,13 @@ public class Startup { } if (!config.isInitialized()) { - System.err.println("Server or port are not defined. Exiting..."); + logger.warning("Server or port are not defined. Exiting..."); System.exit(1); } String userName = JOptionPane.showInputDialog("Please enter your username"); if (userName == null || userName.isEmpty()) { - System.err.println("User name is not set or empty. Exiting..."); + logger.warning("User name is not set or empty. Exiting..."); System.exit(1); } Client client = new Client(config, userName);