From: <cr...@us...> - 2008-05-02 14:32:14
|
Revision: 4039 http://jnode.svn.sourceforge.net/jnode/?rev=4039&view=rev Author: crawley Date: 2008-05-02 07:32:13 -0700 (Fri, 02 May 2008) Log Message: ----------- Changed low level console out/err streams from PrintStreams to OutputStreams. Added support for interception of console output to Shell / CommandShell. (Required to make 'udpout' work without messing with System.out/err.) Modified Paths: -------------- trunk/core/src/driver/org/jnode/driver/console/TextConsole.java trunk/core/src/driver/org/jnode/driver/console/textscreen/KeyboardInputStream.java trunk/core/src/driver/org/jnode/driver/console/textscreen/TextScreenConsolePlugin.java trunk/shell/src/shell/org/jnode/shell/CommandRunner.java trunk/shell/src/shell/org/jnode/shell/CommandShell.java trunk/shell/src/shell/org/jnode/shell/Shell.java trunk/shell/src/shell/org/jnode/shell/command/driver/console/ConsoleCommand.java Modified: trunk/core/src/driver/org/jnode/driver/console/TextConsole.java =================================================================== --- trunk/core/src/driver/org/jnode/driver/console/TextConsole.java 2008-05-02 14:26:52 UTC (rev 4038) +++ trunk/core/src/driver/org/jnode/driver/console/TextConsole.java 2008-05-02 14:32:13 UTC (rev 4039) @@ -22,6 +22,7 @@ package org.jnode.driver.console; import java.io.InputStream; +import java.io.OutputStream; import java.io.PrintStream; /** @@ -166,13 +167,13 @@ * Gets the error stream of this console. * @return */ - public PrintStream getErr(); + public OutputStream getErr(); /** * Gets the output stream of this console. * @return */ - public PrintStream getOut(); + public OutputStream getOut(); /** * Is the cursor visible. Modified: trunk/core/src/driver/org/jnode/driver/console/textscreen/KeyboardInputStream.java =================================================================== --- trunk/core/src/driver/org/jnode/driver/console/textscreen/KeyboardInputStream.java 2008-05-02 14:26:52 UTC (rev 4038) +++ trunk/core/src/driver/org/jnode/driver/console/textscreen/KeyboardInputStream.java 2008-05-02 14:32:13 UTC (rev 4039) @@ -106,7 +106,7 @@ } } this.console = console; - this.out = console.getOut(); + this.out = new PrintStream(console.getOut()); this.currentLine = new Line(console); this.pos = this.lim = 0; } Modified: trunk/core/src/driver/org/jnode/driver/console/textscreen/TextScreenConsolePlugin.java =================================================================== --- trunk/core/src/driver/org/jnode/driver/console/textscreen/TextScreenConsolePlugin.java 2008-05-02 14:26:52 UTC (rev 4038) +++ trunk/core/src/driver/org/jnode/driver/console/textscreen/TextScreenConsolePlugin.java 2008-05-02 14:32:13 UTC (rev 4039) @@ -21,6 +21,8 @@ package org.jnode.driver.console.textscreen; +import java.io.PrintStream; + import javax.naming.NamingException; import org.jnode.driver.console.ConsoleException; @@ -60,9 +62,9 @@ (ConsoleManager.CreateOptions.TEXT | ConsoleManager.CreateOptions.SCROLLABLE)); mgr.focus(first); - System.setOut(first.getOut()); - System.setErr(first.getErr()); - first.getOut().println(VmSystem.getBootLog()); + System.setOut(new PrintStream(first.getOut())); + System.setErr(new PrintStream(first.getErr())); + System.out.println(VmSystem.getBootLog()); } catch (ConsoleException ex) { throw new PluginException(ex); } catch (NamingException ex) { Modified: trunk/shell/src/shell/org/jnode/shell/CommandRunner.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/CommandRunner.java 2008-05-02 14:26:52 UTC (rev 4038) +++ trunk/shell/src/shell/org/jnode/shell/CommandRunner.java 2008-05-02 14:32:13 UTC (rev 4039) @@ -126,10 +126,10 @@ invoker.unblock(); } catch (SyntaxErrorException ex) { try { - Help.getInfo(cmdInfo.getCommandClass()).usage(shell.getConsole().getErr()); - shell.getConsole().getErr().println(ex.getMessage()); + Help.getInfo(cmdInfo.getCommandClass()).usage(shell.getErr()); + shell.getErr().println(ex.getMessage()); } catch (HelpException e) { - shell.getConsole().getErr().println("Exception while trying to get the command usage"); + shell.getErr().println("Exception while trying to get the command usage"); stackTrace(ex); } invoker.unblock(); @@ -137,11 +137,11 @@ setRC(ex.getStatus()); invoker.unblock(); } catch (Exception ex) { - shell.getConsole().getErr().println("Exception in command"); + shell.getErr().println("Exception in command"); stackTrace(ex); invoker.unblock(); } catch (Throwable ex) { - shell.getConsole().getErr().println("Fatal error in command"); + shell.getErr().println("Fatal error in command"); stackTrace(ex); invoker.unblock(); } @@ -161,7 +161,7 @@ void stackTrace(Throwable ex) { if (ex != null && isDebugEnabled()) { - ex.printStackTrace(shell.getConsole().getErr()); + ex.printStackTrace(shell.getErr()); } } Modified: trunk/shell/src/shell/org/jnode/shell/CommandShell.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/CommandShell.java 2008-05-02 14:26:52 UTC (rev 4038) +++ trunk/shell/src/shell/org/jnode/shell/CommandShell.java 2008-05-02 14:32:13 UTC (rev 4039) @@ -91,10 +91,14 @@ */ private static final Logger log = Logger.getLogger(CommandShell.class); - private PrintStream out; + private OutputStream out; + + private OutputStream err; + + private PrintStream outPs; + + private PrintStream errPs; - private PrintStream err; - private InputStream in; private AliasManager aliasMgr; @@ -178,6 +182,8 @@ console = cons; out = console.getOut(); err = console.getErr(); + outPs = new PrintStream(out); + errPs = new PrintStream(err); in = console.getIn(); SystemInputStream.getInstance().initialize(this.in); cons.setCompleter(this); @@ -197,8 +203,11 @@ PrintStream out, PrintStream err) throws ShellException { try { this.console = console; + // FIXME ... kludging the OutputStreams. this.out = out; this.err = err; + this.outPs = out; + this.errPs = err; this.in = in; SystemInputStream.getInstance().initialize(this.in); // cons.setCompleter(this); @@ -223,8 +232,8 @@ protected CommandShell(AliasManager aliasMgr, SyntaxManager syntaxMgr) { this.aliasMgr = aliasMgr; this.syntaxMgr = syntaxMgr; - this.err = System.err; - this.out = System.out; + this.errPs = System.err; + this.outPs = System.out; this.readingCommand = true; this.debugEnabled = true; } @@ -271,11 +280,11 @@ try { if (e.startsWith(COMMAND_KEY)) { final String cmd = e.substring(COMMAND_KEY.length()); - out.println(prompt() + cmd); + outPs.println(prompt() + cmd); processCommand(cmd, false); } } catch (Throwable ex) { - err.println("Error while processing bootarg commands: " + errPs.println("Error while processing bootarg commands: " + ex.getMessage()); stackTrace(ex); } @@ -291,7 +300,7 @@ runCommandFile(shell_ini); } } catch (IOException ex) { - err.println("Error while reading " + shell_ini + ": " + errPs.println("Error while reading " + shell_ini + ": " + ex.getMessage()); stackTrace(ex); } @@ -304,7 +313,7 @@ refreshFromProperties(); clearEof(); - out.print(prompt()); + outPs.print(prompt()); readingCommand = true; String line = readInputLine().trim(); if (line.length() > 0) { @@ -315,7 +324,7 @@ exited = true; } } catch (Throwable ex) { - err.println("Uncaught exception while processing command(s): " + errPs.println("Uncaught exception while processing command(s): " + ex.getMessage()); stackTrace(ex); } @@ -331,7 +340,7 @@ setCommandInvoker(System.getProperty(INVOKER_PROPERTY_NAME, INITIAL_INVOKER)); } catch (Exception ex) { - err.println(ex.getMessage()); + errPs.println(ex.getMessage()); stackTrace(ex); // Use the fallback invoker setCommandInvoker(FALLBACK_INVOKER); @@ -340,7 +349,7 @@ setCommandInterpreter(System.getProperty(INTERPRETER_PROPERTY_NAME, INITIAL_INTERPRETER)); } catch (Exception ex) { - err.println(ex.getMessage()); + errPs.println(ex.getMessage()); stackTrace(ex); // Use the fallback interpreter setCommandInterpreter(FALLBACK_INTERPRETER); @@ -356,13 +365,13 @@ setCommandInterpreter(System.getProperty(INTERPRETER_PROPERTY_NAME, "")); } catch (Exception ex) { - err.println(ex.getMessage()); + errPs.println(ex.getMessage()); stackTrace(ex); } try { setCommandInvoker(System.getProperty(INVOKER_PROPERTY_NAME, "")); } catch (Exception ex) { - err.println(ex.getMessage()); + errPs.println(ex.getMessage()); stackTrace(ex); } } @@ -371,7 +380,7 @@ throws IllegalArgumentException { if (!name.equals(this.invokerName)) { this.invoker = ShellUtils.createInvoker(name, this); - err.println("Switched to " + name + " invoker"); + errPs.println("Switched to " + name + " invoker"); this.invokerName = name; System.setProperty(INVOKER_PROPERTY_NAME, name); } @@ -381,7 +390,7 @@ throws IllegalArgumentException { if (!name.equals(this.interpreterName)) { this.interpreter = ShellUtils.createInterpreter(name); - err.println("Switched to " + name + " interpreter"); + errPs.println("Switched to " + name + " interpreter"); this.interpreterName = name; System.setProperty(INTERPRETER_PROPERTY_NAME, name); } @@ -389,7 +398,7 @@ private void stackTrace(Throwable ex) { if (this.debugEnabled) { - ex.printStackTrace(err); + ex.printStackTrace(errPs); } } @@ -423,7 +432,7 @@ try { rc = interpreter.interpret(this, cmdLineStr); } catch (ShellException ex) { - err.println("Shell exception: " + ex.getMessage()); + errPs.println("Shell exception: " + ex.getMessage()); rc = -1; stackTrace(ex); } @@ -604,13 +613,13 @@ cl.complete(completion, this); } } catch (ShellSyntaxException ex) { - out.println(); // next line - err.println("Cannot parse: " + ex.getMessage()); + outPs.println(); // next line + errPs.println("Cannot parse: " + ex.getMessage()); stackTrace(ex); } catch (CompletionException ex) { - out.println(); // next line - err.println("Problem in completer: " + ex.getMessage()); + outPs.println(); // next line + errPs.println("Problem in completer: " + ex.getMessage()); stackTrace(ex); } @@ -710,7 +719,7 @@ public int runCommandFile(File file) throws IOException { if (!file.exists()) { - err.println("File does not exist: " + file); + errPs.println("File does not exist: " + file); return -1; } try { @@ -727,7 +736,7 @@ try { rc = invokeCommand(line); } catch (ShellException ex) { - err.println("Shell exception: " + ex.getMessage()); + errPs.println("Shell exception: " + ex.getMessage()); stackTrace(ex); } } @@ -786,9 +795,9 @@ if (stream == CommandLine.DEFAULT_STDIN) { return getInputStream(); } else if (stream == CommandLine.DEFAULT_STDOUT) { - return out; + return outPs; } else if (stream == CommandLine.DEFAULT_STDERR) { - return err; + return errPs; } else if (stream == CommandLine.DEVNULL || stream == null) { return input ? new NullInputStream() : new NullOutputStream(); } else { @@ -830,4 +839,28 @@ public SyntaxManager getSyntaxManager() { return syntaxMgr; } + + public PrintStream getErr() { + return errPs; + } + + @Override + public void addConsoleOuputRecorder(OutputStream os) { + // FIXME do security check + if (out == null || err == null) { + throw new UnsupportedOperationException( + "Cannot intercept console output for this shell instance"); + } + if (out instanceof FanoutOutputStream) { + ((FanoutOutputStream) out).addStream(os); + ((FanoutOutputStream) err).addStream(os); + } + else { + out = new FanoutOutputStream(true, out, os); + outPs = new PrintStream(out); + err = new FanoutOutputStream(true, err, os); + errPs = new PrintStream(err); + } + errPs.println("Testing"); + } } Modified: trunk/shell/src/shell/org/jnode/shell/Shell.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/Shell.java 2008-05-02 14:26:52 UTC (rev 4038) +++ trunk/shell/src/shell/org/jnode/shell/Shell.java 2008-05-02 14:32:13 UTC (rev 4039) @@ -23,6 +23,7 @@ import java.io.File; import java.io.IOException; +import java.io.OutputStream; import org.jnode.driver.console.Console; import org.jnode.driver.console.InputCompleter; @@ -52,6 +53,13 @@ public InputHistory getCommandHistory(); /** + * Record all console output from the shell and commands launched by the shell. + * + * @param os The stream for recording output. + */ + public void addConsoleOuputRecorder(OutputStream os); + + /** * Returns the console where the shell is running. * @return the console */ Modified: trunk/shell/src/shell/org/jnode/shell/command/driver/console/ConsoleCommand.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/command/driver/console/ConsoleCommand.java 2008-05-02 14:26:52 UTC (rev 4038) +++ trunk/shell/src/shell/org/jnode/shell/command/driver/console/ConsoleCommand.java 2008-05-02 14:32:13 UTC (rev 4039) @@ -21,6 +21,8 @@ package org.jnode.shell.command.driver.console; +import java.io.PrintStream; + import javax.isolate.Isolate; import javax.isolate.IsolateStartupException; import javax.naming.NameNotFoundException; @@ -121,8 +123,8 @@ final ConsoleManager conMgr = sm.getCurrentShell().getConsole().getManager(); TextConsole console = createConsoleWithShell(conMgr); System.setIn(console.getIn()); - System.setOut(console.getOut()); - System.setErr(console.getErr()); + System.setOut(new PrintStream(console.getOut())); + System.setErr(new PrintStream(console.getErr())); } catch (Exception ex) { // FIXME @@ -149,11 +151,12 @@ String invokerName = System.getProperty(CommandShell.INVOKER_PROPERTY_NAME, ""); // FIXME this is a temporary hack until we decide what to do about these invokers if ("thread".equals(invokerName) || "default".equals(invokerName)) { - console.getErr().println( + PrintStream err = new PrintStream(console.getErr()); + err.println( "Warning: any commands run in this console via their main(String[]) will " + "have the 'wrong' System.out and System.err."); - console.getErr().println("The 'proclet' invoker should give better results."); - console.getErr().println("To use it, type 'exit', run 'set jnode.invoker proclet' " + + err.println("The 'proclet' invoker should give better results."); + err.println("To use it, type 'exit', run 'set jnode.invoker proclet' " + "in the F1 console and run 'console -n' again."); } return console; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |