REBOL [
    Title: "Capture console output"
    Purpose: {Turn on the "echo" port so that console output goes
    there, and make "there" a text file.}
]

;; [---------------------------------------------------------------------------]
;; [ This script was taken from here:                                          ]
;; [ http://rebol2.blogspot.com/2012/09/capting-all-console-output.html        ]
;; [ with the plan of making it part of a little "development environment."    ]
;; [ The way it would be used would be for a main program to launch another    ]
;; [ REBOL program, and then that other program would execute this function    ]
;; [ and then halt.  The halt would bring up the REBOL console with output     ]
;; [ going to the specified file.  That REBOL console with its captured        ]
;; [ output then would be used for running various commands, with the output   ]
;; [ saved for later examination.                                              ]
;; [---------------------------------------------------------------------------]

CAPTURE-CONSOLE: func [
    "Copies console output to a file."
    [catch]
    target [file! none! logic!]
    /append
][
    if port? system/ports/echo [
        close system/ports/echo
        system/ports/echo: none
    ]
    if target = true [target: %rebol-echo.txt]
    if file? target [
        system/ports/echo: throw-on-error [
            either not append [
                open/write/new/direct target
            ] [
                target: open/direct target
                skip target 4294967295 ; highest int should move to the end
                target
          ]
        ]
    ]
]

;;Uncomment to test
;CAPTURE-CONSOLE %ConsoleOutput.txt
;halt