lib/ramaze/inform/informer.rb in ramaze-0.1.1 vs lib/ramaze/inform/informer.rb in ramaze-0.1.2
- old
+ new
@@ -3,27 +3,32 @@
module Ramaze
class Informer
include Informing
- attr_accessor :colorize, :out
+ attr_accessor :out, :colorize
+ # Should Ramaze try to use colors?
+ trait :colorize => true
+
# parameter for Time.now.strftime
trait :timestamp => "%Y-%m-%d %H:%M:%S"
# This is how the final output is arranged.
trait :format => "[%time] %prefix %text"
# Which tag should be in what color
- trait :colors => {
+ COLORS = {
:info => :green,
:debug => :yellow,
:warn => :red,
:error => :red,
}
- def initialize(out = $stdout, colorize = nil)
+ def initialize(out = $stdout)
+ @colorize = false
+
@out =
case out
when STDOUT, :stdout, 'stdout'
$stdout
when STDERR, :stderr, 'stderr'
@@ -32,16 +37,17 @@
out
else
if out.respond_to?(:puts)
out
else
- colorize = false
File.open(out.to_s, 'ab+')
end
end
- @colorize = @out.tty? rescue false if colorize.nil?
+ if @out.respond_to?(:tty?) and class_trait[:colorize]
+ @colorize = @out.tty?
+ end
end
def shutdown
if @out.respond_to?(:close)
Inform.debug("close, #{@out.inspect}")
@@ -54,10 +60,10 @@
messages.flatten!
prefix = tag.to_s.upcase.ljust(5)
if @colorize
- color = class_trait[:colors][tag] ||= :white
+ color = COLORS[tag] ||= :white
prefix.replace prefix.send(color)
end
messages.each do |message|
@out.puts(log_interpolate(prefix, message))