diff options
author | hut <hut@lavabit.com> | 2009-06-12 01:26:41 +0200 |
---|---|---|
committer | hut <hut@lavabit.com> | 2009-06-12 01:26:41 +0200 |
commit | 8a6f5eab0b884acd58ed86c00a30b57901442be6 (patch) | |
tree | 31672fce003ba464c922eb86188e854f7b7ccaa8 /code/debug.rb | |
parent | 9ed06dcd632e3bf6c19fc3943985e9841f1fed41 (diff) | |
download | ranger-8a6f5eab0b884acd58ed86c00a30b57901442be6.tar.gz |
renamed Interface module to CLI, cleanups
Diffstat (limited to 'code/debug.rb')
-rw-r--r-- | code/debug.rb | 147 |
1 files changed, 92 insertions, 55 deletions
diff --git a/code/debug.rb b/code/debug.rb index d3a97190..5c34b932 100644 --- a/code/debug.rb +++ b/code/debug.rb @@ -1,17 +1,32 @@ +## just a little module for easier debugging of ncurses CLI applications +## where it's a bad idea to write debug info directly into the console. +## use this with: include Debug + require 'pp' module Debug - @@logfile = '/tmp/errorlog' - @@logstream = File.open(@@logfile, 'a') + def self.setup(name, stream=nil, level=nil) + if name.is_a? Hash + stream = name[:stream] + level = name[:level] + name = name[:name] + end + + @@name = name || 'debug' + @@stream = stream || STDOUT + @@level = level || 3 + @@level = 3 + + @@stream.sync = true + end def self.write(str) - @@logstream.write(str) - @@logstream.flush + @@stream.write(str) return str end + def self.puts(str) - @@logstream.puts(str) - @@logstream.flush + @@stream.puts(str) return str end @@ -23,67 +38,89 @@ module Debug end end - if LOG_LEVEL > 0 - def bm(descr="benchmark", &block) - # Benchmark - t1 = Time.now + ## if you don't want your program to stop, + ## but still want to retrieve the error information + def lograise(e=nil) + e ||= $! + log_err("#{e.class}: #{e.message}") + log_err(e.backtrace) + end + + def bm(descr="benchmark", &block) + if @@level == 0 yield - dur = Time.now-t1 + return + end - # substract the durtation of a "bm(..) do end" - dur -= bm_dummy(descr) do end + # Benchmark + t1 = Time.now + yield + dur = Time.now-t1 - # Format the duration - dur *= 1000 - dur = dur > 0 ? dur : 0 - dur = '%0.3f' % dur - logerr("#{descr}: #{dur}ms") - end + # substract the durtation of a "bm(..) do end" + dur -= bm_dummy(descr) do end - def bm_dummy(descr="benchmark", &block) - t1 = Time.now - yield - return (Time.now-t1) - end + # Format the duration + dur *= 1000 + dur = dur > 0 ? dur : 0 + dur = '%0.3f' % dur + logerr("#{descr}: #{dur}ms") + end - def __log__(obj, level) - if level <= LOG_LEVEL - obj = obj.nil? ? "checkpoint at #{Time.now}" : obj - Debug.puts(obj) - end + def bm_dummy(descr="benchmark", &block) + t1 = Time.now + yield + return (Time.now-t1) + end + + def __logwrite__(obj, level) + if level <= @@level + Debug.write(obj) end - def __logpp__(obj, level) - if level <= LOG_LEVEL - $stdout = @@logstream - pp obj - $stdout.flush - $stdout = STDOUT - end + obj + end + + def __log__(obj, level) + if level <= @@level + obj = obj.nil? ? "checkpoint at #{Time.now}" : obj + Debug.puts(obj) end + obj + end - def logfatal(obj = nil) __log__(obj, 1) end - def logppfatal(obj = nil) __logpp__(obj, 1) end + def __logpp__(obj, level) + if level <= @@level + old_stdout = $stdout + $stdout = @@stream - def logerr(obj = nil) __log__(obj, 2) end - def logpperr(obj = nil) __logpp__(obj, 2) end + pp(obj) - def log(obj = nil) __log__(obj, 3) end - def logpp(obj = nil) __logpp__(obj, 3) end + $stdout = old_stdout + end + obj + end - def trace() __logpp__(caller, 3) end - else - def __log__(a, b) end - def __logpp__(a, b) end + ## each send a different level to __logXYZ__ - def logfatal(a=nil) end - def logppfatal(a=nil) end + def logfatal( obj = nil ) __log__( obj, 1) end + def logppfatal( obj = nil ) __logpp__( obj, 1) end + def logwritefatal( obj = nil ) __logwrite__(obj, 1) end - def logerr(a=nil) end - def logpperr(a=nil) end + def logerr( obj = nil ) __log__( obj, 2) end + def logpperr( obj = nil ) __logpp__( obj, 2) end + def logwriteerr( obj = nil ) __logwrite__(obj, 2) end - def log(a=nil) end - def logpp(a=nil) end - def trace() end - def bm(*args, &block) yield end - end + def log( obj = nil ) __log__( obj, 3) end + def logpp( obj = nil ) __logpp__( obj, 3) end + def logwrite( obj = nil ) __logwrite__(obj, 3) end + + def trace( n = 1 ) __log__(caller(n), 3) end + + alias log_fatal logfatal + alias logpp_fatal logppfatal + alias logwrite_fatal logwritefatal + alias log_err logerr + alias logpp_err logpperr + alias logwrite_err logwriteerr end + |