Replaced print statements with logger statements.

This commit is contained in:
derharry333 2019-11-27 17:07:25 +01:00
parent 329339f05c
commit 30d380857c
4 changed files with 31 additions and 15 deletions

View File

@ -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 <T, R> R post(String uri, T body, Class<R> 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;

View File

@ -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));
}
}

View File

@ -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();
}
});

View File

@ -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);