From 2643999e8d265641e2300a8197c6bdc77e33477a Mon Sep 17 00:00:00 2001 From: Carlos <28845529+mesacarlos@users.noreply.github.com> Date: Fri, 29 May 2020 16:37:35 +0200 Subject: [PATCH] Improved time prefix --- client/index.html | 4 ++++ client/scripts/WebConsole.js | 15 +++++++++++---- client/scripts/WebConsolePersistenceManager.js | 18 ------------------ client/scripts/object/Setting.js | 6 ++++++ client/scripts/object/WSServer.js | 10 ++++++++++ .../webconsole/util/DateTimeUtils.java | 4 ++++ .../webconsole/websocket/WSServer.java | 3 ++- .../websocket/command/ReadLogFileCommand.java | 2 +- .../websocket/response/ConsoleOutput.java | 9 ++++++++- 9 files changed, 46 insertions(+), 25 deletions(-) create mode 100644 client/scripts/object/Setting.js create mode 100644 client/scripts/object/WSServer.js diff --git a/client/index.html b/client/index.html index 94a07a1..3656175 100644 --- a/client/index.html +++ b/client/index.html @@ -257,6 +257,10 @@ + + + + diff --git a/client/scripts/WebConsole.js b/client/scripts/WebConsole.js index 38401dd..7afb03e 100644 --- a/client/scripts/WebConsole.js +++ b/client/scripts/WebConsole.js @@ -52,7 +52,7 @@ function onWebSocketsMessage(message){ switch (message.status) { case 10: //Console Output - writeToWebConsole(message.message); + writeToWebConsole(message.message, message.time); break; case 200: //Processed @@ -106,7 +106,7 @@ function onWebSocketsMessage(message){ /** * Write to console */ -function writeToWebConsole(msg){ +function writeToWebConsole(msg, time){ var isScrolledDown = document.getElementById("consoleTextArea").scrollHeight - document.getElementById("consoleTextArea").scrollTop - 40 == $("#consoleTextArea").height(); //Write to div, replacing < to < (to avoid XSS) and replacing new line to br. @@ -159,8 +159,15 @@ function writeToWebConsole(msg){ msg = msg.replace(/§r/g, ""); //&r //Append datetime if enabled - if(persistenceManager.getSetting("dateTimePrefix")) - msg = "[" + new Date().toLocaleTimeString() + "] " + msg; + if(persistenceManager.getSetting("dateTimePrefix")){ + if(typeof time !== 'undefined' && time !== null) //if time is present and not null + msg = "[" + time + "] " + msg; + else if(typeof time !== 'undefined' && time === null) //if time is present and null + ; //no time (is already printed) + else + msg = "[" + new Date().toLocaleTimeString() + "] " + msg; + } + $("#consoleTextArea").append(msg + "
"); diff --git a/client/scripts/WebConsolePersistenceManager.js b/client/scripts/WebConsolePersistenceManager.js index c1abf50..2277f8e 100644 --- a/client/scripts/WebConsolePersistenceManager.js +++ b/client/scripts/WebConsolePersistenceManager.js @@ -173,22 +173,4 @@ class WebConsolePersistenceManager{ return JSON.parse(window.localStorage.WebConsole).settings[name]; } -} - -class WSServer{ - constructor(serverName, serverURI){ - this.serverName = serverName; - this.serverURI = serverURI; - } - - setPassword(pwd){ - this.serverPassword = pwd; - } -} - -class Setting{ - constructor(name, defaultValue){ - this.name = name; - this.defaultValue = defaultValue; - } } \ No newline at end of file diff --git a/client/scripts/object/Setting.js b/client/scripts/object/Setting.js new file mode 100644 index 0000000..7107dee --- /dev/null +++ b/client/scripts/object/Setting.js @@ -0,0 +1,6 @@ +class Setting{ + constructor(name, defaultValue){ + this.name = name; + this.defaultValue = defaultValue; + } +} \ No newline at end of file diff --git a/client/scripts/object/WSServer.js b/client/scripts/object/WSServer.js new file mode 100644 index 0000000..a42c13d --- /dev/null +++ b/client/scripts/object/WSServer.js @@ -0,0 +1,10 @@ +class WSServer{ + constructor(serverName, serverURI){ + this.serverName = serverName; + this.serverURI = serverURI; + } + + setPassword(pwd){ + this.serverPassword = pwd; + } +} \ No newline at end of file diff --git a/src/es/mesacarlos/webconsole/util/DateTimeUtils.java b/src/es/mesacarlos/webconsole/util/DateTimeUtils.java index 53f2262..867f985 100644 --- a/src/es/mesacarlos/webconsole/util/DateTimeUtils.java +++ b/src/es/mesacarlos/webconsole/util/DateTimeUtils.java @@ -7,4 +7,8 @@ public class DateTimeUtils { public static String getDateAsString() { return new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date()); } + + public static String getTimeAsString() { + return new SimpleDateFormat("HH:mm:ss").format(new Date()); + } } \ No newline at end of file diff --git a/src/es/mesacarlos/webconsole/websocket/WSServer.java b/src/es/mesacarlos/webconsole/websocket/WSServer.java index 2129fe5..a48e160 100644 --- a/src/es/mesacarlos/webconsole/websocket/WSServer.java +++ b/src/es/mesacarlos/webconsole/websocket/WSServer.java @@ -11,6 +11,7 @@ import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.server.WebSocketServer; import es.mesacarlos.webconsole.WebConsole; +import es.mesacarlos.webconsole.util.DateTimeUtils; import es.mesacarlos.webconsole.util.Internationalization; import es.mesacarlos.webconsole.util.LoginManager; import es.mesacarlos.webconsole.websocket.command.WSCommandFactory; @@ -97,7 +98,7 @@ public class WSServer extends WebSocketServer { Collection connections = getConnections(); for (WebSocket connection : connections) { if (LoginManager.getInstance().isLoggedIn(connection.getRemoteSocketAddress())) - sendToClient(connection, new ConsoleOutput(line)); + sendToClient(connection, new ConsoleOutput(line, DateTimeUtils.getTimeAsString())); } } diff --git a/src/es/mesacarlos/webconsole/websocket/command/ReadLogFileCommand.java b/src/es/mesacarlos/webconsole/websocket/command/ReadLogFileCommand.java index a2271f1..8eed2a0 100644 --- a/src/es/mesacarlos/webconsole/websocket/command/ReadLogFileCommand.java +++ b/src/es/mesacarlos/webconsole/websocket/command/ReadLogFileCommand.java @@ -35,7 +35,7 @@ public class ReadLogFileCommand implements WSCommand{ } for(String line : lines) - wsServer.sendToClient(conn, new ConsoleOutput(line)); + wsServer.sendToClient(conn, new ConsoleOutput(line, null)); } } \ No newline at end of file diff --git a/src/es/mesacarlos/webconsole/websocket/response/ConsoleOutput.java b/src/es/mesacarlos/webconsole/websocket/response/ConsoleOutput.java index b5cdf73..2f371af 100644 --- a/src/es/mesacarlos/webconsole/websocket/response/ConsoleOutput.java +++ b/src/es/mesacarlos/webconsole/websocket/response/ConsoleOutput.java @@ -4,9 +4,11 @@ import com.google.gson.JsonObject; public class ConsoleOutput implements JSONOutput{ private String message; + private String time; - public ConsoleOutput(String message) { + public ConsoleOutput(String message, String time) { this.message = message; + this.time = time; } @Override @@ -18,12 +20,17 @@ public class ConsoleOutput implements JSONOutput{ public String getMessage() { return message; } + + public String getTime() { + return time; + } @Override public String toJSON() { JsonObject object = new JsonObject(); object.addProperty("status", getStatusCode()); object.addProperty("statusDescription", "Console Output"); + object.addProperty("time", getTime()); object.addProperty("message", getMessage()); return object.toString(); }