From 41c5cc84c9d88e1519d27ac438ff04357648553b Mon Sep 17 00:00:00 2001 From: Artur Wozniak Date: Tue, 23 Jan 2018 21:50:54 +0000 Subject: [PATCH] #88 added new clear log file button --- .../icons/active/clear_active.gif | Bin 0 -> 583 bytes .../icons/passive/clear_passive.gif | Bin 0 -> 332 bytes .../logviewer/plugin/ILogViewerConstants.java | 2 + .../eclipse/logviewer/plugin/LogViewer.java | 71 +++++++++++++++--- .../plugin/LogfileUIMessages.properties | 5 ++ .../eclipse/logviewer/plugin/UIImages.java | 5 +- .../plugin/action/FileClearAction.java | 41 ++++++++++ .../delegate/FileClearActionDelegate.java | 44 +++++++++++ .../plugin/file/BackgroundReader.java | 15 +++- .../logviewer/plugin/file/ConsoleTail.java | 30 ++++---- .../logviewer/plugin/file/FileTail.java | 33 ++++++-- .../plugin/file/document/LogDocument.java | 4 + 12 files changed, 217 insertions(+), 33 deletions(-) create mode 100644 de.anbos.eclipse.logviewer.plugin/icons/active/clear_active.gif create mode 100644 de.anbos.eclipse.logviewer.plugin/icons/passive/clear_passive.gif create mode 100644 de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/action/FileClearAction.java create mode 100644 de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/action/delegate/FileClearActionDelegate.java diff --git a/de.anbos.eclipse.logviewer.plugin/icons/active/clear_active.gif b/de.anbos.eclipse.logviewer.plugin/icons/active/clear_active.gif new file mode 100644 index 0000000000000000000000000000000000000000..8d1061b80348a9b567a07042d7089c43f35964df GIT binary patch literal 583 zcmV-N0=WH0Nk%w1VGsZi0B`^RA^8LW004XdEC2ui01yBW0EPen0Liy@%D8yRxOK*} zbIG}U^YZG_xOK+0cE-7Y_xb$WzJAfScEq!Jzq50~wu1Ec{E?-{g_^&3j<#NQqu#`a z+rE9yvTwh#dBC%T^7i}Y?Dgg9^yBIC;O6kVu6ez&hVbh$61^Wo_8;OFw+=kng>@!aI_wW@x%u8ZsO`mv~hvaF5i@cQ28 z^xfw3-R1M#dr(f`EX4ii(PYf`YKBj_2moSch_hIx5; zgoK2wsgUID`Pt<2*yHlp;_}zw@z&w-)!^{1w7!pzkd>8{jEszdfq`~*cB!Y6;_LW` zoxy*UyLODUZG*0uo1ln@h>eYngM))|a&o7nmEh|5*5dNj;qlbq@YCP$(%tX1yTpQn zgocKOaBy&+osOfTmVA79e}8{=c64iNYj<~dmzR%jZf$IAY-wp}ZEbC6XlMWc00000 z00000000N1fP4ajgoAvCh=&3S1_q1<7Xpchiwc&O76J~Fd>INOBPJ9I6&4pClNk^O zWdg~G)t39G{iAw*FHZ$hhT@) zBF(#UM__ee<$OzJI_)trI&0|YLx)*3;m&1cUts=-aM-2MktR(UdFIR=vXkYALpVp^ V)S0-4A}2(9_8_XG@uJ5-06V}cM1BAO literal 0 HcmV?d00001 diff --git a/de.anbos.eclipse.logviewer.plugin/icons/passive/clear_passive.gif b/de.anbos.eclipse.logviewer.plugin/icons/passive/clear_passive.gif new file mode 100644 index 0000000000000000000000000000000000000000..0d2571ea2ae0ff701dba8a03b83d00d3d1938d65 GIT binary patch literal 332 zcmV-S0ki%`Nk%w1VGsZi0B`^RA^8LW002G!EC2ui01yBW0EGYm0M5?N&(F`y%*@cx z(DCu{&CSjA_4U-$)XU4u^z`(@!^6G3y|%WtsHmve*x2sw?(OaE?Ck97>gvhK$@22@ zz`(${xw*2kva74B$jHdb%F65O>*?v~$H&LU#>VjQ@aX91!NI|>u&}YQvG4EiwY9aZ ztgPqf=jP_-xVX5yyu7!!x2meD#l^+HzrVV=y0o;k#Kgqq<>k@Q(X+F&rlzLC!osbs zt*@`Isi~=?q@<;#rJ|yu00000000JQK;PgoC=v(0q%t{x91aj?aj6_UuZIVbDm)BU zs}|&lTsEUoY`F>ngIi(9nu)GQ=R*0r#g|j94|YW>NrZV{dvIZIJ}XgyfqHH&bZ&8N eaFm60kpr1wg_ek!J$#y6J}o^;s;I6$K>#~`dcWuZ literal 0 HcmV?d00001 diff --git a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/ILogViewerConstants.java b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/ILogViewerConstants.java index 9bcb4d3..1e4e99c 100644 --- a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/ILogViewerConstants.java +++ b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/ILogViewerConstants.java @@ -54,6 +54,8 @@ public interface ILogViewerConstants { public static final String IMG_COPY_ACTIVE = "IMG_COPY_ACTIVE"; //$NON-NLS-1$ public static final String IMG_COPY_PASSIVE = "IMG_COPY_PASSIVE"; //$NON-NLS-1$ public static final String IMG_LOG_VIEWER = "IMG_LOG_VIEWER"; + public static final String IMG_FILE_CLEAR_ACTIVE = "IMG_CLEAR_ACTIVE"; //$NON-NLS-1$ + public static final String IMG_FILE_CLEAR_PASSIVE = "IMG_CLEAR_PASSIVE"; //$NON-NLS-1$ // Preferences ------------------------------------------------------------- diff --git a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/LogViewer.java b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/LogViewer.java index 497e898..09414e5 100644 --- a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/LogViewer.java +++ b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/LogViewer.java @@ -16,6 +16,8 @@ import java.io.File; import java.io.IOException; +import java.io.PrintWriter; +import java.nio.Buffer; import java.util.Hashtable; import java.util.Iterator; import java.util.List; @@ -42,6 +44,7 @@ import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.TabFolder; @@ -50,11 +53,17 @@ import org.eclipse.ui.IWorkbenchActionConstants; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.console.ConsolePlugin; +import org.eclipse.ui.console.IConsole; +import org.eclipse.ui.console.IOConsole; +import org.eclipse.ui.console.IOConsoleOutputStream; +import org.eclipse.ui.console.TextConsole; import org.eclipse.ui.part.ViewPart; import de.anbos.eclipse.logviewer.plugin.LogFile.LogFileType; import de.anbos.eclipse.logviewer.plugin.action.ClearHistoryAction; import de.anbos.eclipse.logviewer.plugin.action.CloseAllFilesViewAction; +import de.anbos.eclipse.logviewer.plugin.action.FileClearAction; import de.anbos.eclipse.logviewer.plugin.action.FileCloseViewAction; import de.anbos.eclipse.logviewer.plugin.action.FileEncondingViewAction; import de.anbos.eclipse.logviewer.plugin.action.FileOpenViewAction; @@ -66,17 +75,13 @@ import de.anbos.eclipse.logviewer.plugin.action.StopTailOnCurrentFileViewAction; import de.anbos.eclipse.logviewer.plugin.action.TabRenameAction; import de.anbos.eclipse.logviewer.plugin.action.delegate.FileOpenViewActionDelegate; +import de.anbos.eclipse.logviewer.plugin.file.FileTail; import de.anbos.eclipse.logviewer.plugin.file.document.LogDocument; import de.anbos.eclipse.logviewer.plugin.preferences.FileHistoryTracker; import de.anbos.eclipse.logviewer.plugin.preferences.PreferenceValueConverter; import de.anbos.eclipse.logviewer.plugin.ui.menu.LocalPullDownMenu; import de.anbos.eclipse.logviewer.plugin.viewer.LogFileViewer; -import org.eclipse.ui.console.ConsolePlugin; -import org.eclipse.ui.console.IConsole; -import org.eclipse.ui.console.IOConsole; -import org.eclipse.ui.console.IOConsoleOutputStream; - public class LogViewer extends ViewPart { // Attribute --------------------------------------------------------------- @@ -108,6 +113,7 @@ public class LogViewer extends ViewPart { private StopTailOnAllFileViewAction stopTailOnAllFiles; private FileEncondingViewAction fileEncodingAction; private TabRenameAction tabRenameAction; + private FileClearAction fileClearAction; private int monitorCounter; private int monitorCounterMax; @@ -172,6 +178,7 @@ public void closeCurrentLogFile() { } catch(IOException e) { logger.logError("unable to remove the current; active tab"); //$NON-NLS-1$ } + int index = tabfolder.getSelectionIndex(); getSelectedItem().dispose(); if (!greyAllOutIfNoFiles()) { @@ -183,7 +190,38 @@ public void closeCurrentLogFile() { } } - public void closeAllLogFiles() { + public void clearCurrentLogFile() { + try { + LogFileType type = getSelectedTab().getDocument().getFile().getType(); + if (type == LogFileType.LOGFILE_ECLIPSE_CONSOLE) { + + final IConsole con = getSelectedTab().getDocument().getReader().getConsoleTail().getConsole(); + if (con instanceof TextConsole) { + Display.getDefault().syncExec(new Runnable() { + public void run() { + ((TextConsole) con).clearConsole(); + } + }); + } else { + logger.logWarning("Console" + "[ " + con.getName() + " ]" + " clear not supported");//$NON-NLS-1$ + } + } else { + FileTail file = getSelectedTab().getDocument().getReader().getFileTail(); + Buffer buffer = file.getBuffer(); + if (buffer != null) { + file.stopFileMapping(buffer); + } + PrintWriter pw = new PrintWriter(getCurrentLogFilePath()); + pw.write(" "); + pw.close(); + } + } catch (Exception e) { + logger.logError("unable to clear selected file: " + getCurrentLogFilePath()); //$NON-NLS-1$ + } + refreshCurrentLogFile(); + } + + public void closeAllLogFiles() { Iterator keyIterator = logTab.keySet().iterator(); while(keyIterator.hasNext()) { Object key = keyIterator.next(); @@ -264,6 +302,7 @@ boolean greyAllOutIfNoFiles() { startTailOnAllFiles.setEnabled(false); stopTailOnAllFiles.setEnabled(false); tabRenameAction.setEnabled(false); + fileClearAction.setEnabled(false); resetMonitorCounter(); return true; } @@ -352,15 +391,16 @@ public void openLogFile(LogFile file) { if(!logTab.containsKey(key)) { try { if (file.getNamePattern().equals(LogViewerPlugin.getResourceString("logviewer.plugin.console.name"))) { - createConsole(); + createConsole(); } + String encoding = LogViewerPlugin.getDefault().getPreferenceStore().getString(ILogViewerConstants.PREF_ENCODING); - LogDocument document = new LogDocument(file, encoding); - TabItem item = new TabItem(tabfolder, 0); + LogDocument document = new LogDocument(file,encoding); + TabItem item = new TabItem(tabfolder,0); item.setControl(viewer.getControl()); item.setText(file.getNamePattern()); item.setToolTipText(file.getPath()); - logTab.put(key,new LogFileTab(key, item, document)); + logTab.put(key,new LogFileTab(key,item,document)); document.addDocumentListener(documentListener); // restore monitor status @@ -376,6 +416,7 @@ public void openLogFile(LogFile file) { tabRenameAction.setEnabled(true); startTailOnAllFiles.setEnabled(true); stopTailOnAllFiles.setEnabled(true); + fileClearAction.setEnabled(true); } catch(Exception e) { logger.logError("unable to open the selected logfile",e); //$NON-NLS-1$ LogViewerPlugin.getDefault().showErrorMessage(LogViewerPlugin.getResourceString("main.error.open.file",new String[]{file.getPath()})); //$NON-NLS-1$ @@ -446,6 +487,10 @@ public void setCurrentLogFileTabName(String name) { getSelectedTab().getDocument().getFile().setNamePattern(name); } + public String getCurrentLogFilePath() { + return getSelectedTab().getDocument().getFile().getPath(); + } + public void dispose() { viewer.removeListeners(); storeAllCurrentlyOpenFiles(); @@ -504,6 +549,7 @@ private void fillLocalPullDown(IMenuManager manager) { menu.addAction(fileOpenAction); menu.addFilelist(); menu.addAction(clearHistoryAction); + menu.addAction(fileClearAction); menu.addSeparator(); menu.addAction(refreshCurrentFileAction); menu.addAction(startTailOnCurrentFile); @@ -527,6 +573,7 @@ private void fillContextMenu(IMenuManager manager) { manager.add(startTailOnCurrentFile); manager.add(stopTailOnCurrentFile); manager.add(fileCloseAction); + manager.add(fileClearAction); manager.add(new Separator()); manager.add(fileEncodingAction); manager.add(tabRenameAction); @@ -536,6 +583,7 @@ private void fillContextMenu(IMenuManager manager) { private void fillLocalToolBar(IToolBarManager manager) { manager.add(fileOpenAction); manager.add(preferencesAction); + manager.add(fileClearAction); manager.add(new Separator()); manager.add(refreshCurrentFileAction); manager.add(startTailOnCurrentFile); @@ -624,6 +672,9 @@ private void makeActions() { // tab rename action tabRenameAction = new TabRenameAction(this,parent.getShell()); tabRenameAction.setEnabled(false); + // clear file + fileClearAction = new FileClearAction(this,parent.getShell()); + fileClearAction.setEnabled(true); } private void storeAllCurrentlyOpenFiles() { diff --git a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/LogfileUIMessages.properties b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/LogfileUIMessages.properties index 58bd81d..ba43625 100644 --- a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/LogfileUIMessages.properties +++ b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/LogfileUIMessages.properties @@ -50,6 +50,8 @@ menu.findreplace.text=Find menu.findreplace.tooltip=Find any string menu.tabrename.text=Rename tab... menu.tabrename.tooltip=Rename active tab +menu.file.clear.text=Clear +menu.file.clear.tooltip=Clear Logfile main.error.open.file=Unable to open resource {0} @@ -139,6 +141,9 @@ dialog.encoding.title=choose encoding dialog.encoding.defaults.button=Restore Default dialog.tabrename.title=rename tab +dialog.file.clear.title=Confirm Logfile Clear +dialog.file.clear.text=Are you sure you want to clear the log file?\nYou won't be able to revert! + # misc misc.clearhisrory.title=Clear history misc.clearhisrory.text=Do You want to clear the log history? diff --git a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/UIImages.java b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/UIImages.java index 287561a..6bba83d 100644 --- a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/UIImages.java +++ b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/UIImages.java @@ -93,8 +93,11 @@ private static ImageRegistry initializeImageRegistry() { declareRegistryImage(ILogViewerConstants.IMG_RENAME_ACTIVE, "active/rename_active.gif"); //$NON-NLS-1$ declareRegistryImage(ILogViewerConstants.IMG_RENAME_PASSIVE, "passive/rename_passive.gif"); //$NON-NLS-1$ declareRegistryImage(ILogViewerConstants.IMG_COPY_ACTIVE, "active/copy_active.gif"); //$NON-NLS-1$ - declareRegistryImage(ILogViewerConstants.IMG_COPY_PASSIVE, "active/copy_passive.gif"); //$NON-NLS-1$ + declareRegistryImage(ILogViewerConstants.IMG_COPY_PASSIVE, "passive/copy_passive.gif"); //$NON-NLS-1$ + declareRegistryImage(ILogViewerConstants.IMG_FILE_CLEAR_ACTIVE, "active/clear_active.gif"); //$NON-NLS-1$ + declareRegistryImage(ILogViewerConstants.IMG_FILE_CLEAR_PASSIVE, "passive/clear_passive.gif"); //$NON-NLS-1$ declareRegistryImage(ILogViewerConstants.IMG_LOG_VIEWER, "logviewer.gif"); + return imageRegistry; } diff --git a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/action/FileClearAction.java b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/action/FileClearAction.java new file mode 100644 index 0000000..f8cbb72 --- /dev/null +++ b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/action/FileClearAction.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2009 - 2018 by Andre Bossert + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Andre Bossert - initial API and implementation and/or initial documentation + *******************************************************************************/ + +package de.anbos.eclipse.logviewer.plugin.action; + +import org.eclipse.swt.widgets.Shell; + +import de.anbos.eclipse.logviewer.plugin.ILogViewerConstants; +import de.anbos.eclipse.logviewer.plugin.LogViewer; +import de.anbos.eclipse.logviewer.plugin.LogViewerPlugin; +import de.anbos.eclipse.logviewer.plugin.UIImages; +import de.anbos.eclipse.logviewer.plugin.action.delegate.FileClearActionDelegate; + +public class FileClearAction extends AbstractViewAction { + + // Constructor + // ------------------------------------------------------------------ + + public FileClearAction(LogViewer view, Shell shell) { + super(view, shell, new FileClearActionDelegate()); + } + + // Public + // ----------------------------------------------------------------------- + + public void init() { + this.setText(LogViewerPlugin.getResourceString("menu.file.clear.text")); //$NON-NLS-1$ + this.setToolTipText(LogViewerPlugin.getResourceString("menu.file.clear.tooltip")); //$NON-NLS-1$ + this.setImageDescriptor(UIImages.getImageDescriptor(ILogViewerConstants.IMG_FILE_CLEAR_ACTIVE)); + this.setDisabledImageDescriptor(UIImages.getImageDescriptor(ILogViewerConstants.IMG_FILE_CLEAR_PASSIVE)); + } +} diff --git a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/action/delegate/FileClearActionDelegate.java b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/action/delegate/FileClearActionDelegate.java new file mode 100644 index 0000000..07b92e1 --- /dev/null +++ b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/action/delegate/FileClearActionDelegate.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2009 - 2018 by Andre Bossert + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Andre Bossert - initial API and implementation and/or initial documentation + *******************************************************************************/ + +package de.anbos.eclipse.logviewer.plugin.action.delegate; + +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.swt.widgets.Shell; +import de.anbos.eclipse.logviewer.plugin.LogFile.LogFileType; +import de.anbos.eclipse.logviewer.plugin.LogViewer; +import de.anbos.eclipse.logviewer.plugin.LogViewerPlugin; + +public class FileClearActionDelegate implements ILogViewerActionDelegate { + + /* + * (non-Javadoc) + * + * @see + * de.anbos.eclipse.logviewer.plugin.action.ILogfileAction#run(de.anbos. + * eclipse.logviewer.plugin.LogViewer, org.eclipse.swt.widgets.Shell) + */ + public void run(LogViewer view, Shell shell) { + + LogFileType type = view.getCurrentDocument().getFile().getType(); + if (type == LogFileType.LOGFILE_ECLIPSE_CONSOLE) { + view.clearCurrentLogFile(); + } else if (confirmFileClear(shell)) { + view.clearCurrentLogFile(); + } + } + + private boolean confirmFileClear(Shell shell) { + return MessageDialog.openQuestion(shell, LogViewerPlugin.getResourceString("dialog.file.clear.title"), //$NON-NLS-1$ + LogViewerPlugin.getResourceString("dialog.file.clear.text")); //$NON-NLS-1$ + } +} diff --git a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/BackgroundReader.java b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/BackgroundReader.java index 309a931..1842a14 100644 --- a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/BackgroundReader.java +++ b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/BackgroundReader.java @@ -1,15 +1,16 @@ /******************************************************************************* * Copyright (c) 2009 - 2018 by Andre Bossert - * + * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html - * + * * Contributors: * Andre Bossert - initial API and implementation and/or initial documentation *******************************************************************************/ + package de.anbos.eclipse.logviewer.plugin.file; import java.lang.reflect.InvocationTargetException; import java.nio.charset.Charset; @@ -32,7 +33,7 @@ public BackgroundReader(LogFileType type, String path, String namePattern, Chars consoleTail = new ConsoleTail(path, namePattern, listener); } } - + public void setMonitorStatus(boolean monitor) { if (type == LogFileType.LOGFILE_SYSTEM_FILE) { fileTail.setMonitorStatus(monitor); @@ -40,4 +41,12 @@ public void setMonitorStatus(boolean monitor) { consoleTail.setMonitorStatus(monitor); } } + + public FileTail getFileTail() { + return fileTail; + } + + public ConsoleTail getConsoleTail() { + return consoleTail; + } } diff --git a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/ConsoleTail.java b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/ConsoleTail.java index e2c7c74..f681edc 100644 --- a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/ConsoleTail.java +++ b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/ConsoleTail.java @@ -1,11 +1,11 @@ /******************************************************************************* * Copyright (c) 2009 - 2018 by Andre Bossert - * + * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html - * + * * Contributors: * Andre Bossert - initial API and implementation and/or initial documentation *******************************************************************************/ @@ -42,7 +42,7 @@ public class ConsoleTail implements IDocumentListener, Runnable { - private Logger logger; + private Logger logger; private String path; private String namePattern; private IFileChangedListener listener; @@ -90,12 +90,12 @@ public String getPath() { public String getConsolePath(IConsole console) { return console.getClass().toString().replaceFirst("class ", "") + System.getProperty("file.separator") + console.getName(); - } + } public String getNamePattern() { return namePattern; } - + public String getClassName() { int idx = path.indexOf(System.getProperty("file.separator")); return idx != -1 ? path.substring(0, idx) : path; @@ -109,7 +109,7 @@ public void documentChanged(DocumentEvent event) { listener.fileChanged(event.getText().toCharArray(), isFirstTimeRead); isFirstTimeRead = false; } - + public synchronized void run() { isRunning = true; try { @@ -123,7 +123,7 @@ public synchronized void run() { event.fText = doc.get(); documentAboutToBeChanged(event); documentChanged(event); - } + } } else { throw new ThreadInterruptedException("document was null"); //$NON-NLS-1$ } @@ -142,7 +142,7 @@ public synchronized void run() { try { if(doc != null) { doc.removeDocumentListener(this); - isFirstTimeRead = true; + isFirstTimeRead = true; } } catch(Exception e) { // ignore this @@ -178,7 +178,7 @@ private synchronized IDocument openConsole() throws ThreadInterruptedException { } throw new ThreadInterruptedException("no console found"); //$NON-NLS-1$ } - + private IConsole findConsole() throws FileNotFoundException { ConsolePlugin conPlugin = ConsolePlugin.getDefault(); IConsoleManager conMan = conPlugin.getConsoleManager(); @@ -217,12 +217,12 @@ private IConsole createConsole(String className) throws ClassNotFoundException, //return null; } */ - + private IDocument getConsoleDocument() throws FileNotFoundException { if (con != null) { if(con instanceof TextConsole) { return ((TextConsole)con).getDocument(); - } else { + } else { // Now open the view and console in UI-Thread UIJob uiJob = new UIJob("Update UI") { @Override @@ -234,7 +234,7 @@ public IStatus runInUIThread(IProgressMonitor monitor) { e.printStackTrace(); } if (view != null) { - // show it + // show it view.display(con); IViewPart vp =(IViewPart)view; if (vp instanceof PageBookView) { @@ -249,9 +249,13 @@ public IStatus runInUIThread(IProgressMonitor monitor) { }; uiJob.schedule(); if (viewer != null) - return viewer.getDocument(); + return viewer.getDocument(); } } throw new FileNotFoundException("no document found"); } + + public IConsole getConsole() { + return con; + } } diff --git a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/FileTail.java b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/FileTail.java index 849cecd..f6a9841 100644 --- a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/FileTail.java +++ b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/FileTail.java @@ -18,9 +18,11 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.CharBuffer; -import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileChannel.MapMode; import java.nio.charset.Charset; @@ -49,6 +51,7 @@ public class FileTail implements Runnable { private boolean isFirstTimeRead; private int bufferCapacity; + private ByteBuffer mappedBuffer; // Constructor ------------------------------------------------------------- @@ -113,6 +116,11 @@ public synchronized void run() { try { if(file != null) { file.close(); + + if(isRunning==false && mappedBuffer!=null) + { + stopFileMapping(mappedBuffer); + } } } catch(Exception e) { // ignore this @@ -121,6 +129,15 @@ public synchronized void run() { isRunning = false; } + public void stopFileMapping(Buffer buffer)throws Exception { + Method cleaner = buffer.getClass().getMethod("cleaner"); + cleaner.setAccessible(true); + Method clean = Class.forName("sun.misc.Cleaner").getMethod("clean"); + clean.setAccessible(true); + clean.invoke(cleaner.invoke(buffer)); + buffer=null; + } + // Private ----------------------------------------------------------------- private synchronized RandomAccessFile openFile() throws ThreadInterruptedException, FileNotFoundException { @@ -169,17 +186,21 @@ private void read(FileChannel channel) throws IOException { startPosition = endPosition - INITIAL_LOAD_SIZE; size = INITIAL_LOAD_SIZE; } - MappedByteBuffer mappedBuffer = channel.map(MapMode.READ_ONLY, startPosition, size); + mappedBuffer = channel.map(MapMode.READ_ONLY, startPosition, size); CharBuffer mappedChars = decoder.decode(mappedBuffer); channel.position(endPosition); listener.fileChanged(mappedChars.array(), true); } return; } - ByteBuffer buffer = ByteBuffer.allocate(bufferCapacity); - channel.read(buffer); - buffer.flip(); - CharBuffer chars = decoder.decode(buffer); + mappedBuffer = ByteBuffer.allocate(bufferCapacity); + channel.read(mappedBuffer); + mappedBuffer.flip(); + CharBuffer chars = decoder.decode(mappedBuffer); listener.fileChanged(chars.array(),false); } + + public Buffer getBuffer() { + return mappedBuffer; + } } diff --git a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/document/LogDocument.java b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/document/LogDocument.java index 8bf6e16..22b33e6 100644 --- a/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/document/LogDocument.java +++ b/de.anbos.eclipse.logviewer.plugin/src/de/anbos/eclipse/logviewer/plugin/file/document/LogDocument.java @@ -263,4 +263,8 @@ public void propertyChange(PropertyChangeEvent event) { } } } + + public BackgroundReader getReader() { + return reader; + } }