Class: Card::Log::Performance

Inherits:
Object
  • Object
show all
Defined in:
lib/card/log.rb

Defined Under Namespace

Modules: BigBrother Classes: Entry

Constant Summary collapse

DEFAULT_CLASS =

To enable logging add a performance_logger hash to your configuration and change the log_level to :wagn config options

Example: config.performance_logger =

:min_time => 100,                              # show only method calls that are slower than 100ms
:max_depth => 3,                               # show nested method calls only up to depth 3
:details=> true                                # show method arguments and sql
:methods => [:event, :search, :fetch, :view],  # choose methods to log

If you give :methods a hash you can log arbitrary methods. The syntax is as follows:

class =>  method type => method name => log options

Example:

Card  => {
           :instance  => [ :fetch, :search ],
           :singleton => { :fetch    => { :title => 'Card.fetch' } },
           :all       => {
                           :fetch    => {
                                          :message => 2                           # use second argument passed to fetch
                                          :details => :to_s                       # use return value of to_s in method context
                                          :title => proc { |method_context| method_context.name }
                                        },
                         },
         },

class, method type and log options are optional. Default values are ‘Card’, ‘:all’ and { :title => method name, :message => first argument, :details=> remaining arguments }. For example [:fetch] is equivalent to Card => { :all => { :fetch => { :message=>1, :details=>1..-1 } }

Card
DEFAULT_METHOD_TYPE =
:all
DEFAULT_METHOD_OPTIONS =
{
  :title   => :method_name,
  :message => 1,
  :details => 1..-1,
  :context => nil
}
SPECIAL_METHODS =

these methods have already a Wagn.with_logging block

[:search, :view, :event]
TAB_SIZE =
3
@@log =
[]
@@context_entries =
[]
@@active_entries =
[]
@@current_level =
0

Class Method Summary collapse

Class Method Details

.enable_method(method_name) ⇒ Object



157
158
159
160
# File 'lib/card/log.rb', line 157

def enable_method method_name
  @enabled_methods ||= ::Set.new
  @enabled_methods << method_name
end

.enabled_method?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/card/log.rb', line 162

def enabled_method? method_name
  @enabled_methods && @enabled_methods.include?(method_name)
end

.load_config(args) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/card/log.rb', line 94

def load_config args
  @details   = args[:details]   || false
  @max_depth = args[:max_depth] || false
  @min_time  = args[:min_time]  || false
  @enabled_methods = ::Set.new
  prepare_methods_for_logging args[:methods] if args[:methods]
end

.start(args = {}) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/card/log.rb', line 102

def start args={}
  @@current_level = 0
  @@log = []
  @@context_entries = []
  @@active_entries = []
  @@first_entry = new_entry(args)
end

.stopObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/card/log.rb', line 110

def stop
  while (entry = @@context_entries.pop) do
    finish_entry entry
  end
  if @@first_entry
    @@first_entry.save_duration
    finish_entry @@first_entry
  end
  print_log
end

.with_timer(method, args, &block) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/card/log.rb', line 122

def with_timer method, args, &block
  if args[:context]

    # if the previous context was created by an entry on the same level
    # then finish the current context if it's a different context
    if @@context_entries.last && @@current_level == @@context_entries.last.level+1 &&
                                 args[:context] != @@context_entries.last.context
      finish_entry @@context_entries.pop
    end

    # start new context if it's different from the parent context
    if  @@context_entries.empty? || args[:context] != @@context_entries.last.context
      @@context_entries << new_entry( :title=>'process', :message=>args[:context], :context=>args[:context] )
    end
  end

  timer = new_entry args.merge(:method=>method )
  begin
    result = block.call
  ensure
    timer.save_duration
    finish_entry timer

    # finish all deeper nested contexts
    while @@context_entries.last && @@context_entries.last.level >= @@current_level
      finish_entry @@context_entries.pop
    end
    # we don't know whether the next entry will belong to the same context or will start a new one
    # so we save the time
    @@context_entries.last.save_duration if @@context_entries.last
  end
  result
end