Class: Roda::DebugBar::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/debug_bar/instance.rb

Overview

Logger instance for this application

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, env, instance_id, root, filter) ⇒ Instance

Returns a new instance of Instance.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/roda/debug_bar/instance.rb', line 37

def initialize(logger, env, instance_id, root, filter)
  @logger = logger
  @root = root
  @log_entries = []
  # @views = []
  @messages = []
  @matches = []
  @timer = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @filter = filter || proc { false }
  if env["debug_bar_id"].nil?
    @primary = true
    env["debug_bar"] = instance_id
  else
    @primary = false
  end
end

Instance Attribute Details

#debug_dataObject

added



31
32
33
# File 'lib/roda/debug_bar/instance.rb', line 31

def debug_data
  @debug_data
end

#filterObject (readonly)

Callable object to filter log entries



26
27
28
# File 'lib/roda/debug_bar/instance.rb', line 26

def filter
  @filter
end

#log_entriesObject (readonly)

Log entries generated during request



14
15
16
# File 'lib/roda/debug_bar/instance.rb', line 14

def log_entries
  @log_entries
end

#loggerObject (readonly)

Logger instance



17
18
19
# File 'lib/roda/debug_bar/instance.rb', line 17

def logger
  @logger
end

#matchesObject (readonly)

Route matches during request



20
21
22
# File 'lib/roda/debug_bar/instance.rb', line 20

def matches
  @matches
end

#rootObject (readonly)

Application root



11
12
13
# File 'lib/roda/debug_bar/instance.rb', line 11

def root
  @root
end

Instance Method Details

#add(status, request, trace = false) ⇒ Object

Add log entry for request

Parameters:

  • status (Integer)

    status code for the response

  • request (Roda::RodaRequest)

    request object

  • trace (Boolean) (defaults to: false)

    tracing was enabled



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
# File 'lib/roda/debug_bar/instance.rb', line 66

def add(status, request, trace = false)
  if (last_matched_caller = matches.last)
    handler = format("%s:%d",
      Pathname(last_matched_caller.path).relative_path_from(root),
      last_matched_caller.lineno)
  end

  if handler.nil?
    handler = 'nilString'
  end

  meth =
    case status
    when 400..499
      :warn
    when 500..599
      :error
    else
      :info
    end

  data = {
    duration: (Process.clock_gettime(Process::CLOCK_MONOTONIC) - timer).round(4),
    status: status,
    verb: request.request_method,
    path: request.path,
    remaining_path: request.remaining_path,
    handler: handler,
    params: request.params
  }

  if (db = Roda::DebugBar::Current.accrued_database_time)
    data[:db] = db.round(6)
  else
    data[:db] = false
  end

  if (query_count = Roda::DebugBar::Current.database_query_count)
    data[:db_queries] = query_count
  end

  if (queries = Roda::DebugBar::Current.database_queries)
    data[:db_messages] = queries
  end

  if (models = Roda::DebugBar::Current.models)
    data[:models] = models
  end

  if (views = Roda::DebugBar::Current.views)
    data[:views] = views
  end

  if @messages
    data[:messages] = @messages
  end

  data[:request] = request

  # data[:request_inspect] = request.env

  # data[:views] = @views

  if trace
    p "matches: #{matches}"
    matches.each do |match|
      add_log_entry([meth, format("  %s (%s:%s)",
        File.readlines(match.path)[match.lineno - 1].strip.sub(" do", ""),
        Pathname(match.path).relative_path_from(root),
        match.lineno)])


      handler = File.readlines(match.path)[match.lineno - 1].strip.sub(" do", "")
      path = "#{Pathname(match.path).relative_path_from(root)}:#{match.lineno}"
      Roda::DebugBar::Current.add_route(handler, path)
    end
  end

  if (routes = Roda::DebugBar::Current.routes)
    data[:routes] = routes
  end

  return if filter.call(request.path)

  @debug_data = data
  add_log_entry([meth, "#{request.request_method} #{request.path}", data])
end

#add_match(caller) ⇒ Object

Add a matched route handler



55
56
57
# File 'lib/roda/debug_bar/instance.rb', line 55

def add_match(caller)
  @matches << caller
end

#add_message(log_level, message) ⇒ Object

logged to message tab



194
195
196
# File 'lib/roda/debug_bar/instance.rb', line 194

def add_message(log_level, message)
  @messages << {type: log_level, message: message}
end

#drainBoolean

Drain the log entry queue, writing each to the logger at their respective level

Returns:

  • (Boolean)


162
163
164
165
166
167
168
169
170
# File 'lib/roda/debug_bar/instance.rb', line 162

def drain
  return unless primary?

  log_entries.each do |args|
    logger.public_send(*args)
  end

  true
end

#primary?Boolean

This instance is the primary logger

Returns:

  • (Boolean)


156
157
158
# File 'lib/roda/debug_bar/instance.rb', line 156

def primary?
  @primary
end

#resetBoolean

Reset the counters for this thread

Returns:

  • (Boolean)


174
175
176
# File 'lib/roda/debug_bar/instance.rb', line 174

def reset
  Roda::DebugBar::Current.reset
end