Class: Cardio::Logger::Performance

Inherits:
Object
  • Object
show all
Extended by:
MethodPreparation
Defined in:
lib/cardio/logger/performance.rb,
lib/cardio/logger/performance/entry.rb,
lib/cardio/logger/performance/big_brother.rb,
lib/cardio/logger/performance/category_log.rb,
lib/cardio/logger/performance/html_formatter.rb,
lib/cardio/logger/performance/text_formatter.rb,
lib/cardio/logger/performance/method_preparation.rb

Defined Under Namespace

Modules: BigBrother, MethodPreparation Classes: CategoryLog, Entry, HtmlFormatter, TextFormatter

Constant Summary collapse

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

these methods have already a Decko.with_logging block

[:search, :view, :event, :rule, :execute]
TAB_SIZE =

we don’t have to monkey patch them, only turn the logging on with adding the symbol to the methods hash

3
@@log =
[]
@@category_log =
CategoryLog.new
@@context_entries =
[]
@@active_entries =
[]
@@current_level =
0

Class Method Summary collapse

Class Method Details

.default_methods_configObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cardio/logger/performance.rb', line 29

def default_methods_config
  [:execute, :rule, :fetch, :view]
#         {
#           # ActiveRecord => {
#           #   :instance => {
#           #     :save      => { :category => 'SQL'},
#           #     :take => { :category => 'SQL'},
#           #     :_create_record => { :category => 'SQL'},
#           #     :_update_record => { :category => 'SQL'}
#           #   },
#           #   :singleton=> {
#           #     :delete    => { :category => 'SQL'},
#           #     :find    => { :category => 'SQL'},
#           #     :update    => { :category => 'SQL'},
#           #     :create    => { :category => 'SQL'},
#           #     :where    => { :category => 'SQL'}
#           #   }
#           # },
#           Card => {
#             :instance => {
#               :rule_card => { :category => 'rule' }
#               # :'update!' => { :category => 'SQL'},
#               # :update    => { :category => 'SQL'},
#               # :save      => { :category => 'SQL'},
#               # :'save!'   => { :category => 'SQL'},
#               # :delete    => { :category => 'SQL'},
#               # :'delete!' => { :category => 'SQL'}
#             },
#             :singleton => {
#               :fetch => { :category => 'fetch' },
#               :view  => { :category => 'content'},
#               # :delete => { :category => 'SQL'},
#               # :find => { :category => 'SQL'},
#               # :update => { :category => 'SQL'},
#               # :create => { :category => 'SQL'},
#               :where   => { :category => 'SQL'},
#               :execute => { :category => 'SQL'}
# #              :take   => { :category => 'Take'}
#             }
#           },
#           # Card::Query => {
#           #   :instance => {
#           #     :run_sql => { :category => 'SQL'},
#           #
#           #   }
#           # }
#         }
end

.enable_method(method_name) ⇒ Object



127
128
129
130
# File 'lib/cardio/logger/performance.rb', line 127

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

.enabled_method?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/cardio/logger/performance.rb', line 132

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

.load_config(args) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cardio/logger/performance.rb', line 78

def load_config args
  args = params_to_config args
  @details = args[:details] || false
  @max_depth = args[:max_depth] || false
  @min_time = args[:min_time] || false
  @log_level = args[:log_level] || DEFAULT_LOG_LEVEL
  @output = args[:output] || :text
  @output_card = args[:output_card] || '*all'
  @enabled_methods = ::Set.new
  if args[:methods] == :default
    args[:methods] = default_methods_config
  end
  prepare_methods_for_logging args[:methods] if args[:methods]
end

.start(args = {}) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/cardio/logger/performance.rb', line 93

def start args = {}
  @@current_level = 0
  @@log = []
  @@context_entries = []
  @@active_entries = []
  @@first_entry = new_entry(args)
  @@category_log = CategoryLog.new args[:category]
end

.stopObject



102
103
104
105
106
107
108
109
# File 'lib/cardio/logger/performance.rb', line 102

def stop
  finish_all_context_entries
  if @@first_entry
    @@first_entry.save_duration
    finish_entry @@first_entry
  end
  print_log
end

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cardio/logger/performance.rb', line 111

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

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