From 7b51e10bee15c9596bdc688251b33074cdf37b5c Mon Sep 17 00:00:00 2001 From: Alphonse Mariya Date: Mon, 26 Nov 2018 18:47:47 +0100 Subject: [PATCH] Simplify log() by using default params --- src/Coordinator.java | 8 ++++---- src/Logger.java | 21 ++++++++++++++++----- src/Subordinate.java | 12 ++++++------ 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/Coordinator.java b/src/Coordinator.java index 1e3ac2d..20197ab 100644 --- a/src/Coordinator.java +++ b/src/Coordinator.java @@ -285,17 +285,17 @@ private void checkVotes() throws IOException { if (decision) { - this.coordinatorLog.log("COMMIT", true, true, true); + this.coordinatorLog.log("COMMIT", true); } else { - this.coordinatorLog.log("ABORT", true, true, true); + this.coordinatorLog.log("ABORT", true); } if(this.sockets.size() == 0) { - this.coordinatorLog.log("END", false, true, true); + this.coordinatorLog.log("END"); this.failedSubordinatesLog.emptyLog(); } @@ -415,7 +415,7 @@ private void checkAcknowledgements(List subordinatesToBeChecked) throws } else { - this.coordinatorLog.log("END", false, true, true); + this.coordinatorLog.log("END"); if (this.isRecoveryProcessStarted) Printer.print("=============== END OF RECOVERY PROCESS =================\n", "orange"); diff --git a/src/Logger.java b/src/Logger.java index e3942d8..d3ec466 100644 --- a/src/Logger.java +++ b/src/Logger.java @@ -42,16 +42,27 @@ public Logger(String filename, String nodeType, boolean append) throws IOExcepti } + public void log(String msg) throws IOException { + log(msg, false, true, true); + } + + public void log(String msg, boolean forceWrite) throws IOException { + log(msg, forceWrite, true, true); + } + + public void log(String msg, boolean forceWrite, boolean appendDate) throws IOException { + log(msg, forceWrite, appendDate, true); + } + public void log(String msg, boolean forceWrite, boolean appendDate, boolean verbose) throws IOException { - String newLogEntry; + String newLogEntry = msg; if(appendDate) { - String timeStamp = dateFormat.format(new Date()); - newLogEntry = msg + " " + timeStamp + "\n"; - } else { - newLogEntry = msg + "\n"; + String timeStamp = dateFormat.format(new Date()); + newLogEntry += " " + timeStamp; } + newLogEntry += "\n"; this.bw.write(newLogEntry); this.bw.flush(); diff --git a/src/Subordinate.java b/src/Subordinate.java index 6638474..3c3a2d7 100644 --- a/src/Subordinate.java +++ b/src/Subordinate.java @@ -142,7 +142,7 @@ private void phaseOne() throws IOException { inputHandler.getUserInput().toUpperCase().equals("Y") && ((System.currentTimeMillis() - startTime) < Coordinator.TIMEOUT_MILLIS)) { - this.SubordinateLog.log("PREPARED", true, true, true); + this.SubordinateLog.log("PREPARED", true); this.send("Y"); Printer.print("=============== END OF PHASE 1 =================\n", "blue"); this.phaseTwo(); @@ -151,7 +151,7 @@ private void phaseOne() throws IOException { inputHandler.getUserInput().toUpperCase().equals("N") && ((System.currentTimeMillis() - startTime) < Coordinator.TIMEOUT_MILLIS)) { - this.SubordinateLog.log("ABORT", true, true, true); + this.SubordinateLog.log("ABORT", true); this.send("N"); Printer.print("=============== END OF PHASE 1 =================\n", "blue"); this.phaseTwo(); @@ -238,11 +238,11 @@ private void phaseTwo() throws IOException { if(!this.SubordinateLog.readLogBottom().split(" ")[0].equals("ABORT")){ - this.SubordinateLog.log("ABORT", true, true, true); + this.SubordinateLog.log("ABORT", true); } - this.SubordinateLog.log("END", false, true, true); + this.SubordinateLog.log("END"); Printer.print("=============== END OF RECOVERY PROCESS =================", "orange"); Printer.print("=============== UNILATERAL ABORT =================\n", "red"); @@ -263,7 +263,7 @@ private void phaseTwo() throws IOException { if (!this.SubordinateLog.readLogBottom().split(" ")[0].equals("ABORT") && !this.SubordinateLog.readLogBottom().split(" ")[0].equals("COMMIT")) { - this.SubordinateLog.log(decisionMsg, true, true, true); + this.SubordinateLog.log(decisionMsg, true); } @@ -373,7 +373,7 @@ private void sendAck() throws IOException { (timeDiff < Coordinator.TIMEOUT_MILLIS)) { this.send("ACK"); - this.SubordinateLog.log("END", false, true, true); + this.SubordinateLog.log("END"); Printer.print("=============== END OF PHASE 2 =================\n", "green");