Sha256: a01393b445b6a4326dbaefd0a53970ccfb7df61b511b34f90004af8855b07ded

Contents?: true

Size: 1.39 KB

Versions: 7

Compression:

Stored size: 1.39 KB

Contents

# -*- ruby -*-
# vim: set nosta noet ts=4 sw=4:
# frozen_string_literal: true

require 'loggability' unless defined?( Loggability )


### An abstract base class for Loggability log formatters.
class Loggability::Formatter

	##
	# Derivative classes, keyed by name
	singleton_class.attr_reader( :derivatives )
	@derivatives = {}


	### Inherited hook -- add subclasses to the ::derivatives Array.
	def self::inherited( subclass )
		super

		if ( name = subclass.name )
			classname = name.sub( /.*::/, '' ).downcase
		else
			classname = "anonymous_%d" % [ subclass.object_id ]
		end

		Loggability::Formatter.derivatives[ classname.to_sym ] = subclass
	end


	### Create a formatter of the specified +type+, loading it if it hasn't already been
	### loaded.
	def self::create( type, *args )
		require "loggability/formatter/#{type}"
		type = type.to_sym

		if self.derivatives.key?( type )
			return self.derivatives[ type ].new( *args )
		else
			raise LoadError,
				"require of %s formatter succeeded (%p), but it didn't load a class named %p::%s" %
				[ type, self.derivatives, self, type.to_s.capitalize ]
		end
	end


	### Create a log message from the given +severity+, +time+, +progname+, and +message+
	### and return it.
	def call( severity, time, progname, message )
		raise NotImplementedError, "%p doesn't implement required method %s" %
			[ self.class, __method__ ]
	end


end # class Loggability::Formatter

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
loggability-0.18.2 lib/loggability/formatter.rb
loggability-0.18.1 lib/loggability/formatter.rb
loggability-0.18.0 lib/loggability/formatter.rb
loggability-0.17.0 lib/loggability/formatter.rb
loggability-0.16.0 lib/loggability/formatter.rb
loggability-0.15.1 lib/loggability/formatter.rb
loggability-0.15.0.pre20190714094638 lib/loggability/formatter.rb