From 60fd3c29649e2727bde2d80d8309f7bf36463945 Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Thu, 26 Jan 2012 13:24:48 +0100 Subject: [PATCH] Removed the need for passing in 'this' as log source to LoggingReceive. See #1720 --- .../scala/akka/event/LoggingReceiveSpec.scala | 10 +++++----- .../main/scala/akka/event/LoggingReceive.scala | 15 +++++++++------ .../akka/docs/actor/FaultHandlingDocSample.scala | 8 ++++---- .../code/akka/docs/testkit/TestkitDocSpec.scala | 2 +- akka-docs/scala/testing.rst | 3 --- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/akka-actor-tests/src/test/scala/akka/event/LoggingReceiveSpec.scala b/akka-actor-tests/src/test/scala/akka/event/LoggingReceiveSpec.scala index 00c2e7d406..1ac765e400 100644 --- a/akka-actor-tests/src/test/scala/akka/event/LoggingReceiveSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/event/LoggingReceiveSpec.scala @@ -57,9 +57,9 @@ class LoggingReceiveSpec extends WordSpec with BeforeAndAfterEach with BeforeAnd new TestKit(appLogging) { system.eventStream.subscribe(testActor, classOf[Logging.Debug]) val a = system.actorOf(Props(new Actor { - def receive = LoggingReceive("funky") { + def receive = new LoggingReceive(Some("funky"), { case null ⇒ - } + }) })) a ! "hallo" expectMsg(1 second, Logging.Debug("funky", classOf[DummyClassForStringSources], "received unhandled message hallo")) @@ -78,7 +78,7 @@ class LoggingReceiveSpec extends WordSpec with BeforeAndAfterEach with BeforeAnd val actor = TestActorRef(new Actor { def switch: Actor.Receive = { case "becomenull" ⇒ context.become(r, false) } - def receive = switch orElse LoggingReceive(this) { + def receive = switch orElse LoggingReceive { case x ⇒ sender ! "x" } }) @@ -86,7 +86,7 @@ class LoggingReceiveSpec extends WordSpec with BeforeAndAfterEach with BeforeAnd val name = actor.path.toString actor ! "buh" within(1 second) { - expectMsg(Logging.Debug(name, actor.underlyingActor.getClass, "received handled message buh")) + expectMsg(Logging.Debug(actor.path.toString, actor.underlyingActor.getClass, "received handled message buh")) expectMsg("x") } @@ -106,7 +106,7 @@ class LoggingReceiveSpec extends WordSpec with BeforeAndAfterEach with BeforeAnd new TestKit(appLogging) with ImplicitSender { system.eventStream.subscribe(testActor, classOf[Logging.Debug]) val actor = TestActorRef(new Actor { - def receive = LoggingReceive(this)(LoggingReceive(this) { + def receive = LoggingReceive(LoggingReceive { case _ ⇒ sender ! "x" }) }) diff --git a/akka-actor/src/main/scala/akka/event/LoggingReceive.scala b/akka-actor/src/main/scala/akka/event/LoggingReceive.scala index e313ee59f9..452b2b6b19 100644 --- a/akka-actor/src/main/scala/akka/event/LoggingReceive.scala +++ b/akka-actor/src/main/scala/akka/event/LoggingReceive.scala @@ -5,6 +5,7 @@ package akka.event import akka.actor.Actor.Receive import akka.actor.ActorContext +import akka.actor.ActorCell import akka.event.Logging.Debug object LoggingReceive { @@ -15,7 +16,7 @@ object LoggingReceive { * This includes messages which are not handled. * *

-   * def receive = LoggingReceive(this) {
+   * def receive = LoggingReceive {
    *   case x => ...
    * }
    * 
@@ -23,20 +24,22 @@ object LoggingReceive { * This method does NOT modify the given Receive unless * akka.actor.debug.receive is set within akka.conf. */ - def apply(source: AnyRef)(r: Receive)(implicit context: ActorContext): Receive = r match { + def apply(r: Receive)(implicit context: ActorContext): Receive = r match { case _: LoggingReceive ⇒ r - case _ if !context.system.settings.AddLoggingReceive ⇒ r - case _ ⇒ new LoggingReceive(source, r) + case _ ⇒ + if (context.system.settings.AddLoggingReceive) new LoggingReceive(None, r) + else r } } /** * This decorator adds invocation logging to a Receive function. + * @param source the log source, if not defined the actor of the context will be used */ -class LoggingReceive(source: AnyRef, r: Receive)(implicit context: ActorContext) extends Receive { +class LoggingReceive(source: Option[AnyRef], r: Receive)(implicit context: ActorContext) extends Receive { def isDefinedAt(o: Any) = { val handled = r.isDefinedAt(o) - val (str, clazz) = LogSource.fromAnyRef(source) + val (str, clazz) = LogSource.fromAnyRef(source getOrElse context.asInstanceOf[ActorCell].actor) context.system.eventStream.publish(Debug(str, clazz, "received " + (if (handled) "handled" else "unhandled") + " message " + o)) handled } diff --git a/akka-docs/scala/code/akka/docs/actor/FaultHandlingDocSample.scala b/akka-docs/scala/code/akka/docs/actor/FaultHandlingDocSample.scala index f6661ef92a..551e96f70b 100644 --- a/akka-docs/scala/code/akka/docs/actor/FaultHandlingDocSample.scala +++ b/akka-docs/scala/code/akka/docs/actor/FaultHandlingDocSample.scala @@ -79,7 +79,7 @@ class Worker extends Actor with ActorLogging { var progressListener: Option[ActorRef] = None val counterService = context.actorOf(Props[CounterService], name = "counter") - def receive = LoggingReceive(this) { + def receive = LoggingReceive { case Start if progressListener.isEmpty ⇒ progressListener = Some(sender) context.system.scheduler.schedule(Duration.Zero, 1 second, self, Do) @@ -143,7 +143,7 @@ class CounterService extends Actor { storage.get ! Get(key) } - def receive = LoggingReceive(this) { + def receive = LoggingReceive { case Entry(k, v) if k == key && counter == None ⇒ // Reply from Storage of the initial value, now we can create the Counter @@ -206,7 +206,7 @@ class Counter(key: String, initialValue: Long) extends Actor { var count = initialValue var storage: Option[ActorRef] = None - def receive = LoggingReceive(this) { + def receive = LoggingReceive { case UseStorage(s) ⇒ storage = s storeCount() @@ -246,7 +246,7 @@ class Storage extends Actor { val db = DummyDB - def receive = LoggingReceive(this) { + def receive = LoggingReceive { case Store(Entry(key, count)) ⇒ db.save(key, count) case Get(key) ⇒ sender ! Entry(key, db.load(key).getOrElse(0L)) } diff --git a/akka-docs/scala/code/akka/docs/testkit/TestkitDocSpec.scala b/akka-docs/scala/code/akka/docs/testkit/TestkitDocSpec.scala index bcd8b63a9e..3a4608e840 100644 --- a/akka-docs/scala/code/akka/docs/testkit/TestkitDocSpec.scala +++ b/akka-docs/scala/code/akka/docs/testkit/TestkitDocSpec.scala @@ -61,7 +61,7 @@ object TestkitDocSpec { class LoggingActor extends Actor { //#logging-receive import akka.event.LoggingReceive - def receive = LoggingReceive(this) { + def receive = LoggingReceive { case msg ⇒ // Do something... } //#logging-receive diff --git a/akka-docs/scala/testing.rst b/akka-docs/scala/testing.rst index fb1513dea4..40abfc5d5c 100644 --- a/akka-docs/scala/testing.rst +++ b/akka-docs/scala/testing.rst @@ -600,9 +600,6 @@ options: .. includecode:: code/akka/docs/testkit/TestkitDocSpec.scala#logging-receive . - The first argument to :meth:`LoggingReceive` defines the source to be used in the - logging events, which should be the current actor. - If the abovementioned setting is not given in the :ref:`configuration`, this method will pass through the given :class:`Receive` function unmodified, meaning that there is no runtime cost unless actually enabled.