Merge pull request #32 from informatik-ag-ngl/f/logging

Added proper logging
This commit is contained in:
Kai S. K. Engelbart 2019-11-29 21:06:56 +01:00 committed by GitHub
commit 93f5c79a32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 324 additions and 298 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;
@ -37,6 +38,8 @@ public class LocalDB {
private ObjectFactory objectFactory = new ObjectFactory();
private DatatypeFactory datatypeFactory;
private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName());
private Sync unreadMessagesSync = objectFactory.createSync();
private Sync sync = objectFactory.createSync();
private Sync readMessages = objectFactory.createSync();
@ -44,10 +47,9 @@ public class LocalDB {
/**
* Constructs an empty local database.
*
* @param client the user that is logged in with this client
* @param sender the user that is logged in with this client
* @since Envoy v0.1-alpha
*/
public LocalDB(User sender) {
this.sender = sender;
try {
@ -84,13 +86,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");
}
}
@ -114,7 +116,9 @@ public class LocalDB {
* Creates a {@link Message} object serializable to XML.
*
* @param textContent The content (text) of the message
* @param recipient The recipient of the message
* @return prepared {@link Message} object
* @since Envoy v0.1-alpha
*/
public Message createMessage(String textContent, User recipient) {
Message.Metadata metaData = objectFactory.createMessageMetadata();
@ -140,7 +144,7 @@ public class LocalDB {
sync.getMessages().addAll(readMessages.getMessages());
readMessages.getMessages().clear();
System.out.println(sync.getMessages().size());
logger.info(String.format("Filled sync with %d messages.", sync.getMessages().size()));
return sync;
}
@ -185,21 +189,28 @@ 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.");
if (getChats().get(j)
.getRecipient()
.getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
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: " + getChats().get(j).getModel().get(k).getMetadata().getMessageId() + "was selected.");
getChats().get(j).getModel().get(k).getMetadata().setState(returnSync.getMessages().get(i).getMetadata().getState());
System.out
.println("Message State is now: " + getChats().get(j).getModel().get(k).getMetadata().getState().toString());
logger.info("Message with ID: "
+ getChats().get(j).getModel().get(k).getMetadata().getMessageId()
+ "was selected.");
getChats().get(j)
.getModel()
.get(k)
.getMetadata()
.setState(returnSync.getMessages().get(i).getMetadata().getState());
logger.info("Message State is now: "
+ getChats().get(j).getModel().get(k).getMetadata().getState().toString());
}
}
}
@ -213,7 +224,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());
}
}
}
@ -266,7 +277,8 @@ public class LocalDB {
for (Chat chat : getChats())
for (int i = 0; i < chat.getModel().size(); i++)
if (chat.getModel().get(i).getMetadata().getState() == MessageState.WAITING) {
System.out.println("Got Waiting Message");
// addMessageToSync(localDB.getChats().get(i).getModel().get(j));
logger.info("Got Waiting Message");
sync.getMessages().add(chat.getModel().get(i));
}
}

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;
@ -57,6 +58,8 @@ public class ChatWindow extends JFrame {
private JTextArea messageEnterTextArea;
private static final Logger logger = Logger.getLogger(ChatWindow.class.getSimpleName());
public ChatWindow(Client client, LocalDB localDB) {
this.client = client;
this.localDB = localDB;
@ -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();
// Load the configuration from client.properties first
@ -42,6 +48,7 @@ public class Startup {
config.load(args);
if (!config.isInitialized()) {
logger.warning("Server or port are not defined. Exiting...");
JOptionPane.showMessageDialog(null, "Error loading configuration values.", "Configuration error",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
@ -49,7 +56,7 @@ public class Startup {
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);