Class: Safely::Backtrace

Inherits:
Object
  • Object
show all
Defined in:
lib/safely/backtrace.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.trace_directoryObject

Returns the value of attribute trace_directory.



6
7
8
# File 'lib/safely/backtrace.rb', line 6

def trace_directory
  @trace_directory
end

Class Method Details

.enable!Object



8
9
10
11
# File 'lib/safely/backtrace.rb', line 8

def enable!
  @enabled = true
  at_exit { log_exceptions }
end

.log_exceptionsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/safely/backtrace.rb', line 17

def log_exceptions
  return unless @enabled && self.trace_directory && File.directory?( self.trace_directory )
  return if (last_exception = $!).nil?

  require 'logger'

  trace_file = File.join( self.trace_directory, "backtrace-#{Time.now.strftime('%Y%m%d%H%M%S')}-#{Process.pid}.log" )
  trace_log = Logger.new( trace_file )

  # Log the last exception
  trace_log.info "*** Below you'll find the most recent exception thrown, this will likely (but not certainly) be the exception that made your application exit abnormally ***"
  trace_log.error last_exception

  trace_log.info "*** Below you'll find all the exception objects in memory, some of them may have been thrown in your application, others may just be in memory because they are standard exceptions ***"
  ObjectSpace.each_object {|o|
    if ::Exception === o
      trace_log.error o
    end
  }

  trace_log.close
end

.safe_shutdown!Object



13
14
15
# File 'lib/safely/backtrace.rb', line 13

def safe_shutdown!
  @enabled = false
end