From bf60e9675d6c95a6e9db55b0d7027e6f1dd8707a Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 11 Jun 2020 12:06:06 +0200 Subject: [PATCH] Replaces print statements by logger calls Closes #34 --- src/main/java/envoy/server/Startup.java | 2 +- .../server/net/ObjectMessageProcessor.java | 8 ++++++-- .../envoy/server/net/ObjectWriteProxy.java | 6 +++++- .../processors/ContactOperationProcessor.java | 7 +++++-- .../processors/GroupMessageProcessor.java | 10 +++++++--- .../IDGeneratorRequestProcessor.java | 7 +++++-- .../processors/LoginCredentialProcessor.java | 18 +++++++++++------- .../server/processors/MessageProcessor.java | 11 +++++++---- .../processors/UserStatusChangeProcessor.java | 14 +++++++++----- 9 files changed, 56 insertions(+), 27 deletions(-) diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java index c14423f..e5d4fe7 100755 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -40,7 +40,7 @@ public class Startup { items.put("homeDirectory", new ConfigItem<>("homeDirectory", "h", File::new, new File(System.getProperty("user.home"), ".envoy-server"), true)); items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.SEVERE, true)); - items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.INFO, true)); + items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.FINE, true)); final var config = new Config(); config.load(items); diff --git a/src/main/java/envoy/server/net/ObjectMessageProcessor.java b/src/main/java/envoy/server/net/ObjectMessageProcessor.java index a35f955..8fb120d 100755 --- a/src/main/java/envoy/server/net/ObjectMessageProcessor.java +++ b/src/main/java/envoy/server/net/ObjectMessageProcessor.java @@ -4,12 +4,14 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.util.Set; +import java.util.logging.Logger; import com.jenkov.nioserver.IMessageProcessor; import com.jenkov.nioserver.Message; import com.jenkov.nioserver.WriteProxy; import envoy.server.processors.ObjectProcessor; +import envoy.util.EnvoyLog; /** * Handles incoming objects.
@@ -25,6 +27,8 @@ public class ObjectMessageProcessor implements IMessageProcessor { private final Set> processors; + private static final Logger logger = EnvoyLog.getLogger(ObjectMessageProcessor.class); + /** * The constructor to set the {@link ObjectProcessor}s. * @@ -39,11 +43,11 @@ public class ObjectMessageProcessor implements IMessageProcessor { try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) { Object obj = in.readObject(); if (obj == null) { - System.out.println("received a null object"); + logger.warning("Received a null object"); return; } - System.out.println("Read object: " + obj); + logger.fine("Received " + obj); // Process object processors.stream().filter(p -> p.getInputClass().isInstance(obj)).forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> { diff --git a/src/main/java/envoy/server/net/ObjectWriteProxy.java b/src/main/java/envoy/server/net/ObjectWriteProxy.java index bc37885..eea2908 100755 --- a/src/main/java/envoy/server/net/ObjectWriteProxy.java +++ b/src/main/java/envoy/server/net/ObjectWriteProxy.java @@ -1,10 +1,12 @@ package envoy.server.net; import java.io.IOException; +import java.util.logging.Logger; import com.jenkov.nioserver.Message; import com.jenkov.nioserver.WriteProxy; +import envoy.util.EnvoyLog; import envoy.util.SerializationUtils; /** @@ -21,6 +23,8 @@ public class ObjectWriteProxy { private final WriteProxy writeProxy; + private static final Logger logger = EnvoyLog.getLogger(ObjectWriteProxy.class); + /** * Creates an instance of {@link ObjectWriteProxy}. * @@ -40,7 +44,7 @@ public class ObjectWriteProxy { final Message response = writeProxy.getMessage(); response.socketId = recipientSocketID; - System.out.println("Sending object " + obj); + logger.fine("Sending " + obj); // Serialize object to byte array final byte[] objBytes = SerializationUtils.writeToByteArray(obj); diff --git a/src/main/java/envoy/server/processors/ContactOperationProcessor.java b/src/main/java/envoy/server/processors/ContactOperationProcessor.java index c218059..c47e516 100755 --- a/src/main/java/envoy/server/processors/ContactOperationProcessor.java +++ b/src/main/java/envoy/server/processors/ContactOperationProcessor.java @@ -1,12 +1,14 @@ package envoy.server.processors; import java.io.IOException; +import java.util.logging.Logger; import envoy.event.ElementOperation; import envoy.event.contact.ContactOperationEvent; import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; +import envoy.util.EnvoyLog; /** * Project: envoy-server-standalone
@@ -19,6 +21,7 @@ import envoy.server.net.ObjectWriteProxy; public class ContactOperationProcessor implements ObjectProcessor { private static final ConnectionManager connectionManager = ConnectionManager.getInstance(); + private static final Logger logger = EnvoyLog.getLogger(ContactOperationProcessor.class); @Override public void process(ContactOperationEvent evt, long socketId, ObjectWriteProxy writeProxy) throws IOException { @@ -27,7 +30,7 @@ public class ContactOperationProcessor implements ObjectProcessorenvoy-server-standalone
@@ -22,6 +24,8 @@ import envoy.server.net.ObjectWriteProxy; */ public class GroupMessageProcessor implements ObjectProcessor { + private static final Logger logger = EnvoyLog.getLogger(GroupCreationProcessor.class); + @Override public void process(GroupMessage groupMessage, long socketID, ObjectWriteProxy writeProxy) { groupMessage.nextStatus(); @@ -29,7 +33,7 @@ public class GroupMessageProcessor implements ObjectProcessor { try { writeProxy.write(socketID, new MessageStatusChangeEvent(groupMessage)); } catch (IOException e) { - System.err.println("Could not send messageStatusChangeEvent to the sender of this groupMessage with ID: " + groupMessage.getID()); + logger.warning("Could not send messageStatusChangeEvent to the sender of this groupMessage with ID: " + groupMessage.getID()); e.printStackTrace(); } @@ -48,7 +52,7 @@ public class GroupMessageProcessor implements ObjectProcessor { try { PersistenceManager.getInstance().addMessage(new envoy.server.data.GroupMessage(groupMessage, new Date())); } catch (EntityExistsException e) { - System.err.println("Received a groupMessage with an ID that already exists"); + logger.warning("Received a groupMessage with an ID that already exists"); } } @@ -57,7 +61,7 @@ public class GroupMessageProcessor implements ObjectProcessor { // If recipient is online, send the groupMessage directly writeProxy.write(connectionManager.getSocketId(memberID), groupMessage); } catch (IOException e) { - System.err.println("Recipient online. Failed to send message" + groupMessage.getID()); + logger.warning("Recipient online. Failed to send message" + groupMessage.getID()); e.printStackTrace(); } } diff --git a/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java b/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java index 4a00618..e30f18d 100755 --- a/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java +++ b/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java @@ -1,12 +1,14 @@ package envoy.server.processors; import java.io.IOException; +import java.util.logging.Logger; import envoy.data.IDGenerator; import envoy.event.IDGeneratorRequest; import envoy.server.data.ConfigItem; import envoy.server.data.PersistenceManager; import envoy.server.net.ObjectWriteProxy; +import envoy.util.EnvoyLog; /** * Project: envoy-server-standalone
@@ -20,15 +22,16 @@ import envoy.server.net.ObjectWriteProxy; public class IDGeneratorRequestProcessor implements ObjectProcessor { private static final long ID_RANGE = 200; + private static final Logger logger = EnvoyLog.getLogger(IDGeneratorRequestProcessor.class); @Override public Class getInputClass() { return IDGeneratorRequest.class; } @Override public void process(IDGeneratorRequest input, long socketID, ObjectWriteProxy writeProxy) throws IOException { - System.out.println("Received id generation request."); + logger.fine("Received id generation request."); var generator = createIDGenerator(); - System.out.println("Sending new id generator " + generator); + logger.info("Sending new id generator " + generator); writeProxy.write(socketID, generator); } diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 2835043..7c12ec2 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -2,6 +2,7 @@ package envoy.server.processors; import java.io.IOException; import java.util.*; +import java.util.logging.Logger; import javax.persistence.NoResultException; @@ -15,6 +16,7 @@ import envoy.server.data.Message; import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; +import envoy.util.EnvoyLog; /** * This {@link ObjectProcessor} handles {@link LoginCredentials}.
@@ -32,16 +34,18 @@ public class LoginCredentialProcessor implements ObjectProcessor pendingMessages = PersistenceManager.getInstance().getPendingMessages(user); for (Message msg : pendingMessages) if (msg.getStatus() == MessageStatus.SENT) { - System.out.println("Sending message " + msg.toCommon()); + logger.info("Sending message " + msg.toCommon()); writeProxy.write(socketID, msg.toCommon()); msg.setReceivedDate(new Date()); msg.setStatus(MessageStatus.RECEIVED); if (connectionManager.isOnline(msg.getSender().getID())) { var evt = new MessageStatusChangeEvent(msg.toCommon()); - System.out.println("Sending messageStatusChangeEvent to sender " + evt); + logger.info("Sending messageStatusChangeEvent to sender " + evt); writeProxy.write(connectionManager.getSocketId(msg.getSender().getID()), evt); } PersistenceManager.getInstance().updateMessage(msg); } else { var evt = new MessageStatusChangeEvent(msg.toCommon()); - System.out.println("Sending messageStatusChangeEvent " + evt); + logger.info("Sending messageStatusChangeEvent " + evt); writeProxy.write(socketID, evt); } } diff --git a/src/main/java/envoy/server/processors/MessageProcessor.java b/src/main/java/envoy/server/processors/MessageProcessor.java index 75757ab..a326d8c 100755 --- a/src/main/java/envoy/server/processors/MessageProcessor.java +++ b/src/main/java/envoy/server/processors/MessageProcessor.java @@ -2,6 +2,7 @@ package envoy.server.processors; import java.io.IOException; import java.util.Date; +import java.util.logging.Logger; import javax.persistence.EntityExistsException; @@ -11,6 +12,7 @@ import envoy.event.MessageStatusChangeEvent; import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; +import envoy.util.EnvoyLog; /** * This {@link ObjectProcessor} handles incoming {@link Message}s.
@@ -25,6 +27,8 @@ import envoy.server.net.ObjectWriteProxy; */ public class MessageProcessor implements ObjectProcessor { + private static final Logger logger = EnvoyLog.getLogger(MessageProcessor.class); + @Override public void process(Message message, long socketID, ObjectWriteProxy writeProxy) { message.nextStatus(); @@ -36,14 +40,14 @@ public class MessageProcessor implements ObjectProcessor { try { writeProxy.write(socketID, new MessageStatusChangeEvent(message)); } catch (IOException e) { - System.err.println("Could not send messageStatusChangeEvent to the sender of this message with ID: " + message.getID()); + logger.warning("Could not send messageStatusChangeEvent to the sender of this message with ID: " + message.getID()); e.printStackTrace(); } } try { PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message)); } catch (EntityExistsException e) { - System.err.println("Received a message with an id that already exists"); + logger.warning("Received a message with an id that already exists"); } } @@ -54,9 +58,8 @@ public class MessageProcessor implements ObjectProcessor { // Update the message status to RECEIVED message.setReceivedDate(new Date()); message.nextStatus(); - } catch (IOException e) { - System.err.println("Recipient online. Failed to send message" + message.getID()); + logger.warning("Recipient online. Failed to send message" + message.getID()); e.printStackTrace(); } } diff --git a/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java b/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java index ad63480..6cc6980 100755 --- a/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java @@ -1,6 +1,7 @@ package envoy.server.processors; import java.io.IOException; +import java.util.logging.Logger; import envoy.data.User.UserStatus; import envoy.event.UserStatusChangeEvent; @@ -8,6 +9,7 @@ import envoy.server.data.PersistenceManager; import envoy.server.data.User; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; +import envoy.util.EnvoyLog; /** * This processor handles incoming {@link UserStatusChangeEvent}.
@@ -24,6 +26,8 @@ public class UserStatusChangeProcessor implements ObjectProcessor getInputClass() { return UserStatusChangeEvent.class; } @@ -31,11 +35,10 @@ public class UserStatusChangeProcessor implements ObjectProcessor