Ruby Console access from C extension
-
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. -
The
Sketchup::Consoledoes 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 } #endifif
puts()does not work, trywrite()(...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 -
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 } #endifor perhaps right from C++:
rb_eval_string("$stdout.write('some message')\n"); -
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.
-
@unknownuser said:
I used OutputDebugString and DbgView to view the messages
Ditto. But is there a OSX alternative?
-
system this command:
logger "this is my message in the console.app All Messages" -
@unknownuser said:
system this command:
logger "this is my message in the console.app All Messages"Thankyou!

-
@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 -
@unknownuser said:
system this command:
logger "this is my message in the console.app All Messages"See docs:
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. -
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!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register LoginAdvertisement