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
29
30
31
32
# 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

.countsObject



86
87
88
# File 'lib/ctioga2/log.rb', line 86

def self.counts
  return @@counts
end

.debug(channel = nil) ⇒ Object

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



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

def debug(channel = nil)
  @@logger.debug {yield + Log.context}
  @@counts[:debug] += 1
end

.errorObject

Prints an error message



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

def error
  @@logger.error {yield + Log.context}
  @@counts[:error] += 1
end

.fatalObject

Prints a fatal error message and initiates program termination.



60
61
62
63
64
# 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

.infoObject

Prints an informational message



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

def info
  @@logger.info {yield + Log.context}
  @@counts[:info] += 1
end

.init_logger(stream = STDERR) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# 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) ⇒ Object

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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

.loggerObject

Simple accessor for the @@log class variable.



109
110
111
# File 'lib/ctioga2/log.rb', line 109

def self.logger
  return @@logger
end

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

Sets the logging level.



114
115
116
# File 'lib/ctioga2/log.rb', line 114

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

.warnObject

Prints a warning message



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

def warn
  @@logger.warn {yield + Log.context} 
  @@counts[:warn] += 1
end

Instance Method Details

#format_exception(e) ⇒ Object

Format an exception for displaying



70
71
72
# 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) ⇒ Object

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



137
138
139
# File 'lib/ctioga2/log.rb', line 137

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

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

A logged replacement for system



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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