Redirect Ruby console to file(s) - example

Attached is an example file that shows how to redirect the Ruby console to file(s). At present, the path is set to ENV['TMPDIR'] || ENV['TEMPDIR']. Usage after load:

SU_IO.file         stderr & stdout to ruby_all.log
SU_IO.file :both   stderr to ruby_err.log, stdout to ruby_out.log
SU_IO.file :err    stderr to ruby_err.log
SU_IO.file :out    stdout to ruby_out.log

SU_IO.rc           reconnect Ruby console

Tested on Windows, there may be buffering issues. Also, I set it to work with a UDP socket, and everything worked fine.

console_redirect.rb (2.2 KB)

Both of which are nil (not present) on MS Windows. (I think these may be Unix-like, ie Mac.)

On windows, we do have ENV["TEMP"] and it’s alias ENV["TMP"].

There was some problems in the past with using these and or the tmpdir Ruby Standard lib, so the SketchUp API (v2014+) has defined a module method to retrieve the path. (This applies to all SketchUp versions running Ruby 2.x) …

#=> C:/Users/Dan/AppData/Local/Temp
#=> UTF-8

SU_IO.file        # stderr & stdout to ruby_all.log
SU_IO.file :all   # stderr to ruby_err.log, stdout to ruby_out.log

These seem the opposite of what we’d think intuitively.
Ie, the :all argument would redirect both to the log with the “all” in the name.

I’d be nice not to interfere with other extension like the Ruby Code Editor.

Would the only way to echo (rather than redirect) be to alias and change the behavior of the Sketchup::Console class’ puts method ? (There’d also need to be a internal array of IO objects to echo to within the class.)


Ah, old habits. A long time ago I always set up Windows systems with ENV['TMPDIR'], as Ruby tmpdir uses that first. At the time, some Ruby code had issues if the normal temp folder had spaces in the path (common with Windows user names). I was doing C# development at the time, so it allowed both…

Fixed the :all issue (changed to :both), and added looking at ENV['RC_PATH'] first, which allows the dev to set the path easily. Uploaded the revision.

Re some kind of echo, that’s a little trickier, sometime later I may look at it. *nix has ‘tee’, and one can do it with PowerShell (encoding fun!).

In some ‘similar’ situations Ruby’s prepend would work, but I don’t think it would here, as SKETCHUP_CONSOLE already exists when our code can have an effect. I think one would have to create a subclass of IO or File, use it instead of the file object, and create the ‘tee’ in it…

Hi MSP_Greg
My boss was complaining about sketchup dropping a console file on the server this morning and after some googling I came upon this thread. I had everyone who uses sketchup copy your ruby file to their plugins directory. I’m not sure if that will take care of the issue or not. Is there anything else I should be aware of when doing this?

I guess the first question is how did SketchUp do that? Or, does the same file exist when SketchUp is run on a system removed from the network, and where is the file located?

Regardless, the file here allows one to redirect the SketchUp Ruby console to file(s), but it doesn’t do so automatically. It could do that, but I’m not sure if that is a good default/automatic setting to use…