forked from simonsj/bt.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bt.rb
49 lines (39 loc) · 1.11 KB
/
bt.rb
1
2
3
4
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
38
39
40
41
42
43
44
45
46
47
48
49
require "digest/md5"
module BT
module_function
if ENV.key?("BT_INIT")
def start(name)
return if ENV.key?("BT_DISABLED") and ENV["BT_DISABLED"] != "0"
caller, desc_checksum, timestamp = _metadata(name)
file = "/tmp/bt.#{desc_checksum}.#{timestamp}"
file_alias = "/tmp/bt.#{desc_checksum}"
File.write(file, "#{caller} #{name}\n")
File.symlink(file, file_alias)
end
def end(name)
return if ENV.key?("BT_DISABLED") and ENV["BT_DISABLED"] != "0"
caller, desc_checksum, timestamp = _metadata(name)
File.open("/tmp/bt.#{desc_checksum}", 'a') do |f|
f.puts("#{timestamp} #{caller} #{name}")
end
end
def _metadata(name)
caller = /([[:alnum:]\.\/\-]+\:[[:digit:]]+)/.match(caller_locations(1, 1)[0].to_s)[0]
desc_checksum = Digest::MD5.hexdigest(name)
time = Time.now
timestamp = "#{time.to_i}#{time.nsec}".ljust(19, '0')
[caller, desc_checksum, timestamp]
end
else
def start(name)
end
def end(name)
end
end
def time(name)
self.start(name)
yield
ensure
self.end(name)
end
end