diff --git a/src/main/java/com/rohitawate/everest/controllers/search/SearchablePaneController.java b/src/main/java/com/rohitawate/everest/controllers/search/SearchablePaneController.java index 2e2bfbb..a577b66 100644 --- a/src/main/java/com/rohitawate/everest/controllers/search/SearchablePaneController.java +++ b/src/main/java/com/rohitawate/everest/controllers/search/SearchablePaneController.java @@ -16,9 +16,9 @@ package com.rohitawate.everest.controllers.search; +import com.google.common.util.concurrent.MoreExecutors; import com.jfoenix.controls.JFXButton; import com.rohitawate.everest.logging.LoggingService; -import com.rohitawate.everest.misc.Services; import javafx.application.Platform; import javafx.concurrent.Task; import javafx.fxml.FXML; @@ -36,6 +36,7 @@ import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; public abstract class SearchablePaneController implements Initializable { @FXML @@ -125,7 +126,7 @@ public abstract class SearchablePaneController implements Initializable { entryLoader.setOnFailed(e -> LoggingService.logWarning("Failed to load history.", (Exception) entryLoader.getException(), LocalDateTime.now())); - Services.singleExecutor.execute(entryLoader); + MoreExecutors.directExecutor().execute(entryLoader); } private void addSearchItem(T state) { diff --git a/src/main/java/com/rohitawate/everest/logging/Logger.java b/src/main/java/com/rohitawate/everest/logging/Logger.java index ff6a109..806dd40 100644 --- a/src/main/java/com/rohitawate/everest/logging/Logger.java +++ b/src/main/java/com/rohitawate/everest/logging/Logger.java @@ -36,14 +36,18 @@ class Logger { /** * Appends the log to the respective day's log file. * - * @param log - The log to be written to file. */ - synchronized void log(Log log) { - System.out.println(log.message); - if (log.level.greaterThanEqualTo(this.writerLevel)) { + synchronized void log() { + if (LoggingService.log.level.equals(Level.INFO)) { + System.out.println(LoggingService.log.level + " " + LoggingService.log.time + ": " + LoggingService.log.message); + } else { + System.err.println(LoggingService.log.level + " " + LoggingService.log.time + ": " + LoggingService.log.message); + } + + if (LoggingService.log.level.greaterThanEqualTo(this.writerLevel)) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(logFilePath, true))) { writer.flush(); - writer.append(getLogEntry(log)); + writer.append(getLogEntry(LoggingService.log)); } catch (IOException e) { e.printStackTrace(); } diff --git a/src/main/java/com/rohitawate/everest/logging/LoggingService.java b/src/main/java/com/rohitawate/everest/logging/LoggingService.java index 360c728..44715e2 100644 --- a/src/main/java/com/rohitawate/everest/logging/LoggingService.java +++ b/src/main/java/com/rohitawate/everest/logging/LoggingService.java @@ -16,35 +16,31 @@ package com.rohitawate.everest.logging; -import com.rohitawate.everest.misc.Services; +import com.google.common.util.concurrent.MoreExecutors; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.concurrent.Executor; public class LoggingService { - private static final Logger logger; - private static final DateTimeFormatter dateFormat; - private static final Log log; - - static { - log = new Log(); - logger = new Logger(Level.INFO); - dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"); - } + private static Executor executor = MoreExecutors.directExecutor(); + private static final Logger logger = new Logger(Level.INFO); + private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"); + static final Log log = new Log(); public static void logSevere(String message, Exception exception, LocalDateTime time) { setValues(message, exception, time); - Services.singleExecutor.execute(severeLogger); + executor.execute(severeLogger); } public static void logWarning(String message, Exception exception, LocalDateTime time) { setValues(message, exception, time); - Services.singleExecutor.execute(warningLogger); + executor.execute(warningLogger); } public static void logInfo(String message, LocalDateTime time) { setValues(message, null, time); - Services.singleExecutor.execute(infoLogger); + executor.execute(infoLogger); } private static void setValues(String message, Exception exception, LocalDateTime time) { @@ -55,16 +51,16 @@ public class LoggingService { private static Runnable severeLogger = () -> { log.level = Level.SEVERE; - logger.log(log); + logger.log(); }; private static Runnable warningLogger = () -> { log.level = Level.WARNING; - logger.log(log); + logger.log(); }; private static Runnable infoLogger = () -> { log.level = Level.INFO; - logger.log(log); + logger.log(); }; } diff --git a/src/main/java/com/rohitawate/everest/misc/Services.java b/src/main/java/com/rohitawate/everest/misc/Services.java deleted file mode 100644 index 2301d37..0000000 --- a/src/main/java/com/rohitawate/everest/misc/Services.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2018 Rohit Awate. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.rohitawate.everest.misc; - -import com.google.common.util.concurrent.MoreExecutors; - -import java.util.concurrent.Executor; - -public class Services { - public static Executor singleExecutor; - - static { - singleExecutor = MoreExecutors.directExecutor(); - } -} diff --git a/src/main/java/com/rohitawate/everest/sync/SQLiteManager.java b/src/main/java/com/rohitawate/everest/sync/SQLiteManager.java index 3a379f2..0aa820d 100644 --- a/src/main/java/com/rohitawate/everest/sync/SQLiteManager.java +++ b/src/main/java/com/rohitawate/everest/sync/SQLiteManager.java @@ -67,10 +67,9 @@ class SQLiteManager implements DataManager { conn = DriverManager.getConnection("jdbc:sqlite:Everest/config/history.sqlite"); createDatabase(); + LoggingService.logInfo("Connected to database", LocalDateTime.now()); } catch (Exception E) { LoggingService.logSevere("Exception while initializing DataManager.", E, LocalDateTime.now()); - } finally { - System.out.println("Connected to database."); } }