LoggingReceive with implicit ActorContext instead of ActorSystem. See #1720

This commit is contained in:
Patrik Nordwall 2012-01-26 10:58:35 +01:00
parent 519ce8a142
commit 52aa4f2a64
4 changed files with 13 additions and 17 deletions

View file

@ -56,11 +56,12 @@ class LoggingReceiveSpec extends WordSpec with BeforeAndAfterEach with BeforeAnd
"decorate a Receive" in { "decorate a Receive" in {
new TestKit(appLogging) { new TestKit(appLogging) {
system.eventStream.subscribe(testActor, classOf[Logging.Debug]) system.eventStream.subscribe(testActor, classOf[Logging.Debug])
val r: Actor.Receive = { val a = system.actorOf(Props(new Actor {
case null def receive = LoggingReceive("funky") {
} case null
val log = LoggingReceive("funky")(r) }
log.isDefinedAt("hallo") }))
a ! "hallo"
expectMsg(1 second, Logging.Debug("funky", classOf[DummyClassForStringSources], "received unhandled message hallo")) expectMsg(1 second, Logging.Debug("funky", classOf[DummyClassForStringSources], "received unhandled message hallo"))
} }
} }

View file

@ -4,7 +4,7 @@
package akka.event package akka.event
import akka.actor.Actor.Receive import akka.actor.Actor.Receive
import akka.actor.ActorSystem import akka.actor.ActorContext
import akka.event.Logging.Debug import akka.event.Logging.Debug
object LoggingReceive { object LoggingReceive {
@ -23,21 +23,21 @@ object LoggingReceive {
* This method does NOT modify the given Receive unless * This method does NOT modify the given Receive unless
* akka.actor.debug.receive is set within akka.conf. * akka.actor.debug.receive is set within akka.conf.
*/ */
def apply(source: AnyRef)(r: Receive)(implicit system: ActorSystem): Receive = r match { def apply(source: AnyRef)(r: Receive)(implicit context: ActorContext): Receive = r match {
case _: LoggingReceive r case _: LoggingReceive r
case _ if !system.settings.AddLoggingReceive r case _ if !context.system.settings.AddLoggingReceive r
case _ new LoggingReceive(source, r) case _ new LoggingReceive(source, r)
} }
} }
/** /**
* This decorator adds invocation logging to a Receive function. * This decorator adds invocation logging to a Receive function.
*/ */
class LoggingReceive(source: AnyRef, r: Receive)(implicit system: ActorSystem) extends Receive { class LoggingReceive(source: AnyRef, r: Receive)(implicit context: ActorContext) extends Receive {
def isDefinedAt(o: Any) = { def isDefinedAt(o: Any) = {
val handled = r.isDefinedAt(o) val handled = r.isDefinedAt(o)
val (str, clazz) = LogSource.fromAnyRef(source) val (str, clazz) = LogSource.fromAnyRef(source)
system.eventStream.publish(Debug(str, clazz, "received " + (if (handled) "handled" else "unhandled") + " message " + o)) context.system.eventStream.publish(Debug(str, clazz, "received " + (if (handled) "handled" else "unhandled") + " message " + o))
handled handled
} }
def apply(o: Any): Unit = r(o) def apply(o: Any): Unit = r(o)

View file

@ -68,7 +68,6 @@ object Worker {
class Worker extends Actor with ActorLogging { class Worker extends Actor with ActorLogging {
import Worker._ import Worker._
import CounterService._ import CounterService._
implicit def system = context.system
implicit val askTimeout = Timeout(5 seconds) implicit val askTimeout = Timeout(5 seconds)
// Stop the CounterService child if it throws ServiceUnavailable // Stop the CounterService child if it throws ServiceUnavailable
@ -113,7 +112,6 @@ class CounterService extends Actor {
import CounterService._ import CounterService._
import Counter._ import Counter._
import Storage._ import Storage._
implicit def system = context.system
// Restart the storage child when StorageException is thrown. // Restart the storage child when StorageException is thrown.
// After 3 restarts within 5 seconds it will be stopped. // After 3 restarts within 5 seconds it will be stopped.
@ -204,7 +202,6 @@ class Counter(key: String, initialValue: Long) extends Actor {
import Counter._ import Counter._
import CounterService._ import CounterService._
import Storage._ import Storage._
implicit def system = context.system
var count = initialValue var count = initialValue
var storage: Option[ActorRef] = None var storage: Option[ActorRef] = None
@ -246,7 +243,6 @@ object Storage {
*/ */
class Storage extends Actor { class Storage extends Actor {
import Storage._ import Storage._
implicit def system = context.system
val db = DummyDB val db = DummyDB

View file

@ -61,7 +61,6 @@ object TestkitDocSpec {
class LoggingActor extends Actor { class LoggingActor extends Actor {
//#logging-receive //#logging-receive
import akka.event.LoggingReceive import akka.event.LoggingReceive
implicit def system = context.system
def receive = LoggingReceive(this) { def receive = LoggingReceive(this) {
case msg // Do something... case msg // Do something...
} }