|
| 1 | +/* |
| 2 | + * Copy from /netbeans/platform/o.n.core/src/org/netbeans/core/actions/LogAction.java |
| 3 | + */ |
| 4 | +package org.netbeans.modules.python.log; |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | +import java.util.*; |
| 8 | +import java.util.logging.Level; |
| 9 | +import java.util.logging.Logger; |
| 10 | +import org.openide.util.*; |
| 11 | +import org.openide.windows.*; |
| 12 | + |
| 13 | +/** |
| 14 | + * Connects the output stream of a file to the IDE output window. |
| 15 | + * |
| 16 | + */ |
| 17 | +public class LogViewerSupport implements Runnable { |
| 18 | + |
| 19 | + private static final RequestProcessor RP = new RequestProcessor(LogViewerSupport.class); |
| 20 | + boolean shouldStop = false; |
| 21 | + FileInputStream filestream = null; |
| 22 | + BufferedReader ins; |
| 23 | + InputOutput io; |
| 24 | + File fileName; |
| 25 | + String ioName; |
| 26 | + int lines; |
| 27 | + Ring ring; |
| 28 | + private final RequestProcessor.Task task = RP.create(this); |
| 29 | + |
| 30 | + /** |
| 31 | + * Connects a given process to the output window. Returns immediately, but |
| 32 | + * threads are started that copy streams of the process to/from the output |
| 33 | + * window. |
| 34 | + * |
| 35 | + * @param fileName process whose streams to connect to the output window |
| 36 | + * @param ioName name of the output window tab to use |
| 37 | + */ |
| 38 | + public LogViewerSupport(final File fileName, final String ioName) { |
| 39 | + |
| 40 | + this.fileName = fileName; |
| 41 | + this.ioName = ioName; |
| 42 | + } |
| 43 | + |
| 44 | + private void init() { |
| 45 | + final int LINES = 2000; |
| 46 | + final int OLD_LINES = 2000; |
| 47 | + ring = new Ring(OLD_LINES); |
| 48 | + String line; |
| 49 | + |
| 50 | + // Read the log file without |
| 51 | + // displaying everything |
| 52 | + try { |
| 53 | + while ((line = ins.readLine()) != null) { |
| 54 | + ring.add(line); |
| 55 | + } // end of while ((line = ins.readLine()) != null) |
| 56 | + } catch (IOException e) { |
| 57 | + Logger.getLogger(LogViewerSupport.class.getName()).log(Level.INFO, null, e); |
| 58 | + } // end of try-catch |
| 59 | + |
| 60 | + // Now show the last OLD_LINES |
| 61 | + lines = ring.output(); |
| 62 | + ring.setMaxCount(LINES); |
| 63 | + } |
| 64 | + |
| 65 | + public void run() { |
| 66 | + final int MAX_LINES = 10000; |
| 67 | + String line; |
| 68 | + |
| 69 | + shouldStop = io.isClosed(); |
| 70 | + |
| 71 | + if (!shouldStop) { |
| 72 | + try { |
| 73 | + if (lines >= MAX_LINES) { |
| 74 | + io.getOut().reset(); |
| 75 | + lines = ring.output(); |
| 76 | + } // end of if (lines >= MAX_LINES) |
| 77 | + |
| 78 | + while ((line = ins.readLine()) != null) { |
| 79 | + if ((line = ring.add(line)) != null) { |
| 80 | + io.getOut().println(line); |
| 81 | + lines++; |
| 82 | + } // end of if ((line = ring.add(line)) != null) |
| 83 | + } |
| 84 | + |
| 85 | + } catch (IOException e) { |
| 86 | + Logger.getLogger(LogViewerSupport.class.getName()).log(Level.INFO, null, e); |
| 87 | + } |
| 88 | + task.schedule(10000); |
| 89 | + } else { |
| 90 | + ///System.out.println("end of infinite loop for log viewer\n\n\n\n"); |
| 91 | + stopUpdatingLogViewer(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /* display the log viewer dialog |
| 96 | + * |
| 97 | + **/ |
| 98 | + public void showLogViewer() throws IOException { |
| 99 | + shouldStop = false; |
| 100 | + io = IOProvider.getDefault().getIO(ioName, false); |
| 101 | + io.getOut().reset(); |
| 102 | + io.select(); |
| 103 | + filestream = new FileInputStream(fileName); |
| 104 | + ins = new BufferedReader(new InputStreamReader(filestream)); |
| 105 | + RP.post(new Runnable() { |
| 106 | + @Override |
| 107 | + public void run() { |
| 108 | + init(); |
| 109 | + task.schedule(0); |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + /* stop to update the log viewer dialog |
| 115 | + * |
| 116 | + **/ |
| 117 | + public void stopUpdatingLogViewer() { |
| 118 | + try { |
| 119 | + ins.close(); |
| 120 | + filestream.close(); |
| 121 | + io.closeInputOutput(); |
| 122 | + io.setOutputVisible(false); |
| 123 | + } catch (IOException e) { |
| 124 | + Logger.getLogger(LogViewerSupport.class.getName()).log(Level.INFO, null, e); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private class Ring { |
| 129 | + |
| 130 | + private int maxCount; |
| 131 | + private int count; |
| 132 | + private LinkedList<String> anchor; |
| 133 | + |
| 134 | + public Ring(int max) { |
| 135 | + maxCount = max; |
| 136 | + count = 0; |
| 137 | + anchor = new LinkedList<String>(); |
| 138 | + } |
| 139 | + |
| 140 | + public String add(String line) { |
| 141 | + if (line == null || line.equals("")) { // NOI18N |
| 142 | + return null; |
| 143 | + } // end of if (line == null || line.equals("")) |
| 144 | + |
| 145 | + while (count >= maxCount) { |
| 146 | + anchor.removeFirst(); |
| 147 | + count--; |
| 148 | + } // end of while (count >= maxCount) |
| 149 | + |
| 150 | + anchor.addLast(line); |
| 151 | + count++; |
| 152 | + |
| 153 | + return line; |
| 154 | + } |
| 155 | + |
| 156 | + public void setMaxCount(int newMax) { |
| 157 | + maxCount = newMax; |
| 158 | + } |
| 159 | + |
| 160 | + public int output() { |
| 161 | + int i = 0; |
| 162 | + for (String s : anchor) { |
| 163 | + io.getOut().println(s); |
| 164 | + i++; |
| 165 | + } |
| 166 | + |
| 167 | + return i; |
| 168 | + } |
| 169 | + |
| 170 | + public void reset() { |
| 171 | + anchor = new LinkedList<String>(); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments