diff --git a/java_console/io/src/main/java/com/rusefi/autodetect/PortDetector.java b/java_console/io/src/main/java/com/rusefi/autodetect/PortDetector.java index d1b2f0bc69..cb11682881 100644 --- a/java_console/io/src/main/java/com/rusefi/autodetect/PortDetector.java +++ b/java_console/io/src/main/java/com/rusefi/autodetect/PortDetector.java @@ -95,7 +95,7 @@ public class PortDetector { if (autoDetectResult == null) autoDetectResult = new SerialAutoChecker.AutoDetectResult(null, null); log.debug("Found " + autoDetectResult + " now stopping threads"); -// FileLog.MAIN.logLine("Returning " + result.get()); +// log.info("Returning " + result.get()); return autoDetectResult; } @@ -106,7 +106,7 @@ public class PortDetector { private static String[] getPortNames() { // long now = System.currentTimeMillis(); String[] portNames = LinkManager.getCommPorts(); -// FileLog.MAIN.logLine("Took " + (System.currentTimeMillis() - now)); +// log.info("Took " + (System.currentTimeMillis() - now)); return portNames; } diff --git a/java_console/logging-api/src/main/java/com/devexperts/logging/DefaultLogging.java b/java_console/logging-api/src/main/java/com/devexperts/logging/DefaultLogging.java index 6d1b321bfd..dc1da4a11d 100644 --- a/java_console/logging-api/src/main/java/com/devexperts/logging/DefaultLogging.java +++ b/java_console/logging-api/src/main/java/com/devexperts/logging/DefaultLogging.java @@ -49,8 +49,13 @@ class DefaultLogging { handler.getLevel() == Level.INFO; } + void configureLogFileAndConsole(String log_file) { + configureLogFile(log_file); + initAndAdd(new ConsoleHandler(), Level.ALL, getRootLogger()); + } + Map configureLogFile(String log_file) { - Logger root = Logger.getLogger(""); + Logger root = getRootLogger(); Map errors = new LinkedHashMap(); try { @@ -71,18 +76,14 @@ class DefaultLogging { } if (handler == null) handler = new ConsoleHandler(); - handler.setFormatter(new LogFormatter()); - handler.setLevel(Level.ALL); - root.addHandler(handler); + initAndAdd(handler, Level.ALL, root); // configure "err" file String err_file = getProperty(Logging.ERR_FILE_PROPERTY, null); if (err_file != null) { try { handler = new FileHandler(err_file, getLimit(Logging.ERR_MAX_FILE_SIZE_PROPERTY, errors), 2, true); - handler.setFormatter(new LogFormatter()); - handler.setLevel(Level.WARNING); - root.addHandler(handler); + initAndAdd(handler, Level.WARNING, root); } catch (IOException e) { errors.put(err_file, e); } @@ -93,6 +94,16 @@ class DefaultLogging { return errors; } + private void initAndAdd(Handler handler, Level all, Logger root) { + handler.setFormatter(new LogFormatter()); + handler.setLevel(all); + root.addHandler(handler); + } + + private Logger getRootLogger() { + return Logger.getLogger(""); + } + Object getPeer(String name) { return Logger.getLogger(name); } diff --git a/java_console/logging/src/main/java/com/rusefi/FileLog.java b/java_console/logging/src/main/java/com/rusefi/FileLog.java index bd0bcac7cf..33bcd79f35 100644 --- a/java_console/logging/src/main/java/com/rusefi/FileLog.java +++ b/java_console/logging/src/main/java/com/rusefi/FileLog.java @@ -13,6 +13,7 @@ import java.io.*; * 6/30/13 * Andrey Belomutskiy, (c) 2013-2020 */ +@Deprecated public enum FileLog { MAIN, SIMULATOR_CONSOLE; diff --git a/java_console/ui/src/main/java/com/rusefi/StartupFrame.java b/java_console/ui/src/main/java/com/rusefi/StartupFrame.java index d2d291c539..388a4f2b8f 100644 --- a/java_console/ui/src/main/java/com/rusefi/StartupFrame.java +++ b/java_console/ui/src/main/java/com/rusefi/StartupFrame.java @@ -1,5 +1,6 @@ package com.rusefi; +import com.devexperts.logging.Logging; import com.rusefi.autodetect.PortDetector; import com.rusefi.autodetect.SerialAutoChecker; import com.rusefi.autoupdate.Autoupdate; @@ -24,6 +25,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import static com.devexperts.logging.Logging.getLogging; import static com.rusefi.ui.storage.PersistentConfiguration.getConfig; import static com.rusefi.ui.util.UiUtils.*; import static javax.swing.JOptionPane.YES_NO_OPTION; @@ -38,6 +40,8 @@ import static javax.swing.JOptionPane.YES_NO_OPTION; * @see FirmwareFlasher */ public class StartupFrame { + private static final Logging log = getLogging(Launcher.class); + public static final String LOGO_PATH = "/com/rusefi/"; private static final String LOGO = LOGO_PATH + "logo.png"; public static final String LINK_TEXT = "rusEFI (c) 2012-2021"; @@ -219,7 +223,7 @@ public class StartupFrame { private void applyKnownPorts() { List ports = SerialPortScanner.INSTANCE.getKnownPorts(); if (!currentlyDisplayedPorts.equals(ports) || isFirstTimeApplyingPorts) { - FileLog.MAIN.logLine("Rendering available ports: " + ports); + log.info("Rendering available ports: " + ports); isFirstTimeApplyingPorts = false; connectPanel.setVisible(!ports.isEmpty()); noPortsMessage.setVisible(ports.isEmpty()); diff --git a/java_console/ui/src/main/java/com/rusefi/tools/ConsoleTools.java b/java_console/ui/src/main/java/com/rusefi/tools/ConsoleTools.java index 63fd41496b..4e4d2faf3c 100644 --- a/java_console/ui/src/main/java/com/rusefi/tools/ConsoleTools.java +++ b/java_console/ui/src/main/java/com/rusefi/tools/ConsoleTools.java @@ -42,9 +42,9 @@ import static com.rusefi.binaryprotocol.IoHelper.getCrc32; public class ConsoleTools { public static final String SET_AUTH_TOKEN = "set_auth_token"; public static final String RUS_EFI_NOT_DETECTED = "rusEFI not detected"; - private static Map TOOLS = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + private static final Map TOOLS = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); - private static Map toolsHelp = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + private static final Map toolsHelp = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); static { registerTool("help", args -> printTools(), "Print this help."); diff --git a/java_console/ui/src/main/java/com/rusefi/ui/StatusWindow.java b/java_console/ui/src/main/java/com/rusefi/ui/StatusWindow.java index 50636af8d9..7debd49d99 100644 --- a/java_console/ui/src/main/java/com/rusefi/ui/StatusWindow.java +++ b/java_console/ui/src/main/java/com/rusefi/ui/StatusWindow.java @@ -1,6 +1,8 @@ package com.rusefi.ui; +import com.devexperts.logging.Logging; import com.rusefi.FileLog; +import com.rusefi.ui.util.DefaultExceptionHandler; import com.rusefi.ui.util.FrameHelper; import com.rusefi.ui.util.UiUtils; import org.jetbrains.annotations.NotNull; @@ -8,11 +10,15 @@ import org.jetbrains.annotations.NotNull; import javax.swing.*; import java.awt.*; +import static com.devexperts.logging.Logging.getLogging; + /** * Andrey Belomutskiy, (c) 2013-2020 * 3/7/2015 */ public class StatusWindow implements StatusConsumer { + private static final Logging log = getLogging(StatusWindow.class); + private static final Color LIGHT_RED = new Color(255, 102, 102); private static final Color LIGHT_GREEN = new Color(102, 255 ,102); // todo: extract driver from console bundle? find a separate driver bundle? @@ -66,7 +72,7 @@ public class StatusWindow implements StatusConsumer { public void append(final String string) { SwingUtilities.invokeLater(() -> { String s = string.replaceAll(Character.toString((char) 219), ""); - FileLog.MAIN.logLine(s); + log.info(s); logTextArea.append(s + "\r\n"); UiUtils.trueLayout(logTextArea); }); diff --git a/java_console/ui/src/main/java/com/rusefi/ui/console/MainFrame.java b/java_console/ui/src/main/java/com/rusefi/ui/console/MainFrame.java index a28b14453a..065489dddc 100644 --- a/java_console/ui/src/main/java/com/rusefi/ui/console/MainFrame.java +++ b/java_console/ui/src/main/java/com/rusefi/ui/console/MainFrame.java @@ -1,5 +1,6 @@ package com.rusefi.ui.console; +import com.devexperts.logging.Logging; import com.rusefi.*; import com.rusefi.binaryprotocol.BinaryProtocol; import com.rusefi.config.generated.Fields; @@ -15,9 +16,12 @@ import org.jetbrains.annotations.NotNull; import java.util.Objects; import java.util.TimeZone; +import static com.devexperts.logging.Logging.getLogging; import static com.rusefi.ui.storage.PersistentConfiguration.getConfig; public class MainFrame { + private static final Logging log = getLogging(Launcher.class); + @NotNull private final ConsoleUI consoleUI; private final TabbedPanel tabbedPane; @@ -27,7 +31,7 @@ public class MainFrame { private final FrameHelper frame = new FrameHelper() { @Override protected void onWindowOpened() { - FileLog.MAIN.logLine("onWindowOpened"); + log.info("onWindowOpened"); windowOpenedHandler(); } @@ -40,7 +44,7 @@ public class MainFrame { /** * here we would close the log file */ - FileLog.MAIN.logLine("onWindowClosed"); + log.info("onWindowClosed"); FileLog.MAIN.close(); } }; diff --git a/java_console/ui/src/main/java/com/rusefi/ui/light/LightweightGUI.java b/java_console/ui/src/main/java/com/rusefi/ui/light/LightweightGUI.java index 4e0d43b135..e8021b09bc 100644 --- a/java_console/ui/src/main/java/com/rusefi/ui/light/LightweightGUI.java +++ b/java_console/ui/src/main/java/com/rusefi/ui/light/LightweightGUI.java @@ -1,5 +1,6 @@ package com.rusefi.ui.light; +import com.devexperts.logging.Logging; import com.rusefi.*; import com.rusefi.autodetect.PortDetector; import com.rusefi.autoupdate.Autoupdate; @@ -16,12 +17,14 @@ import org.putgemin.VerticalFlowLayout; import javax.swing.*; import java.awt.*; +import static com.devexperts.logging.Logging.getLogging; import static com.rusefi.StartupFrame.createLogoLabel; public class LightweightGUI { + private static final Logging log = getLogging(LightweightGUI.class); - private JPanel connectedPanel = new JPanel(); - private JLabel connectedLabel = new JLabel(); + private final JPanel connectedPanel = new JPanel(); + private final JLabel connectedLabel = new JLabel(); public LightweightGUI(UIContext uiContext) { final FrameHelper frameHelper = new FrameHelper(); @@ -102,7 +105,7 @@ public class LightweightGUI { linkManager.startAndConnect(autoDetectedPort, ConnectionStateListener.VOID); new ConnectionWatchdog(Timeouts.CONNECTION_RESTART_DELAY, () -> { - FileLog.MAIN.logLine("ConnectionWatchdog.reconnectTimer restarting: " + Timeouts.CONNECTION_RESTART_DELAY); + log.info("ConnectionWatchdog.reconnectTimer restarting: " + Timeouts.CONNECTION_RESTART_DELAY); linkManager.restart(); }).start(); } diff --git a/java_console/ui/src/main/java/com/rusefi/ui/util/DefaultExceptionHandler.java b/java_console/ui/src/main/java/com/rusefi/ui/util/DefaultExceptionHandler.java index 6e42d24ce9..56c1a9ad15 100644 --- a/java_console/ui/src/main/java/com/rusefi/ui/util/DefaultExceptionHandler.java +++ b/java_console/ui/src/main/java/com/rusefi/ui/util/DefaultExceptionHandler.java @@ -1,12 +1,15 @@ package com.rusefi.ui.util; +import com.devexperts.logging.Logging; import com.rusefi.FileLog; +import com.rusefi.ui.light.LightweightGUI; import javax.swing.*; import java.awt.*; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import static com.devexperts.logging.Logging.getLogging; import static com.rusefi.Launcher.*; /** @@ -14,6 +17,8 @@ import static com.rusefi.Launcher.*; * Andrey Belomutskiy, (c) 2013-2020 */ public class DefaultExceptionHandler implements Thread.UncaughtExceptionHandler { + private static final Logging log = getLogging(DefaultExceptionHandler.class); + private static boolean hadExceptionAlready; public void uncaughtException(Thread t, Throwable e) { @@ -22,7 +27,7 @@ public class DefaultExceptionHandler implements Thread.UncaughtExceptionHandler public static void handleException(Throwable e) { if (e == null) { - FileLog.MAIN.logLine("Null exception?"); + log.info("Null exception?"); throw new NullPointerException("Throwable e"); } e.printStackTrace(); // output to error log @@ -51,7 +56,7 @@ public class DefaultExceptionHandler implements Thread.UncaughtExceptionHandler content.add(scrollPane, BorderLayout.CENTER); JOptionPane.showConfirmDialog(findActiveFrame(), content, CONSOLE_VERSION + ": Exception Occurred", JOptionPane.DEFAULT_OPTION); - FileLog.MAIN.logLine("handleException: " + baos.toString()); + log.info("handleException: " + baos.toString()); } private static Frame findActiveFrame() { diff --git a/java_tools/proxy_server/src/main/java/com/devexperts/logging/Log4j2Logging.java b/java_tools/proxy_server/src/main/java/com/devexperts/logging/Log4j2Logging.java index a9da590cc0..e52c03f38e 100644 --- a/java_tools/proxy_server/src/main/java/com/devexperts/logging/Log4j2Logging.java +++ b/java_tools/proxy_server/src/main/java/com/devexperts/logging/Log4j2Logging.java @@ -125,6 +125,21 @@ class Log4j2Logging extends DefaultLogging { return builder.build(); } + @Override + void configureLogFileAndConsole(String logFile) { + reconfigure(logFile); + + LoggerContext ctx = (LoggerContext)LogManager.getContext(false); + Configuration config = ctx.getConfiguration(); + + Appender appender = ConsoleAppender.newBuilder() + .withName("common") + .withLayout(getDetailedLayout()) + .setTarget(ConsoleAppender.Target.SYSTEM_OUT) + .build(); + config.getRootLogger().addAppender(appender, DEBUG, null); + } + @Override Map configureLogFile(String logFile) { return reconfigure(logFile);