Module: CheapAdvice::Trace

Defined in:
lib/cheap_advice/trace.rb

Overview

Sample Tracing Advice factory.

Defined Under Namespace

Modules: Behavior Classes: BaseFormatter, DefaultFormatter, YamlFormatter

Class Method Summary collapse

Class Method Details

.new(opts = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cheap_advice/trace.rb', line 8

def self.new opts = nil
  opts ||= { }
  trace = CheapAdvice.new(:around, opts) do | ar, body |
    a = ar.advice
    ad = ar.advised
    logger = ad.logger[:name] || ad.logger_default[:name]
    logger = a.logger[logger] || a.logger_default[logger]

    formatter = nil
    if ad[:log_before] != false
      a.log(logger) do
        formatter = a.new_formatter(logger)
        ar[:time_before] = Time.now
        formatter.record(ar, :before)
      end
    end

    body.call

    if ad[:log_after] != false
      a.log(logger) do
        formatter ||= a.new_formatter(logger)
        ar[:time_after] = Time.now
        if ar.error
          ar[:error] = ar.error   if ad[:log_error] != false
        else
          ar[:result] = ar.result if ad[:log_result] != false
        end
        formatter.record(ar, :after)
      end
    end
  end
  trace.extend(Behavior)
  trace.advised_extend = Behavior
  trace
end