Console standard implementable avec un inputloglistener

Contenu du snippet

Cette Console Java permet de recuperer le flux System.out afin de l'afficher avec un autre composant de votre choix.
Il suffit d'implementer une interface InputLogListener avec une seule methode de transmission des messages et le tour est joué. Cela s'avère très pratique pour garder un log complet des messages print de votre code. (à noter que la sortie d'erreur system.err n'est pas incluse, à vous de l'ajouter dans le code si vous le désirez)

b23:production 2007 GNU/GPL

Source / Exemple :


/*

  • Console.java
*
  • Created on 14 mai 2007, 00:56
*
  • To change this template, choose Tools | Template Manager
  • and open the template in the editor.
  • /
package sf3.system; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.PrintStream; import java.util.Collections; import java.util.Formatter; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; /**
  • This class implements a console to get log data from System.out and can be used in any log component implementing the InputLogListener.
  • @see InputLogListener
  • @author www.b23prodtm.com
  • /
public class Console implements Appendable { /** the System.out default PrintStream */ public final static PrintStream _stdout = System.out; /***/ public final static PrintStream _stderr = System.err; /** the System default line separator String (e.g. \n)*/ public final static String newLine = System.getProperty("line.separator"); /** print error event */ protected final static int PRINT_ERROR = 0; /** print wait event */ protected final static int PRINT_WAIT = 1; /** print stop event */ protected final static int PRINT_STOP = 2; /** print resume constant */ protected final static int PRINT_RESUME = 3; /***/ private final static int INIT_BUFFER = 200; /***/ private SpritesCacheManager<Integer, StringBuffer> buffer_spm = new SpritesCacheManager<Integer, StringBuffer>(INIT_BUFFER); /** the console buffer (init. to 200 lines)*/ private Map<Integer,StringBuffer> buffer = Collections.synchronizedMap(buffer_spm); /**
  • the switch to _stdout
*
  • @see #setStdoutEnabled(boolean)
  • @default true
  • /
private boolean toStdout = true; /**
  • the switch to _stdout input PrintStream to the console
*
  • @see #setLogStdoutEnabled(boolean)
  • @default false
  • /
private boolean fromStdout = false; /** the console output error listeners */ private Set<ErrorListener> output = Collections.synchronizedSet(new HashSet<ErrorListener>()); /** the console input listeners */ private Set<InputLogListener> input = Collections.synchronizedSet(new HashSet<InputLogListener>()); /** a standard error listener to setLogStdoutEnabled(true)*/ private final ErrorListener stdOutCallback = new ErrorListener() { public void printStreamError(IOException e) { e.printStackTrace(); try { setLogStdoutEnabled(false); } catch (IOException ex) { ex.printStackTrace(); } } }; /** a standard error listener to setLogStdoutEnabled(true)*/ private final ErrorListener stdErrCallback = new ErrorListener() { public void printStreamError(IOException e) { e.printStackTrace(); try { setLogStderrEnabled(false); } catch (IOException ex) { ex.printStackTrace(); } } }; /** the output if setOutput(PrintStream) is called . default is null.
  • @see #setOutput(PrintStream)*/
private PrintStream out = null; /** the input streams indentified by a "long" ID*/ private Map<? super Long, ? super PrintStream> in; /** the back-cache manager*/ SpritesCacheManager<Object, Object> spm = new SpritesCacheManager<Object, Object>(); /***/ public String _format = "%s"; /** Creates a new instance of Console. the input is initialized with no printstream */ public Console() { spm.setDebugEnabled(false); buffer_spm.setDebugEnabled(false); in = Collections.synchronizedMap(spm); //spm.setAutoCleanupEnabled(true); } /** returns the buffer
  • @return the current buffer*/
public StringBuffer getBuffer() { StringBuffer strBuf = new StringBuffer(); for(int i = 0; i < buffer.size(); i++) { StringBuffer line = buffer.get(i); strBuf.append((line instanceof StringBuffer)?line:null); } return strBuf; } /** sends a new input to InputLogListeners
  • @see #input
  • @param message the characters data */
public void notifyInput(String message) { synchronized(input) { for(Iterator<InputLogListener> i = input.iterator(); i.hasNext(); ) { InputLogListener ill = i.next(); if(ill != null) ill.newLogPacket(message); } } } /** adds a new InputLogListener to input. it will receive data from notifyInput() calls. the methods in the listeners should be synchronized with the listener buffers updates
  • @see #notifyInput(String)
  • @param l the input listener*/
public void addInputLogListener(InputLogListener l) { sendConsoleEvent(PRINT_WAIT); synchronized(l) { l.notifyAll(); } input.add(l); sendConsoleEvent(PRINT_RESUME); } /** removes an input
  • @param l the listening input to remove */
public void removeInputLogListener(InputLogListener l) { sendConsoleEvent(PRINT_WAIT); synchronized(l) { l.notifyAll(); } input.remove(l); sendConsoleEvent(PRINT_RESUME); } /** returns the current line in buffer
  • @return current line characters data */
public StringBuffer getCurrentLine() { StringBuffer line = buffer.get(buffer.size() - 1); return (line instanceof StringBuffer)?line:new StringBuffer(); } /** dis/enables the System.out trace of the console.
  • @param b true or false */
public void setStdoutEnabled(boolean b) { try { setLogStdoutEnabled(false); } catch (IOException ex) { ex.printStackTrace(); } finally { toStdout = b; } } /** checks whether the System.out trace is enabled or not
  • @return true or false
  • @see #setStdoutEnabled(boolean)*/
public boolean isStdoutEnabled() { return toStdout; } /** checks whether the System.out is read to this console or not
  • @return true or false
  • @see #setLogStdoutEnabled(boolean)*/
public boolean isLogStdoutEnabled() { return fromStdout; } /**
  • dis/enables the System.out as an input to this console. an IOException is thrown if the method fails to init.
  • @param b dis/enable
  • @throws java.io.IOException
  • /
public void setLogStdoutEnabled(boolean b) throws IOException { int pty = Thread.currentThread().getPriority(); Thread.currentThread().setPriority(Thread.MAX_PRIORITY); fromStdout = b; toStdout = false; clearContents(); if(b) { Map<String, Object> ps = newPrintStream(stdOutCallback); PrintStream printstream = (PrintStream)ps.get("ps"); if(!printstream.checkError()) System.setOut(printstream); sendConsoleEvent(PRINT_RESUME, (Long)ps.get("id")); } else { if(!_stdout.checkError()) System.setOut(_stdout); } toStdout = !b; Thread.currentThread().setPriority(pty); } /**
  • dis/enables the System.out as an input to this console. an IOException is thrown if the method fails to init.
  • @param b dis/enable
  • @throws java.io.IOException
  • /
public void setLogStderrEnabled(boolean b) throws IOException { int pty = Thread.currentThread().getPriority(); Thread.currentThread().setPriority(Thread.MAX_PRIORITY); clearContents(); if(b) { Map<String, Object> ps = newPrintStream(stdErrCallback); PrintStream printstream = (PrintStream)ps.get("ps"); if(!printstream.checkError()) System.setErr(printstream); sendConsoleEvent(PRINT_RESUME, (Long)ps.get("id")); } else { if(!_stderr.checkError()) System.setErr(_stderr); } Thread.currentThread().setPriority(pty); } /** sets an different output PrintStream for this console.
  • @param newOut the output
  • @return the previous printstream*/
public PrintStream setOutput(PrintStream newOut) { PrintStream old = out; out = newOut; return old; } /**
  • @param in an InputStream to add to this console, it should be buffered.*/
public Thread getNewInput(InputStream in) throws IOException{ return getNewInput(in, _format); } /**
  • @param in an InputStream to add to this console, it should be buffered.*/
public Thread getNewInput(InputStream in, String format) throws IOException{ final String _f = format; final BufferedInputStream _in = new BufferedInputStream(in); final Map<String, Object> map = newPrintStream(new ErrorListener() { public void printStreamError(IOException e) { e.printStackTrace(); } }); Thread t = new Thread(new Runnable() { public void run(){ try{ sendConsoleEvent(PRINT_RESUME, (Long)map.get("id")); PrintStream ps = (PrintStream)map.get("ps"); ps.println("New Console input has been created."); Formatter f = new Formatter(ps); byte[] s = new byte[128]; while(_in.read(s) != -1) f.format(_f, new String(s)); } catch(IOException e) { e.printStackTrace(); } }}, "T-Console inputStream"); t.setDaemon(true); t.setPriority(Thread.MAX_PRIORITY); return t; } /** adds an error listener to the console output
  • @param l the error listener*/
public void addErrorListener(ErrorListener l) { output.add(l); } /** removes an error listener from the console output
  • @param l the error listener to remove */
public void removeErrorListener(ErrorListener l) { output.remove(l); } /** sends an error notice to the error listeners
  • @param event the error event
  • @param e the IOException caught
  • @see #PRINT_ERROR */
private void notifyError(int event, IOException e) { synchronized(output) { switch(event) { case PRINT_ERROR: synchronized(output) { for(Iterator<ErrorListener> i = output.iterator(); i.hasNext();) { ErrorListener l = i.next(); if(l instanceof ErrorListener) l.printStreamError(e); } } break; default: break; } } } /***/ private int tflag = 0; /**
  • returns a new PrintStream to print on loggin' stack.
  • it is simply a 32bits packet atream read from the printStream that will be sent to the InputListeners.
  • NOTICE: after this method you MUST send a PRINT_RESUME event to the console otherwise the returned PrintStream will stay inactive.
*
  • @return new buffered PrintStream instance ready to be used as an input and it associated "long" ID.
  • That is, when you get the map Map<String, Object>:
  • long id = (Long)map.get("id");
  • PrintStream ps = (PrintStream)map.get("ps");
  • // assign the PrintStream ps somewhere
  • // and send a PRINT_RESUME to start writing to the console.
  • console.sendConsoleEvent(Console.PRINT_RESUME);
  • @see #sendConsoleEvent(int)
  • @see #PRINT_RESUME
  • @see ErrorListener
  • @see #input
  • @see InputLogListener#newLogPacket(String)
  • @param l Output listener to the printStream
  • @throws java.io.IOException
  • /
protected synchronized Map<String, Object> newPrintStream(ErrorListener l) throws IOException { int pty = Thread.currentThread().getPriority(); Thread.currentThread().setPriority(Thread.MAX_PRIORITY - 1); addErrorListener(l); PipedOutputStream pipedOutputStream = new PipedOutputStream(); final BufferedInputStream pis = new BufferedInputStream(new PipedInputStream(pipedOutputStream)); final long id = System.currentTimeMillis(); final PrintStream ps; tflag = 0; in.put(id, ps = new PrintStream(new BufferedOutputStream(pipedOutputStream))); Thread t = new Thread(new Runnable() { public void run() { try { new Thread(new Runnable() { public void run() { synchronized(Console.this) { while(tflag == 0) { System.out.println("new PrintStream notice"); Console.this.notify(); try { Console.this.wait(0,50); } catch (InterruptedException ex) { ex.printStackTrace(); Thread.currentThread().interrupt(); } } } }}).start(); synchronized(ps) { ps.wait(); } System.out.println("new PrintStream is startin'..."); byte[] b = new byte[128]; while(pis.read(b) != -1) { final String s = new String(b); switch(readConsoleEvent(id)) { case Console.PRINT_WAIT: synchronized(ps) { ps.wait(); } break; default: break; } notifyInput(s); try { append(s); } catch (IOException ex) { ex.printStackTrace(); } } pis.close(); } catch (InterruptedException ex) { ex.printStackTrace(); Thread.currentThread().interrupt(); } catch (IOException ex) { notifyError(PRINT_ERROR, ex); } }}, "T-logPrintStream"); t.setPriority(Thread.MAX_PRIORITY); t.setDaemon(true); spm.buffer(t); try { spm.memorySensitiveCallback("start", t, null, null); System.out.println("new PrintStream is waitin'..."); wait(); tflag = 1; } catch (InterruptedException ex) { ex.printStackTrace(); Thread.currentThread().interrupt(); } catch(Throwable tr) { tr.printStackTrace(); } finally { Map<String, Object> map = Collections.synchronizedMap(new HashMap<String, Object>()); map.put("id", id); map.put("ps", ps); Thread.currentThread().setPriority(pty); return map; } } /** removes a identified input
  • @param id the "long" id of the PrintStream input
  • @return the same PrintStream
  • @see #newPrintStream(ErrorListener)*/
protected PrintStream removePrintStream(long id) { return (PrintStream) in.remove(id); } /** the events map that will be used by sendConsoleEvent and readConsoleEvent
  • @see #sendConsoleEvent(int)
  • @see #readConsoleEvent(long)*/
Map<Integer, Map<Long, Integer>> events = Collections.synchronizedMap(new HashMap<Integer, Map<Long, Integer>>()); /** reads for a sent event in the events map for the given "long" id
  • @param id the id to distinguish events
  • @return the event
  • @see #PRINT_WAIT
  • @see #PRINT_RESUME
  • @see #PRINT_STOP
  • /
private int readConsoleEvent(long id) { Set<Integer> set = events.keySet(); synchronized(events) { for(Iterator<Integer> i = set.iterator(); i.hasNext();) { int key = i.next(); Map<Long, Integer> event = events.get(key); if(event == null) return -1; else return (event.get(id) != null)?events.remove(key).get(id):-1; } return -1; } } /** sends a new event to the console
  • @param event the event
  • @see #PRINT_WAIT
  • @see #PRINT_RESUME
  • @see #PRINT_STOP*/
public void sendConsoleEvent(int event) { Set<? super Long> set = in.keySet(); synchronized(in){ for(Iterator<? super Long> i = set.iterator(); i.hasNext();) sendConsoleEvent(event, (Long)i.next()); } } /** sends a new console event to the selected "long" id, usually a PrintStream registered input
  • @see #newPrintStream(ErrorListener)
  • @param event the event
  • @param id the "long" id
  • @return true or false whether the event could be sent to a registered PrintStream
  • @see #sendConsoleEvent(int)*/
private boolean sendConsoleEvent(int event, long id) { if(id != -1) { PrintStream ps = (PrintStream)in.get(id); if(ps == null) return false; synchronized(ps) { switch(event) { case PRINT_RESUME: ps.notifyAll(); return true; case PRINT_STOP: ps.close(); return true; default: ps.notify(); break; } } } Map<Long, Integer> map = new HashMap(); map.put(id, event); events.put(events.size(), map); return true; } /**
  • appends new characters data to the console buffer and notifies the output
*
  • @return the buffer with the new data appended
  • @see #buffer
  • @param charSequence the data to append
  • @throws java.io.IOException
  • /
public Appendable append(CharSequence charSequence) throws IOException { charSequence = new Formatter().format(_format, charSequence).toString().subSequence(0, charSequence.length()); if(toStdout) _stdout.append(charSequence); if(out instanceof PrintStream) out.append(charSequence); String[] s = charSequence.toString().split(newLine); for(int i = 0; i < s.length; i++){ String line = s[i]; if(i < s.length - 1) line += newLine; if(getCurrentLine().lastIndexOf(newLine) == -1) buffer.put(buffer.size() - 1, getCurrentLine().append(line)); else buffer.put(buffer.size(), new StringBuffer(line)); } return this; } /**
  • appends new characters data to the console buffer and notifies the output
*
  • @return the buffer with the new data appended
  • @see #buffer
  • @param charSequence the data to append
  • @param i the starting index of the characters data to append
  • @param i0 the 1+end index
  • @throws java.io.IOException
  • /
public Appendable append(CharSequence charSequence, int i, int i0) throws IOException { charSequence = new Formatter().format(_format, charSequence).toString().subSequence(i, i0); if(toStdout) _stdout.append(charSequence, i, i0); if(out instanceof PrintStream) out.append(charSequence, i, i0); String[] s = charSequence.toString().split(newLine); for(int j = 0; j < s.length; j++){ String line = s[j]; if(j < s.length - 1) line += newLine; if(getCurrentLine().lastIndexOf(newLine) == -1) buffer.put(buffer.size() - 1, getCurrentLine().append(line, i, i0)); else buffer.put(buffer.size(), new StringBuffer().append(line, i, i0)); } return this; } /**
  • appends new characters data to the console buffer and notifies the output
*
  • @return the buffer with the new data appended
  • @see #buffer
  • @param c the data to append
  • @throws java.io.IOException
  • /
public Appendable append(char c) throws IOException { c = new Formatter().format(_format, c).toString().charAt(0); if(toStdout) _stdout.append(c); if(out instanceof PrintStream) out.append(c); if(getCurrentLine().lastIndexOf(newLine) == -1) buffer.put(buffer.size() - 1, getCurrentLine().append(c)); else buffer.put(buffer.size(), new StringBuffer(c)); return this; } /** clears the contents of buffer. all previous from input unread data in the buffer will be lost.
  • @see #buffer*/
public void clearContents() { buffer.clear(); } /**
  • overriden to disable the System.out read stream to this console and reset the System.out to _stdout.
  • /
public void finalize() { try { setLogStdoutEnabled(false); } catch (IOException ex) { ex.printStackTrace(); } } /***/ public PrintStream getNewPrintStream(ErrorListener l) { try { Map<String, Object> map = newPrintStream(l); PrintStream ps = (PrintStream)map.get("ps"); sendConsoleEvent(PRINT_RESUME, (Long)map.get("id")); return ps; } catch (IOException ex) { ex.printStackTrace(); return null; } } }

Conclusion :


src du package installer complet sur sf.net/projects/sf3jswing rubrique CVS en libre consultation

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.