Writing Ruby Console messages to a text file
-
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:
# 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.3117Console 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] -
I might have the perfect candidate for testing. I experience crash without bugsplat with a script in some unknown condition.
-
Wow, very neat!
-
Thanks, Al - good idea. You could also log $stderr which might catch some thing additional events.
$stderr = $stdout
-
@thomthom said:
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:
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
-
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.3117Console messages are going to c:\tmp\ruby_trace.txt
!U
-
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.
-
Keep your ese on this thread as well: [Plugin] Trace Ruby messages
-
Hi Al,
Thanks - will do.
Advertisement