Module: CTioga2::Log

Overview

TODO:

The debug information should contain the command being

This module should be included (or extended) by every class that need logging/debugging facilities.

currently executed.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.debug(channel = nil) ⇒ Object

Prints a debug message, on channel channel. Channel handling is not implemented yet.



30
31
32
# File 'lib/ctioga2/log.rb', line 30

def debug(channel = nil)
  @@logger.debug {yield}
end

.errorObject

Prints an error message



45
46
47
# File 'lib/ctioga2/log.rb', line 45

def error
  @@logger.error {yield}
end

.fatalObject

Prints a fatal error message and initiates program termination.



50
51
52
53
# File 'lib/ctioga2/log.rb', line 50

def fatal
  @@logger.fatal {yield}
  exit 1                    # Fatal error.
end

.infoObject

Prints an informational message



40
41
42
# File 'lib/ctioga2/log.rb', line 40

def info
  @@logger.info {yield}
end

.init_logger(stream = STDERR) ⇒ Object



63
64
65
66
67
# File 'lib/ctioga2/log.rb', line 63

def self.init_logger(stream = STDERR)
  Logger::Formatter::Format.replace("[%4$s] %6$s\n")
  @@logger = Logger.new(stream)
  @@logger.level = Logger::WARN # Warnings and more only by default
end

.loggerObject

Simple accessor for the @@log class variable.



70
71
72
# File 'lib/ctioga2/log.rb', line 70

def self.logger
  return @@logger
end

.set_level(level = Logger::WARN) ⇒ Object

Sets the logging level.



75
76
77
# File 'lib/ctioga2/log.rb', line 75

def self.set_level(level = Logger::WARN)
  @@logger.level = level
end

.warnObject

Prints a warning message



35
36
37
# File 'lib/ctioga2/log.rb', line 35

def warn
  @@logger.warn {yield}
end

Instance Method Details

#format_exception(e) ⇒ Object

Format an exception for displaying



59
60
61
# File 'lib/ctioga2/log.rb', line 59

def format_exception(e)
  return "#{e.class}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
end

#identify(obj) ⇒ Object

Returns a string suitable for identification of an object, a bit in the spirit of #inspect, but without displaying instance variables.



95
96
97
# File 'lib/ctioga2/log.rb', line 95

def identify(obj)
  return "#<%s 0x%x>" % [obj.class, obj.object_id]
end

#spawn(cmd, priority = :info) ⇒ Object

A logged replacement for system



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ctioga2/log.rb', line 80

def spawn(cmd, priority = :info)
  retval = system(cmd)
  self.send(priority) { "Spawned #{cmd} -> " + 
    if retval
      "success"
    else
      "failure"
    end
  }
  return retval
end