""" Simulator Logging	embed Simlog().instance().msg( "subject", "content" ) in your application	set Simlog().instance().watch(["subject"]) for all message subjects and only those subjects are logged"""import types;import fnmatch;import string;import sys;class Simlog:	"Logging with subject filtering"	myInstance= None;	def __init__( self ):		self.subjectSet= [];		self.all= 0;	def instance( self ):		"The single instance of this class"		if( Simlog.myInstance == None ):			Simlog.myInstance= self;		return Simlog.myInstance;	def summary( self ):		"summary log - only selected subjects will print"		self.all= 0;		return self;	def detail( self ):		"detail log - all subjects will print"		self.all= 1;		return self;	def watch( self, subject ):		"set a watch on a specific subject or list of subjects"		if( type(subject) == types.StringType ): self.subjectSet.append( subject );		else: self.subjectSet= subject;		return self;	def msg( self, subject, *payload ):		"if watch set on this subject, print the payload"		if( len(self.subjectSet) == 0 ):			m= 0;		else:			m= reduce( lambda x,y:x or y, 				map( lambda pat,sub=subject:fnmatch.fnmatch(sub,pat), self.subjectSet ) );		if( m or self.all ): 			print subject, string.join(payload,"; ");			sys.stdout.flush();			def theLog():	"Utility function to return the singleton instance of the log"	return Simlog().instance();	def test():	Simlog().instance().watch( [ "bet.anycraps", "sim.*" ] );	Simlog().instance().msg( "hack", "skip this" );	Simlog().instance().msg( "bet.pass", "skip this" );	Simlog().instance().msg( "bet.anycraps", "any craps bet" );	Simlog().instance().msg( "sim.round", "start of round" );	Simlog().instance().msg( "sim.end", "end of simulation" );if( __name__ == "__main__" ):	test();