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



26
27
28
# File 'lib/ctioga2/log.rb', line 26

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.



32
33
34
# File 'lib/ctioga2/log.rb', line 32

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

.errorObject

Prints an error message



47
48
49
# File 'lib/ctioga2/log.rb', line 47

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

.fatalObject

Prints a fatal error message and initiates program termination.



52
53
54
55
# File 'lib/ctioga2/log.rb', line 52

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

.infoObject

Prints an informational message



42
43
44
# File 'lib/ctioga2/log.rb', line 42

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

.init_logger(stream = STDERR) ⇒ Object



65
66
67
68
69
# File 'lib/ctioga2/log.rb', line 65

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

.log_to(target_file, message = nil) ⇒ Object

Logs to the target file, and fall back onto stderr should opening fail.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ctioga2/log.rb', line 74

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

.loggerObject

Simple accessor for the @@log class variable.



91
92
93
# File 'lib/ctioga2/log.rb', line 91

def self.logger
  return @@logger
end

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

Sets the logging level.



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

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

.warnObject

Prints a warning message



37
38
39
# File 'lib/ctioga2/log.rb', line 37

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

Instance Method Details

#format_exception(e) ⇒ Object

Format an exception for displaying



61
62
63
# File 'lib/ctioga2/log.rb', line 61

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.



119
120
121
# File 'lib/ctioga2/log.rb', line 119

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

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

A logged replacement for system



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ctioga2/log.rb', line 101

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