Logging irb stdin & stdout to file but still remain showing

Discussion in 'Ruby' started by Shin guey Wong, Dec 12, 2007.

  1. Hi,

    I search the ruby-talk and find a few discussion on redirecting the
    STDIN, STDOUT and STDERR. However, I still couldn't get it to work on
    redirecting the IO to file and also to the console. I am working on
    windows.

    Here is what I want to do,
    1. run irb,
    2. run a command like :
    log "c:\irb_log.txt"
    3. continue on irb and run other command, all the stdin, stdout and
    stderr will be logging into the irb_log.txt and still showing on screen.
    4. run nolog, :it will stop loggin the stdin, stdout and stderr.

    Anyone has any idea how to do this?
    I have try to redirect the output like STDOUT.repopen("c:\tmp.log",
    "w"), but i will not get the output on the console.

    Thanks in advance.
    --
    Posted via http://www.ruby-forum.com/.
     
    Shin guey Wong, Dec 12, 2007
    #1
    1. Advertising

  2. Re: Logging irb stdin & stdout to file but still remain show

    Shin guey Wong wrote:

    > Anyone has any idea how to do this?
    > I have try to redirect the output like STDOUT.repopen("c:\tmp.log",
    > "w"), but i will not get the output on the console.


    No answer? well, I just found an ugly hack on myself....
    heres the code, just put it in the .irbrc file and you will get those 2
    functions, log, nolog for free...:p

    For the STDIN, I am really no idea how to get it, I had to hack the IRB
    to get it work, here, I only get the STDIN from the Readline(if the user
    didn't enable readline in IRB, they will never get the stdin log in the
    file.

    Did anyone has better idea on accomplish this? How to get the STDIN from
    the IRB history?

    module Kernel
    def log(file_name, attr)
    $irb_log_file = File.open(file_name, attr)
    end

    def nolog
    if ($irb_log_file )
    $irb_log_file.close
    end
    $irb_log_file = nil
    end
    end

    class << STDOUT
    alias :eek:ld_write :write
    def write(*args)
    if ($irb_log_file)
    $irb_log_file.write args
    end
    old_write(args)
    end
    end

    class << STDERR
    alias :eek:ld_write :write
    def write(*args)
    if ($irb_log_file)
    $irb_log_file.write args
    end
    old_write(args)
    end
    end

    module IRB
    class ReadlineInputMethod < InputMethod
    alias :eek:ld_gets :gets
    def gets
    l = old_gets
    if($irb_log_file)
    $irb_log_file.puts @prompt << l
    end
    l
    end
    end
    end

    Here is how you use it:
    ---------------------------
    >> log 'c:\irb.log', 'w'

    => #<File:c:\irb.log>
    >> puts 'hehe'

    hehe
    => nil
    >> puts 'hello'

    hello
    => nil
    >> nolog

    => nil
    >>


    And the log file:
    --------------------
    => #<File:c:\irb.log>
    >> puts 'hehe'

    hehe
    => nil
    >> puts 'hello'

    hello
    => nil
    >> nolog


    --
    Posted via http://www.ruby-forum.com/.
     
    Shin guey Wong, Dec 12, 2007
    #2
    1. Advertising

  3. Shin guey Wong

    Guest

    Re: Logging irb stdin & stdout to file but still remain showin

    Hey,
    I don't have the answer but I think it can be done by overriding the
    conf settings.

    Open up IRB and type conf
    I am on a mac and I see
    conf.io=#<IRB::ReadlineInputMethod:0x3b63c>
    conf.irb=#<IRB::Irb:0x41758>
    conf.irb_name="irb"
    conf.irb_path="(irb)"
    conf.last_value=...
    conf.line_no=12
    conf.load_modules=[]
    conf.output_method=#<IRB::StdioOutputMethod:0x1c96c>

    The StdioOutputMethod rdoc only shows a print method
    http://www.ruby-doc.org/core/classes/IRB/StdioOutputMethod.html
    And all the code does is pass the args to STDOUT.print. If your log
    class has a print method try setting your conf.output_method to an
    instance of it.

    The ReadLineInput is the same but with more methods.
    http://www.ruby-doc.org/core/classes/IRB/ReadlineInputMethod.html
    Once you figure this part out you can drop it in your irbrc file it
    will load every time you start up irb.

    Thanks
    Becker

    ---------- Forwarded message ----------
    From: Shin guey Wong <>
    Date: Dec 12, 2007 9:58 AM
    Subject: Logging irb stdin & stdout to file but still remain showin
    To: ruby-talk ML <>


    Hi,

    I search the ruby-talk and find a few discussion on redirecting the
    STDIN, STDOUT and STDERR. However, I still couldn't get it to work on
    redirecting the IO to file and also to the console. I am working on
    windows.

    Here is what I want to do,
    1. run irb,
    2. run a command like :
    log "c:\irb_log.txt"
    3. continue on irb and run other command, all the stdin, stdout and
    stderr will be logging into the irb_log.txt and still showing on screen.
    4. run nolog, :it will stop loggin the stdin, stdout and stderr.

    Anyone has any idea how to do this?
    I have try to redirect the output like STDOUT.repopen("c:\tmp.log",
    "w"), but i will not get the output on the console.

    Thanks in advance.
    --
    Posted via http://www.ruby-forum.com/.
     
    , Dec 16, 2007
    #3
  4. Giles Bowkett, Dec 16, 2007
    #4
  5. Shin guey Wong

    James Gray Guest

    Re: Logging irb stdin & stdout to file but still remain showin

    On Dec 16, 2007, at 10:07 AM, Giles Bowkett wrote:

    > I thought Windows didn't have STDIN/OUT/ERR.


    Really? Why?

    James Edward Gray II
     
    James Gray, Dec 16, 2007
    #5
  6. Giles Bowkett, Dec 17, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    2,626
  2. Prakhar
    Replies:
    3
    Views:
    309
    Default User
    Jul 19, 2007
  3. Ronald Fischer
    Replies:
    13
    Views:
    174
    Robert Klemme
    Jul 4, 2007
  4. Ed Hames
    Replies:
    0
    Views:
    380
    Ed Hames
    Apr 16, 2008
  5. Edgardo Hames
    Replies:
    1
    Views:
    354
    Ed Hames
    May 6, 2008
Loading...

Share This Page