Console logging is a mess #3930

This commit is contained in:
rusefillc 2022-02-12 16:09:31 -05:00
parent 83e879f6c1
commit 1f97452359
10 changed files with 69 additions and 20 deletions

View File

@ -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;
}

View File

@ -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<String, Exception> configureLogFile(String log_file) {
Logger root = Logger.getLogger("");
Logger root = getRootLogger();
Map<String, Exception> errors = new LinkedHashMap<String, Exception>();
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);
}

View File

@ -13,6 +13,7 @@ import java.io.*;
* 6/30/13
* Andrey Belomutskiy, (c) 2013-2020
*/
@Deprecated
public enum FileLog {
MAIN,
SIMULATOR_CONSOLE;

View File

@ -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<String> 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());

View File

@ -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<String, ConsoleTool> TOOLS = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
private static final Map<String, ConsoleTool> TOOLS = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
private static Map<String, String> toolsHelp = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
private static final Map<String, String> toolsHelp = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
static {
registerTool("help", args -> printTools(), "Print this help.");

View File

@ -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);
});

View File

@ -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();
}
};

View File

@ -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();
}

View File

@ -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() {

View File

@ -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<String, Exception> configureLogFile(String logFile) {
return reconfigure(logFile);