Professional Web Applications Themes

Questions about stdout/stderr combining (for Windows & Linux) - Ruby

I'm working on a build tool, and I need to execute various compilers/linkers and capture their output (both stdout and stderr) for Windows and Linux compilations (msvc & gcc). The problem is I want the output to stderr and stdout to go to the same pipe (same result as if you were running the program from the command-line). I can use a hack like: out = `cmd /c \"#{cmdLine}\" >bs.out 2>&1` on windows and then simply read bs.out (which will contain both stdout and stderr), but I'd prefer to avoid the overhead (and platform specific code) of spawning cmd.exe to ...

  1. #1

    Default Questions about stdout/stderr combining (for Windows & Linux)

    I'm working on a build tool, and I need to execute various
    compilers/linkers and capture their output (both stdout and stderr) for
    Windows and Linux compilations (msvc & gcc).
    The problem is I want the output to stderr and stdout to go to the same
    pipe (same result as if you were running the program from the
    command-line). I can use a hack like:
    out = `cmd /c \"#{cmdLine}\" >bs.out 2>&1`
    on windows and then simply read bs.out (which will contain both stdout
    and stderr), but I'd prefer to avoid the overhead (and platform specific
    code) of spawning cmd.exe to run another program.
    I'd prefer to use something like popen, but in a way that lets me get a
    single pipe I can read from that the process's stdout & stderr pipes are
    connected to.
    Due to different compiler's general messages and warnings/errors being
    logged interchangably to stdout or stderr, it's not really feasible to
    have the pipes be separate.

    Any suggestions?

    --
    Patrick




    Patrick Guest

  2. #2

    Default Nuby: I'am only want to copy a File!

    Hello,

    I am currently reading the book 'Programming Ruby' and during this time
    I want to write same scripts with the new stuff I've learned. At the
    moment I search a smart way to copy a file and I want to say sorry but I
    didn't find a methode like FILE#copy(theNewFileName) or
    FILE.copy(theOldFileName, theNewFileName). Is there another way to do
    this?

    One way I find:
    - open the file which I want to copy
    - read each line and write each one into the new file at a the place I
    want to copy it..

    But that is not so smart like the other stuff of ruby..

    At last I want to say sorry for my bad english, it is so hard for me to
    speak and write my native language and so there is no way for me to
    write english a better way (-;



    best regards

    bovi



    Daniel Guest

  3. #3

    Default Re: Nuby: I'am only want to copy a File!

    fileutils wouldnt do the trick, no ?

    http://www.ruby-doc.org/stdlib/libdoc/fileutils/rdoc/index.html


    Cheers

    Rove


    Daniel Bovensiepen wrote:
     

    --
    Rove Monteux
    Systems Administrator

    com




    Rove Guest

  4. #4

    Default Re: Nuby: I'am only want to copy a File!

    thanks thanks
    and thanks..



    On Mon, 2004-02-02 at 21:09, Rove Monteux wrote: [/ref]



    Daniel Guest

  5. #5

    Default Re: Questions about stdout/stderr combining (for Windows & Linux)

    Patrick Bennett wrote: 

    What about separate pipes, two threads reading them and putting messages
    in a queue, and a third thread pulling messages in from the queue?


    Joel Guest

  6. #6

    Default Re: Questions about stdout/stderr combining (for Windows & Linux)

    Joel VanderWerf wrote:
     

    It wouldn't work. Messages could still appear out of order unfortunately.




    Patrick Guest

  7. #7

    Default Re: Questions about stdout/stderr combining (for Windows & Linux)

    Patrick Bennett wrote: 

    This works for me on Windows 2000, Ruby 1.8.0:


    D:\Temp>type r.rb

    def parent
    IO.popen "ruby #{__FILE__} child 2>&1" do |io|
    while line = io.gets
    puts "got #{line}"
    end
    end
    end

    def child
    $stdout.puts "child 1 out"; $stdout.flush
    $stderr.puts "child 2 err"; $stderr.flush
    $stderr.puts "child 3 err"; $stderr.flush
    $stdout.puts "child 4 out"; $stdout.flush
    end

    ARGV.empty? ? parent : child


    D:\Temp>r
    got child 1 out
    got child 2 err
    got child 3 err
    got child 4 out

    D:\Temp>


    HTH

    Regards,
    Pit


    Pit Guest

  8. #8

    Default Re: Questions about stdout/stderr combining (for Windows & Linux)

    On Tue, 3 Feb 2004, Patrick Bennett wrote:
     
    >
    > It wouldn't work. Messages could still appear out of order unfortunately.[/ref]

    why not use ruby to do the deed for you?

    ~/eg/ruby > cat a.rb
    #!/usr/bin/env ruby
    require 'rbconfig'

    class Spawn
    class << self; alias [] new; end
    RUBY = File.join(Config::CONFIG["bindir"], Config::CONFIG["ruby_install_name"])
    PROGRAM = <<-code
    STDERR.reopen(STDOUT)
    exec(*ARGV)
    code
    attr :out
    def initialize argv
    out = `#{ RUBY } -e '#{ PROGRAM }' #{ argv.join(' ') }`
    end
    end

    stdout_stderr = Spawn[ ARGV ].out
    puts "STDOUT_STDERR:\n"
    puts stdout_stderr

    ~/eg/ruby > a.rb ls a.rb
    STDOUT_STDERR:
    a.rb

    ~/eg/ruby > a.rb ls does-not-exist
    STDOUT_STDERR:
    ls: does-not-exist: No such file or directory


    i have not tested this on windows - but something like this should work.

    -a
    --

    ATTN: please update your address books with address below!

    ================================================== =============================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | STP :: http://www.ngdc.noaa.gov/stp/
    | NGDC :: http://www.ngdc.noaa.gov/
    | NESDIS :: http://www.nesdis.noaa.gov/
    | NOAA :: http://www.noaa.gov/
    | US DOC :: http://www.commerce.gov/
    |
    | The difference between art and science is that science is what we
    | understand well enough to explain to a computer.
    | Art is everything else.
    | -- Donald Knuth, "Discover"
    |
    | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
    ================================================== =============================

    Ara.T.Howard Guest

Similar Threads

  1. Redirect stdout, stderr to file and stdout
    By Larry Guest in forum PERL Beginners
    Replies: 8
    Last Post: January 20th, 10:42 AM
  2. Run one process and get the stdout and stderr
    By Marcos Rebelo in forum PERL Beginners
    Replies: 2
    Last Post: October 16th, 12:01 PM
  3. [PHP-DEV] STDOUT, STDERR not defined in CLI mode
    By Alex Kiesel in forum PHP Development
    Replies: 0
    Last Post: August 8th, 11:36 AM
  4. 1.8.0, fcgi and $stdout/$stderr
    By Brian Candler in forum Ruby
    Replies: 1
    Last Post: July 28th, 10:11 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139