Idea: show console on script error
-
Perhaps something like...
begin ### do all of your code here ### ## # rescue Exception => e Sketchup.send_action("showRubyPanel;") puts e.message end
-
@tig said:
> begin > ### do all of your code here > ### > ## > # > rescue Exception => e > Sketchup.send_action("showRubyPanel;") > puts e.message > end >
Great idea TIG!
-
The Ruby Console is not required to be visible for using stdout, and SketchUp doesn't wait for the Ruby Console to be opened. The
puts
will be long finished before the Ruby Console has a chance to appear.A messagebox might work. Or if you want the Ruby Console, you could queue the messages until it is open.
-
Another technique I have used is to redirect stdout and stderr when starting SketchUp.
In a .bat file, I run SU:
"c:\Program Files\Google\Google Sketchup 7\Sketchup.exe" 1> _sulog.txt 2>&1
When the Ruby Console is open, output goes there. WHen closed, it goes to the _sulog.txt file.
This is not a perfect solution because the output is buffered, and in the case of a BugSplat, the buffered output is lost. I tried flushing the output buffer using a timer, but haven't been successful so far.
A better solution is to write output to a file from Ruby. One option is to redefine
puts
to write to a file instead of stdout. Another option is to create your own log method for debug output.def log(s); File.open("log.txt", "a") { |f| f.puts(s) }; end
(Not efficient with all the file handle opening and closing, but it works and output is flushed immediately.)Then use the (unix) command
tail -f log.txt
to follow the output log.You might then want to truncate the log file when SU starts, maybe writing the date and time to it.
-
@jim said:
A better solution is to write output to a file from Ruby. One option is to redefine
puts
to write to a file instead of stdout. Another option is to create your own log method for debug output.Has anyone tried running the standard Ruby simple logger (logger.rb) under SU embedded Ruby?
It looks easy to use. Example from logger.rb RDoc
(with module wrapper, require statement and method wrapper added by me.)# A simple example ... module A_Logger_Test require 'logger.rb' def log_file_error( path ) log = Logger.new(STDOUT) log.level = Logger;;WARN log.debug("Created logger") log.info("Program started") log.warn("Nothing to do!") begin File.each_line(path) do |line| unless line =~ /^(\w+) = (.*)$/ log.error("Line in wrong format; #{line}") end end rescue => err log.fatal("Caught exception; exiting") log.fatal(err) end end # method end # module
It can also output to log files (with auto file aging and max file sizes.)
-
@jim said:
The Ruby Console is not required to be visible for using stdout, and SketchUp doesn't wait for the Ruby Console to be opened. The
puts
will be long finished before the Ruby Console has a chance to appear.I solved this on a little ruby I wrote to display SU Options.
Here's the lister method:def self.list_options(output=$stdout) # param chooses stdout or messagebox case output when $stdout, STDOUT Sketchup.send_action( CMD_RUBY_CONSOLE ) UI.start_timer(0.5, false) { $stdout.write( get_options_text ) } when 'messagebox',;messagebox, 'MB', ;MB UI.messagebox( get_options_text,MB_MULTILINE,' OPTIONS') end end
So the 'trick' is to pause for a half-second allowing the console dialog time to load and initialize, etc. So TIG's example can work like this:
begin ## ### do all of your code here ### rescue Exception => e if $VERBOSE # true is verbose mode Sketchup.send_action(CMD_RUBY_CONSOLE) UI.start_timer(0.5, false) { puts e.message } elsif $VERBOSE.nil? # nil is silent mode logfile.write( e.message ) else # $VERBOSE is false (medium mode) puts e.message raise # (optional) end end
pssst! JJ, it's not necessary to requote codeblocks unless your suggesting changes.
-
ThomThom tells me UI.start_timer will not wait at all, if I set the time to anything less than 1 second.
So change the code above to 1.0 instead of 0.5
-
Thanks Dan! I have tested it with timer set to 0.1 and it works fine. (Win Vista/SU 8.0.4811)
-
@unknownuser said:
Thanks Dan! I have tested it with timer set to 0.1 and it works fine. (Win Vista/SU 8.0.4811)
Yea, the timer was fixed in SU8. In older version it rounds down to whole integers.
-
@thomthom said:
Yea, the timer was fixed in SU8. In older version it rounds down to whole integers.
Thanks! I was going to check whether it works with older versions.
-
The docs should really be mentioning this.
Advertisement