about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--code/cli.rb4
-rw-r--r--code/debug.rb147
-rw-r--r--code/draw.rb2
-rw-r--r--code/fm/fm.rb4
-rwxr-xr-xranger.rb22
5 files changed, 105 insertions, 74 deletions
diff --git a/code/cli.rb b/code/cli.rb
index 3736f60c..0f915634 100644
--- a/code/cli.rb
+++ b/code/cli.rb
@@ -1,6 +1,6 @@
 require 'ncurses'
 
-module Interface
+module CLI
 	def self.keytable(key)
 		case key
 		when 12
@@ -71,7 +71,7 @@ module Interface
 	end
 
 	def geti
-		Interface::keytable(Ncurses.getch)
+		CLI.keytable(Ncurses.getch)
 	end
 
 	def set_title(x)
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
+
diff --git a/code/draw.rb b/code/draw.rb
index 8e032199..36688642 100644
--- a/code/draw.rb
+++ b/code/draw.rb
@@ -169,7 +169,7 @@ module Fm
 	end
 
 	def self.get_boundaries(column)
-		cols = Interface.cols # to cache
+		cols = CLI.cols # to cache
 		case column
 		when 0
 			return 0, cols / 8
diff --git a/code/fm/fm.rb b/code/fm/fm.rb
index 81b868ca..8e60286b 100644
--- a/code/fm/fm.rb
+++ b/code/fm/fm.rb
@@ -58,7 +58,7 @@ module Fm
 		@memory['0'] = pwd
 
 		# Give me some way to redraw screen while waiting for
-		# input from Interface.geti
+		# input from CLI.geti
 
 #		for i in 1..20
 #			eval "Signal.trap(#{i}) do
@@ -98,7 +98,7 @@ module Fm
 	end
 
 	def lines
-		Interface::lines - @bars.size
+		CLI::lines - @bars.size
 	end
 
 	def dump
diff --git a/ranger.rb b/ranger.rb
index 5b074317..d503dc50 100755
--- a/ranger.rb
+++ b/ranger.rb
@@ -1,14 +1,6 @@
 #!/usr/bin/ruby -Ku
 version = '0.2.2'
 
-# Log details
-# 0 = do not log
-# 1 = log fatalities
-# 2 = log errors
-# 3 = log everything
-LOG_LEVEL = 3
-#LOG_LEVEL = 0
-
 require 'pathname'
 $: << MYDIR = File.dirname(Pathname.new(__FILE__).realpath)
 
@@ -40,10 +32,8 @@ require 'ostruct'
 class OpenStruct; def __table__() @table end end
 require 'thread'
 
-require 'interface/ncurses.rb'
-for file in Dir["#{MYDIR}/code/**/*.rb"]
-	file.slice! 0..MYDIR.size
-	require file
+for file in Dir.glob "#{MYDIR}/code/**/*.rb"
+	require file [MYDIR.size + 1 ... -3]
 end
 
 require 'data/screensaver/clock.rb'
@@ -52,9 +42,13 @@ unless ARGV.empty? or File.directory?(pwd)
 	exec(Fm.getfilehandler_frompath(pwd))
 end
 
-include Interface
+include CLI
 include Debug
 
+Debug.setup( :name   => 'nyuron',
+             :stream => File.open('/tmp/errorlog', 'a'),
+             :level  => 3 )
+
 ERROR_STREAM = File.open('/tmp/errorlog', 'a')
 
 Signal.trap(Scheduler::UPDATE_SIGNAL) do
@@ -67,7 +61,7 @@ begin
 ensure
 	log "exiting!"
 	log ""
-	closei if Interface.running?
+	closei if CLI.running?
 #	Fm.dump
 	ERROR_STREAM.close