Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sketchup Ruby Console output to file #296

Open
J-E-L opened this issue Jul 24, 2019 · 22 comments
Open

Sketchup Ruby Console output to file #296

J-E-L opened this issue Jul 24, 2019 · 22 comments

Comments

@J-E-L
Copy link

J-E-L commented Jul 24, 2019

For connecting an IDE with Sketchup it would be help full
to get all output from the Ruby Console into a file.

If I redirect “stdout” to a File, I do not get the “ERROR MESSAGES”

Example: (missing ERROR MESSAGES)

$stdout = File.open “C:\temp\my.log“, "a+:utf-8"
# Important ! Switch off Buffer 
$stdout.sync = true

Example: (new feature)
Switch on

SKETCHUP_CONSOLE.file = “C:\temp\my.log“ # Important ! Switch off Buffer

Switch off

SKETCHUP_CONSOLE.file = nil
@thomthom
Copy link
Member

If I redirect “stdout” to a File, I do not get the “ERROR MESSAGES”

Have you tried to also redirect stderr?

@J-E-L
Copy link
Author

J-E-L commented Jul 24, 2019

stderr do not get the „ERROR MESSAGE“

@thomthom
Copy link
Member

What error message are you referring to?

@J-E-L
Copy link
Author

J-E-L commented Jul 25, 2019

Here is an Example.
Run the ruby script, appended below.
You would see:
stdout.log includes the String!
stderr.log is empty!
the error is only printed in the Sketchup ruby Console!

# ruby test start
testPath = "c:/temp"
outFile = File.new("#{testPath}/stdout.log", "w+")
errFile = File.new("#{testPath}/stderr.log", "w+")

outOrg = $stdout
errOrg = $stderr

$stdout = outFile
$stderr = errFile

begin
    puts "Hello"
    raise ArgumentError,"Test ERROR"
ensure
    $stdout = outOrg
    $stderr = errOrg
    outFile.close
    errFile.close
end
# ruby test end 

@thomthom
Copy link
Member

I tried this in standalone Ruby and it doesn't work there either.

If you want to catch errors form your extension you could try something similar to how I collect errors for mine: https://github.com/thomthom/true-bend/blob/master/src/tt_truebend/command.rb

I add an exception handler in a custom UI::Command class that will catch any uncaught exceptions.

@thomthom
Copy link
Member

For connecting an IDE with Sketchup it would be help full to get all output from the Ruby Console into a file.

What IDE are you using? And why do you need to output errors to file when using an ide for debugging? The IDE should be able to let you inspect errors.

@J-E-L
Copy link
Author

J-E-L commented Jul 25, 2019

I use Netbeans.
Netbeans starts as standard ruby script. (PseudoConsole)
The PseudoConsole starts Sketchup with redirecting $stdout and $stderr to files.
The PseudoConsole runs 3 Threads
1.) Read $stdout and print it in PseudoConsole
2.) Read $stderr and print it in PseudoConsole
3.) Read PseudoConsole.input and send it to Sketchup.

To get the Error messages to the file I use TracePoint but it do not get all ERROR yet.

Now I get nearly all output to Netbeans.

The advantage is:
I can jump with a DoubbleClick on the Error Message to the Source Code Line where it happens.

It would we better and faster if I get the Error Message directly.

@MSP-Greg
Copy link

orig_out = $stdout.dup
orig_err = $stderr.dup

path = ENV['TMPDIR'] || ENV['TEMPDIR']

File.open("#{path}/stdout.log", mode: 'w+') { |f| $stdout.reopen f }
File.open("#{path}/stderr.log", mode: 'w+') { |f| $stderr.reopen f }

puts 'Hello from puts'
warn 'Hello from warn'

$stdout = orig_out
$stderr = orig_err

puts 'stdout works'
warn 'stderr works'```

@J-E-L
Copy link
Author

J-E-L commented Jul 25, 2019

Hello MSP-Greg
Your Code did not work on Windows 10 and SketchUp 2019
Error: #<NoMethodError: undefined method `reopen' for #Sketchup::Console:0x000002b0c2aae4f0>

@MSP-Greg
Copy link

@SoapSkinBubble

My bad. Sorry. I saw the mention of stand-alone Ruby, made a few changes, and kind of forgot that SKETCHUP_CONSOLE only has a method or two in common with IO...

@DanRathbun
Copy link

Just for reference, I asked how to send STDIO to an IDE in forum post ...
https://forums.sketchup.com/t/how-to-send-console-output-to-debugger/77659

... but most discussion was in a thread of the SU Debugger GitHub project ...
SketchUp/sketchup-ruby-debugger#19

@MSP-Greg
Copy link

MSP-Greg commented Jul 26, 2019

@SoapSkinBubble

Again, sorry for the previous post. Try the following:

# frozen_string_literal: true

$orig_out, $stdout = $stdout, STDOUT.dup
$orig_err, $stderr = $stderr, STDERR.dup

path = ENV['TMPDIR'] || ENV['TEMPDIR']

File.open("#{path}/stdout.log", mode: 'w+') { |f| $stdout.reopen f }
File.open("#{path}/stderr.log", mode: 'w+') { |f| $stderr.reopen f }

puts "[#{Time.now}] Hello from puts"
warn "[#{Time.now}] Hello from warn"

$stdout.close
$stderr.close

$stdout = $orig_out
$stderr = $orig_err

puts 'Restore SU Ruby console: stdout works'
warn 'Restore SU Ruby console: stderr works'

@J-E-L
Copy link
Author

J-E-L commented Jul 28, 2019

@MSP-Greg
Sorry but you misunderstand my Problem.
Writing to $stdout and $ stderr is working fine but if an error occurs,
the Error Message is not written to one of the Files!

@MSP-Greg
Copy link

@SoapSkinBubble

I posted something in the forum with an attached file. See:

https://forums.sketchup.com/t/redirect-ruby-console-to-file-s-example/99790

I required it in a plugin and set SU_IO.file :err, which routes stderr to a file.

Normally when I load the plugin, a bunch of warnings ('warning: File.exists? is a deprecated name') appear in the console. With the SU_IO.file :err command in the plugin, the warnings do not appear in the console, but are in the file.

This seems to show that stderr is routed to a file, at least for loaded Ruby code.

I did notice that if you use the Ruby console and enter something that generates an error, it appears in the Ruby console... Not sure about that...

@DanRathbun
Copy link

This seems to show that stderr is routed to a file, at least for loaded Ruby code.

I think some of the API exception code writes to $stdout and not $stderr (if memory serves. It seems this has been a complaint in the past.)

@MSP-Greg
Copy link

Since my code never has errors, I just used the warnings that appear in the Ruby console on SU start. Ok, just kidding.

I think some of the API exception code writes to $stdout and not $stderr

Could be. Kind of odd, because the standard Ruby C calls are build with errors going to stderr. Then again, some of the API c code could be quite old...

But, what I meant above is that if one uses the code to route both stdout & stderr to file(s), and then types some nonsense into the console like t = nil + 5, the error info appears in the console.

But, I did that, then triggered something in my plugin UI that writes with warn commands, and the warn string still went to the file. So I don't know...

@DanRathbun
Copy link

The SketchUp Ruby Console has always been weird. I've always wanted to replace it with a better one.

@thomthom
Copy link
Member

Some output in the Ruby Console might be printed directly instead of via stdout or stderr.

@J-E-L
Copy link
Author

J-E-L commented Sep 4, 2019

It would be very good if the entire output goes over stdout and stderr.
That would help me a lot.

@DanRathbun
Copy link

DanRathbun commented Aug 29, 2020

@thomthom Can we get this issue changed to a bug, as ALL output should go through $stdout and $stderr ?

@sketchupbot
Copy link

Logged as: SU-47093

@DanRathbun
Copy link

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants