From 2b36fde70a90f2ea8990eb58bedc07414deedde4 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Sun, 16 Aug 2020 12:04:40 +0200 Subject: [PATCH] logs --- .../litvinovg/w2phtml/W2PHTMLExtension.java | 2 +- .../w2phtml/gui/ConfigurationWindow.java | 4 +- .../litvinovg/w2phtml/gui/LogAppender.java | 136 +++++++++++++----- .../gui/{Document.java => ODTDocument.java} | 6 +- .../w2phtml/rdf/DocumentStructureTests.java | 21 --- 5 files changed, 110 insertions(+), 59 deletions(-) rename src/main/java/pro/litvinovg/w2phtml/gui/{Document.java => ODTDocument.java} (95%) delete mode 100644 src/test/java/w2phtml/rdf/DocumentStructureTests.java diff --git a/src/main/java/pro/litvinovg/w2phtml/W2PHTMLExtension.java b/src/main/java/pro/litvinovg/w2phtml/W2PHTMLExtension.java index 8354937..4652a3b 100644 --- a/src/main/java/pro/litvinovg/w2phtml/W2PHTMLExtension.java +++ b/src/main/java/pro/litvinovg/w2phtml/W2PHTMLExtension.java @@ -5,7 +5,7 @@ import com.sun.star.uno.XComponentContext; import pro.litvinovg.w2phtml.gui.ConfigurationWindow; import pro.litvinovg.w2phtml.gui.ConversionExecutor; -import pro.litvinovg.w2phtml.gui.Document; +import pro.litvinovg.w2phtml.gui.ODTDocument; import com.sun.star.lib.uno.helper.Factory; diff --git a/src/main/java/pro/litvinovg/w2phtml/gui/ConfigurationWindow.java b/src/main/java/pro/litvinovg/w2phtml/gui/ConfigurationWindow.java index 904ba8a..867dd24 100644 --- a/src/main/java/pro/litvinovg/w2phtml/gui/ConfigurationWindow.java +++ b/src/main/java/pro/litvinovg/w2phtml/gui/ConfigurationWindow.java @@ -77,7 +77,7 @@ public class ConfigurationWindow extends JFrame { createEvents(); } - public ConfigurationWindow(XComponentContext context, Document doc) { + public ConfigurationWindow(XComponentContext context, ODTDocument doc) { this.context = context; fileName = doc.getFileName(); setTitle("Converter from ODT to HTML, ePub and RDF"); @@ -954,7 +954,7 @@ public class ConfigurationWindow extends JFrame { } public static void runGUI(XComponentContext context) { - Document doc = new Document(context); + ODTDocument doc = new ODTDocument(context); if (singleFrame != null) { singleFrame.dispose(); } diff --git a/src/main/java/pro/litvinovg/w2phtml/gui/LogAppender.java b/src/main/java/pro/litvinovg/w2phtml/gui/LogAppender.java index d20c7e8..79567ee 100644 --- a/src/main/java/pro/litvinovg/w2phtml/gui/LogAppender.java +++ b/src/main/java/pro/litvinovg/w2phtml/gui/LogAppender.java @@ -3,41 +3,113 @@ package pro.litvinovg.w2phtml.gui; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import javax.swing.JTextPane; +import javax.swing.SwingUtilities; +import javax.swing.text.AbstractDocument; +import javax.swing.text.BadLocationException; +import javax.swing.text.Document; +import javax.swing.text.Element; +import javax.swing.text.SimpleAttributeSet; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.PatternLayout; +import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.AppenderBase; -public class LogAppender extends AppenderBase { +public class LogAppender extends AppenderBase { - private Map eventMap = new ConcurrentHashMap(); - private String prefix; - private static LogAppender LOG_APPENDER_INSTANCE = null; + private static SimpleAttributeSet ERROR_ATT, WARN_ATT, INFO_ATT, DEBUG_ATT, TRACE_ATT, RESTO_ATT; + private PatternLayout patternLayout; - public String getPrefix() { - return prefix; - } - - public void setPrefix(String prefix) { - this.prefix = prefix; - } @Override - protected void append(ILoggingEvent event) { - if (prefix == null || "".equals(prefix)) { - addError("Prefix is not set for LogAppender."); - return; - } - eventMap.put(prefix + System.currentTimeMillis(), event); - } - - public LogAppender createInstance() { - if (LOG_APPENDER_INSTANCE == null) { - LOG_APPENDER_INSTANCE = new LogAppender(); - } - return LOG_APPENDER_INSTANCE; - } - - public void putEvent(String key, String value) { - eventMap.put(key, value); - } - public Map getEventMap() { - return eventMap; - } + protected void append(ch.qos.logback.classic.spi.ILoggingEvent event) { + // Formata mensagem do log + String formattedMsg = patternLayout.doLayout( event); + + // Forma segura de atualizar o JTextpane + SwingUtilities.invokeLater(() -> { + // Alias para o JTextPane no frame da aplicação + JTextPane textPane = null;//App.MAIN_FORM.getTextPane(); + + try { + // Trunca linhas para economizar memória + // Quando atingir 2000 linhas, eu quero que + // apague as 500 primeiras linhas + int limite = 1000; + int apaga = 200; + if (textPane.getDocument().getDefaultRootElement().getElementCount() > limite) { + int end = getLineEndOffset(textPane, apaga); + replaceRange(textPane, null, 0, end); + } + + // Decide qual atributo (estilo) devo usar de acordo com o nível o log + if (event.getLevel() == Level.ERROR) + textPane.getDocument().insertString(textPane.getDocument().getLength(), formattedMsg, ERROR_ATT); + else if (event.getLevel() == Level.WARN) + textPane.getDocument().insertString(textPane.getDocument().getLength(), formattedMsg, WARN_ATT); + else if (event.getLevel() == Level.INFO) + textPane.getDocument().insertString(textPane.getDocument().getLength(), formattedMsg, INFO_ATT); + else if (event.getLevel() == Level.DEBUG) + textPane.getDocument().insertString(textPane.getDocument().getLength(), formattedMsg, DEBUG_ATT); + else if (event.getLevel() == Level.TRACE) + textPane.getDocument().insertString(textPane.getDocument().getLength(), formattedMsg, TRACE_ATT); + else + textPane.getDocument().insertString(textPane.getDocument().getLength(), formattedMsg, RESTO_ATT); + + } catch (BadLocationException e) { + // Faz nada + } + + // Vai para a última linha + textPane.setCaretPosition(textPane.getDocument().getLength()); + }); + } + + public void start() { + patternLayout = new PatternLayout(); + patternLayout.setContext(getContext()); + patternLayout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"); + patternLayout.start(); + + super.start(); + } + + private int getLineCount(JTextPane textPane) { + return textPane.getDocument().getDefaultRootElement().getElementCount(); + } + + + private void replaceRange(JTextPane textPane, String str, int start, int end) throws IllegalArgumentException { + if (end < start) { + throw new IllegalArgumentException("end before start"); + } + Document doc = textPane.getDocument(); + if (doc != null) { + try { + if (doc instanceof AbstractDocument) { + ((AbstractDocument)doc).replace(start, end - start, str, null); + } + else { + doc.remove(start, end - start); + doc.insertString(start, str, null); + } + } catch (BadLocationException e) { + throw new IllegalArgumentException(e.getMessage()); + } + } +} + private int getLineEndOffset(JTextPane textPane, int line) throws BadLocationException { + int lineCount = getLineCount(textPane); + if (line < 0) { + throw new BadLocationException("Negative line", -1); + } else if (line >= lineCount) { + throw new BadLocationException("No such line", textPane.getDocument().getLength()+1); + } else { + Element map = textPane.getDocument().getDefaultRootElement(); + Element lineElem = map.getElement(line); + int endOffset = lineElem.getEndOffset(); + // hide the implicit break at the end of the document + return ((line == lineCount - 1) ? (endOffset - 1) : endOffset); + } +} } diff --git a/src/main/java/pro/litvinovg/w2phtml/gui/Document.java b/src/main/java/pro/litvinovg/w2phtml/gui/ODTDocument.java similarity index 95% rename from src/main/java/pro/litvinovg/w2phtml/gui/Document.java rename to src/main/java/pro/litvinovg/w2phtml/gui/ODTDocument.java index 9f00306..12c2a02 100644 --- a/src/main/java/pro/litvinovg/w2phtml/gui/Document.java +++ b/src/main/java/pro/litvinovg/w2phtml/gui/ODTDocument.java @@ -23,9 +23,9 @@ import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; import com.sun.star.util.XModifiable; -public class Document { +public class ODTDocument { - private static final Logger logger = LoggerFactory.getLogger(Document.class); + private static final Logger logger = LoggerFactory.getLogger(ODTDocument.class); private XComponentContext context; @@ -41,7 +41,7 @@ public class Document { private XDispatchProvider dispatchProvider; XTextDocument textDocument; - public Document(XComponentContext componentContext) { + public ODTDocument(XComponentContext componentContext) { if (componentContext != null) { context = componentContext; multiComponentFactory = context.getServiceManager(); diff --git a/src/test/java/w2phtml/rdf/DocumentStructureTests.java b/src/test/java/w2phtml/rdf/DocumentStructureTests.java deleted file mode 100644 index 05e29b4..0000000 --- a/src/test/java/w2phtml/rdf/DocumentStructureTests.java +++ /dev/null @@ -1,21 +0,0 @@ -package w2phtml.rdf; - -import static org.junit.Assert.*; - -import java.util.Vector; - -import org.junit.Test; - -import w2phtml.xhtml.XhtmlDocument; - -public class DocumentStructureTests { - - @Test - public void DocumentStructureExists() { - - String fileName = ""; - Vector files = new Vector(); - DocumentStructure docStructure = new DocumentStructure(files , fileName); - } - -}