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

.contextObject



28
29
30
# File 'lib/ctioga2/log.rb', line 28

def self.context
  return " while processing #{PlotMaker.plotmaker.interpreter.context.to_s}"
end

.debug(channel = nil) ⇒ Object

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



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

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

.errorObject

Prints an error message



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

def error
  @@logger.error {yield + Log.context}
end

.fatalObject

Prints a fatal error message and initiates program termination.



54
55
56
57
# File 'lib/ctioga2/log.rb', line 54

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

.infoObject

Prints an informational message



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

def info
  @@logger.info {yield + Log.context}
end

.init_logger(stream = STDERR) ⇒ Object



67
68
69
70
71
# File 'lib/ctioga2/log.rb', line 67

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.



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

def self.logger
  return @@logger
end

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

Sets the logging level.



79
80
81
# File 'lib/ctioga2/log.rb', line 79

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

.warnObject

Prints a warning message



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

def warn
  @@logger.warn {yield + Log.context} 
end

Instance Method Details

#format_exception(e) ⇒ Object

Format an exception for displaying



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

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.



99
100
101
# File 'lib/ctioga2/log.rb', line 99

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

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

A logged replacement for system



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ctioga2/log.rb', line 84

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