Redirecting/Logging output

Hello,
the following code solves my problem for now, but is there a "nicer"
solution?
--------------------- SOS --------------------
$logfile = File.new ‘log.txt’,'w’
def puts (*args) $defout.puts args; $logfile.puts args end
def putc (arg) $defout.putc arg; $logfile.putc arg end
def print(*args) $defout.print args; $logfile.print args end
def printf (format,a1=nil,a2=nil,a3=nil,a4=nil,a5=nil,a6=nil,a7=nil,a8
=nil)
$defout.printf format, a1,a2,a3,a4,a5,a6,a7,a8
$logfile.printf format, a1,a2,a3,a4,a5,a6,a7,a8
end
puts Time.new,1,2,3,4; printf "%02x ",127; print 1,2,3; putc ‘a’
-------------------- EOS ------------------------

Michael B.

Hi,

the following code solves my problem for now, but is there a “nicer”
solution?

class << (log = File.new(‘log.txt’,‘w’))
def write(s)
STDOUT.write(s)
super
end
def putc(s)
STDOUT.putc(s)
super
end
end
$defout = log

···

At Thu, 27 Feb 2003 21:59:18 +0900, Michael Bruschkewitz wrote:


Nobu Nakada

how about:

class SplitOutput
def initialize(*ios)
@outputs = ios
end

def print(*args)
@outputs.each{ |io| io.print(*args) }
end

def printf(*args)
@outputs.each{ |io| io.printf(*args) }
end

def puts(*args)
@outputs.each{ |io| io.puts(*args) }
end

def putc(arg)
@outputs.each{ |io| io.putc(arg) }
end
end

$logfile = File.new ‘log.txt’,‘w’
$logger = SplitOutput.new($stdout, $logfile)

$logger.printf “test”

robert

“Michael Bruschkewitz” brusch2@gmx.net schrieb im Newsbeitrag
news:MPG.18c82611c3c3067b989699@192.168.0.61

···

Hello,
the following code solves my problem for now, but is there a “nicer”
solution?
--------------------- SOS --------------------
$logfile = File.new ‘log.txt’,‘w’
def puts (*args) $defout.puts args; $logfile.puts args end
def putc (arg) $defout.putc arg; $logfile.putc arg end
def print(*args) $defout.print args; $logfile.print args end
def printf (format,a1=nil,a2=nil,a3=nil,a4=nil,a5=nil,a6=nil,a7=nil,a8
=nil)
$defout.printf format, a1,a2,a3,a4,a5,a6,a7,a8
$logfile.printf format, a1,a2,a3,a4,a5,a6,a7,a8
end
puts Time.new,1,2,3,4; printf "%02x ",127; print 1,2,3; putc ‘a’
-------------------- EOS ------------------------

Michael B.

Under 1.6.8, overriding of 'putc' does not work properly. Example:

  print "Hello"
  putc 32
  printf "world%s\n", "!"

prints "Helloworld!\n" to STDOUT, but puts the correct answer in the log
file. But it works if you change it to:

  $defout.putc 32

Any idea what is going on?

Regards,

Brian.

···

On Thu, Feb 27, 2003 at 10:13:01PM +0900, nobu.nokada@softhome.net wrote:

class << (log = File.new('log.txt','w'))
  def write(s)
    STDOUT.write(s)
    super
  end
  def putc(s)
    STDOUT.putc(s)
    super
  end
end
$defout = log

$logfile = File.new 'log.txt','w'
$logger = SplitOutput.new($stdout, $logfile)

$logger.printf "test"

You can't use it as $defout unless you add a 'write' method as well:

  $defout = $logger
  >> $> must have write method, SplitOutput given (TypeError)

Regards,

Brian.

···

On Thu, Feb 27, 2003 at 10:59:50PM +0900, Robert Klemme wrote:

Hi,

Under 1.6.8, overriding of ‘putc’ does not work properly. Example:

print “Hello”
putc 32
printf “world%s\n”, “!”

prints “Helloworld!\n” to STDOUT, but puts the correct answer in the log
file. But it works if you change it to:

$defout.putc 32

Any idea what is going on?

I’d forgot about 1.6.

class << (log = File.new(‘log.txt’,‘w’))
def write(s)
STDOUT.write(s)
super
end
end
$defout = log
#if RUBY_VERSION < “1.7” ### same as builtin in 1.8, but slower
def putc(c)
$defout.write(c)
end
#end

···

At Thu, 27 Feb 2003 23:03:43 +0900, Brian Candler wrote:


Nobu Nakada