Merge pull request #37 from informatik-ag-ngl/f/logger

Improved logging and code readability
This commit is contained in:
delvh 2019-12-07 11:31:18 +01:00 committed by GitHub
commit 381bc6b334
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 774 additions and 787 deletions

View File

@ -133,7 +133,9 @@ public class Client {
* Updating UserStatus of all users in LocalDB. (Server sends all users with * Updating UserStatus of all users in LocalDB. (Server sends all users with
* their updated UserStatus to the client.) <br> * their updated UserStatus to the client.) <br>
* *
* @param userId * @param userId the id of the {@link Client} who sends the {@link Sync}
* @param sync the {@link Sync} to send
* @return a sync
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public Sync sendSync(long userId, Sync sync) { public Sync sendSync(long userId, Sync sync) {
@ -169,6 +171,7 @@ public class Client {
/** /**
* Sets the recipient. * Sets the recipient.
*
* @param recipient - the recipient to set * @param recipient - the recipient to set
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
@ -180,4 +183,3 @@ public class Client {
*/ */
public boolean hasRecipient() { return recipient != null; } public boolean hasRecipient() { return recipient != null; }
} }

View File

@ -114,7 +114,7 @@ public class Config {
* Changes the default local database. * Changes the default local database.
* Exclusively intended for development purposes. * Exclusively intended for development purposes.
* *
* @param the file containing the local database * @param localDB the file containing the local database
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
**/ **/
public void setLocalDB(File localDB) { this.localDB = localDB; } public void setLocalDB(File localDB) { this.localDB = localDB; }

View File

@ -82,19 +82,13 @@ public class LocalDB {
* @throws IOException if something went wrong during saving * @throws IOException if something went wrong during saving
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public void saveToLocalDB() { public void saveToLocalDB() throws IOException {
try {
localDB.getParentFile().mkdirs(); localDB.getParentFile().mkdirs();
localDB.createNewFile(); localDB.createNewFile();
} catch (IOException e) {
e.printStackTrace();
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(); throw ex;
logger.warning("unable to save the messages");
} }
} }
@ -146,6 +140,7 @@ public class LocalDB {
* *
* @param userId the ID of the user that is synchronized by this client * @param userId the ID of the user that is synchronized by this client
* @return {@link Sync} object filled with the current changes * @return {@link Sync} object filled with the current changes
* @since Envoy v0.1-alpha
*/ */
public Sync fillSync(long userId) { public Sync fillSync(long userId) {
addWaitingMessagesToSync(); addWaitingMessagesToSync();
@ -153,7 +148,7 @@ public class LocalDB {
sync.getMessages().addAll(readMessages.getMessages()); sync.getMessages().addAll(readMessages.getMessages());
readMessages.getMessages().clear(); readMessages.getMessages().clear();
logger.info(String.format("Filled sync with %d messages.", sync.getMessages().size())); logger.finest(String.format("Filled sync with %d messages.", sync.getMessages().size()));
return sync; return sync;
} }
@ -161,6 +156,7 @@ public class LocalDB {
* Applies the changes carried by a {@link Sync} object to the local database * Applies the changes carried by a {@link Sync} object to the local database
* *
* @param returnSync the {@link Sync} object to apply * @param returnSync the {@link Sync} object to apply
* @since Envoy v0.1-alpha
*/ */
public void applySync(Sync returnSync) { public void applySync(Sync returnSync) {
for (int i = 0; i < returnSync.getMessages().size(); i++) { for (int i = 0; i < returnSync.getMessages().size(); i++) {
@ -233,7 +229,6 @@ public class LocalDB {
* Adds the unread messages returned from the server in the latest sync to the * Adds the unread messages returned from the server in the latest sync to the
* right chats in the LocalDB. * right chats in the LocalDB.
* *
* @param localDB
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public void addUnreadMessagesToLocalDB() { public void addUnreadMessagesToLocalDB() {
@ -251,7 +246,7 @@ public class LocalDB {
* <br> * <br>
* Adds these messages to the {@code readMessages} {@link Sync} object. * Adds these messages to the {@code readMessages} {@link Sync} object.
* *
* @param currentChat * @param currentChat the {@link Chat} that was just opened
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public void setMessagesToRead(Chat currentChat) { public void setMessagesToRead(Chat currentChat) {

View File

@ -10,6 +10,8 @@ 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.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.swing.DefaultListModel; import javax.swing.DefaultListModel;
@ -73,7 +75,14 @@ public class ChatWindow extends JFrame {
addWindowListener(new WindowAdapter() { addWindowListener(new WindowAdapter() {
@Override @Override
public void windowClosing(WindowEvent e) { localDB.saveToLocalDB(); } public void windowClosing(WindowEvent evt) {
try {
localDB.saveToLocalDB();
} catch (IOException e1) {
e1.printStackTrace();
logger.log(Level.WARNING, "Unable to save the messages", e1);
}
}
}); });
contentPane.setBackground(new Color(0, 0, 0)); contentPane.setBackground(new Color(0, 0, 0));
@ -126,11 +135,9 @@ public class ChatWindow extends JFrame {
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER if (e.getKeyCode() == KeyEvent.VK_ENTER
&& ((SettingsScreen.enterToSend && e.getModifiersEx() == 0) || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) { && ((SettingsScreen.enterToSend && e.getModifiersEx() == 0) || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK)))
postMessage(messageList); postMessage(messageList);
} }
}
}); });
// Checks for changed Message // Checks for changed Message
messageEnterTextArea.setWrapStyleWord(true); messageEnterTextArea.setWrapStyleWord(true);
@ -187,7 +194,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();
logger.warning("An error occured while opening the settings screen: " + e); logger.log(Level.WARNING, "An error occured while opening the settings screen", e);
e.printStackTrace(); e.printStackTrace();
} }
}); });

View File

@ -26,8 +26,7 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer<Mess
private static final long serialVersionUID = 5164417379767181198L; private static final long serialVersionUID = 5164417379767181198L;
@Override @Override
public Component getListCellRendererComponent(JList<? extends Message> list, Message value, int index, public Component getListCellRendererComponent(JList<? extends Message> list, Message value, int index, boolean isSelected, boolean cellHasFocus) {
boolean isSelected, boolean cellHasFocus) {
if (isSelected) { if (isSelected) {
setBackground(list.getSelectionBackground()); setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground()); setForeground(list.getSelectionForeground());
@ -41,14 +40,10 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer<Mess
final String text = value.getContent().get(0).getText(); final String text = value.getContent().get(0).getText();
final String state = value.getMetadata().getState().toString(); final String state = value.getMetadata().getState().toString();
final String date = value.getMetadata().getDate() == null ? "" final String date = value.getMetadata().getDate() == null ? ""
: new SimpleDateFormat("dd.MM.yyyy HH:mm ") : new SimpleDateFormat("dd.MM.yyyy HH:mm ").format(value.getMetadata().getDate().toGregorianCalendar().getTime());
.format(value.getMetadata().getDate().toGregorianCalendar().getTime());
setText(String.format( setText(String
"<html><p style=\"color:#d2d235\"><b><small>%s</b></small><br><p style=\"color:white\">%s :%s</html>", .format("<html><p style=\"color:#d2d235\"><b><small>%s</b></small><br><p style=\"color:white\">%s :%s</html>", date, text, state));
date,
text,
state));
return this; return this;
} }
} }

View File

@ -41,7 +41,6 @@ public class SettingsScreen extends JDialog {
* It personalises the screen more. * It personalises the screen more.
* *
* @param username The name of the User * @param username The name of the User
* @param Email The Email that is associated with that Account
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public static void open(String username) {// , String Email) {AUSKLAMMERN, WENN ANMELDUNG PER public static void open(String username) {// , String Email) {AUSKLAMMERN, WENN ANMELDUNG PER
@ -101,7 +100,6 @@ public class SettingsScreen extends JDialog {
* It personalises the screen more. * It personalises the screen more.
* *
* @param Username The name of the User * @param Username The name of the User
* @param Email The Email that is associated with that Account
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public SettingsScreen(String Username) {// , String Email, String hashedPwd) {AUSKLAMMERN, WENN ANMELDUNG PER EMAIL public SettingsScreen(String Username) {// , String Email, String hashedPwd) {AUSKLAMMERN, WENN ANMELDUNG PER EMAIL
@ -145,7 +143,7 @@ public class SettingsScreen extends JDialog {
public static boolean isEnterToSend() { return enterToSend; } public static boolean isEnterToSend() { return enterToSend; }
/** /**
* @param enterToSend <br> * @param enterForSend <br>
* toggles whether a message should be sent via * toggles whether a message should be sent via
* <br> * <br>
* buttonpress "enter" or "ctrl"+"enter" * buttonpress "enter" or "ctrl"+"enter"

View File

@ -26,7 +26,7 @@ import envoy.exception.EnvoyException;
*/ */
public class Startup { public class Startup {
private static final Logger logger = Logger.getLogger(Client.class.getSimpleName()); private static final Logger logger = Logger.getLogger(Startup.class.getSimpleName());
public static void main(String[] args) { public static void main(String[] args) {
logger.setLevel(Level.ALL); logger.setLevel(Level.ALL);
@ -44,19 +44,17 @@ public class Startup {
} }
// Override configuration values with command line arguments // Override configuration values with command line arguments
if (args.length > 0) if (args.length > 0) config.load(args);
config.load(args);
if (!config.isInitialized()) { if (!config.isInitialized()) {
logger.warning("Server or port are not defined. Exiting..."); logger.severe("Server or port are not defined. Exiting...");
JOptionPane.showMessageDialog(null, "Error loading configuration values.", "Configuration error", JOptionPane.showMessageDialog(null, "Error loading configuration values.", "Configuration error", JOptionPane.ERROR_MESSAGE);
JOptionPane.ERROR_MESSAGE);
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()) {
logger.warning("User name is not set or empty. Exiting..."); logger.severe("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);
@ -67,7 +65,8 @@ public class Startup {
e.printStackTrace(); e.printStackTrace();
JOptionPane.showMessageDialog(null, JOptionPane.showMessageDialog(null,
"Error while loading local database: " + e.toString() + "\nChats will not be stored locally.", "Error while loading local database: " + e.toString() + "\nChats will not be stored locally.",
"Local DB error", JOptionPane.WARNING_MESSAGE); "Local DB error",
JOptionPane.WARNING_MESSAGE);
} }
EventQueue.invokeLater(() -> { EventQueue.invokeLater(() -> {

View File

@ -24,9 +24,9 @@ public class UserListRenderer extends JLabel implements ListCellRenderer<User> {
private static final long serialVersionUID = 5164417379767181198L; private static final long serialVersionUID = 5164417379767181198L;
@SuppressWarnings("incomplete-switch")
@Override @Override
public Component getListCellRendererComponent(JList<? extends User> list, User value, int index, boolean isSelected, public Component getListCellRendererComponent(JList<? extends User> list, User value, int index, boolean isSelected, boolean cellHasFocus) {
boolean cellHasFocus) {
if (isSelected) { if (isSelected) {
setBackground(list.getSelectionBackground()); setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground()); setForeground(list.getSelectionForeground());
@ -38,28 +38,19 @@ public class UserListRenderer extends JLabel implements ListCellRenderer<User> {
// Enable background rendering // Enable background rendering
setOpaque(true); setOpaque(true);
final String name = value.getName(); final String name = value.getName();
final UserStatus status = value.getStatus(); final UserStatus status = value.getStatus();
switch (status) { switch (status) {
case ONLINE: case ONLINE:
setText(String.format( setText(String
"<html><p style=\"color:#03fc20\"><b><small>%s</b></small><br><p style=\"color:white\">%s</html>", .format("<html><p style=\"color:#03fc20\"><b><small>%s</b></small><br><p style=\"color:white\">%s</html>", status, name));
status,
name));
break; break;
case OFFLINE: case OFFLINE:
setText(String.format( setText(String
"<html><p style=\"color:#fc0303\"><b><small>%s</b></small><br><p style=\"color:white\">%s</html>", .format("<html><p style=\"color:#fc0303\"><b><small>%s</b></small><br><p style=\"color:white\">%s</html>", status, name));
status,
name));
break; break;
} }
return this; return this;
} }
} }