about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-07-20 00:14:30 +0200
committerhut <hut@lavabit.com>2009-07-20 00:14:30 +0200
commite655b1dfd1a25c701e0596646f87cb182a67ea97 (patch)
tree8e09709a707b4656a6204420ace96def035ebac9
parent1d9ec2a14f4de9468238c833a0d947fccab353f7 (diff)
downloadranger-e655b1dfd1a25c701e0596646f87cb182a67ea97.tar.gz
debugfile is only created/opened when it's needed
additionally debug *has* to be initialized with a hash.
-rw-r--r--code/debug.rb39
1 files changed, 22 insertions, 17 deletions
diff --git a/code/debug.rb b/code/debug.rb
index 5281cb15..de532c06 100644
--- a/code/debug.rb
+++ b/code/debug.rb
@@ -24,33 +24,38 @@ module Debug
 	##     1: log fatal errors
 	##     2: log all errors
 	##     3: log everything
-	def self.setup(name=nil, stream=nil, level=nil)
-		if name.is_a? Hash
-			if name[:file]
-				stream = File.open(name[:file], 'a') rescue nil
-			end
-			stream ||= name[:stream]
-			level    = name[:level]
-			name     = name[:name]
+	def self.setup(hash)
+		@@file   =    hash[:file]
+		@@stream =    hash[:stream]
+		@@level  =    hash[:level]  || 3
+		@@name   = "#{hash[:name]   || 'debug'}: "
+	end
+
+	## a getter that, if stream is nil, opens @@file as the new stream
+	## or uses STDERR if there is no @@file.
+	## Doing it like this will delay opening the file until it
+	## is actually needed.
+	def stream
+		if @@stream
+			@@stream
+		elsif @@file
+			@@stream = File.open(@@file, 'a') rescue STDERR
+			@@stream.sync = true
+			@@stream
+		else
+			@@stream = STDERR
 		end
-
-		@@name   = name   || 'debug'
-		@@stream = stream || STDERR
-		@@level  = level  || 3
-
-		@@name = "#{@@name}: "
-		@@stream.sync = true
 	end
 
 	## Write something to the output stream.
 	def self.write(str)
-		@@stream.write(@@name + str.to_s)
+		stream.write(@@name + str.to_s)
 		return str
 	end
 	
 	## Write something to the output stream with a newline at the end.
 	def self.puts(str)
-		@@stream.puts(@@name + str.to_s)
+		stream.puts(@@name + str.to_s)
 		return str
 	end