|
[ruby-list:42077] Re: puts,printの出力をファイルにも出力するには: msg#00108lang.ruby.japanese
From: 岩崎 弘孝 <IH000667-Hi7X7xagcuRnb2RyOFEZTHf5DAMn2ifp@xxxxxxxxxxxxxxxx> Subject: [ruby-list:42074] Re: puts,printの出力をファイルにも出力するには Date: Fri, 21 Apr 2006 14:47:14 +0900 > 実際の利用はインタラクティブな処理のログのようなものなので > 最後にまとめて出力されるのは用途に向きません。 > 今までは標準出力だけでよいという仕様だったのですが、 > ファイルにも出力するようにと要求があがってきました。 ではこれで class << IO # Redirect stdout to STDOUT and executes the block. def redirect(stdout) begin stdout_sv = STDOUT.dup STDOUT.reopen(stdout) yield ensure STDOUT.flush STDOUT.reopen(stdout_sv) end end def tee(filename, mode="w+", &block) File.open(filename, mode) do |fio| pos = File.exist?(filename) ? File.size(filename) : 0 redirect(fio, &block) fio.pos = pos print fio.read end end def teea(filename, mode="a+", &block) tee(filename, mode, &block) end end require 'tmpdir' require 'fileutils' file = "#{Dir.tmpdir}/output" # FileUtils.rm_f file IO.teea(file) { puts "puts to file1" system "echo system to file1" } sleep 1 puts "puts to STDOUT" sleep 1 IO.teea(file) { puts "puts to file2" system "echo system to file2" } 出力したいタイミングごとにIO.teeaを使っています。 # aはappendの略 -- rubikitch http://www.rubyist.net/~rubikitch/
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | [ruby-list:42076] Re: puts,printの出力をファイルにも出力するには, Yukihiro Matsumoto |
|---|---|
| Next by Date: | [ruby-list:42078] Re: puts,printの出力をファイルにも出力するには, 岩崎 弘孝 |
| Previous by Thread: | [ruby-list:42076] Re: puts,printの出力をファイルにも出力するには, Yukihiro Matsumoto |
| Next by Thread: | [ruby-list:42078] Re: puts,printの出力をファイルにも出力するには, 岩崎 弘孝 |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |