From 67732c0ed60dfc6133534998f6acef31c3e12c3b Mon Sep 17 00:00:00 2001 From: Eugene Kudelevsky Date: Sun, 13 Sep 2009 15:16:00 +0400 Subject: [PATCH] extend API of LogConsole --- .../intellij/diagnostic/logging/LogConsole.java | 4 + .../diagnostic/logging/IndependentLogFilter.java | 18 + .../{LogConsoleImpl.java => LogConsoleBase.java} | 390 ++++++++------- .../diagnostic/logging/LogConsoleImpl.java | 556 ++------------------- .../diagnostic/logging/LogConsolePreferences.java | 8 +- .../diagnostic/logging/StandartLogFilter.java | 18 - .../impl/ui/DebuggerLogConsoleManagerBase.java | 42 +- 7 files changed, 306 insertions(+), 730 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/diagnostic/logging/IndependentLogFilter.java copy platform/lang-impl/src/com/intellij/diagnostic/logging/{LogConsoleImpl.java => LogConsoleBase.java} (67%) rewrite platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsoleImpl.java (97%) delete mode 100644 platform/lang-impl/src/com/intellij/diagnostic/logging/StandartLogFilter.java diff --git a/platform/lang-api/src/com/intellij/diagnostic/logging/LogConsole.java b/platform/lang-api/src/com/intellij/diagnostic/logging/LogConsole.java index 6d71c1e6bd..c01c9a8bc3 100644 --- a/platform/lang-api/src/com/intellij/diagnostic/logging/LogConsole.java +++ b/platform/lang-api/src/com/intellij/diagnostic/logging/LogConsole.java @@ -11,4 +11,8 @@ public interface LogConsole { boolean isShowStandardFilters(); void setShowStandardFilters(boolean showStandardFilters); + + void registerLogFilter(LogFilter filter); + + void unregisterLogFilter(LogFilter filter); } diff --git a/platform/lang-impl/src/com/intellij/diagnostic/logging/IndependentLogFilter.java b/platform/lang-impl/src/com/intellij/diagnostic/logging/IndependentLogFilter.java new file mode 100644 index 0000000000..bf46fff547 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/diagnostic/logging/IndependentLogFilter.java @@ -0,0 +1,18 @@ +package com.intellij.diagnostic.logging; + +/** + * Created by IntelliJ IDEA. + * User: Eugene.Kudelevsky + * Date: Sep 11, 2009 + * Time: 2:34:25 PM + * To change this template use File | Settings | File Templates. + */ +public abstract class IndependentLogFilter extends LogFilter { + protected IndependentLogFilter(String name) { + super(name); + } + + public abstract void selectFilter(); + + public abstract boolean isSelected(); +} diff --git a/platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsoleImpl.java b/platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsoleBase.java similarity index 67% copy from platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsoleImpl.java copy to platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsoleBase.java index 7120575136..92831bb5e6 100644 --- a/platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsoleImpl.java +++ b/platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsoleBase.java @@ -24,62 +24,110 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; import javax.swing.event.ChangeEvent; -import javax.swing.event.ChangeListener; import java.awt.*; -import java.awt.event.ActionListener; import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.io.*; import java.util.ArrayList; import java.util.List; /** - * User: anna - * Date: Apr 19, 2005 + * Created by IntelliJ IDEA. + * User: Eugene.Kudelevsky + * Date: Sep 11, 2009 + * Time: 9:26:11 PM + * To change this template use File | Settings | File Templates. */ -public abstract class LogConsoleImpl extends AdditionalTabComponent - implements LogConsole, ChangeListener, LogConsolePreferences.FilterListener { +public abstract class LogConsoleBase extends AdditionalTabComponent implements LogConsole, LogConsolePreferences.FilterListener { + private static final Logger LOG = Logger.getInstance("com.intellij.diagnostic.logging.LogConsoleImpl"); + private ConsoleView myConsole; private final LightProcessHandler myProcessHandler = new LightProcessHandler(); private ReaderThread myReaderThread; - private final long mySkippedContents; - private StringBuffer myOriginalDocument = null; - private String myPrevType = null; private String myLineUnderSelection = null; private int myLineOffset = -1; - - /*private FilterComponent myFilter = new FilterComponent("LOG_FILTER_HISTORY", 5) { - public void filter() { - getPreferences().updateCustomFilter(getFilter()); - } - };*/ - private LogContentPreprocessor myContentPreprocessor; private boolean myShowStandardFilters = true; - private String myTitle = null; private final Project myProject; - private final String myPath; private boolean myWasInitialized; private final JPanel myTopComponent = new JPanel(new BorderLayout()); private ActionGroup myActions; private final boolean myBuildInActions; + private final List myLogFilters = new ArrayList(); - public LogConsoleImpl(Project project, File file, long skippedContents, String title, final boolean buildInActions) { + /*private final FilterComponent myFilter = new FilterComponent("LOG_FILTER_HISTORY", 5) { + public void filter() { + getPreferences().updateCustomFilter(getFilter()); + } + };*/ + + public LogConsoleBase(Project project, @Nullable Reader reader, String title, final boolean buildInActions) { super(new BorderLayout()); - mySkippedContents = skippedContents; myTitle = title; + myReaderThread = new ReaderThread(reader); myProject = project; - myPath = file.getAbsolutePath(); myBuildInActions = buildInActions; - myReaderThread = new ReaderThread(file); TextConsoleBuilder builder = TextConsoleBuilderFactory.getInstance().createBuilder(project); myConsole = builder.getConsole(); myConsole.attachToProcess(myProcessHandler); getPreferences().addFilterListener(this); } + @SuppressWarnings({"IOResourceOpenedButNotSafelyClosed"}) + public LogConsoleBase(Project project, File file, long skippedContents, String title, boolean buildInActions) { + this(project, getReader(file, skippedContents), title, buildInActions); + } + + public LogConsoleBase(Project project, Reader reader, long skippedContents, String title, boolean buildInActions) { + this(project, getReader(reader, skippedContents), title, buildInActions); + } + + @Nullable + private static Reader getReader(Reader reader, long skippedContents) { + reader = new BufferedReader(reader); + try { + reader.skip(skippedContents); + } + catch (IOException e) { + reader = null; + } + return reader; + } + + @Nullable + private static Reader getReader(File file, long skippedContents) { + Reader reader = null; + try { + try { + final FileInputStream inputStream = new FileInputStream(file); + reader = new BufferedReader(new InputStreamReader(inputStream)); + if (file.length() >= skippedContents) { //do not skip forward + inputStream.skip(skippedContents); + } + } + catch (FileNotFoundException e) { + if (FileUtil.createIfDoesntExist(file)) { + reader = new BufferedReader(new FileReader(file)); + } + } + } + catch (Throwable e) { + reader = null; + } + return reader; + } + + public void registerLogFilter(LogFilter filter) { + myLogFilters.add(filter); + } + + public void unregisterLogFilter(LogFilter filter) { + myLogFilters.remove(filter); + } + public LogContentPreprocessor getContentPreprocessor() { return myContentPreprocessor; } @@ -151,7 +199,6 @@ public abstract class LogConsoleImpl extends AdditionalTabComponent return myActions; } - public void onFilterStateChange(final LogFilter filter) { filterConsoleOutput(new Condition() { public boolean value(final String line) { @@ -200,26 +247,17 @@ public abstract class LogConsoleImpl extends AdditionalTabComponent return myTitle; } - @Nullable - public String getTooltip() { - return myPath; - } - - public String getPath() { - return myPath; - } - public void dispose() { getPreferences().removeFilterListener(this); - if (myReaderThread != null && myReaderThread.myFileStream != null) { + if (myReaderThread != null && myReaderThread.myReader != null) { myReaderThread.stopRunning(); try { - myReaderThread.myFileStream.close(); + myReaderThread.myReader.close(); } catch (IOException e) { LOG.warn(e); } - myReaderThread.myFileStream = null; + myReaderThread.myReader = null; myReaderThread = null; } if (myConsole != null) { @@ -239,10 +277,10 @@ public abstract class LogConsoleImpl extends AdditionalTabComponent } } - private void addMessage(final String text) { + protected void addMessage(final String text) { if (text == null) return; if (myContentPreprocessor != null) { - final List fragments = myContentPreprocessor.parseLogLine(text + "\n"); + final java.util.List fragments = myContentPreprocessor.parseLogLine(text + "\n"); myOriginalDocument = getOriginalDocument(); for (LogFragment fragment : fragments) { myProcessHandler.notifyTextAvailable(fragment.getText(), fragment.getOutputType()); @@ -270,7 +308,7 @@ public abstract class LogConsoleImpl extends AdditionalTabComponent } } - private LogConsolePreferences getPreferences() { + protected LogConsolePreferences getPreferences() { return LogConsolePreferences.getInstance(myProject); } @@ -321,13 +359,7 @@ public abstract class LogConsoleImpl extends AdditionalTabComponent int offset = 0; boolean caretPositioned = false; for (String line : lines) { - final String contentType = LogConsolePreferences.getType(line); - if (isApplicable.value(line)) { - myConsole.print(line + "\n", contentType != null - ? LogConsolePreferences.getContentType(contentType) - : (myPrevType == LogConsolePreferences.ERROR - ? ConsoleViewContentType.ERROR_OUTPUT - : ConsoleViewContentType.NORMAL_OUTPUT)); + if (printMessageToConsole(line, isApplicable)) { if (!caretPositioned) { if (Comparing.strEqual(myLineUnderSelection, line)) { caretPositioned = true; @@ -338,14 +370,147 @@ public abstract class LogConsoleImpl extends AdditionalTabComponent } } } - if (contentType != null) { - myPrevType = contentType; - } } myConsole.scrollTo(offset); } } + private boolean printMessageToConsole(String line, Condition isApplicable) { + if (myContentPreprocessor != null) { + List fragments = myContentPreprocessor.parseLogLine(line + '\n'); + for (LogFragment fragment : fragments) { + ConsoleViewContentType consoleViewType = ConsoleViewContentType.getConsoleViewType(fragment.getOutputType()); + if (consoleViewType != null) { + myConsole.print(fragment.getText(), consoleViewType); + } + } + return true; + } + else { + final String contentType = LogConsolePreferences.getType(line); + boolean applicable = isApplicable.value(line); + if (applicable) { + myConsole.print(line + "\n", contentType != null + ? LogConsolePreferences.getContentType(contentType) + : (myPrevType == LogConsolePreferences.ERROR + ? ConsoleViewContentType.ERROR_OUTPUT + : ConsoleViewContentType.NORMAL_OUTPUT)); + } + if (contentType != null) { + myPrevType = contentType; + } + return applicable; + } + } + + public ActionGroup getToolbarActions() { + return getOrCreateActions(); + } + + public String getToolbarPlace() { + return ActionPlaces.UNKNOWN; + } + + public JComponent getToolbarContextComponent() { + return myConsole.getComponent(); + } + + public JComponent getPreferredFocusableComponent() { + return myConsole.getPreferredFocusableComponent(); + } + + public String getTitle() { + return myTitle; + } + + private abstract class MyFilter extends IndependentLogFilter { + private final LogConsolePreferences myPrefs; + + protected MyFilter(String name, LogConsolePreferences prefs) { + super(name); + myPrefs = prefs; + } + + public boolean isAcceptable(String line) { + return myPrefs.isApplicable(line, myPrevType); + } + } + + private List getLogFilters(final LogConsolePreferences prefs) { + final ArrayList filters = new ArrayList(); + if (myShowStandardFilters) { + addStandartFilters(filters, prefs); + } + filters.addAll(myLogFilters); + filters.addAll(prefs.getRegisteredLogFilters()); + return filters; + } + + private void addStandartFilters(ArrayList filters, final LogConsolePreferences prefs) { + filters.add(new MyFilter(DiagnosticBundle.message("log.console.filter.show.all"), prefs) { + @Override + public void selectFilter() { + prefs.FILTER_ERRORS = false; + prefs.FILTER_INFO = false; + prefs.FILTER_WARNINGS = false; + } + + @Override + public boolean isSelected() { + return !prefs.FILTER_ERRORS && !prefs.FILTER_INFO && !prefs.FILTER_WARNINGS; + } + }); + filters.add(new MyFilter(DiagnosticBundle.message("log.console.filter.show.errors.and.warnings"), prefs) { + @Override + public void selectFilter() { + prefs.FILTER_ERRORS = false; + prefs.FILTER_INFO = true; + prefs.FILTER_WARNINGS = false; + } + + @Override + public boolean isSelected() { + return !prefs.FILTER_ERRORS && prefs.FILTER_INFO && !prefs.FILTER_WARNINGS; + } + }); + filters.add(new MyFilter(DiagnosticBundle.message("log.console.filter.show.errors"), prefs) { + @Override + public void selectFilter() { + prefs.FILTER_ERRORS = false; + prefs.FILTER_INFO = true; + prefs.FILTER_WARNINGS = true; + } + + @Override + public boolean isSelected() { + return !prefs.FILTER_ERRORS && prefs.FILTER_INFO && prefs.FILTER_WARNINGS; + } + }); + } + + public JComponent getSearchComponent() { + final LogConsolePreferences prefs = getPreferences(); + List filters = getLogFilters(prefs); + final JComboBox combo = new JComboBox(filters.toArray(new LogFilter[filters.size()])); + for (LogFilter filter : filters) { + if (prefs.isFilterSelected(filter)) { + combo.setSelectedItem(filter); + break; + } + } + combo.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + final LogFilter filter = (LogFilter)combo.getSelectedItem(); + prefs.selectOnlyFilter(filter); + } + }); + return combo; + } + + public boolean isContentBuiltIn() { + return myBuildInActions; + } + private static class LightProcessHandler extends ProcessHandler { protected void destroyProcessImpl() { throw new UnsupportedOperationException(); @@ -365,40 +530,22 @@ public abstract class LogConsoleImpl extends AdditionalTabComponent } } - private static final Logger LOG = Logger.getInstance("com.intellij.diagnostic.logging.LogConsoleImpl"); - - private class ReaderThread implements Runnable { - private BufferedReader myFileStream; + protected class ReaderThread implements Runnable { + private BufferedReader myReader; private boolean myRunning = false; - @SuppressWarnings({"IOResourceOpenedButNotSafelyClosed"}) - public ReaderThread(File file) { - try { - try { - final FileInputStream inputStream = new FileInputStream(file); - myFileStream = new BufferedReader(new InputStreamReader(inputStream)); - if (file.length() >= mySkippedContents) { //do not skip forward - inputStream.skip(mySkippedContents); - } - } - catch (FileNotFoundException e) { - if (!FileUtil.createIfDoesntExist(file)) return; - myFileStream = new BufferedReader(new FileReader(file)); - } - } - catch (Throwable e) { - myFileStream = null; - } + public ReaderThread(@Nullable Reader reader) { + myReader = reader != null ? new BufferedReader(reader) : null; } public void run() { - if (myFileStream == null) return; + if (myReader == null) return; while (myRunning) { try { int i = 0; while (i++ < 100) { - if (myRunning && myFileStream != null && myFileStream.ready()) { - addMessage(myFileStream.readLine()); + if (myRunning && myReader != null && myReader.ready()) { + addMessage(myReader.readLine()); } else { break; @@ -412,7 +559,7 @@ public abstract class LogConsoleImpl extends AdditionalTabComponent LOG.error(e); } catch (InterruptedException e) { - Disposer.dispose(LogConsoleImpl.this); + Disposer.dispose(LogConsoleBase.this); } } } @@ -428,99 +575,4 @@ public abstract class LogConsoleImpl extends AdditionalTabComponent } } } - - public ActionGroup getToolbarActions() { - return getOrCreateActions(); - } - - public String getToolbarPlace() { - return ActionPlaces.UNKNOWN; - } - - public JComponent getToolbarContextComponent() { - return myConsole.getComponent(); - } - - public JComponent getPreferredFocusableComponent() { - return myConsole.getPreferredFocusableComponent(); - } - - private List getLogFilters(final LogConsolePreferences prefs) { - abstract class MyFilter extends StandartLogFilter { - protected MyFilter(String name) { - super(name); - } - - public boolean isAcceptable(String line) { - return prefs.isApplicable(line, myPrevType); - } - } - final ArrayList filters = new ArrayList(); - if (myShowStandardFilters) { - filters.add(new MyFilter(DiagnosticBundle.message("log.console.filter.show.all")) { - @Override - public void selectFilter(LogConsolePreferences prefs) { - prefs.FILTER_ERRORS = false; - prefs.FILTER_INFO = false; - prefs.FILTER_WARNINGS = false; - } - - @Override - public boolean isSelected(LogConsolePreferences prefs) { - return !prefs.FILTER_ERRORS && !prefs.FILTER_INFO && !prefs.FILTER_WARNINGS; - } - }); - filters.add(new MyFilter(DiagnosticBundle.message("log.console.filter.show.errors.and.warnings")) { - @Override - public void selectFilter(LogConsolePreferences prefs) { - prefs.FILTER_ERRORS = false; - prefs.FILTER_INFO = true; - prefs.FILTER_WARNINGS = false; - } - - @Override - public boolean isSelected(LogConsolePreferences prefs) { - return !prefs.FILTER_ERRORS && prefs.FILTER_INFO && !prefs.FILTER_WARNINGS; - } - }); - filters.add(new MyFilter(DiagnosticBundle.message("log.console.filter.show.errors")) { - @Override - public void selectFilter(LogConsolePreferences prefs) { - prefs.FILTER_ERRORS = false; - prefs.FILTER_INFO = true; - prefs.FILTER_WARNINGS = true; - } - - @Override - public boolean isSelected(LogConsolePreferences prefs) { - return !prefs.FILTER_ERRORS && prefs.FILTER_INFO && prefs.FILTER_WARNINGS; - } - }); - } - filters.addAll(prefs.getRegisteredLogFilters()); - return filters; - } - - public JComponent getSearchComponent() { - final LogConsolePreferences prefs = getPreferences(); - List filters = getLogFilters(prefs); - final JComboBox combo = new JComboBox(filters.toArray(new LogFilter[filters.size()])); - for (LogFilter filter : filters) { - if (prefs.isFilterSelected(filter)) { - combo.setSelectedItem(filter); - break; - } - } - combo.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - LogFilter filter = (LogFilter)combo.getSelectedItem(); - prefs.selectOnlyFilter(filter); - } - }); - return combo; - } - - public boolean isContentBuiltIn() { - return myBuildInActions; - } } diff --git a/platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsoleImpl.java b/platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsoleImpl.java dissimilarity index 97% index 7120575136..d06229b2ac 100644 --- a/platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsoleImpl.java +++ b/platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsoleImpl.java @@ -1,526 +1,30 @@ -package com.intellij.diagnostic.logging; - -import com.intellij.diagnostic.DiagnosticBundle; -import com.intellij.execution.filters.TextConsoleBuilder; -import com.intellij.execution.filters.TextConsoleBuilderFactory; -import com.intellij.execution.process.ProcessAdapter; -import com.intellij.execution.process.ProcessEvent; -import com.intellij.execution.process.ProcessHandler; -import com.intellij.execution.process.ProcessOutputTypes; -import com.intellij.execution.ui.ConsoleView; -import com.intellij.execution.ui.ConsoleViewContentType; -import com.intellij.openapi.actionSystem.*; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Comparing; -import com.intellij.openapi.util.Condition; -import com.intellij.openapi.util.Disposer; -import com.intellij.openapi.util.io.FileUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import javax.swing.*; -import javax.swing.event.ChangeEvent; -import javax.swing.event.ChangeListener; -import java.awt.*; -import java.awt.event.ActionListener; -import java.awt.event.ActionEvent; -import java.io.*; -import java.util.ArrayList; -import java.util.List; - -/** - * User: anna - * Date: Apr 19, 2005 - */ -public abstract class LogConsoleImpl extends AdditionalTabComponent - implements LogConsole, ChangeListener, LogConsolePreferences.FilterListener { - private ConsoleView myConsole; - private final LightProcessHandler myProcessHandler = new LightProcessHandler(); - private ReaderThread myReaderThread; - private final long mySkippedContents; - - private StringBuffer myOriginalDocument = null; - - private String myPrevType = null; - private String myLineUnderSelection = null; - private int myLineOffset = -1; - - /*private FilterComponent myFilter = new FilterComponent("LOG_FILTER_HISTORY", 5) { - public void filter() { - getPreferences().updateCustomFilter(getFilter()); - } - };*/ - - private LogContentPreprocessor myContentPreprocessor; - private boolean myShowStandardFilters = true; - - private String myTitle = null; - private final Project myProject; - private final String myPath; - private boolean myWasInitialized; - private final JPanel myTopComponent = new JPanel(new BorderLayout()); - private ActionGroup myActions; - private final boolean myBuildInActions; - - public LogConsoleImpl(Project project, File file, long skippedContents, String title, final boolean buildInActions) { - super(new BorderLayout()); - mySkippedContents = skippedContents; - myTitle = title; - myProject = project; - myPath = file.getAbsolutePath(); - myBuildInActions = buildInActions; - myReaderThread = new ReaderThread(file); - TextConsoleBuilder builder = TextConsoleBuilderFactory.getInstance().createBuilder(project); - myConsole = builder.getConsole(); - myConsole.attachToProcess(myProcessHandler); - getPreferences().addFilterListener(this); - } - - public LogContentPreprocessor getContentPreprocessor() { - return myContentPreprocessor; - } - - public void setContentPreprocessor(final LogContentPreprocessor contentPreprocessor) { - myContentPreprocessor = contentPreprocessor; - } - - public boolean isShowStandardFilters() { - return myShowStandardFilters; - } - - public void setShowStandardFilters(final boolean showStandardFilters) { - myShowStandardFilters = showStandardFilters; - } - - @SuppressWarnings({"NonStaticInitializer"}) - private JComponent createToolbar() { - - /*myFilter.reset(); - myFilter.setSelectedItem(registrar.CUSTOM_FILTER != null ? registrar.CUSTOM_FILTER : ""); - new AnAction() { - { - registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.SHIFT_DOWN_MASK)), - LogConsoleImpl.this); - } - - public void actionPerformed(final AnActionEvent e) { - myFilter.requestFocusInWindow(); - } - };*/ - - if (myBuildInActions) { - final JComponent tbComp = - ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, getOrCreateActions(), true).getComponent(); - myTopComponent.add(tbComp, BorderLayout.CENTER); - //myTopComponent.add(myFilter, BorderLayout.EAST); - } - - - return myTopComponent; - } - - public ActionGroup getOrCreateActions() { - if (myActions != null) return myActions; - DefaultActionGroup group = new DefaultActionGroup(); - - final AnAction[] actions = myConsole.createConsoleActions(); - for (AnAction action : actions) { - group.add(action); - } - - group.addSeparator(); - - /*for (final LogFilter filter : filters) { - group.add(new ToggleAction(filter.getName(), filter.getName(), filter.getIcon()) { - public boolean isSelected(AnActionEvent e) { - return prefs.isFilterSelected(filter); - } - - public void setSelected(AnActionEvent e, boolean state) { - prefs.setFilterSelected(filter, state); - } - }); - }*/ - - myActions = group; - - return myActions; - } - - - public void onFilterStateChange(final LogFilter filter) { - filterConsoleOutput(new Condition() { - public boolean value(final String line) { - return filter.isAcceptable(line); - } - }); - } - - public void onTextFilterChange() { - filterConsoleOutput(new Condition() { - public boolean value(final String line) { - return getPreferences().isApplicable(line, myPrevType); - } - }); - } - - @NotNull - public JComponent getComponent() { - if (!myWasInitialized) { - myWasInitialized = true; - add(myConsole.getComponent(), BorderLayout.CENTER); - add(createToolbar(), BorderLayout.NORTH); - } - return this; - } - - public abstract boolean isActive(); - - public void activate() { - if (myReaderThread == null) return; - if (isActive() && !myReaderThread.myRunning) { - //myFilter.setSelectedItem(getPreferences().CUSTOM_FILTER); - myReaderThread.startRunning(); - ApplicationManager.getApplication().executeOnPooledThread(myReaderThread); - } - else if (!isActive() && myReaderThread.myRunning) { - myReaderThread.stopRunning(); - } - } - - public void stateChanged(final ChangeEvent e) { - activate(); - } - - public String getTabTitle() { - return myTitle; - } - - @Nullable - public String getTooltip() { - return myPath; - } - - public String getPath() { - return myPath; - } - - public void dispose() { - getPreferences().removeFilterListener(this); - if (myReaderThread != null && myReaderThread.myFileStream != null) { - myReaderThread.stopRunning(); - try { - myReaderThread.myFileStream.close(); - } - catch (IOException e) { - LOG.warn(e); - } - myReaderThread.myFileStream = null; - myReaderThread = null; - } - if (myConsole != null) { - Disposer.dispose(myConsole); - myConsole = null; - } - /*if (myFilter != null) { - myFilter.dispose(); - myFilter = null; - }*/ - myOriginalDocument = null; - } - - private void stopRunning() { - if (myReaderThread != null && !isActive()) { - myReaderThread.stopRunning(); - } - } - - private void addMessage(final String text) { - if (text == null) return; - if (myContentPreprocessor != null) { - final List fragments = myContentPreprocessor.parseLogLine(text + "\n"); - myOriginalDocument = getOriginalDocument(); - for (LogFragment fragment : fragments) { - myProcessHandler.notifyTextAvailable(fragment.getText(), fragment.getOutputType()); - if (myOriginalDocument != null) { - myOriginalDocument.append(fragment.getText()); - } - } - } - else { - final String key = LogConsolePreferences.getType(text); - if (getPreferences().isApplicable(text, myPrevType)) { - myProcessHandler.notifyTextAvailable(text + "\n", key != null - ? LogConsolePreferences.getProcessOutputTypes(key) - : (myPrevType == LogConsolePreferences.ERROR - ? ProcessOutputTypes.STDERR - : ProcessOutputTypes.STDOUT)); - } - if (key != null) { - myPrevType = key; - } - myOriginalDocument = getOriginalDocument(); - if (myOriginalDocument != null) { - myOriginalDocument.append(text).append("\n"); - } - } - } - - private LogConsolePreferences getPreferences() { - return LogConsolePreferences.getInstance(myProject); - } - - public void attachStopLogConsoleTrackingListener(final ProcessHandler process) { - if (process != null) { - final ProcessAdapter stopListener = new ProcessAdapter() { - public void processTerminated(final ProcessEvent event) { - process.removeProcessListener(this); - stopRunning(); - } - }; - process.addProcessListener(stopListener); - } - } - - private StringBuffer getOriginalDocument() { - if (myOriginalDocument == null) { - final Editor editor = getEditor(); - if (editor != null) { - myOriginalDocument = new StringBuffer(editor.getDocument().getText()); - } - } - return myOriginalDocument; - } - - @Nullable - private Editor getEditor() { - return myConsole != null ? (Editor)((DataProvider)myConsole).getData(DataConstants.EDITOR) : null; - } - - private void filterConsoleOutput(Condition isApplicable) { - myOriginalDocument = getOriginalDocument(); - if (myOriginalDocument != null) { - final Editor editor = getEditor(); - LOG.assertTrue(editor != null); - final Document document = editor.getDocument(); - final int caretOffset = editor.getCaretModel().getOffset(); - if (caretOffset > -1) { - int line = document.getLineNumber(caretOffset); - if (line > -1 && line < document.getLineCount()) { - final int startOffset = document.getLineStartOffset(line); - myLineUnderSelection = document.getText().substring(startOffset, document.getLineEndOffset(line)); - myLineOffset = caretOffset - startOffset; - } - } - myConsole.clear(); - final String[] lines = myOriginalDocument.toString().split("\n"); - int offset = 0; - boolean caretPositioned = false; - for (String line : lines) { - final String contentType = LogConsolePreferences.getType(line); - if (isApplicable.value(line)) { - myConsole.print(line + "\n", contentType != null - ? LogConsolePreferences.getContentType(contentType) - : (myPrevType == LogConsolePreferences.ERROR - ? ConsoleViewContentType.ERROR_OUTPUT - : ConsoleViewContentType.NORMAL_OUTPUT)); - if (!caretPositioned) { - if (Comparing.strEqual(myLineUnderSelection, line)) { - caretPositioned = true; - offset += myLineOffset != -1 ? myLineOffset : 0; - } - else { - offset += line.length() + 1; - } - } - } - if (contentType != null) { - myPrevType = contentType; - } - } - myConsole.scrollTo(offset); - } - } - - private static class LightProcessHandler extends ProcessHandler { - protected void destroyProcessImpl() { - throw new UnsupportedOperationException(); - } - - protected void detachProcessImpl() { - throw new UnsupportedOperationException(); - } - - public boolean detachIsDefault() { - return false; - } - - @Nullable - public OutputStream getProcessInput() { - return null; - } - } - - private static final Logger LOG = Logger.getInstance("com.intellij.diagnostic.logging.LogConsoleImpl"); - - private class ReaderThread implements Runnable { - private BufferedReader myFileStream; - private boolean myRunning = false; - - @SuppressWarnings({"IOResourceOpenedButNotSafelyClosed"}) - public ReaderThread(File file) { - try { - try { - final FileInputStream inputStream = new FileInputStream(file); - myFileStream = new BufferedReader(new InputStreamReader(inputStream)); - if (file.length() >= mySkippedContents) { //do not skip forward - inputStream.skip(mySkippedContents); - } - } - catch (FileNotFoundException e) { - if (!FileUtil.createIfDoesntExist(file)) return; - myFileStream = new BufferedReader(new FileReader(file)); - } - } - catch (Throwable e) { - myFileStream = null; - } - } - - public void run() { - if (myFileStream == null) return; - while (myRunning) { - try { - int i = 0; - while (i++ < 100) { - if (myRunning && myFileStream != null && myFileStream.ready()) { - addMessage(myFileStream.readLine()); - } - else { - break; - } - } - synchronized (this) { - wait(100); - } - } - catch (IOException e) { - LOG.error(e); - } - catch (InterruptedException e) { - Disposer.dispose(LogConsoleImpl.this); - } - } - } - - public void startRunning() { - myRunning = true; - } - - public void stopRunning() { - myRunning = false; - synchronized (this) { - notifyAll(); - } - } - } - - public ActionGroup getToolbarActions() { - return getOrCreateActions(); - } - - public String getToolbarPlace() { - return ActionPlaces.UNKNOWN; - } - - public JComponent getToolbarContextComponent() { - return myConsole.getComponent(); - } - - public JComponent getPreferredFocusableComponent() { - return myConsole.getPreferredFocusableComponent(); - } - - private List getLogFilters(final LogConsolePreferences prefs) { - abstract class MyFilter extends StandartLogFilter { - protected MyFilter(String name) { - super(name); - } - - public boolean isAcceptable(String line) { - return prefs.isApplicable(line, myPrevType); - } - } - final ArrayList filters = new ArrayList(); - if (myShowStandardFilters) { - filters.add(new MyFilter(DiagnosticBundle.message("log.console.filter.show.all")) { - @Override - public void selectFilter(LogConsolePreferences prefs) { - prefs.FILTER_ERRORS = false; - prefs.FILTER_INFO = false; - prefs.FILTER_WARNINGS = false; - } - - @Override - public boolean isSelected(LogConsolePreferences prefs) { - return !prefs.FILTER_ERRORS && !prefs.FILTER_INFO && !prefs.FILTER_WARNINGS; - } - }); - filters.add(new MyFilter(DiagnosticBundle.message("log.console.filter.show.errors.and.warnings")) { - @Override - public void selectFilter(LogConsolePreferences prefs) { - prefs.FILTER_ERRORS = false; - prefs.FILTER_INFO = true; - prefs.FILTER_WARNINGS = false; - } - - @Override - public boolean isSelected(LogConsolePreferences prefs) { - return !prefs.FILTER_ERRORS && prefs.FILTER_INFO && !prefs.FILTER_WARNINGS; - } - }); - filters.add(new MyFilter(DiagnosticBundle.message("log.console.filter.show.errors")) { - @Override - public void selectFilter(LogConsolePreferences prefs) { - prefs.FILTER_ERRORS = false; - prefs.FILTER_INFO = true; - prefs.FILTER_WARNINGS = true; - } - - @Override - public boolean isSelected(LogConsolePreferences prefs) { - return !prefs.FILTER_ERRORS && prefs.FILTER_INFO && prefs.FILTER_WARNINGS; - } - }); - } - filters.addAll(prefs.getRegisteredLogFilters()); - return filters; - } - - public JComponent getSearchComponent() { - final LogConsolePreferences prefs = getPreferences(); - List filters = getLogFilters(prefs); - final JComboBox combo = new JComboBox(filters.toArray(new LogFilter[filters.size()])); - for (LogFilter filter : filters) { - if (prefs.isFilterSelected(filter)) { - combo.setSelectedItem(filter); - break; - } - } - combo.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - LogFilter filter = (LogFilter)combo.getSelectedItem(); - prefs.selectOnlyFilter(filter); - } - }); - return combo; - } - - public boolean isContentBuiltIn() { - return myBuildInActions; - } -} +package com.intellij.diagnostic.logging; + +import com.intellij.openapi.project.Project; +import org.jetbrains.annotations.Nullable; + +import java.io.File; + +/** + * User: anna + * Date: Apr 19, 2005 + */ +public abstract class LogConsoleImpl extends LogConsoleBase { + private final String myPath; + + public LogConsoleImpl(Project project, File file, long skippedContents, String title, final boolean buildInActions) { + super(project, file, skippedContents, title, buildInActions); + myPath = file.getAbsolutePath(); + } + + + @Nullable + public String getTooltip() { + return myPath; + } + + public String getPath() { + return myPath; + } + +} diff --git a/platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsolePreferences.java b/platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsolePreferences.java index 052eed1133..d0286d325c 100644 --- a/platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsolePreferences.java +++ b/platform/lang-impl/src/com/intellij/diagnostic/logging/LogConsolePreferences.java @@ -193,15 +193,15 @@ public class LogConsolePreferences extends LogFilterRegistrar { if (isSelected != null) { return isSelected.booleanValue(); } - if (filter instanceof StandartLogFilter) { - return ((StandartLogFilter)filter).isSelected(this); + if (filter instanceof IndependentLogFilter) { + return ((IndependentLogFilter)filter).isSelected(); } return false; } public void setFilterSelected(LogFilter filter, boolean state) { - if (filter instanceof StandartLogFilter) { - ((StandartLogFilter)filter).selectFilter(this); + if (filter instanceof IndependentLogFilter) { + ((IndependentLogFilter)filter).selectFilter(); } else if (myRegisteredLogFilters.containsKey(filter)) { myRegisteredLogFilters.put(filter, state); diff --git a/platform/lang-impl/src/com/intellij/diagnostic/logging/StandartLogFilter.java b/platform/lang-impl/src/com/intellij/diagnostic/logging/StandartLogFilter.java deleted file mode 100644 index 0181d728c2..0000000000 --- a/platform/lang-impl/src/com/intellij/diagnostic/logging/StandartLogFilter.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.intellij.diagnostic.logging; - -/** - * Created by IntelliJ IDEA. - * User: Eugene.Kudelevsky - * Date: Sep 11, 2009 - * Time: 2:34:25 PM - * To change this template use File | Settings | File Templates. - */ -public abstract class StandartLogFilter extends LogFilter { - protected StandartLogFilter(String name) { - super(name); - } - - public abstract void selectFilter(LogConsolePreferences preferences); - - public abstract boolean isSelected(LogConsolePreferences preferences); -} diff --git a/platform/xdebugger-impl/src/com/intellij/xdebugger/impl/ui/DebuggerLogConsoleManagerBase.java b/platform/xdebugger-impl/src/com/intellij/xdebugger/impl/ui/DebuggerLogConsoleManagerBase.java index 5f1346527d..292722b3e4 100644 --- a/platform/xdebugger-impl/src/com/intellij/xdebugger/impl/ui/DebuggerLogConsoleManagerBase.java +++ b/platform/xdebugger-impl/src/com/intellij/xdebugger/impl/ui/DebuggerLogConsoleManagerBase.java @@ -1,9 +1,6 @@ package com.intellij.xdebugger.impl.ui; -import com.intellij.diagnostic.logging.AdditionalTabComponent; -import com.intellij.diagnostic.logging.DebuggerLogConsoleManager; -import com.intellij.diagnostic.logging.LogConsoleImpl; -import com.intellij.diagnostic.logging.LogFilesManager; +import com.intellij.diagnostic.logging.*; import com.intellij.execution.configurations.RunConfigurationBase; import com.intellij.execution.configurations.RunProfile; import com.intellij.execution.process.ProcessHandler; @@ -23,9 +20,11 @@ import com.intellij.ui.content.ContentManagerEvent; import com.intellij.ui.content.ContentManagerListener; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.io.File; +import java.io.Reader; import java.util.HashMap; import java.util.Map; @@ -67,28 +66,40 @@ public abstract class DebuggerLogConsoleManagerBase implements DebuggerLogConsol myEnvironment = env; } - public void addLogConsole(final String name, final String path, final long skippedContent, Icon icon) { + public void addLogConsole(String name, Reader reader, long skippedContent, Icon icon) { final Ref content = new Ref(); + addLogConsole(new LogConsoleBase(myProject, reader, skippedContent, name, false) { + public boolean isActive() { + final Content logContent = content.get(); + return logContent != null && logContent.isSelected(); + } + }, icon, content); + } - final LogConsoleImpl log = new LogConsoleImpl(myProject, new File(path), skippedContent, name, false) { + public void addLogConsole(final String name, final String path, final long skippedContent, Icon icon) { + final Ref content = new Ref(); + addLogConsole(new LogConsoleImpl(myProject, new File(path), skippedContent, name, false) { public boolean isActive() { final Content logContent = content.get(); return logContent != null && logContent.isSelected(); } - }; - log.attachStopLogConsoleTrackingListener(getRunContentDescriptor().getProcessHandler()); + }, icon, content); + } + + private void addLogConsole(final LogConsoleBase logConsole, Icon icon, Ref content) { + logConsole.attachStopLogConsoleTrackingListener(getRunContentDescriptor().getProcessHandler()); // Attach custom log handlers if (myEnvironment != null && myEnvironment.getRunProfile() instanceof RunConfigurationBase) { - ((RunConfigurationBase)myEnvironment.getRunProfile()).customizeLogConsole(log); + ((RunConfigurationBase)myEnvironment.getRunProfile()).customizeLogConsole(logConsole); } - content.set(addLogComponent(log, icon)); + content.set(addLogComponent(logConsole, icon)); final ContentManagerAdapter l = new ContentManagerAdapter() { public void selectionChanged(final ContentManagerEvent event) { - log.activate(); + logConsole.activate(); } }; - myContentListeners.put(log, l); + myContentListeners.put(logConsole, l); getUi().addListener(l, this); } @@ -96,6 +107,11 @@ public abstract class DebuggerLogConsoleManagerBase implements DebuggerLogConsol addLogConsole(name, path, skippedContent, DEFAULT_TAB_COMPONENT_ICON); } + @Nullable + public static String getLogContentId(@NotNull String tabTitle) { + return "Log-" + tabTitle; + } + public void removeLogConsole(final String path) { LogConsoleImpl componentToRemove = null; for (AdditionalTabComponent tabComponent : myAdditionalContent.keySet()) { @@ -126,7 +142,7 @@ public abstract class DebuggerLogConsoleManagerBase implements DebuggerLogConsol } private Content addLogComponent(AdditionalTabComponent tabComponent, Icon icon) { - @NonNls final String id = "Log-" + tabComponent.getTabTitle(); + @NonNls final String id = getLogContentId(tabComponent.getTabTitle()); return addLogComponent(tabComponent, id, icon); } -- 2.11.4.GIT