Module: CTioga2::Log
- Included in:
- Commands::Documentation::Man, Commands::Documentation::MarkedUpText, Commands::Parsers::CommandLineParser, Commands::Parsers::FileParser, Commands::Parsers::OldFileParser, Data::Backends::Backend, Data::Backends::BackendFactory, Data::Backends::GnuplotBackend, Data::DataStack, Data::Dataset, Graphics::Elements::Curve2D, Graphics::Elements::Histogram, Graphics::Elements::Parametric2D, Graphics::Elements::TiogaElement, Graphics::Elements::XYZContour, Graphics::Elements::XYZMap, Graphics::Legends::CurveLegend, Graphics::Styles::AxisTicks, Graphics::Styles::CurveStyleFactory, Graphics::Styles::PlotStyle, Graphics::Styles::TwoPointGradient, PlotMaker, PostProcess
- Defined in:
- lib/ctioga2/log.rb
Overview
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
- .context ⇒ Object
- .counts ⇒ Object
-
.debug(channel = nil) ⇒ Object
Prints a debug message, on channel channel.
-
.error ⇒ Object
Prints an error message.
-
.fatal ⇒ Object
Prints a fatal error message and initiates program termination.
-
.info ⇒ Object
Prints an informational message.
- .init_logger(stream = STDERR) ⇒ Object
-
.log_to(target_file, message = nil) ⇒ Object
Logs to the target file, and fall back onto stderr should opening fail.
-
.logger ⇒ Object
Simple accessor for the @@log class variable.
-
.set_level(level = Logger::WARN) ⇒ Object
Sets the logging level.
-
.warn ⇒ Object
Prints a warning message.
Instance Method Summary collapse
-
#format_exception(e) ⇒ Object
Format an exception for displaying.
-
#identify(obj) ⇒ Object
Returns a string suitable for identification of an object, a bit in the spirit of #inspect, but without displaying instance variables.
-
#spawn(cmd, priority = :info) ⇒ Object
A logged replacement for system.
Class Method Details
.context ⇒ Object
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 |
.counts ⇒ Object
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 |
.error ⇒ Object
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 |
.fatal ⇒ Object
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 |
.info ⇒ Object
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, = nil) if target_file.is_a? String begin target_file = File.open(target_file, "w") if target_file.puts end rescue target_file = STDERR end end old = @@logger @@logger = Logger.new(target_file) @@logger.level = old.level end |
.logger ⇒ Object
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 |
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.}\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 |