Extract logging with break-line into `logLine` and `appendLine` methods (#6699)

This commit is contained in:
kifir23917 2024-07-13 14:34:01 +03:00 committed by rusefillc
parent ace2c67e5a
commit a58876edd3
9 changed files with 74 additions and 66 deletions

View File

@ -23,9 +23,9 @@ public class DfuHelper {
try {
stream.sendPacket(command);
stream.close();
callbacks.log(String.format("Reboot command [%s] sent into %s!\n", cmd, stream), true);
callbacks.logLine(String.format("Reboot command [%s] sent into %s!\n", cmd, stream));
} catch (IOException e) {
callbacks.log("Error " + e, true);
callbacks.logLine("Error " + e);
}
}

View File

@ -3,10 +3,18 @@ package com.rusefi.io;
public interface UpdateOperationCallbacks {
void log(String message, boolean breakLineOnTextArea);
default void logLine(final String message) {
log(message, true);
}
default void append(final String message, final boolean breakLineOnTextArea) {
log(message, breakLineOnTextArea);
}
default void appendLine(final String message) {
append(message, true);
}
void done();
void error();

View File

@ -51,8 +51,8 @@ public class DfuFlasher {
return;
if (isSignatureValidated.get()) {
if (!FileLog.isWindows()) {
callbacks.append("Switched to DFU mode!", true);
callbacks.append("rusEFI console can only program on Windows", true);
callbacks.appendLine("Switched to DFU mode!");
callbacks.appendLine("rusEFI console can only program on Windows");
return;
}
@ -61,7 +61,7 @@ public class DfuFlasher {
executeDFU(callbacks, FindFileHelper.FIRMWARE_BIN_FILE);
});
} else {
callbacks.log("Please use manual DFU to change bundle type.", true);
callbacks.logLine("Please use manual DFU to change bundle type.");
}
}
@ -73,7 +73,7 @@ public class DfuFlasher {
public static AtomicBoolean rebootToDfu(JComponent parent, String port, UpdateOperationCallbacks callbacks, String command) {
AtomicBoolean isSignatureValidated = new AtomicBoolean(true);
if (!PortDetector.isAutoPort(port)) {
callbacks.log("Using selected " + port + "\n", true);
callbacks.logLine("Using selected " + port + "\n");
IoStream stream = BufferedSerialIoStream.openPort(port);
AtomicReference<String> signature = new AtomicReference<>();
new SerialAutoChecker(PortDetector.DetectorMode.DETECT_TS, port, new CountDownLatch(1)).checkResponse(stream, new Function<SerialAutoChecker.CallbackContext, Void>() {
@ -84,22 +84,22 @@ public class DfuFlasher {
}
});
if (signature.get() == null) {
callbacks.append("", true);
callbacks.append("", true);
callbacks.append("", true);
callbacks.append("Make sure TUNERSTUDIO IS DISCONNECTED FROM ECU", true);
callbacks.append("", true);
callbacks.append("", true);
callbacks.append("", true);
callbacks.append("*** ERROR *** rusEFI has not responded on selected " + port + "\n" +
"Maybe try automatic serial port detection?", true);
callbacks.appendLine("");
callbacks.appendLine("");
callbacks.appendLine("");
callbacks.appendLine("Make sure TUNERSTUDIO IS DISCONNECTED FROM ECU");
callbacks.appendLine("");
callbacks.appendLine("");
callbacks.appendLine("");
callbacks.appendLine("*** ERROR *** rusEFI has not responded on selected " + port + "\n" +
"Maybe try automatic serial port detection?");
callbacks.error();
return null;
}
boolean isSignatureValidatedLocal = DfuHelper.sendDfuRebootCommand(parent, signature.get(), stream, callbacks, command);
isSignatureValidated.set(isSignatureValidatedLocal);
} else {
callbacks.log("Auto-detecting port...\n", true);
callbacks.logLine("Auto-detecting port...\n");
// instead of opening the just-detected port we execute the command using the same stream we used to discover port
// it's more reliable this way
// ISSUE: that's blocking stuff on UI thread at the moment, TODO smarter threading!
@ -109,11 +109,11 @@ public class DfuFlasher {
return null;
}).getSerialPort();
if (port == null) {
callbacks.append("*** ERROR *** rusEFI serial port not detected", true);
callbacks.appendLine("*** ERROR *** rusEFI serial port not detected");
callbacks.error();
return null;
} else {
callbacks.append("Detected rusEFI on " + port + "\n", true);
callbacks.appendLine("Detected rusEFI on " + port + "\n");
}
}
return isSignatureValidated;
@ -133,7 +133,7 @@ public class DfuFlasher {
getDfuEraseCommand(),
DFU_CMD_TOOL, callbacks);
} catch (FileNotFoundException e) {
callbacks.log(e.toString(), true);
callbacks.logLine(e.toString());
callbacks.error();
}
}
@ -149,7 +149,7 @@ public class DfuFlasher {
private static void executeDFU(UpdateOperationCallbacks callbacks, String firmwareBinFile) {
boolean driverIsHappy = detectSTM32BootloaderDriverState(callbacks);
if (!driverIsHappy) {
callbacks.append("*** DRIVER ERROR? *** Did you have a chance to try 'Install Drivers' button on top of rusEFI console start screen?", true);
callbacks.appendLine("*** DRIVER ERROR? *** Did you have a chance to try 'Install Drivers' button on top of rusEFI console start screen?");
callbacks.error();
return;
}
@ -161,27 +161,27 @@ public class DfuFlasher {
getDfuWriteCommand(firmwareBinFile),
DFU_CMD_TOOL, callbacks, stdout);
} catch (FileNotFoundException e) {
callbacks.log("ERROR: " + e, true);
callbacks.logLine("ERROR: " + e);
callbacks.error();
return;
}
if (stdout.toString().contains("Download verified successfully")) {
// looks like sometimes we are not catching the last line of the response? 'Upgrade' happens before 'Verify'
callbacks.log("SUCCESS!", true);
callbacks.log("Please power cycle device to exit DFU mode", true);
callbacks.logLine("SUCCESS!");
callbacks.logLine("Please power cycle device to exit DFU mode");
callbacks.done();
} else if (stdout.toString().contains("Target device not found")) {
callbacks.append("ERROR: Device not connected or STM32 Bootloader driver not installed?", true);
callbacks.appendLine("ERROR: Device not connected or STM32 Bootloader driver not installed?");
appendWindowsVersion(callbacks);
callbacks.append("ERROR: Please try installing drivers using 'Install Drivers' button on rusEFI splash screen", true);
callbacks.append("ERROR: Alternatively please install drivers using Device Manager pointing at 'drivers/silent_st_drivers/DFU_Driver' folder", true);
callbacks.appendLine("ERROR: Please try installing drivers using 'Install Drivers' button on rusEFI splash screen");
callbacks.appendLine("ERROR: Alternatively please install drivers using Device Manager pointing at 'drivers/silent_st_drivers/DFU_Driver' folder");
appendDeviceReport(callbacks);
callbacks.error();
} else {
appendWindowsVersion(callbacks);
appendDeviceReport(callbacks);
callbacks.log(stdout.length() + " / " + errorResponse.length(), true);
callbacks.logLine(stdout.length() + " / " + errorResponse.length());
callbacks.error();
}
}
@ -191,22 +191,22 @@ public class DfuFlasher {
}
private static void appendWindowsVersion(UpdateOperationCallbacks callbacks) {
callbacks.log("ERROR: does not look like DFU has worked!", true);
callbacks.logLine("ERROR: does not look like DFU has worked!");
}
private static void appendDeviceReport(UpdateOperationCallbacks callbacks) {
for (String line : getDevicesReport()) {
if (line.contains("STM Device in DFU Mode")) {
callbacks.log(" ******************************************************************", true);
callbacks.log(" ************* YOU NEED TO REMOVE LEGACY DFU DRIVER ***************", true);
callbacks.log(" ******************************************************************", true);
callbacks.logLine(" ******************************************************************");
callbacks.logLine(" ************* YOU NEED TO REMOVE LEGACY DFU DRIVER ***************");
callbacks.logLine(" ******************************************************************");
}
callbacks.log("Devices: " + line, true);
callbacks.logLine("Devices: " + line);
}
}
private static void timeForDfuSwitch(UpdateOperationCallbacks callbacks) {
callbacks.log("Giving time for USB enumeration...", true);
callbacks.logLine("Giving time for USB enumeration...");
try {
// two seconds not enough on my Windows 10
Thread.sleep(3 * Timeouts.SECOND);

View File

@ -47,7 +47,7 @@ public class DriverInstall {
FileLog.MAIN.logLine("IsWindows=" + FileLog.isWindows());
if (!new File(FOLDER).exists()) {
String message = FOLDER + " not found";
wnd.append(message, true);
wnd.appendLine(message);
FileLog.MAIN.logLine(message);
return;
}
@ -60,7 +60,7 @@ public class DriverInstall {
String batch = isWindows7orBelow() ? WINDOWS7_BATCH : WINDOWS8_BATCH;
ExecHelper.executeCommand(UNPACKED_FOLDER, ExecHelper.getBatchCommand(batch), batch, wnd);
} catch (FileNotFoundException e) {
wnd.append(e.toString(), true);
wnd.appendLine(e.toString());
wnd.error();
}
}

View File

@ -38,12 +38,12 @@ public class ExecHelper {
String line = bis.readLine();
if (line == null)
break;
callbacks.log(line, true);
callbacks.logLine(line);
buffer.append(line);
wasRunningTime = System.currentTimeMillis();
}
} catch (IOException e) {
callbacks.log("Stream " + e, true);
callbacks.logLine("Stream " + e);
callbacks.error();
}
});
@ -65,7 +65,7 @@ public class ExecHelper {
StringBuffer error = new StringBuffer();
String binaryFullName = workingDirPath + File.separator + binaryRelativeName;
if (!new File(binaryFullName).exists()) {
callbacks.log(binaryFullName + " not found :(", true);
callbacks.logLine(binaryFullName + " not found :(");
throw new FileNotFoundException(binaryFullName);
}
@ -75,17 +75,17 @@ public class ExecHelper {
@NotNull
public static String executeCommand(String command, UpdateOperationCallbacks callbacks, StringBuffer output, StringBuffer error, File workingDir) {
callbacks.log("Executing " + command, true);
callbacks.logLine("Executing " + command);
try {
Process p = Runtime.getRuntime().exec(command, null, workingDir);
startStreamThread(p, p.getInputStream(), output, callbacks);
startStreamThread(p, p.getErrorStream(), error, callbacks);
p.waitFor(3, TimeUnit.MINUTES);
} catch (IOException e) {
callbacks.log("IOError: " + e, true);
callbacks.logLine("IOError: " + e);
callbacks.error();
} catch (InterruptedException e) {
callbacks.log("WaitError: " + e, true);
callbacks.logLine("WaitError: " + e);
callbacks.error();
}

View File

@ -14,8 +14,8 @@ public class MaintenanceUtil {
StringBuffer output = new StringBuffer();
StringBuffer error = new StringBuffer();
ExecHelper.executeCommand(queryCommand, callbacks, output, error, null);
callbacks.log(output.toString(), true);
callbacks.log(error.toString(), true);
callbacks.logLine(output.toString());
callbacks.logLine(error.toString());
// long cost = System.currentTimeMillis() - now;
// System.out.println("DFU lookup cost " + cost + "ms");
return output.toString().contains(pattern);

View File

@ -156,10 +156,10 @@ public class ProgramSelector {
try {
OpenbltJni.flashCan(FindFileHelper.findSrecFile(), cb);
callbacks.log("Update completed successfully!", true);
callbacks.logLine("Update completed successfully!");
callbacks.done();
} catch (Throwable e) {
callbacks.log("Error: " + e, true);
callbacks.logLine("Error: " + e);
callbacks.error();
} finally {
OpenbltJni.stop(cb);
@ -178,10 +178,10 @@ public class ProgramSelector {
// Check that the ECU disappeared from the "after" list
if (!PortDetector.AUTO.equals(ecuPort) && Arrays.stream(portsAfter).anyMatch(ecuPort::equals)) {
callbacks.log("Looks like your ECU didn't reboot to OpenBLT fast enough", true);
callbacks.log("", true);
callbacks.log("Try closing and opening console again", true);
callbacks.log("", true);
callbacks.logLine("Looks like your ECU didn't reboot to OpenBLT fast enough");
callbacks.logLine("");
callbacks.logLine("Try closing and opening console again");
callbacks.logLine("");
callbacks.error();
return;
}
@ -196,21 +196,21 @@ public class ProgramSelector {
}
if (newItems.isEmpty()) {
callbacks.log("Looks like your ECU disappeared during the update process. Please try again.", true);
callbacks.logLine("Looks like your ECU disappeared during the update process. Please try again.");
callbacks.error();
return;
}
if (newItems.size() > 1) {
// More than one port appeared? whattt?
callbacks.log("Unable to find ECU after reboot as multiple serial ports appeared. Before: " + portsBefore.length + " After: " + portsAfter.length, true);
callbacks.logLine("Unable to find ECU after reboot as multiple serial ports appeared. Before: " + portsBefore.length + " After: " + portsAfter.length);
callbacks.error();
return;
}
String openbltPort = newItems.get(0);
callbacks.log("Serial port " + openbltPort + " appeared, programming firmware...", true);
callbacks.logLine("Serial port " + openbltPort + " appeared, programming firmware...");
flashOpenbltSerialJni(parent, openbltPort, callbacks);
}
@ -219,12 +219,12 @@ public class ProgramSelector {
return new OpenbltJni.OpenbltCallbacks() {
@Override
public void log(String line) {
callbacks.log(line, true);
callbacks.logLine(line);
}
@Override
public void updateProgress(int percent) {
callbacks.log("Progress: " + percent + "%", true);
callbacks.logLine("Progress: " + percent + "%");
}
@Override
@ -234,7 +234,7 @@ public class ProgramSelector {
@Override
public void setPhase(String title, boolean hasProgress) {
callbacks.log("Phase: " + title, true);
callbacks.logLine("Phase: " + title);
}
};
}
@ -254,13 +254,13 @@ public class ProgramSelector {
try {
String fileName = FindFileHelper.findSrecFile();
callbacks.log("flashSerial " + fileName, true);
callbacks.logLine("flashSerial " + fileName);
OpenbltJni.flashSerial(fileName, port, cb);
callbacks.log("Update completed successfully!", true);
callbacks.logLine("Update completed successfully!");
callbacks.done();
} catch (Throwable e) {
callbacks.log("Error: " + e.toString(), true);
callbacks.logLine("Error: " + e.toString());
callbacks.error();
} finally {
OpenbltJni.stop(cb);

View File

@ -65,7 +65,7 @@ public class StLinkFlasher {
private static void doFlashFirmware(StatusWindow wnd, String fileName) {
if (!new File(fileName).exists()) {
wnd.append(fileName + " not found, cannot proceed !!!", true);
wnd.appendLine(fileName + " not found, cannot proceed !!!");
wnd.setStatus("ERROR");
return;
}
@ -76,18 +76,18 @@ public class StLinkFlasher {
fileName +
" verify reset exit 0x08000000\"", wnd);
} catch (FileNotFoundException e) {
wnd.append(e.toString(), true);
wnd.appendLine(e.toString());
wnd.error();
return;
}
if (error.contains(SUCCESS_MESSAGE_TAG) && !error.toLowerCase().contains(FAILED_MESSAGE_TAG)) {
wnd.append("Flashing looks good!", true);
wnd.appendLine("Flashing looks good!");
sa.stop();
wnd.setStatus(DONE);
wnd.setSuccessState();
} else {
wnd.setErrorState();
wnd.append("!!! FIRMWARE FLASH: DOES NOT LOOK RIGHT !!!", true);
wnd.appendLine("!!! FIRMWARE FLASH: DOES NOT LOOK RIGHT !!!");
}
}

View File

@ -41,9 +41,9 @@ public class StatusWindow implements StatusConsumer, UpdateOperationCallbacks {
content.add(messagesScroll, BorderLayout.CENTER);
content.add(bottomStatusLabel, BorderLayout.SOUTH);
append("Console version " + rusEFIVersion.CONSOLE_VERSION, true);
append("Windows " + System.getProperty("os.version"), true);
append("Bundle " + BundleUtil.readBundleFullNameNotNull(), true);
appendLine("Console version " + rusEFIVersion.CONSOLE_VERSION);
appendLine("Windows " + System.getProperty("os.version"));
appendLine("Bundle " + BundleUtil.readBundleFullNameNotNull());
}
@NotNull
@ -113,7 +113,7 @@ public class StatusWindow implements StatusConsumer, UpdateOperationCallbacks {
SwingUtilities.invokeLater(() -> Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(new StringSelection(logTextArea.getText()), null));
append("hint: error state is already in your clipboard, please use PASTE or Ctrl-V while reporting issues", true);
appendLine("hint: error state is already in your clipboard, please use PASTE or Ctrl-V while reporting issues");
}
public void setStatus(String status) {