From 07466a4bfa3ab1b292a30f0f711d9180b5ca7ff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A1udio=20Gomes?= Date: Tue, 12 Mar 2024 11:14:27 -0300 Subject: [PATCH] refactor log4j and appender --- cli/src/test/resources/log4j.properties | 8 +++ core/src/main/resources/log4j.properties | 8 +++ core/src/main/resources/log4j.xml | 18 ------- .../pirilampo/gui/form/MainForm.java | 20 ++++++++ .../clagomess/pirilampo/gui/ui/MainUI.java | 12 +++-- .../pirilampo/gui/util/AppenderUtil.java | 50 +++++++++++++++++-- gui/src/main/resources/log4j.properties | 11 ++++ 7 files changed, 99 insertions(+), 28 deletions(-) create mode 100644 cli/src/test/resources/log4j.properties create mode 100644 core/src/main/resources/log4j.properties delete mode 100644 core/src/main/resources/log4j.xml create mode 100644 gui/src/main/resources/log4j.properties diff --git a/cli/src/test/resources/log4j.properties b/cli/src/test/resources/log4j.properties new file mode 100644 index 0000000..dbe95cd --- /dev/null +++ b/cli/src/test/resources/log4j.properties @@ -0,0 +1,8 @@ +# Root logger option +log4j.rootLogger=INFO, console + +# Direct log messages to stdout +log4j.appender.console=org.apache.log4j.ConsoleAppender +log4j.appender.console.Target=System.out +log4j.appender.console.layout=org.apache.log4j.PatternLayout +log4j.appender.console.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss} - %m%n diff --git a/core/src/main/resources/log4j.properties b/core/src/main/resources/log4j.properties new file mode 100644 index 0000000..dbe95cd --- /dev/null +++ b/core/src/main/resources/log4j.properties @@ -0,0 +1,8 @@ +# Root logger option +log4j.rootLogger=INFO, console + +# Direct log messages to stdout +log4j.appender.console=org.apache.log4j.ConsoleAppender +log4j.appender.console.Target=System.out +log4j.appender.console.layout=org.apache.log4j.PatternLayout +log4j.appender.console.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss} - %m%n diff --git a/core/src/main/resources/log4j.xml b/core/src/main/resources/log4j.xml deleted file mode 100644 index 0bb6f99..0000000 --- a/core/src/main/resources/log4j.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - - - diff --git a/gui/src/main/java/com/github/clagomess/pirilampo/gui/form/MainForm.java b/gui/src/main/java/com/github/clagomess/pirilampo/gui/form/MainForm.java index 6011839..230e233 100644 --- a/gui/src/main/java/com/github/clagomess/pirilampo/gui/form/MainForm.java +++ b/gui/src/main/java/com/github/clagomess/pirilampo/gui/form/MainForm.java @@ -7,9 +7,11 @@ import com.github.clagomess.pirilampo.gui.component.ColorChooserComponent; import com.github.clagomess.pirilampo.gui.component.FileChooserComponent; import com.github.clagomess.pirilampo.gui.component.RadioButtonGroupComponent; +import com.github.clagomess.pirilampo.gui.util.AppenderUtil; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; +import java.awt.*; import java.util.Arrays; public class MainForm { @@ -85,5 +87,23 @@ public class MainForm { new RadioButtonGroupComponent.RadioButton<>(LayoutPdfEnum.LANDSCAPE) )); + // progress + public JProgressBar progress = new JProgressBar(){{setStringPainted(true);}}; + public JTextArea console = new JTextArea(){{ + setLineWrap(true); + setWrapStyleWord(true); + setEditable(false); + setFont(new Font( + "Consolas", + this.getFont().getStyle(), + this.getFont().getSize() + )); + + AppenderUtil.setOnChange(text -> { + this.setText(text); + this.setCaretPosition(this.getDocument().getLength()); + }); + }}; + public final JButton btnCompile = new JButton("Compile!"); } diff --git a/gui/src/main/java/com/github/clagomess/pirilampo/gui/ui/MainUI.java b/gui/src/main/java/com/github/clagomess/pirilampo/gui/ui/MainUI.java index a7b1e9c..d5a43a4 100644 --- a/gui/src/main/java/com/github/clagomess/pirilampo/gui/ui/MainUI.java +++ b/gui/src/main/java/com/github/clagomess/pirilampo/gui/ui/MainUI.java @@ -55,13 +55,15 @@ public MainUI() { pTabArtifact.addTab("PDF", pPdf); add(pTabArtifact, "wrap"); - /* JPanel pProgress = new JPanel(new MigLayout("", "[grow,fill]")); pProgress.setBorder(BorderFactory.createTitledBorder("Progress")); - pProgress.add(new JProgressBar(), "wrap"); - pProgress.add(new JTextArea(), "wrap"); - add(pProgress); - */ + pProgress.add(form.progress, "wrap"); + pProgress.add(new JScrollPane( + form.console, + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, + JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS + ), "height 50, wrap"); + add(pProgress, "wrap"); add(form.btnCompile); getRootPane().setDefaultButton(form.btnCompile); diff --git a/gui/src/main/java/com/github/clagomess/pirilampo/gui/util/AppenderUtil.java b/gui/src/main/java/com/github/clagomess/pirilampo/gui/util/AppenderUtil.java index 4fc43c3..8a11fd6 100644 --- a/gui/src/main/java/com/github/clagomess/pirilampo/gui/util/AppenderUtil.java +++ b/gui/src/main/java/com/github/clagomess/pirilampo/gui/util/AppenderUtil.java @@ -1,18 +1,53 @@ package com.github.clagomess.pirilampo.gui.util; +import lombok.Getter; +import lombok.Setter; import org.apache.log4j.AppenderSkeleton; +import org.apache.log4j.Level; import org.apache.log4j.spi.LoggingEvent; +import java.time.Instant; +import java.time.ZoneId; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + public class AppenderUtil extends AppenderSkeleton { + @Getter + private static final List events = Collections.synchronizedList(new LinkedList<>()); + + @Setter + private static OnChangeFI onChange = e -> {}; + + private final AtomicBoolean canOnChange = new AtomicBoolean(true); + @Override protected void append(LoggingEvent event) { - /* - if((event.getLevel() == Level.ERROR || event.getLevel() == Level.WARN) && event.getThrowableInformation() != null){ - ConsoleBind.setLogData(event.getThrowableInformation().getThrowable().getMessage()); + String message; + + if((event.getLevel() == Level.ERROR || event.getLevel() == Level.WARN) && + event.getThrowableInformation() != null + ){ + message = event.getThrowableInformation().getThrowable().getMessage(); }else{ - ConsoleBind.setLogData(event.getRenderedMessage()); + message = event.getRenderedMessage(); + } + + events.add(String.format( + "%s %s: - %s", + event.getLevel(), + Instant.ofEpochMilli(event.getTimeStamp()).atZone(ZoneId.systemDefault()).toLocalDateTime(), + message + )); + + if (events.size() > 10) events.remove(0); + + if (canOnChange.get()) { + canOnChange.set(false); + onChange.change(String.join("\n", events)); + canOnChange.set(true); } - */ } @Override @@ -22,4 +57,9 @@ public void close() {} public boolean requiresLayout() { return false; } + + @FunctionalInterface + public interface OnChangeFI { + void change(String event); + } } diff --git a/gui/src/main/resources/log4j.properties b/gui/src/main/resources/log4j.properties new file mode 100644 index 0000000..3414b31 --- /dev/null +++ b/gui/src/main/resources/log4j.properties @@ -0,0 +1,11 @@ +# Root logger option +log4j.rootLogger=INFO, console, appenderutil + +# Direct log messages to stdout +log4j.appender.console=org.apache.log4j.ConsoleAppender +log4j.appender.console.Target=System.out +log4j.appender.console.layout=org.apache.log4j.PatternLayout +log4j.appender.console.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss} - %m%n + +# To Java +log4j.appender.appenderutil=com.github.clagomess.pirilampo.gui.util.AppenderUtil