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
-
.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 |
# 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 |
.error ⇒ Object
Prints an error message
47 48 49 |
# File 'lib/ctioga2/log.rb', line 47 def error @@logger.error {yield + Log.context} end |
.fatal ⇒ Object
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 |
.info ⇒ Object
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, = 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.
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 |
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.}\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 |