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::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, 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 } #endif
or 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!
Advertisement