Replaced print statements with logger statements.

This commit is contained in:
derharry333 2019-11-27 17:07:25 +01:00
parent 0b5dbe59dc
commit c93f9fe230
4 changed files with 31 additions and 15 deletions

View File

@ -1,5 +1,7 @@
package envoy.client; package envoy.client;
import java.util.logging.Logger;
import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity; import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget; import javax.ws.rs.client.WebTarget;
@ -28,16 +30,18 @@ public class Client {
private Config config; private Config config;
private User sender, recipient; private User sender, recipient;
private static final Logger logger = Logger.getLogger(Client.class.getSimpleName());
public Client(Config config, String username) { public Client(Config config, String username) {
this.config = config; this.config = config;
sender = getUser(username); sender = getUser(username);
System.out.println("ID: " + sender.getID());
logger.info("ID: " + sender.getID());
} }
private <T, R> R post(String uri, T body, Class<R> responseBodyClass) { private <T, R> R post(String uri, T body, Class<R> responseBodyClass) {
javax.ws.rs.client.Client client = ClientBuilder.newClient(); javax.ws.rs.client.Client client = ClientBuilder.newClient();
WebTarget target = client.target(uri); WebTarget target = client.target(uri);
Response response = target.request().post(Entity.entity(body, "application/xml")); Response response = target.request().post(Entity.entity(body, "application/xml"));
R responseBody = response.readEntity(responseBodyClass); R responseBody = response.readEntity(responseBodyClass);
response.close(); response.close();
@ -92,7 +96,7 @@ public class Client {
if (returnSenderSync.getUsers().size() == 1) { if (returnSenderSync.getUsers().size() == 1) {
returnSender = returnSenderSync.getUsers().get(0); returnSender = returnSenderSync.getUsers().get(0);
} else { } else {
System.out.println("ERROR exiting..."); logger.warning("ERROR exiting...");
} }
return returnSender; return returnSender;

View File

@ -9,6 +9,7 @@ import java.io.ObjectOutputStream;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Logger;
import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.DatatypeFactory;
@ -36,6 +37,8 @@ public class LocalDB {
private ObjectFactory objectFactory = new ObjectFactory(); private ObjectFactory objectFactory = new ObjectFactory();
private DatatypeFactory datatypeFactory; private DatatypeFactory datatypeFactory;
private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName());
/** /**
* Constructs an empty local database. * Constructs an empty local database.
* *
@ -80,13 +83,13 @@ public class LocalDB {
localDB.createNewFile(); localDB.createNewFile();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); 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))) { try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) {
out.writeObject(chats); out.writeObject(chats);
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); 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(); readMessages.getMessages().clear();
System.out.println(sync.getMessages().size()); logger.info(String.format("Filled sync with %d messages.", sync.getMessages().size()));
return sync; return sync;
} }
@ -207,20 +210,20 @@ public class LocalDB {
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) { && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) {
// Update local Messages to state 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."); + "was initialized to be set to READ in localDB.");
for (int j = 0; j < getChats().size(); j++) { for (int j = 0; j < getChats().size(); j++) {
if (getChats().get(j) if (getChats().get(j)
.getRecipient() .getRecipient()
.getID() == returnSync.getMessages().get(i).getMetadata().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++) { for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync
.getMessages() .getMessages()
.get(i) .get(i)
.getMetadata() .getMetadata()
.getMessageId()) { .getMessageId()) {
System.out.println("Message with ID: " logger.info("Message with ID: "
+ getChats().get(j).getModel().get(k).getMetadata().getMessageId() + getChats().get(j).getModel().get(k).getMetadata().getMessageId()
+ "was selected."); + "was selected.");
getChats().get(j) getChats().get(j)
@ -228,7 +231,7 @@ public class LocalDB {
.get(k) .get(k)
.getMetadata() .getMetadata()
.setState(returnSync.getMessages().get(i).getMetadata().getState()); .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()); + 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()) { if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) {
getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus()); 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++) { for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) {
if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.WAITING) { if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.WAITING) {
// addMessageToSync(localDB.getChats().get(i).getModel().get(j)); // 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)); sync.getMessages().add(0, getChats().get(i).getModel().get(j));
} }
} }

View File

@ -10,6 +10,7 @@ import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.util.logging.Logger;
import javax.swing.DefaultListModel; import javax.swing.DefaultListModel;
import javax.swing.JButton; import javax.swing.JButton;
@ -56,6 +57,8 @@ public class ChatWindow extends JFrame {
private Chat currentChat; private Chat currentChat;
private JTextArea messageEnterTextArea; private JTextArea messageEnterTextArea;
private static final Logger logger = Logger.getLogger(ChatWindow.class.getSimpleName());
public ChatWindow(Client client, LocalDB localDB) { public ChatWindow(Client client, LocalDB localDB) {
this.client = client; this.client = client;
@ -186,7 +189,7 @@ public class ChatWindow extends JFrame {
SettingsScreen.open(localDB.getUser().getName()); SettingsScreen.open(localDB.getUser().getName());
} catch (Exception e) { } catch (Exception e) {
SettingsScreen.open(); 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(); e.printStackTrace();
} }
}); });

View File

@ -3,6 +3,8 @@ package envoy.client.ui;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.io.IOException; import java.io.IOException;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@ -24,7 +26,11 @@ import envoy.exception.EnvoyException;
*/ */
public class Startup { public class Startup {
private static final Logger logger = Logger.getLogger(Client.class.getSimpleName());
public static void main(String[] args) { public static void main(String[] args) {
logger.setLevel(Level.ALL);
Config config = Config.getInstance(); Config config = Config.getInstance();
if (args.length > 0) { if (args.length > 0) {
config.load(args); config.load(args);
@ -40,13 +46,13 @@ public class Startup {
} }
if (!config.isInitialized()) { 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); System.exit(1);
} }
String userName = JOptionPane.showInputDialog("Please enter your username"); String userName = JOptionPane.showInputDialog("Please enter your username");
if (userName == null || userName.isEmpty()) { 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); System.exit(1);
} }
Client client = new Client(config, userName); Client client = new Client(config, userName);