Writing Ruby Console messages to a text file

Writing Ruby Console messages to a text file

Postby Al Hart » Fri Sep 24, 2010 7:53 am

Sketchup.exe has a feature where, if you redirect its standard output to a file, then any messages which would be sent to the ruby console are written to the file. However, Sketchup.exe does not flush this file, so if there is a big splat, or if you want to examine the file while Sketchup.exe is running, it will not contain all of the messages.

I have created a ruby script which overrides the Ruby Console, and write its messages to a file, and flushes each message.

Place this in the plugins folder, and it should create a file called c:\tmp\ruby_trace.txt which contains all of the messages which are sent to the Ruby Console (whether the Ruby Console is open or not).

Give it a try, see what happens, and offer any suggestions you may have:

!trace_console.rb


Code: Select all
# Copyright 2010, Render Plus Systems.

# Permission to use, copy, modify, and distribute this software for
# any purpose and without fee is hereby granted, provided that the above
# copyright notice appear in all copies.

# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#-----------------------------------------------------------------------------


printf("***** Loading: %s\n", File.basename(__FILE__))

# create a class to trap comsole output and send
# it to a file int c:\tmp
class CL_rps_console < Sketchup::Console
attr_accessor :debug_file
attr_accessor :sfile

   def initialize
      # create a folder called c:\tmp
      sdir = "c:\\tmp"
      if (!FileTest.exists?sdir)
          Dir.mkdir(sdir)
      end#if
      @sfile = "c:\\tmp\\ruby_trace.txt"
      @debug_file = File.new(@sfile, "w")

      @debug_file.write(::Time.now.to_s  + "\n")
      @debug_file.write("SketchUp Version: " + Sketchup.version.to_s  + "\n")
   end#def

   def write(smess)
      @debug_file.write(smess)
      @debug_file.flush
      super(smess) # call inherited class write function
   end#def
end#class

# redefine standard output to our new class
$old_stdout = $stdout # in case you want to turn off traces
$stdout = CL_rps_console.new
$stderr = $stdout # trap error displays as well
puts "-----"
puts "Console messages are going to " + $stdout.sfile
puts "-----"



Sample file contents:

    C:\tmp>type ruby_trace.txt

    Fri Sep 24 00:51:28 -0600 2010
    SketchUp Version: 8.0.3117
    -----
    Console messages are going to c:\tmp\ruby_trace.txt
    -----

[EDIT: changed name to !trace_console.rb and added $stderr = $stdout]
[EDIT: Revised script for mkdir error]
0
Last edited by Al Hart on Fri Sep 24, 2010 8:20 pm, edited 2 times in total.
Al Hart
IRender nXt from Render Plus
User avatar
Al Hart 
PluginStore Author
PluginStore Author
 

Re: Writing Ruby Console messages to a text file

Postby thomthom » Fri Sep 24, 2010 8:10 am

I might have the perfect candidate for testing. I experience crash without bugsplat with a script in some unknown condition.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: Writing Ruby Console messages to a text file

Postby honoluludesktop » Fri Sep 24, 2010 10:10 am

Wow, very neat!
0
User avatar
honoluludesktop 
In Remembrance
 

Re: Writing Ruby Console messages to a text file

Postby Jim » Fri Sep 24, 2010 2:26 pm

Thanks, Al - good idea. You could also log $stderr which might catch some thing additional events.

$stderr = $stdout
0
Hi

Jim 
Global Moderator
 

Re: Writing Ruby Console messages to a text file

Postby Al Hart » Fri Sep 24, 2010 4:58 pm

I had place an underscore at the front of the ruby file name, (_trace_ruby.rb), so it would run before other ruby files.

But I was confused. It turns out the _ comes after a-z in sort order, so I changed it to !trace_ruby.rb so it would come first.

I wanted to make sure that it caught printf's and puts's from other .rb files in plugins, so I created !r.rb with prints "!R" and !u.rb which prints "!U", and sure enough, !R did not trace to the file, but !U did.

I have updated the script in the original post, and also added the $stderr stuff as suggested by Jim

    C:\tmp>type ruby_trace.txt
    Fri Sep 24 09:53:21 -0600 2010
    SketchUp Version: 8.0.3117
    -----
    Console messages are going to c:\tmp\ruby_trace.txt
    -----
    !U
0
Last edited by Al Hart on Fri Sep 24, 2010 5:47 pm, edited 1 time in total.
Al Hart
IRender nXt from Render Plus
User avatar
Al Hart 
PluginStore Author
PluginStore Author
 

Re: Writing Ruby Console messages to a text file

Postby Al Hart » Fri Sep 24, 2010 5:46 pm

thomthom wrote:I might have the perfect candidate for testing. I experience crash without bugsplat with a script in some unknown condition.


Let me know how it goes.

You can add:

Code: Select all
printf("FILE: %s LINE: %s\n", File.basename(__FILE__), __LINE__)


after almost every line in the ruby file to trace down the bug splat.

(Or if someone knows another good way to trace ruby files, let me know)

Al
0
Al Hart
IRender nXt from Render Plus
User avatar
Al Hart 
PluginStore Author
PluginStore Author
 

Re: Writing Ruby Console messages to a text file

Postby dburdick » Thu May 05, 2011 12:52 am

I'm trying to redirect $stderr to a file, but it doesn't work. $stdout works just fine but not $stderr. It appears that when Sketchup generates an error message, it does not call $stderr, but outputs to the console through some other mechanism. Anyone have any ideas on this.
0

dburdick 
e-on software
 

Re: Writing Ruby Console messages to a text file

Postby Al Hart » Thu May 05, 2011 2:17 am

Keep your ese on this thread as well: [Plugin] Trace Ruby messages
0
Al Hart
IRender nXt from Render Plus
User avatar
Al Hart 
PluginStore Author
PluginStore Author
 

Re: Writing Ruby Console messages to a text file

Postby dburdick » Thu May 05, 2011 4:31 am

Hi Al,

Thanks - will do.
0

dburdick 
e-on software
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 8 guests

Visit our sponsors: