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

Writing Ruby Console messages to a text file

Scheduled Pinned Locked Moved Developers' Forum
9 Posts 5 Posters 3.8k Views 5 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.
  • A Offline
    Al Hart
    last edited by Al Hart 24 Sept 2010, 06:53

    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

    
    # 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]

    Al Hart

    http:wiki.renderplus.comimageseefRender_plus_colored30x30%29.PNG
    IRender nXt from Render Plus

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 24 Sept 2010, 07:10

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

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

      1 Reply Last reply Reply Quote 0
      • H Offline
        honoluludesktop
        last edited by 24 Sept 2010, 09:10

        Wow, very neat!

        1 Reply Last reply Reply Quote 0
        • J Offline
          Jim
          last edited by 24 Sept 2010, 13:26

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

          $stderr = $stdout

          Hi

          1 Reply Last reply Reply Quote 0
          • A Offline
            Al Hart
            last edited by 24 Sept 2010, 16:46

            @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

            Al Hart

            http:wiki.renderplus.comimageseefRender_plus_colored30x30%29.PNG
            IRender nXt from Render Plus

            1 Reply Last reply Reply Quote 0
            • A Offline
              Al Hart
              last edited by 24 Sept 2010, 16:47

              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

              Al Hart

              http:wiki.renderplus.comimageseefRender_plus_colored30x30%29.PNG
              IRender nXt from Render Plus

              1 Reply Last reply Reply Quote 0
              • D Offline
                dburdick
                last edited by 4 May 2011, 23:52

                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.

                1 Reply Last reply Reply Quote 0
                • A Offline
                  Al Hart
                  last edited by 5 May 2011, 01:17

                  Keep your ese on this thread as well: [Plugin] Trace Ruby messages

                  Al Hart

                  http:wiki.renderplus.comimageseefRender_plus_colored30x30%29.PNG
                  IRender nXt from Render Plus

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    dburdick
                    last edited by 5 May 2011, 03:31

                    Hi Al,

                    Thanks - will do.

                    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