You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pekko.apache.org by jr...@apache.org on 2022/11/03 11:23:34 UTC

[incubator-pekko-http] 04/47: parsing: Scala 3 port of LogHelper macros

This is an automated email from the ASF dual-hosted git repository.

jrudolph pushed a commit to branch scala-3
in repository https://gitbox.apache.org/repos/asf/incubator-pekko-http.git

commit 048f8c5ba1b532e3bc9aa40d38f3018a82797612
Author: Johannes Rudolph <jo...@gmail.com>
AuthorDate: Thu Sep 9 12:13:36 2021 +0200

    parsing: Scala 3 port of LogHelper macros
---
 .../akka/macros/LogHelperMacro.scala}              | 30 ++++------------
 .../main/scala-3/akka/macros/LogHelperMacro.scala  | 19 ++++++++++
 .../src/main/scala/akka/macros/LogHelper.scala     | 42 ++--------------------
 3 files changed, 28 insertions(+), 63 deletions(-)

diff --git a/akka-parsing/src/main/scala/akka/macros/LogHelper.scala b/akka-parsing/src/main/scala-2/akka/macros/LogHelperMacro.scala
similarity index 55%
copy from akka-parsing/src/main/scala/akka/macros/LogHelper.scala
copy to akka-parsing/src/main/scala-2/akka/macros/LogHelperMacro.scala
index c781fe61a..acdf188ab 100644
--- a/akka-parsing/src/main/scala/akka/macros/LogHelper.scala
+++ b/akka-parsing/src/main/scala-2/akka/macros/LogHelperMacro.scala
@@ -1,38 +1,20 @@
-/*
- * Copyright (C) 2009-2021 Lightbend Inc. <https://www.lightbend.com>
- */
-
 package akka.macros
 
 import akka.annotation.InternalApi
-import akka.event.LoggingAdapter
 
 import scala.reflect.macros.blackbox
 
-/**
- * INTERNAL API
- *
- * Provides access to a LoggingAdapter which each call guarded by `if (log.isXXXEnabled)` to prevent evaluating
- * the message expression eagerly.
- */
+/** INTERNAL API */
 @InternalApi
-private[akka] trait LogHelper {
-  def log: LoggingAdapter
-  def isDebugEnabled: Boolean = log.isDebugEnabled
-  def isInfoEnabled: Boolean = log.isInfoEnabled
-  def isWarningEnabled: Boolean = log.isWarningEnabled
-
-  /** Override to prefix every log message with a user-defined context string */
-  def prefixString: String = ""
-
-  def debug(msg: String): Unit = macro LogHelper.debugMacro
-  def info(msg: String): Unit = macro LogHelper.infoMacro
-  def warning(msg: String): Unit = macro LogHelper.warningMacro
+private[akka] trait LogHelperMacro {
+  def debug(msg: String): Unit = macro LogHelperMacro.debugMacro
+  def info(msg: String): Unit = macro LogHelperMacro.infoMacro
+  def warning(msg: String): Unit = macro LogHelperMacro.warningMacro
 }
 
 /** INTERNAL API */
 @InternalApi
-private[akka] object LogHelper {
+private[akka] object LogHelperMacro {
   type LoggerContext = blackbox.Context { type PrefixType = LogHelper }
 
   def debugMacro(ctx: LoggerContext)(msg: ctx.Expr[String]): ctx.Expr[Unit] =
diff --git a/akka-parsing/src/main/scala-3/akka/macros/LogHelperMacro.scala b/akka-parsing/src/main/scala-3/akka/macros/LogHelperMacro.scala
new file mode 100644
index 000000000..1b6646733
--- /dev/null
+++ b/akka-parsing/src/main/scala-3/akka/macros/LogHelperMacro.scala
@@ -0,0 +1,19 @@
+package akka.macros
+
+import akka.annotation.InternalApi
+
+import scala.quoted._
+
+/** INTERNAL API */
+@InternalApi
+private[akka] trait LogHelperMacro { self: LogHelper =>
+  inline def debug(inline msg: String): Unit = ${LogHelperMacro.guard('{isDebugEnabled}, '{log.debug(prefixString + msg)})}
+  inline def info(inline msg: String): Unit = ${LogHelperMacro.guard('{isInfoEnabled}, '{log.info(prefixString + msg)})}
+  inline def warning(inline msg: String): Unit = ${LogHelperMacro.guard('{isWarningEnabled}, '{log.warning(prefixString + msg)})}
+}
+
+/** INTERNAL API */
+@InternalApi
+private[akka] object LogHelperMacro {
+  def guard(isEnabled: Expr[Boolean], log: Expr[Unit])(using Quotes): Expr[Unit] = '{ if ($isEnabled) $log }
+}
diff --git a/akka-parsing/src/main/scala/akka/macros/LogHelper.scala b/akka-parsing/src/main/scala/akka/macros/LogHelper.scala
index c781fe61a..b2999c93b 100644
--- a/akka-parsing/src/main/scala/akka/macros/LogHelper.scala
+++ b/akka-parsing/src/main/scala/akka/macros/LogHelper.scala
@@ -5,9 +5,7 @@
 package akka.macros
 
 import akka.annotation.InternalApi
-import akka.event.LoggingAdapter
-
-import scala.reflect.macros.blackbox
+//import akka.event.LoggingAdapter
 
 /**
  * INTERNAL API
@@ -16,47 +14,13 @@ import scala.reflect.macros.blackbox
  * the message expression eagerly.
  */
 @InternalApi
-private[akka] trait LogHelper {
-  def log: LoggingAdapter
+private[akka] trait LogHelper extends LogHelperMacro {
+  def log: akka.event.LoggingAdapter
   def isDebugEnabled: Boolean = log.isDebugEnabled
   def isInfoEnabled: Boolean = log.isInfoEnabled
   def isWarningEnabled: Boolean = log.isWarningEnabled
 
   /** Override to prefix every log message with a user-defined context string */
   def prefixString: String = ""
-
-  def debug(msg: String): Unit = macro LogHelper.debugMacro
-  def info(msg: String): Unit = macro LogHelper.infoMacro
-  def warning(msg: String): Unit = macro LogHelper.warningMacro
 }
 
-/** INTERNAL API */
-@InternalApi
-private[akka] object LogHelper {
-  type LoggerContext = blackbox.Context { type PrefixType = LogHelper }
-
-  def debugMacro(ctx: LoggerContext)(msg: ctx.Expr[String]): ctx.Expr[Unit] =
-    ctx.universe.reify {
-      {
-        val logHelper = ctx.prefix.splice
-        if (logHelper.isDebugEnabled)
-          logHelper.log.debug(logHelper.prefixString + msg.splice)
-      }
-    }
-  def infoMacro(ctx: LoggerContext)(msg: ctx.Expr[String]): ctx.Expr[Unit] =
-    ctx.universe.reify {
-      {
-        val logHelper = ctx.prefix.splice
-        if (logHelper.isInfoEnabled)
-          logHelper.log.info(logHelper.prefixString + msg.splice)
-      }
-    }
-  def warningMacro(ctx: LoggerContext)(msg: ctx.Expr[String]): ctx.Expr[Unit] =
-    ctx.universe.reify {
-      {
-        val logHelper = ctx.prefix.splice
-        if (logHelper.isWarningEnabled)
-          logHelper.log.warning(logHelper.prefixString + msg.splice)
-      }
-    }
-}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pekko.apache.org
For additional commands, e-mail: commits-help@pekko.apache.org