'From VisualWorks(R) Release 2.0 of 4 August 1994 on 7 July 1997 at 2:02:52 pm'! nil subclass: #Tracer instanceVariableNames: 'object recorder ' classVariableNames: '' poolDictionaries: '' category: 'Interface-Debugger'! Tracer comment: 'Tracer implements a simple tracing mechanism which displays message sends to an object, and the results returned in the form: classname[ message #(list of arguments)] ^results To create a tracer on an object which prints to the Transcript, use aTracerObject := Tracer on: anObject Then send the messages you would send to anObject to aTracerObject. For examples of how this works, see example1 and example2 class methods. See example2 if you want to print to a stream. '! !Tracer methodsFor: 'message handling'! doesNotUnderstand: aMessage "trace the selector then pass the message on" | result | recorder nextPutAll: object class printString; nextPutAll: '['; nextPutAll: aMessage selector asString. aMessage arguments size = 0 ifTrue: [result := object perform: aMessage selector] ifFalse: [recorder nextPutAll: aMessage arguments printString. result := object perform: aMessage selector withArguments: aMessage arguments]. recorder nextPutAll: ']^'; nextPutAll: result printString; cr. recorder class == TextCollector ifTrue: [recorder endEntry]. ^result! ! !Tracer methodsFor: 'accessing'! setTracerObjectTo: newObject recordingOn: aStreamOrTranscript "change the object which I trace, and the stream I record on. Hopefully a suitably obscure selector!!" object := newObject. recorder := aStreamOrTranscript! ! "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "! Tracer class instanceVariableNames: ''! !Tracer class methodsFor: 'class initialization'! initialize "Remove my superclass pointer. All messages to me will then generate 'doesNotUnderstand'" superclass := nil "Tracer initialize"! ! !Tracer class methodsFor: 'instance creation'! on: anObject "open a tracer on anObject" ^self new setTracerObjectTo: anObject recordingOn: Transcript! on: anObject loggedOn: aStream "open a tracer on anObject, recording messages on aStream" ^self new setTracerObjectTo: anObject recordingOn: aStream! ! !Tracer class methodsFor: 'examples'! example1 "demonstrate a simple trace. must have a transcript handy!!" "Tracer example1" "<--- click on `Print It'" | anArray | anArray := Tracer on: (Array new: 3). anArray at: 2 put: 'hello'. ^anArray size! example2 "demonstrate a simple trace, logged on a stream." "Tracer example2" "<--- click on `Print It'" | anArray aStream | aStream := ReadWriteStream on: ''. anArray := Tracer on: (Array new: 3) loggedOn: aStream. anArray at: 2 put: 'hello'. anArray at: 2. anArray printString. ^aStream contents! ! Tracer initialize!