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

context() click to toggle source
# 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
counts() click to toggle source
# File lib/ctioga2/log.rb, line 86
def self.counts
  return @@counts
end
debug(channel = nil) { |+ context| ... } click to toggle source

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
error() { |+ context| ... } click to toggle source

Prints an error message

# File lib/ctioga2/log.rb, line 54
def error
  @@logger.error {yield + Log.context}
  @@counts[:error] += 1
end
fatal() { |+ context| ... } click to toggle source

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
info() { |+ context| ... } click to toggle source

Prints an informational message

# File lib/ctioga2/log.rb, line 48
def info
  @@logger.info {yield + Log.context}
  @@counts[:info] += 1
end
init_logger(stream = STDERR) click to toggle source
# 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
log_to(target_file, message = nil) click to toggle source

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
logger() click to toggle source

Simple accessor for the @@log class variable.

# File lib/ctioga2/log.rb, line 109
def self.logger
  return @@logger
end
set_level(level = Logger::WARN) click to toggle source

Sets the logging level.

# File lib/ctioga2/log.rb, line 114
def self.set_level(level = Logger::WARN)
  @@logger.level = level
end
warn() { |+ context| ... } click to toggle source

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_exception(e) click to toggle source

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
identify(obj) click to toggle source

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
spawn(cmd, priority = :info) click to toggle source

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

debug(channel = nil) { |+ context| ... } click to toggle source

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
error() { |+ context| ... } click to toggle source

Prints an error message

# File lib/ctioga2/log.rb, line 54
def error
  @@logger.error {yield + Log.context}
  @@counts[:error] += 1
end
fatal() { |+ context| ... } click to toggle source

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
info() { |+ context| ... } click to toggle source

Prints an informational message

# File lib/ctioga2/log.rb, line 48
def info
  @@logger.info {yield + Log.context}
  @@counts[:info] += 1
end
warn() { |+ context| ... } click to toggle source

Prints a warning message

# File lib/ctioga2/log.rb, line 42
def warn
  @@logger.warn {yield + Log.context} 
  @@counts[:warn] += 1
end