LoggingStream.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 5243 3aebf35110ff
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:

"{ Package: 'stx:libbasic2' }"

"{ NameSpace: Smalltalk }"

Stream subclass:#LoggingStream
	instanceVariableNames:'loggedStream logger'
	classVariableNames:''
	poolDictionaries:''
	category:'Streams-Misc'
!

!LoggingStream class methodsFor:'documentation'!

documentation
"
    Useful for debugging - stream operations are logged in human readable form
    on a separate logger stream, while stream operations are forwarded to the underlying
    loggedStream.

    Unfinished - may need more protocol to be intercepted.

    sample use, logging operations on a socket stream:
        sock := ... Socket connectTo: ...
        s := LoggingStream new loggedStream: sock.
        ...
        use s instead of sock
        ...

    [author:]
        cg

    [instance variables:]

    [class variables:]

    [see also:]

"
! !

!LoggingStream class methodsFor:'instance creation'!

on:aStream
    "Create & return a new instance for aStream, logging to the Transcript."

    ^ self on:aStream logger:Transcript
!

on:aStream logger:aLoggerStream
    "Create & return a new instance for aStream, logging to aLoggerStream."

    ^ self basicNew initializeOn:aStream logger:aLoggerStream
! !

!LoggingStream methodsFor:'accessing'!

loggedStream
    ^ loggedStream
!

loggedStream:something
    loggedStream := something.
!

logger
    ^ logger
!

logger:something
    logger := something.
! !

!LoggingStream methodsFor:'initialization'!

initializeOn:aStream logger:aLogger
    loggedStream := aStream.
    logger := aLogger.
! !

!LoggingStream methodsFor:'logging'!

log:messageString
    logger nextPutLine:messageString.
! !

!LoggingStream methodsFor:'reading'!

next
    |el|

    el := loggedStream next.
    self log:( '<<< ' , el storeString ).
    ^ el
! !

!LoggingStream methodsFor:'stream protocol'!

atEnd
    ^ loggedStream atEnd.
!

flush
    loggedStream flush.
!

next:n
    |els|

    els := loggedStream next:n.
    self log:( '<<< ' , els storeString ).
    ^ els
!

nextBytes:n
    |els|

    els := loggedStream nextBytes:n.
    self log:( '<<< ' , els storeString ).
    ^ els
!

nextPut:something
    self log:( '>>> ' , something storeString).
    loggedStream nextPut:something.
    ^ something
!

nextPutAll:aCollection
    self log:( '>>> ' , aCollection storeString).
    loggedStream nextPutAll:aCollection.
!

peek
    ^ loggedStream peek
!

peekOrNil
    ^ loggedStream peekOrNil
!

readWait
    ^ loggedStream readWait.
!

skipSeparators
    |ch|

    [
        (ch := loggedStream peekOrNil) notNil
        and:[ch isSeparator]
    ] whileTrue:[
        self next.
    ].
    ^ ch
!

upToEnd
    |els|

    els := loggedStream upToEnd.
    self log:( '<<< ' , els storeString).
    ^ els
! !

!LoggingStream class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !