sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Idea: show console on script error

    Scheduled Pinned Locked Moved Developers' Forum
    13 Posts 7 Posters 683 Views 7 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R Offline
      remus
      last edited by

      Sounds like a good idea to me πŸ‘

      http://remusrendering.wordpress.com/

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        Perhaps something like...

        
        begin
          ### do all of your code here
          ###
          ##
          #
        rescue Exception => e
          Sketchup.send_action("showRubyPanel;")
          puts e.message
        end
        
        

        TIG

        1 Reply Last reply Reply Quote 0
        • J Offline
          jessejames
          last edited by

          @tig said:

          
          > begin
          >   ### do all of your code here
          >   ###
          >   ##
          >   #
          > rescue Exception => e
          >   Sketchup.send_action("showRubyPanel;")
          >   puts e.message
          > end
          > 
          

          Great idea TIG!

          Always sleep with a loaded gun under your pillow!

          1 Reply Last reply Reply Quote 0
          • J Offline
            Jim
            last edited by

            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.

            Hi

            1 Reply Last reply Reply Quote 0
            • J Offline
              Jim
              last edited by

              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.

              Hi

              1 Reply Last reply Reply Quote 0
              • Dan RathbunD Offline
                Dan Rathbun
                last edited by

                @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.)

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  @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.

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    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

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      tomasz
                      last edited by

                      Thanks Dan! I have tested it with timer set to 0.1 and it works fine. (Win Vista/SU 8.0.4811)

                      Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

                      1 Reply Last reply Reply Quote 0
                      • thomthomT Offline
                        thomthom
                        last edited by

                        @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.

                        Thomas Thomassen β€” SketchUp Monkey & Coding addict
                        List of my plugins and link to the CookieWare fund

                        1 Reply Last reply Reply Quote 0
                        • T Offline
                          tomasz
                          last edited by

                          @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.

                          Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

                          1 Reply Last reply Reply Quote 0
                          • thomthomT Offline
                            thomthom
                            last edited by

                            The docs should really be mentioning this. 😞

                            Thomas Thomassen β€” SketchUp Monkey & Coding addict
                            List of my plugins and link to the CookieWare fund

                            1 Reply Last reply Reply Quote 0
                            • 1 / 1
                            • First post
                              Last post
                            Buy SketchPlus
                            Buy SUbD
                            Buy WrapR
                            Buy eBook
                            Buy Modelur
                            Buy Vertex Tools
                            Buy SketchCuisine
                            Buy FormFonts

                            Advertisement