Skip to content

Commit

Permalink
fixes #436
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnu-liguobin committed Mar 2, 2024
1 parent a6e2c8b commit 59603af
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
31 changes: 21 additions & 10 deletions src/main/scala-3/com/typesafe/scalalogging/LoggerMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -261,30 +261,41 @@ private[scalalogging] object LoggerMacro {
case _ => (message, Seq.empty)
}
}
def formatArgs(args: Expr[Seq[Any]])(using q: Quotes): Seq[Expr[AnyRef]] = {

def formatArgs(args: Expr[Seq[Any]])(using q: Quotes

):
Seq[Expr[AnyRef]] = {
import quotes.reflect.*
import util.*
// we recursively obtain the actual value of inline parameters
def map(term:Term) = {
// we recursively obtain the actual value of inline parameters
def map(term: Term) = {
term match {
case t if t.tpe <:< TypeRepr.of[AnyRef] => t.asExprOf[AnyRef]
case t => '{${t.asExpr}.asInstanceOf[AnyRef]}
case t => '{
${
t.asExpr
}.asInstanceOf[AnyRef]
}
}
}

def rec(tree: Term): Option[Seq[Expr[AnyRef]]] = tree match {
case Repeated(elems, _) => Some(elems.map(map))
case Block(Nil, e) => rec(e)
case tped@Typed(Ident(_), _) =>
tped.symbol.tree match {
case ValDef(_, _, Some(rhs)) => rec(rhs)
case _ => None
}
case Typed(e, _) => rec(e)
case Inlined(_, Nil, e) => rec(e)
// Seq():_*, List():_* , forceVarargs(1,2):_*e.g.,
case Apply(TypeApply(_, _), List(Typed(Repeated(elems, _),_))) =>
case Apply(TypeApply(_, _), List(Typed(Repeated(elems, _), _))) =>
Some(elems.map(map))
case Ident(name) =>
// todo
None
case _ =>
case _ =>
None
}

rec(args.asTerm).getOrElse(Seq.empty)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ class Scala3LoggerSpec extends AnyWordSpec with Matchers with Varargs with Mocki
verify(underlying).info("""Hello {}""", arg5ref)
}

"work when passing a inline value as repeated arguments" in {
"work when passing a val as repeated arguments" in {
val f = fixture(_.isInfoEnabled, isEnabled = true)
import f._
val argss = Seq(arg5ref, arg5ref, arg5ref)
logger.info("""Hello {} {} {}""", argss:_*)
verify(underlying).info("""Hello {} {} {}""", Seq(arg5ref, arg5ref, arg5ref):_*)
}

"work when passing a inline def as repeated arguments" in {
val f = fixture(_.isInfoEnabled, isEnabled = true)
import f._
inline def argss = Seq(arg5ref, arg5ref, arg5ref)
Expand Down

0 comments on commit 59603af

Please sign in to comment.