module CTioga2::Log
This module should be included (or extended) by every class that need logging/debugging facilities.
@todo The debug information should contain the command being currently executed.
Public Class Methods
# File lib/ctioga2/log.rb, line 26 def self.context if defined? PlotMaker return " while processing #{PlotMaker.plotmaker.interpreter.context.to_s}" else return " in the early loading stages" end end
# File lib/ctioga2/log.rb, line 86 def self.counts return @@counts end
Prints a debug message, on channel channel. Channel handling is not implemented yet.
# File lib/ctioga2/log.rb, line 36 def debug(channel = nil) @@logger.debug {yield + Log.context} @@counts[:debug] += 1 end
Prints an error message
# File lib/ctioga2/log.rb, line 54 def error @@logger.error {yield + Log.context} @@counts[:error] += 1 end
Prints a fatal error message and initiates program termination.
# File lib/ctioga2/log.rb, line 60 def fatal @@logger.fatal {yield + Log.context} @@counts[:fatal] += 1 # Though not very useful exit 1 # Fatal error. end
Prints an informational message
# File lib/ctioga2/log.rb, line 48 def info @@logger.info {yield + Log.context} @@counts[:info] += 1 end
# File lib/ctioga2/log.rb, line 74 def self.init_logger(stream = STDERR) @@logger = Logger.new(stream) @@logger.formatter = proc do |severity, datetime, progname, msg| "[#{severity}] #{msg}\n" end @@logger.level = Logger::WARN # Warnings and more only by default @@counts = {} for k in [:error, :debug, :warn, :info, :fatal] @@counts[k] = 0 end end
Logs to the target file, and fall back onto stderr should opening fail.
# File lib/ctioga2/log.rb, line 92 def self.log_to(target_file, message = nil) if target_file.is_a? String begin target_file = File.open(target_file, "w") if message target_file.puts message end rescue target_file = STDERR end end old = @@logger @@logger = Logger.new(target_file) @@logger.level = old.level end
Simple accessor for the @@log class variable.
# File lib/ctioga2/log.rb, line 109 def self.logger return @@logger end
Sets the logging level.
# File lib/ctioga2/log.rb, line 114 def self.set_level(level = Logger::WARN) @@logger.level = level end
Prints a warning message
# File lib/ctioga2/log.rb, line 42 def warn @@logger.warn {yield + Log.context} @@counts[:warn] += 1 end
Public Instance Methods
Format an exception for displaying
# File lib/ctioga2/log.rb, line 70 def format_exception(e) return "#{e.class}: #{e.message}\n\t#{e.backtrace.join("\n\t")}" end
Returns a string suitable for identification of an object, a bit in the spirit of inspect, but without displaying instance variables.
# File lib/ctioga2/log.rb, line 137 def identify(obj) return "#<%s 0x%x>" % [obj.class, obj.object_id] end
A logged replacement for system
# File lib/ctioga2/log.rb, line 119 def spawn(cmd, priority = :info) if cmd.is_a? String cmd = [cmd] end retval = system(*cmd) self.send(priority) { "Spawned #{cmd} -> " + if retval "success" else "failure" end } return retval end
Private Instance Methods
Prints a debug message, on channel channel. Channel handling is not implemented yet.
# File lib/ctioga2/log.rb, line 36 def debug(channel = nil) @@logger.debug {yield + Log.context} @@counts[:debug] += 1 end
Prints an error message
# File lib/ctioga2/log.rb, line 54 def error @@logger.error {yield + Log.context} @@counts[:error] += 1 end
Prints a fatal error message and initiates program termination.
# File lib/ctioga2/log.rb, line 60 def fatal @@logger.fatal {yield + Log.context} @@counts[:fatal] += 1 # Though not very useful exit 1 # Fatal error. end
Prints an informational message
# File lib/ctioga2/log.rb, line 48 def info @@logger.info {yield + Log.context} @@counts[:info] += 1 end
Prints a warning message
# File lib/ctioga2/log.rb, line 42 def warn @@logger.warn {yield + Log.context} @@counts[:warn] += 1 end