Module: CheapAdvice::Trace::Behavior

Defined in:
lib/cheap_advice/trace.rb

Instance Method Summary collapse

Instance Method Details

#formatter(logger) ⇒ Object



58
59
60
61
62
# File 'lib/cheap_advice/trace.rb', line 58

def formatter logger
  logger[:formatter] ||= 
    logger_default[:formatter] ||= 
    DefaultFormatter
end

#formatter_options(logger) ⇒ Object



64
65
66
67
68
# File 'lib/cheap_advice/trace.rb', line 64

def formatter_options logger
  logger[:formatter_options] ||=
    logger_default[:formatter_options] ||=
    [ ]
end

#log(logger, msg = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cheap_advice/trace.rb', line 83

def log logger, msg = nil
  return msg unless logger
  msg ||= yield if block_given?
  return msg if msg.nil?
  dst = logger[:target]
  case dst
  when nil
    nil
  when IO
    dst.seek(0, IO::SEEK_END)
    dst.puts msg.to_s
    dst.flush
  when Proc
    dst.call(msg)
  else
    dst.send(logger[:method] || :debug, msg)
  end
  msg
end

#log_all(msg = nil) ⇒ Object



103
104
105
106
107
108
# File 'lib/cheap_advice/trace.rb', line 103

def log_all msg = nil
  logger.values.each do | dst |
    log(dst) { msg ||= yield if block_given?; msg }
  end
  msg
end

#log_prefix(logger, ar) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cheap_advice/trace.rb', line 70

def log_prefix logger, ar
  pre = 
    logger[:log_prefix] ||= 
    logger_default[:log_prefix] ||= 
    EMPTY_String
  case pre
  when Proc
    pre.call(ar)
  else
    pre
  end
end

#loggerObject



46
47
48
49
# File 'lib/cheap_advice/trace.rb', line 46

def logger
  # $stderr.puts " #{self.class} @options = #{@options.inspect}"
  @options[:logger] ||= { }
end

#logger_defaultObject



50
51
52
# File 'lib/cheap_advice/trace.rb', line 50

def logger_default
  logger[nil] ||= { }
end

#new_formatter(logger) ⇒ Object



54
55
56
# File 'lib/cheap_advice/trace.rb', line 54

def new_formatter logger
  formatter(logger).new(logger, *formatter_options(logger))
end