Module: Narou::Backtracer

Defined in:
lib/backtracer.rb

Constant Summary collapse

LOG_NAME =
"trace_dump.txt"

Class Method Summary collapse

Class Method Details

.argvObject



40
41
42
# File 'lib/backtracer.rb', line 40

def argv
  @argv || ARGV
end

.argv=(argv) ⇒ Object



36
37
38
# File 'lib/backtracer.rb', line 36

def argv=(argv)
  @argv = argv.map(&:dup)
end

.build_commandObject



63
64
65
# File 'lib/backtracer.rb', line 63

def build_command
  "#{$0} #{argv.join(' ')}"
end

.build_traces(exception) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/backtracer.rb', line 52

def build_traces(exception)
  backtrace = exception.backtrace
  head = "#{backtrace.shift}: #{exception.message.encode(Encoding::UTF_8)} (#{exception.class})"
  buffer = StringIO.new
  buffer.puts head
  backtrace.each do |b|
    buffer.puts "  from #{b}"
  end
  buffer.string
end

.capture(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/backtracer.rb', line 13

def capture(&block)
  raise "need a block" unless block
  rescue_level = $debug ? Exception : StandardError
  yield
rescue SystemExit => e
  exit e.status
rescue SyntaxError => e
  warn e
  exit Narou::EXIT_ERROR_CODE
rescue rescue_level => e
  traces = build_traces(e)
  if $display_backtrace
    warn traces
  else
    warn traces.lines[0..2]
    warn ""
    warn "  エラーが発生したため終了しました。"
    warn "  詳細なエラーログは narou trace で表示出来ます。もしくは --backtrace オプションを付けて再度実行して下さい。"
  end
  save_log(traces)
  exit Narou::EXIT_ERROR_CODE
end

.log_pathObject



44
45
46
47
48
49
50
# File 'lib/backtracer.rb', line 44

def log_path
  if Narou.get_root_dir
    File.join(Narou.get_root_dir, LOG_NAME)
  else
    LOG_NAME
  end
end

.save_log(traces) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/backtracer.rb', line 67

def save_log(traces)
  File.open(log_path, "w:UTF-8") do |fp|
    fp.puts "--- #{Time.now.strftime("%Y/%m/%d %H:%M:%S")} ---"
    fp.puts build_command
    fp.puts
    fp.puts traces
  end
end