Module: LogMethods

Included in:
MongoLog
Defined in:
lib/sprockets_fs/log.rb

Instance Method Summary collapse

Instance Method Details

#calling_linesObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sprockets_fs/log.rb', line 5

def calling_lines
  raise 'foo'
rescue => exp
  base = test = other = nil
  dir = File.expand_path(File.dirname(__FILE__) + "/../..")

  File.append "traces.txt", exp.backtrace.join("\n") + "\n\n\n"

  exp.backtrace[3..-1].each do |line|
    if line =~ /rspec-core/
      # do nothing
    elsif line.starts_with?(dir)
      short = line.gsub("#{dir}/","")
      if short.starts_with? "lib"
        base ||= short
      elsif short.starts_with? "spec"
        test ||= short
      else
        raise "unknown #{line} | #{short}"
      end
    elsif line =~ /fattr/ || line.starts_with?("(eval):")
      # do nothing
    else
      other ||= line
    end
    if base && test && other
      test = "#{test} #{get_spec_name(test)}"
      return [base,test,other]
    end
  end
  return [base,test,other] if base && test
  raise 'end'
end

#get_fattr_name(desc) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/sprockets_fs/log.rb', line 50

def get_fattr_name(desc)
  file,line,junk = *desc.split(" ").first.split(":")
  lines = File.read(file).split("\n")[0...(line.to_i)].reverse
  lines.each do |line|
    if line =~ /fattr\(:([a-z0-9_]+)\)\s+do/
      return $1
    end
  end
  raise 'no fattr name'
end

#get_spec_name(desc) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/sprockets_fs/log.rb', line 39

def get_spec_name(desc)
  file,line,junk = *desc.split(" ").first.split(":")
  lines = File.read(file).split("\n")[0...(line.to_i)].reverse
  lines.each do |line|
    if line =~ /it\s+['"]([a-z _0-9]+)['"]\s+do/
      return $1
    end
  end
  raise 'no spec name'
end

#log_special(obj, &b) ⇒ Object



63
64
65
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
# File 'lib/sprockets_fs/log.rb', line 63

def log_special(obj,&b)
  dt = Time.now

  lines = calling_lines
  line = lines.join(" | ")

  raise "bad line, cant find calling function #{lines.first}" unless lines.first =~ /in `([a-z0-9_ <>:]+)'/i
  caller = $1

  if caller.starts_with?("block in <")
    caller = get_fattr_name(lines.first) 
  elsif caller.starts_with?("block in")
    raise "bad line, cant find calling function #{caller}" unless caller =~ /block in ([a-z0-9_!]+)/i
    caller = $1
  end

  library = nil
  if lines.last
    if  lines.last =~ /in `([a-z0-9_ <>:]+)'/i
      library = $1
    end
  end

  res = nil
  message = obj.inspect
  if block_given?
    res = yield
    message += " RES: #{res.inspect}"
  end
  
  make :dt => dt, :base_line => lines.first, :spec_line => lines[1], :spec => get_spec_name(lines[1]), :message => message, :caller => caller, :library => library, :library_line => lines.last

  File.append "log_special.log","#{dt} from #{line}\n#{obj.inspect}\n\n"

  res
end