Added the support class, confined it load within the OLE namespace

git-svn-id: file:///home/svn/framework3/trunk@4542 4d416f70-5f16-0410-b530-b9f4589650da
unstable
HD Moore 2007-03-20 18:25:19 +00:00
parent 9c3bfaeee4
commit 5f2d59ccad
3 changed files with 55 additions and 5 deletions

View File

@ -1,5 +1,6 @@
module Ole # :nodoc:
require 'ole/support'
Log = Logger.new_with_callstack
end

View File

@ -1,20 +1,17 @@
#! /usr/bin/ruby -w
$: << File.dirname(__FILE__) + '/..'
require 'iconv'
require 'date'
require 'support'
require 'stringio'
require 'tempfile'
require 'ole/base'
require 'ole/types'
# not strictly ole related
require 'ole/io_helpers'
module Ole # :nodoc:
#
# = Introduction
#

52
lib/ole/support.rb Normal file
View File

@ -0,0 +1,52 @@
#! /usr/bin/ruby
#
# A file with general support functions used by most files in the project.
#
require 'logger'
class File # :nodoc:
# for consistency with StringIO and others. makes more sense than forcing
# them to provide a #stat
def size
stat.size
end
end
class Symbol # :nodoc:
def to_proc
proc { |a| a.send self }
end
end
module Enumerable # :nodoc:
# 1.9 backport
def group_by
hash = Hash.new { |hash, key| hash[key] = [] }
each { |item| hash[yield(item)] << item }
hash
end
def sum initial=0
inject(initial) { |a, b| a + b }
end
end
class Logger # :nodoc:
# A helper method for creating <tt>Logger</tt>s which produce call stack
# in their output
def self.new_with_callstack logdev=STDERR
log = Logger.new logdev
log.level = WARN
log.formatter = proc do |severity, time, progname, msg|
# find where we were called from, in our code
callstack = caller.dup
callstack.shift while callstack.first =~ /\/logger\.rb:\d+:in/
from = callstack.first.sub(/:in `(.*?)'/, ":\\1")
"[%s %s]\n%-7s%s\n" % [time.strftime('%H:%M:%S'), from, severity, msg.to_s]
end
log
end
end