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

    Ruby Console access from C extension

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 4 Posters 412 Views 4 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.
    • T Offline
      tomasz
      last edited by

      I am trying to write directly from a Ruby C++ extension to the Ruby Console through a

      std;;cout << "Text\n";
      

      and/or

      prinf("Text\n");
      

      , but have no luck. Nothing shows up in the RC.
      Is not Ruby extension standard output redirected to the Ruby Console?

      I guess I can still try invoking from C++ Ruby method SKETCHUP_CONSOLE.write("Text\n) described in this thread:
      , but maybe there is a way to redirect std:cout to the Ruby Console.

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

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

        The Sketchup::Console does weird things to std I/O.

        I would say that you need to create a extern "C" { } block, and try to use one of the C Ruby functions.

        /* Your C++ code */
        
        #ifdef __cplusplus
        extern "C"{
        #endif
        
        // C code in here
        VALUE
        function tomaz_puts_to_con(str)
            VALUE str;
        {
            int argc;
            VALUE *argv;
            VALUE out;
            out = rb_stdout;
            argc = 1;
            argv[0] = rb_str_new2(str)
            rb_io_puts(argc, argv, out)
        }
        
        #ifdef __cplusplus
        }
        #endif
         
        

        if puts() does not work, try write() (...that happens on the Ruby side.)

        /* Your C++ code */
        
        #ifdef __cplusplus
        extern "C"{
        #endif
        
        // C code in here
        VALUE
        function tomaz_write_to_con(str)
            VALUE str;
        {
            VALUE out;
            out = rb_stdout;
            // io_write will convert str to ruby T_STRING
            io_write(out, str);
        }
        
        #ifdef __cplusplus
        }
        #endif
         
        

        I'm not here much anymore.

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

          EDIT: note that rb_eval_string(); is slow.

          And I think you can also do:

          
          /* Your C++ code */
          
            str = "$stdout.write('some message')\n"
            tomaz_write_to_con(str)
          
          #ifdef __cplusplus
          extern "C"{
          #endif
           
          // C code in here
          VALUE
          function tomaz_write_to_con(str)
              VALUE str;
          {
              rb_eval_string(str);
          }
          
          #ifdef __cplusplus
          }
          #endif
          

          or perhaps right from C++:

          rb_eval_string("$stdout.write('some message')\n");
          
          

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • tbdT Offline
            tbd
            last edited by

            I used OutputDebugString and DbgView to view the messages and never had a problem compared with random crashes in Ruby world due stack overwriting.

            you can also create a singleton wrapper if you want to log from Ruby world without going to the slow ruby console.

            SketchUp Ruby Consultant | Podium 1.x developer
            http://plugins.ro

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

              @unknownuser said:

              I used OutputDebugString and DbgView to view the messages

              Ditto. But is there a OSX alternative?

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

              1 Reply Last reply Reply Quote 0
              • tbdT Offline
                tbd
                last edited by

                system this command:

                logger "this is my message in the console.app All Messages"
                

                SketchUp Ruby Consultant | Podium 1.x developer
                http://plugins.ro

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

                  @unknownuser said:

                  system this command:

                  logger "this is my message in the console.app All Messages"
                  

                  Thankyou! 👍 👍

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

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

                    @thomthom said:

                    @unknownuser said:

                    I used OutputDebugString and DbgView to view the messages

                    Ditto. But is there a OSX alternative?

                    There is the "Log Navigator":
                    https://developer.apple.com/library/mac/#recipes/xcode_help-log_navigator/_index.html

                    I'm not here much anymore.

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

                      @unknownuser said:

                      system this command:

                      logger "this is my message in the console.app All Messages"
                      

                      See docs:

                      • logger

                      • syslog

                      • syslogd

                      EDIT(2018-09-17): The old way of calling system('logger','my log message') seems to have been replaced with a unified system wide logging platform.

                      See: https://developer.apple.com/documentation/os/logging

                      I'm not here much anymore.

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

                        I have tried the last solution:

                        rb_eval_string("$stdout.write(\"Message\n\")");
                        

                        and it works well. I am using it for debugging purpose, so the speed is not important for me.

                        Thanks!

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

                        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