Skip to content

Commit 4f7bd3b

Browse files
committed
LoggingPanel: process log messages separately
This enables the LoggingPanel to treat console output and log messages differently.
1 parent c60b561 commit 4f7bd3b

File tree

3 files changed

+99
-23
lines changed

3 files changed

+99
-23
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.ui.swing.console;
33+
34+
import java.io.PrintWriter;
35+
import java.io.StringWriter;
36+
37+
import org.scijava.log.LogLevel;
38+
import org.scijava.log.LogMessage;
39+
40+
/**
41+
* Used by {@link LoggingPanel} to simplify formatting log messages.
42+
*
43+
* @author Matthias Arzt
44+
*/
45+
public class LogFormatter {
46+
47+
public String format(LogMessage message) {
48+
49+
final StringWriter sw = new StringWriter();
50+
final PrintWriter printer = new PrintWriter(sw);
51+
52+
printWithBrackets(printer, message.time().toString());
53+
printWithBrackets(printer, LogLevel.prefix(message.level()));
54+
printWithBrackets(printer, message.source().toString());
55+
printer.println(message.text());
56+
if (message.throwable() != null)
57+
message.throwable().printStackTrace(printer);
58+
59+
return sw.toString();
60+
}
61+
62+
private void printWithBrackets(PrintWriter printer, String prefix) {
63+
printer.print('[');
64+
printer.print(prefix);
65+
printer.print("] ");
66+
}
67+
68+
}

src/main/java/org/scijava/ui/swing/console/LoggingPanel.java

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040

4141
import net.miginfocom.swing.MigLayout;
4242

43-
import org.scijava.console.OutputEvent;
4443
import org.scijava.console.OutputListener;
4544
import org.scijava.log.IgnoreAsCallingClass;
4645
import org.scijava.log.LogListener;
46+
import org.scijava.log.LogMessage;
4747
import org.scijava.log.LogService;
4848
import org.scijava.log.Logger;
4949
import org.scijava.thread.ThreadService;
@@ -62,16 +62,15 @@
6262
* @author Matthias Arzt
6363
*/
6464
@IgnoreAsCallingClass
65-
public class LoggingPanel extends JPanel implements OutputListener
65+
public class LoggingPanel extends JPanel implements LogListener
6666
{
6767
private JTextPane textPane;
6868
private JScrollPane scrollPane;
6969

7070
private StyledDocument doc;
71-
private Style stdoutLocal;
72-
private Style stderrLocal;
73-
private Style stdoutGlobal;
74-
private Style stderrGlobal;
71+
private Style defaultStyle;
72+
73+
private final LogFormatter formatter = new LogFormatter();
7574

7675
private final ThreadService threadService;
7776

@@ -84,16 +83,24 @@ public void clear() {
8483
textPane.setText("");
8584
}
8685

86+
// -- LogListener methods --
87+
8788
@Override
88-
public void outputOccurred(OutputEvent event) {
89+
public void messageLogged(LogMessage message) {
90+
appendText(formatter.format(message), defaultStyle);
91+
}
92+
93+
// -- Helper methods --
94+
95+
private void appendText(final String text, final Style style) {
8996
threadService.queue(new Runnable() {
9097

9198
@Override
9299
public void run() {
93100
final boolean atBottom =
94101
StaticSwingUtils.isScrolledToBottom(scrollPane);
95102
try {
96-
doc.insertString(doc.getLength(), event.getOutput(), getStyle(event));
103+
doc.insertString(doc.getLength(), text, style);
97104
}
98105
catch (final BadLocationException exc) {
99106
throw new RuntimeException(exc);
@@ -112,10 +119,7 @@ private synchronized void initGui() {
112119

113120
doc = textPane.getStyledDocument();
114121

115-
stdoutLocal = createStyle("stdoutLocal", null, Color.black, null, null);
116-
stderrLocal = createStyle("stderrLocal", null, Color.red, null, null);
117-
stdoutGlobal = createStyle("stdoutGlobal", stdoutLocal, null, null, true);
118-
stderrGlobal = createStyle("stderrGlobal", stderrLocal, null, null, true);
122+
defaultStyle = createStyle("stdoutLocal", null, Color.black, null, null);
119123

120124
// NB: We wrap the JTextPane in a JPanel to disable
121125
// the text pane's intelligent line wrapping behavior.
@@ -138,7 +142,6 @@ private synchronized void initGui() {
138142

139143
add(scrollPane);
140144
}
141-
// -- Helper methods --
142145

143146
private Style createStyle(final String name, final Style parent,
144147
final Color foreground, final Boolean bold, final Boolean italic)
@@ -150,14 +153,9 @@ private Style createStyle(final String name, final Style parent,
150153
return style;
151154
}
152155

153-
private Style getStyle(final OutputEvent event) {
154-
final boolean stderr = event.getSource() == OutputEvent.Source.STDERR;
155-
final boolean contextual = event.isContextual();
156-
if (stderr) return contextual ? stderrLocal : stderrGlobal;
157-
return contextual ? stdoutLocal : stdoutGlobal;
158-
}
156+
// -- Helper methods - testing --
159157

160-
public JTextPane getTextPane() {
158+
JTextPane getTextPane() {
161159
return textPane;
162160
}
163161
}

src/main/java/org/scijava/ui/swing/console/SwingConsolePane.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232

3333
import java.awt.Component;
3434

35-
import javax.swing.JPanel;
36-
import javax.swing.JTextPane;
35+
import javax.swing.*;
3736

3837
import net.miginfocom.swing.MigLayout;
3938

4039
import org.scijava.Context;
4140
import org.scijava.console.OutputEvent;
41+
import org.scijava.log.LogService;
4242
import org.scijava.plugin.Parameter;
4343
import org.scijava.thread.ThreadService;
4444
import org.scijava.ui.console.AbstractConsolePane;
@@ -54,8 +54,13 @@ public class SwingConsolePane extends AbstractConsolePane<JPanel> {
5454
@Parameter
5555
private ThreadService threadService;
5656

57+
@Parameter
58+
private LogService logService;
59+
5760
private ConsolePanel consolePanel;
5861

62+
private LoggingPanel loggingPanel;
63+
5964
/**
6065
* The console pane's containing window; e.g., a {@link javax.swing.JFrame} or
6166
* {@link javax.swing.JInternalFrame}.
@@ -121,8 +126,13 @@ private ConsolePanel consolePanel() {
121126
private synchronized void initLoggingPanel() {
122127
if (consolePanel != null) return;
123128
consolePanel = new ConsolePanel(threadService);
129+
loggingPanel = new LoggingPanel(threadService);
130+
logService.addLogListener(loggingPanel);
124131
component = new JPanel(new MigLayout("", "[grow]", "[grow]"));
125-
component.add(consolePanel, "grow");
132+
JTabbedPane tabs = new JTabbedPane();
133+
tabs.addTab("Console", consolePanel);
134+
tabs.addTab("Log Messages", loggingPanel);
135+
component.add(tabs, "grow");
126136
}
127137

128138
// -- Helper methods - testing --

0 commit comments

Comments
 (0)