lib/ztk/base.rb in ztk-0.3.1 vs lib/ztk/base.rb in ztk-1.0.0.rc.0
- old
+ new
@@ -15,11 +15,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
-
require "ostruct"
module ZTK
# ZTK::Base Error Class
@@ -35,11 +34,11 @@
# and extend functionality as appropriate.
class Base
class << self
- # @param [Hash] config Configuration options hash.
+ # @param [Hash] configuration Configuration options hash.
# @option config [IO] :stdout Instance of IO to be used for STDOUT.
# @option config [IO] :stderr Instance of IO to be used for STDERR.
# @option config [IO] :stdin Instance of IO to be used for STDIN.
# @option config [Logger] :logger Instance of Logger to be used for logging.
def build_config(configuration={})
@@ -79,10 +78,18 @@
end
config
end
+ # Logs an exception and then raises it.
+ #
+ # @param [Logger] logger An instance of a class based off the Ruby
+ # *Logger* class.
+ # @param [Exception] exception The exception class to raise.
+ # @param [String] message The message to display with the exception.
+ # @param [Integer] shift (1) How many places to shift the caller stack in
+ # the log statement.
def log_and_raise(logger, exception, message, shift=1)
if logger.is_a?(ZTK::Logger)
logger.shift(:fatal, shift) { "EXCEPTION: #{exception.inspect} - #{message.inspect}" }
else
logger.fatal { "EXCEPTION: #{exception.inspect} - #{message.inspect}" }
@@ -115,12 +122,20 @@
else
@config
end
end
- def log_and_raise(exception, message)
- Base.log_and_raise(config.logger, exception, message, 2)
+ # Logs an exception and then raises it.
+ #
+ # @see Base.log_and_raise
+ #
+ # @param [Exception] exception The exception class to raise.
+ # @param [String] message The message to display with the exception.
+ # @param [Integer] shift (2) How many places to shift the caller stack in
+ # the log statement.
+ def log_and_raise(exception, message, shift=2)
+ Base.log_and_raise(config.logger, exception, message, shift)
end
# Direct logging method.
#
# This method provides direct writing of data to the current log device.
@@ -128,10 +143,10 @@
# ZTK::SSH and ZTK::Command, but could easily be used by other classes.
#
# The value returned in the block is passed down to the logger specified in
# the classes configuration.
#
- # @param [Symbol] method_name This should be any one of [:debug, :info, :warn, :error, :fatal].
+ # @param [Symbol] log_level This should be any one of [:debug, :info, :warn, :error, :fatal].
# @yield No value is passed to the block.
# @yieldreturn [String] The message to log.
def direct_log(log_level, &blocK)
@config.logger.nil? and raise BaseError, "You must supply a logger for direct logging support!"