1

I have a console application on a mac that puts out an error code via NSLog. How can I capture the results of NSLog when running the console application in ruby?

I've tried approaches like redirecting stderr, but that doesn't seem to do the trick.

OK, I will edit this to be crystal clear.

I have a program written for MacOS that currently reports its output via NSLog. For all intents and purposes, it can just have this line in its main.m file:

NSLog(@"Hello world!");

I want to capture the contents of that NSLog. I cannot change the program itself, I just have the log files. I want to do so in Ruby for the purposes of some rspec-based testing.

Right now, I cannot use redirects. Any kind of redirect, as in:

 @output = `#{theProgramToTest}`
 puts @output

results in no output. If I do the redirection of stderr as described in that previous question I linked to, I still have no result. So how can I capture the results of the program? I do not want to redirect them to a file.

Community
  • 1
  • 1
mmr
  • 14,781
  • 29
  • 95
  • 145
  • 1
    You need to give us a lot more information by showing code and errors. Saying "that doesn't seem to do the trick" is very vague. See http://sscce.org/ for the information we need. – the Tin Man Jul 08 '13 at 18:13
  • Really? Right now, I have no way to take the strings produced by NSLog and use them in ruby. That's a pretty straightforward question. There are no errors. – mmr Jul 08 '13 at 18:15
  • Check http://stackoverflow.com/questions/7271528/nslog-into-file. NSLog() normally writes to the Mac's console. You could probably filter the results out of `/private/var/log/system.log` or one of the other logs but that's an indirect path when you can redirect the output. – the Tin Man Jul 08 '13 at 18:22
  • Thanks, but that's not helpful-- I don't want to redirect to a file. I want to run the console application in a ruby script, and then capture and manipulate the strings that come out of the application. It's not an iOS application, and I don't want to use a file intermediary. Basically, I want to treat the output like the result of any other shell command. – mmr Jul 08 '13 at 18:25
  • Then look into using a [StringIO](http://www.ruby-doc.org/stdlib-2.0/libdoc/stringio/rdoc/index.html) device. You can internally redirect `$stdio` or `$stderr` to write to the string, then later rewind it and access its contents. That's how we capture output in a Ruby sub-process that'd normally try to write to console. But, if it's not an IOS app, and command-line only, why not use the normal [Syslog](http://www.ruby-doc.org/stdlib-2.0/libdoc/syslog/rdoc/index.html) or [Logger](http://www.ruby-doc.org/stdlib-2.0/libdoc/logger/rdoc/index.html) classes? – the Tin Man Jul 08 '13 at 18:27
  • @theTinMan-- I tried that, but it didn't work (as I said, I tried redirecting stderr). The output from NSLog does not appear to be either stderr or stdio, but something else. Well, I used the methods outlined in the question I linked to; unless there's another approach? Also, the application itself is a mac console application, not an ios application. It's written in objective-C, and I need to capture its output for testing purposes. – mmr Jul 08 '13 at 18:31

1 Answers1

0

Maybe this'll help:

require 'stringio'

str_stdout, str_stderr = (1..2).map{ StringIO.new }

puts "redirecting outputs"
old_stdout, old_stderr = $stdout, $stderr
$stdout, $stderr = str_stdout, str_stderr

STDOUT.puts "This is written to the old STDOUT"
STDERR.puts "This is written to the old STDERR"

$stdout.puts "This is written to str_stdout"
$stderr.puts "This is written to str_stderr"

puts 'this is output via "puts"'
`date`
`date >&2` # this is output to stderr

STDOUT.puts "resetting STDOUT and STDERR"

$stdout, $stderr = old_stdout, old_stderr

str_stdout.rewind
str_stderr.rewind

puts str_stdout.read
puts str_stderr.read

Which outputs:

redirecting outputs
This is written to the old STDOUT
This is written to the old STDERR
Mon Jul  8 21:51:19 MST 2013
resetting STDOUT and STDERR
This is written to str_stdout
this is output via "puts"
This is written to str_stderr

Single-step that using a debugger or PRY to see when the various outputs occur.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303