summary refs log tree commit diff stats
path: root/code/debug.rb
blob: 6261ed0ed34c4f4ec5e267a43accac6f04336e5e (plain) (blame)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'pp'

module Debug
	@@logfile = '/tmp/errorlog'
	@@logstream = File.open(@@logfile, 'a')

	def self.write(str)
		@@logstream.write(str)
		@@logstream.flush
		return str
	end
	def self.puts(str)
		@@logstream.puts(str)
		@@logstream.flush
		return str
	end

	def try(&block)
		return unless block
		begin
			yield
		rescue Exception
		end
	end

	if LOG_LEVEL > 0
		def __log__(obj, level)
			if level <= LOG_LEVEL
				obj = obj.nil? ? "checkpoint at #{Time.now}" : obj
				Debug.puts(obj)
			end
		end
		def __logpp__(obj, level)
			if level <= LOG_LEVEL
				$stdout = @@logstream
				pp obj
				$stdout.flush
				$stdout = STDOUT
			end
		end

		def logfatal(obj = nil) __log__(obj, 1) end
		def logppfatal(obj = nil) __logpp__(obj, 1) end

		def logerr(obj = nil) __log__(obj, 2) end
		def logpperr(obj = nil) __logpp__(obj, 2) end

		def log(obj = nil) __log__(obj, 3) end
		def logpp(obj = nil) __logpp__(obj, 3) end

		def trace() __logpp__(caller, 3) end
	else
		def __log__(a, b) end
		def __logpp__(a, b) end

		def logfatal(a=nil) end
		def logppfatal(a=nil) end

		def logerr(a=nil) end
		def logpperr(a=nil) end

		def log(a=nil) end
		def logpp(a=nil) end
		def trace() end
	end
end