Class: Localtower::Plugins::Capture

Inherits:
Object
  • Object
show all
Defined in:
lib/localtower/plugins/capture.rb

Constant Summary collapse

LOG_FILE =
lambda { "#{Rails.root}/log/localtower_capture.log" }
LOG_PATH =
lambda { "#{Rails.root}/log" }
EXCLUDE_INSTANCE_VARIABLES =
[
  "@_action_has_layout",
  "@_routes",
  "@_headers",
  "@_status",
  "@_request",
  "@__react_component_helper",
  "@_response",
  "@_env",
  "@env",
  "@controller",
  "@_lookup_context",
  "@_action_name",
  "@_response_body",
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = nil, context_binding = nil) ⇒ Capture

Returns a new instance of Capture.



49
50
51
52
# File 'lib/localtower/plugins/capture.rb', line 49

def initialize(context = nil, context_binding = nil)
  @context = context # self
  @context_binding = context_binding # binding
end

Class Method Details

.log_from_md5(md5) ⇒ Object



31
32
33
34
# File 'lib/localtower/plugins/capture.rb', line 31

def log_from_md5(md5)
  file = Dir["#{Localtower::Plugins::Capture::LOG_PATH.call}/localtower*#{md5}*"][0]
  JSON.parse(open(file).read)
end

.printable(content) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/localtower/plugins/capture.rb', line 23

def printable(content)
  if content.respond_to?(:to_json)
    JSON.pretty_generate(content)
  else
    content.to_s
  end
end

.type_of(value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/localtower/plugins/capture.rb', line 36

def type_of(value)
  if value.instance_of?(Float); return Float.to_s; end;
  if value.instance_of?(Integer); return Integer.to_s; end;
  if value.instance_of?(String); return String.to_s; end;
  if value.instance_of?(Array); return Array.to_s; end;
  if value.instance_of?(Hash); return Hash.to_s; end;
  if value.instance_of?(ApplicationController); return ApplicationController.to_s; end;
  if value.instance_of?(NilClass); return NilClass.to_s; end;

  nil
end

Instance Method Details

#clearObject



172
173
174
175
176
177
178
# File 'lib/localtower/plugins/capture.rb', line 172

def clear
  Dir["#{LOG_PATH.call}/localtower_capture_*.json"].each do |file|
    File.delete(file)
  end

  self
end

#klass_nameObject



162
163
164
165
166
167
168
169
170
# File 'lib/localtower/plugins/capture.rb', line 162

def klass_name
  value = if @context.class == "Class"
    @context
  else
    @context.class
  end

  value.to_s
end

#log(str) ⇒ Object



192
193
194
# File 'lib/localtower/plugins/capture.rb', line 192

def log(str)
  my_logger.info(str)
end

#logsObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/localtower/plugins/capture.rb', line 54

def logs
  list = []

  Dir["#{LOG_PATH.call}/localtower_capture_*.json"].each do |file|
    json = JSON.parse(open(file).read)
    list << json
  end

  list
end

#my_loggerObject



65
66
67
68
69
70
71
72
# File 'lib/localtower/plugins/capture.rb', line 65

def my_logger
  @@my_logger ||= Logger.new(LOG_FILE.call)
  @@my_logger.formatter = proc do |severity, datetime, progname, msg|
    "#{msg}\n"
  end

  @@my_logger
end

#saveObject



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/localtower/plugins/capture.rb', line 180

def save
  # We don't want to save logs in production:
  return nil if Rails.env.production?

  self.clear

  data = self.values
  file = "#{LOG_PATH.call}/localtower_capture_#{data['md5']}.json"

  File.open(file, 'w') { |f| f.write(data.to_json) }
end

#valuesObject



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
153
154
155
156
157
158
159
160
# File 'lib/localtower/plugins/capture.rb', line 74

def values
  hash = {}

  callers = @context.send(:caller)
  a = callers[1] # xx/xx/app/controllers/clients/events_controller.rb:57:in `new'
  a = a.split(Rails.root.to_s).last # events_controller.rb:57:in `new'
  a = a.split("\:")


  file = a[0].strip
  line_number = a[1].strip
  method = a[2].strip.gsub("in \`", "").gsub("\'", "")

  sublime_path = "#{callers[1].split(":")[0]}:#{line_number}"

  hash["class"] = self.klass_name
  hash["file_name"] = file
  hash["file"] = "#{file}##{method}:#{line_number}"
  hash["line_number"] = line_number
  hash["method"] = method
  hash["md5"] = Digest::MD5.hexdigest(hash["file"])
  hash["type"] = "CAPTURE_METHOD"
  hash["time"] = Time.now.utc.strftime('%Y-%m-%d %H:%M:%S.%L')

  variables = []

  @context_binding.local_variables.each do |var|
    next if EXCLUDE_INSTANCE_VARIABLES.include?(var.to_s)

    value = @context_binding.local_variable_get(var)
    klass = self.class.type_of(value)

    data = {
      type: 'CAPTURE',
      time: Time.now.utc.strftime('%Y-%m-%d %H:%M:%S.%L'),
      event_name: var,
      identifier: nil,
      returned: value,
      meta: {
        from_klass: hash["class"],
        from_method: hash["method"],
        klass: klass.to_s,
        method: method.to_s,
        callers: callers,
        sublime_path: sublime_path,
        file: hash["file"],
        line: line_number
      }
    }

    variables << data
  end

  @context.instance_variables.each do |var|
    next if EXCLUDE_INSTANCE_VARIABLES.include?(var.to_s)

    value = @context.instance_variable_get(var.to_sym)
    klass = self.class.type_of(value)

    data = {
      type: 'CAPTURE',
      time: Time.now.utc.strftime('%Y-%m-%d %H:%M:%S.%L'),
      event_name: var,
      identifier: nil,
      returned: value,
      meta: {
        from_klass: hash["class"],
        from_method: hash["method"],
        klass: klass.to_s,
        method: method.to_s,
        # arguments: data[:arguments],
        callers: callers,
        # table_name: data[:table_name],
        # sql: data[:sql],
        sublime_path: sublime_path,
        file: hash["file"],
        line: line_number
      }
    }

    variables << data
  end

  hash["variables"] = variables

  hash
end