Skip to content

Fix for shapeless @@ types passed on as args #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/main/scala/com/typesafe/scalalogging/LoggerMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private object LoggerMacro {
val underlying = q"${c.prefix}.underlying"
val anyRefArgs = formatArgs(c)(args: _*)
if (args.length == 2)
q"if ($underlying.isErrorEnabled) $underlying.error($message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
q"if ($underlying.isErrorEnabled) $underlying.error($message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
else
q"if ($underlying.isErrorEnabled) $underlying.error($message, ..$anyRefArgs)"
}
Expand All @@ -62,7 +62,7 @@ private object LoggerMacro {
val underlying = q"${c.prefix}.underlying"
val anyRefArgs = formatArgs(c)(args: _*)
if (args.length == 2)
q"if ($underlying.isErrorEnabled($marker)) $underlying.error($marker, $message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
q"if ($underlying.isErrorEnabled($marker)) $underlying.error($marker, $message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
else
q"if ($underlying.isErrorEnabled($marker)) $underlying.error($marker, $message, ..$anyRefArgs)"
}
Expand Down Expand Up @@ -91,7 +91,7 @@ private object LoggerMacro {
val underlying = q"${c.prefix}.underlying"
val anyRefArgs = formatArgs(c)(args: _*)
if (args.length == 2)
q"if ($underlying.isWarnEnabled) $underlying.warn($message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
q"if ($underlying.isWarnEnabled) $underlying.warn($message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
else
q"if ($underlying.isWarnEnabled) $underlying.warn($message, ..$anyRefArgs)"
}
Expand All @@ -112,7 +112,7 @@ private object LoggerMacro {
val underlying = q"${c.prefix}.underlying"
val anyRefArgs = formatArgs(c)(args: _*)
if (args.length == 2)
q"if ($underlying.isWarnEnabled($marker)) $underlying.warn($marker, $message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
q"if ($underlying.isWarnEnabled($marker)) $underlying.warn($marker, $message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
else
q"if ($underlying.isWarnEnabled($marker)) $underlying.warn($marker, $message, ..$anyRefArgs)"
}
Expand Down Expand Up @@ -141,7 +141,7 @@ private object LoggerMacro {
val underlying = q"${c.prefix}.underlying"
val anyRefArgs = formatArgs(c)(args: _*)
if (args.length == 2)
q"if ($underlying.isInfoEnabled) $underlying.info($message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
q"if ($underlying.isInfoEnabled) $underlying.info($message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
else
q"if ($underlying.isInfoEnabled) $underlying.info($message, ..$anyRefArgs)"
}
Expand All @@ -162,7 +162,7 @@ private object LoggerMacro {
val underlying = q"${c.prefix}.underlying"
val anyRefArgs = formatArgs(c)(args: _*)
if (args.length == 2)
q"if ($underlying.isInfoEnabled($marker)) $underlying.info($marker, $message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
q"if ($underlying.isInfoEnabled($marker)) $underlying.info($marker, $message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
else
q"if ($underlying.isInfoEnabled($marker)) $underlying.info($marker, $message, ..$anyRefArgs)"
}
Expand Down Expand Up @@ -191,7 +191,7 @@ private object LoggerMacro {
val underlying = q"${c.prefix}.underlying"
val anyRefArgs = formatArgs(c)(args: _*)
if (args.length == 2)
q"if ($underlying.isDebugEnabled) $underlying.debug($message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
q"if ($underlying.isDebugEnabled) $underlying.debug($message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
else
q"if ($underlying.isDebugEnabled) $underlying.debug($message, ..$anyRefArgs)"
}
Expand All @@ -212,7 +212,7 @@ private object LoggerMacro {
val underlying = q"${c.prefix}.underlying"
val anyRefArgs = formatArgs(c)(args: _*)
if (args.length == 2)
q"if ($underlying.isDebugEnabled($marker)) $underlying.debug($marker, $message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
q"if ($underlying.isDebugEnabled($marker)) $underlying.debug($marker, $message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
else
q"if ($underlying.isDebugEnabled($marker)) $underlying.debug($marker, $message, ..$anyRefArgs)"
}
Expand Down Expand Up @@ -241,7 +241,7 @@ private object LoggerMacro {
val underlying = q"${c.prefix}.underlying"
val anyRefArgs = formatArgs(c)(args: _*)
if (args.length == 2)
q"if ($underlying.isTraceEnabled) $underlying.trace($message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
q"if ($underlying.isTraceEnabled) $underlying.trace($message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
else
q"if ($underlying.isTraceEnabled) $underlying.trace($message, ..$anyRefArgs)"
}
Expand All @@ -262,7 +262,7 @@ private object LoggerMacro {
val underlying = q"${c.prefix}.underlying"
val anyRefArgs = formatArgs(c)(args: _*)
if (args.length == 2)
q"if ($underlying.isTraceEnabled($marker)) $underlying.trace($marker, $message, _root_.scala.Array(${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
q"if ($underlying.isTraceEnabled($marker)) $underlying.trace($marker, $message, _root_.scala.Array[AnyRef](${anyRefArgs.head}, ${anyRefArgs(1)}): _*)"
else
q"if ($underlying.isTraceEnabled($marker)) $underlying.trace($marker, $message, ..$anyRefArgs)"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.typesafe.scalalogging

import org.scalatest.{ Matchers, WordSpec }

import java.lang.ClassCastException
import org.slf4j.{ Logger => Underlying }
import org.scalatestplus.mockito.MockitoSugar
import org.mockito.ArgumentMatchers._
import org.mockito.Mockito._

object tag {

def apply[U] = new Tagger[U]

trait Tagged[U]
type @@[+T, U] = T with Tagged[U]

class Tagger[U] {
def apply[T](t: T): T @@ U = t.asInstanceOf[T @@ U]
}
}

class LoggerWithTaggedAargsSpec extends WordSpec with MockitoSugar with Matchers with Varargs {

trait Tag

import tag._

"Calling error with tagged args" should {

"not throw ClassCastException when varargs are passed" in {
val f = fixture(_.isErrorEnabled)
import f._
noException shouldBe thrownBy {
logger.error("This should not throw: {}, {}", arg1, arg2)
}
}

"not throw ClassCastException when interpolated message is passed" in {
val f = fixture(_.isErrorEnabled)
import f._
noException shouldBe thrownBy {
logger.error(s"This should not throw: $arg1, $arg2")
}
}
}

"Calling trace with tagged args" should {

"not throw ClassCastException when varargs are passed" in {
val f = fixture(_.isTraceEnabled)
import f._
noException shouldBe thrownBy {
logger.trace("This should not throw: {}, {}", arg1, arg2)
}
}

"not throw ClassCastException when interpolated message is passed" in {
val f = fixture(_.isTraceEnabled)
import f._
noException shouldBe thrownBy {
logger.trace(s"This should not throw: $arg1, $arg2")
}
}
}

"Calling debug with tagged args" should {

"not throw ClassCastException when varargs are passed" in {
val f = fixture(_.isTraceEnabled)
import f._
noException shouldBe thrownBy {
logger.debug("This should not throw: {}, {}", arg1, arg2)
}
}

"not throw ClassCastException when interpolated message is passed" in {
val f = fixture(_.isTraceEnabled)
import f._
noException shouldBe thrownBy {
logger.debug(s"This should not throw: $arg1, $arg2")
}
}
}

"Calling info with tagged args" should {

"not throw ClassCastException when varargs are passed" in {
val f = fixture(_.isInfoEnabled)
import f._
noException shouldBe thrownBy {
logger.info("This should not throw: {}, {}", arg1, arg2)
}
}

"not throw ClassCastException when interpolated message is passed" in {
val f = fixture(_.isInfoEnabled)
import f._
noException shouldBe thrownBy {
logger.info(s"This should not throw: $arg1, $arg2")
}
}
}

def fixture(p: Underlying => Boolean, isEnabled: Boolean = true) =
new {
val arg1 = tag[Tag][String]("arg1")
val arg2 = tag[Tag][Integer](Integer.valueOf(1))
val underlying = mock[org.slf4j.Logger]
when(p(underlying)).thenReturn(isEnabled)
val logger = Logger(underlying)
}
}