Skip to content

Commit

Permalink
Fixes to logger interpolation in Macros (Resolves #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Mar 16, 2018
1 parent 842eaa8 commit 8a84548
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 54 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sbtcrossproject.{CrossType, crossProject}

name := "scribe"
organization in ThisBuild := "com.outr"
version in ThisBuild := "2.2.1"
version in ThisBuild := "2.2.2-SNAPSHOT"
scalaVersion in ThisBuild := "2.12.4"
crossScalaVersions in ThisBuild := List("2.12.4", "2.11.12")
scalacOptions in ThisBuild ++= Seq("-unchecked", "-deprecation")
Expand Down
28 changes: 7 additions & 21 deletions core/shared/src/main/scala/scribe/LogRecord.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@ import scala.annotation.tailrec
trait LogRecord {
def level: Level
def value: Double
def messageFunction: () => Any
def stringify: Any => String
def message: String
def className: String
def methodName: Option[String]
def lineNumber: Option[Int]
def thread: Thread
def timeStamp: Long

lazy val message: String = stringify(messageFunction())

def boost(booster: Double => Double): LogRecord = copy(value = booster(value))

def copy(level: Level = level,
value: Double = value,
messageFunction: () => Any = messageFunction,
stringify: Any => String = stringify,
message: String = message,
className: String = className,
methodName: Option[String] = methodName,
lineNumber: Option[Int] = lineNumber,
Expand All @@ -31,23 +27,15 @@ trait LogRecord {
}

object LogRecord {
object Stringify {
val Default: Any => String = {
case t: Throwable => throwable2String(t)
case v => String.valueOf(v)
}
}

def apply(level: Level,
value: Double,
messageFunction: () => Any,
stringify: Any => String,
message: String,
className: String,
methodName: Option[String],
lineNumber: Option[Int],
thread: Thread = Thread.currentThread(),
timeStamp: Long = System.currentTimeMillis()): LogRecord = {
SimpleLogRecord(level, value, messageFunction, stringify, className, methodName, lineNumber, thread, timeStamp)
SimpleLogRecord(level, value, message, className, methodName, lineNumber, thread, timeStamp)
}

/**
Expand Down Expand Up @@ -102,23 +90,21 @@ object LogRecord {

case class SimpleLogRecord(level: Level,
value: Double,
messageFunction: () => Any,
stringify: Any => String,
message: String,
className: String,
methodName: Option[String],
lineNumber: Option[Int],
thread: Thread,
timeStamp: Long) extends LogRecord {
def copy(level: Level = level,
value: Double = value,
messageFunction: () => Any = messageFunction,
stringify: Any => String = stringify,
message: String = message,
className: String = className,
methodName: Option[String] = methodName,
lineNumber: Option[Int] = lineNumber,
thread: Thread = thread,
timeStamp: Long = timeStamp): LogRecord = {
SimpleLogRecord(level, value, messageFunction, stringify, className, methodName, lineNumber, thread, timeStamp)
SimpleLogRecord(level, value, message, className, methodName, lineNumber, thread, timeStamp)
}

override def dispose(): Unit = {}
Expand Down
28 changes: 17 additions & 11 deletions core/shared/src/main/scala/scribe/LoggerSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ import scala.language.experimental.macros
trait LoggerSupport {
def log(record: LogRecord): Unit

def log(level: Level, message: => Any): Unit = macro Macros.log
def log(level: Level, message: String): Unit = macro Macros.log

def trace(message: => Any): Unit = macro Macros.trace
def debug(message: => Any): Unit = macro Macros.debug
def info(message: => Any): Unit = macro Macros.info
def warn(message: => Any): Unit = macro Macros.warn
def error(message: => Any): Unit = macro Macros.error
def trace(message: String): Unit = macro Macros.trace
def debug(message: String): Unit = macro Macros.debug
def info(message: String): Unit = macro Macros.info
def warn(message: String): Unit = macro Macros.warn
def error(message: String): Unit = macro Macros.error

def trace(message: => Any, t: => Throwable): Unit = macro Macros.trace2
def debug(message: => Any, t: => Throwable): Unit = macro Macros.debug2
def info(message: => Any, t: => Throwable): Unit = macro Macros.info2
def warn(message: => Any, t: => Throwable): Unit = macro Macros.warn2
def error(message: => Any, t: => Throwable): Unit = macro Macros.error2
def trace(t: Throwable): Unit = macro Macros.traceThrowable
def debug(t: Throwable): Unit = macro Macros.debugThrowable
def info(t: Throwable): Unit = macro Macros.infoThrowable
def warn(t: Throwable): Unit = macro Macros.warnThrowable
def error(t: Throwable): Unit = macro Macros.errorThrowable

def trace(message: String, t: Throwable): Unit = macro Macros.trace2
def debug(message: String, t: Throwable): Unit = macro Macros.debug2
def info(message: String, t: Throwable): Unit = macro Macros.info2
def warn(message: String, t: Throwable): Unit = macro Macros.warn2
def error(message: String, t: Throwable): Unit = macro Macros.error2
}
71 changes: 53 additions & 18 deletions macros/shared/src/main/scala/scribe/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,72 +31,102 @@ object Macros {
}
}

def trace(c: blackbox.Context)(message: c.Tree): c.Tree = {
def trace(c: blackbox.Context)(message: c.Expr[String]): c.Tree = {
import c.universe._

log(c)(q"scribe.Level.Trace", message)
}

def debug(c: blackbox.Context)(message: c.Tree): c.Tree = {
def debug(c: blackbox.Context)(message: c.Expr[String]): c.Tree = {
import c.universe._

log(c)(q"scribe.Level.Debug", message)
}

def info(c: blackbox.Context)(message: c.Tree): c.Tree = {
def info(c: blackbox.Context)(message: c.Expr[String]): c.Tree = {
import c.universe._

log(c)(q"scribe.Level.Info", message)
}

def warn(c: blackbox.Context)(message: c.Tree): c.Tree = {
def warn(c: blackbox.Context)(message: c.Expr[String]): c.Tree = {
import c.universe._

log(c)(q"scribe.Level.Warn", message)
}

def error(c: blackbox.Context)(message: c.Tree): c.Tree = {
def error(c: blackbox.Context)(message: c.Expr[String]): c.Tree = {
import c.universe._

log(c)(q"scribe.Level.Error", message)
}

def trace2(c: blackbox.Context)(message: c.Tree, t: c.Tree): c.Tree = {
def traceThrowable(c: blackbox.Context)(t: c.Expr[Throwable]): c.Tree = {
import c.universe._

logThrowable(c)(q"scribe.Level.Trace", t)
}

def debugThrowable(c: blackbox.Context)(t: c.Expr[Throwable]): c.Tree = {
import c.universe._

logThrowable(c)(q"scribe.Level.Debug", t)
}

def infoThrowable(c: blackbox.Context)(t: c.Expr[Throwable]): c.Tree = {
import c.universe._

logThrowable(c)(q"scribe.Level.Info", t)
}

def warnThrowable(c: blackbox.Context)(t: c.Expr[Throwable]): c.Tree = {
import c.universe._

logThrowable(c)(q"scribe.Level.Warn", t)
}

def errorThrowable(c: blackbox.Context)(t: c.Expr[Throwable]): c.Tree = {
import c.universe._

logThrowable(c)(q"scribe.Level.Error", t)
}

def trace2(c: blackbox.Context)(message: c.Expr[String], t: c.Expr[Throwable]): c.Tree = {
import c.universe._

log(c)(q"scribe.Level.Trace", message)
log(c)(q"scribe.Level.Trace", t)
logThrowable(c)(q"scribe.Level.Trace", t)
}

def debug2(c: blackbox.Context)(message: c.Tree, t: c.Tree): c.Tree = {
def debug2(c: blackbox.Context)(message: c.Expr[String], t: c.Expr[Throwable]): c.Tree = {
import c.universe._

log(c)(q"scribe.Level.Debug", message)
log(c)(q"scribe.Level.Debug", t)
logThrowable(c)(q"scribe.Level.Debug", t)
}

def info2(c: blackbox.Context)(message: c.Tree, t: c.Tree): c.Tree = {
def info2(c: blackbox.Context)(message: c.Expr[String], t: c.Expr[Throwable]): c.Tree = {
import c.universe._

log(c)(q"scribe.Level.Info", message)
log(c)(q"scribe.Level.Info", t)
logThrowable(c)(q"scribe.Level.Info", t)
}

def warn2(c: blackbox.Context)(message: c.Tree, t: c.Tree): c.Tree = {
def warn2(c: blackbox.Context)(message: c.Expr[String], t: c.Expr[Throwable]): c.Tree = {
import c.universe._

log(c)(q"scribe.Level.Warn", message)
log(c)(q"scribe.Level.Warn", t)
logThrowable(c)(q"scribe.Level.Warn", t)
}

def error2(c: blackbox.Context)(message: c.Tree, t: c.Tree): c.Tree = {
def error2(c: blackbox.Context)(message: c.Expr[String], t: c.Expr[Throwable]): c.Tree = {
import c.universe._

log(c)(q"scribe.Level.Error", message)
log(c)(q"scribe.Level.Error", t)
logThrowable(c)(q"scribe.Level.Error", t)
}

def log(c: blackbox.Context)(level: c.Tree, message: c.Tree): c.Tree = {
def log(c: blackbox.Context)(level: c.Tree, message: c.Expr[String]): c.Tree = {
import c.universe._

val logger = c.prefix.tree
Expand All @@ -105,7 +135,6 @@ object Macros {
case -1 => None
case n => Some(n)
}
val stringify = q"scribe.LogRecord.Stringify.Default"
val dcn = if (logger.tpe.toString == "scribe.Logger") {
q"$logger.overrideClassName.getOrElse($className)"
} else {
Expand All @@ -122,7 +151,13 @@ object Macros {
q"$line"
}

q"$logger.log(scribe.LogRecord($level, $level.value, () => $message, $stringify, $dcn, $dmn, $dln))"
q"$logger.log(scribe.LogRecord($level, $level.value, $message, $dcn, $dmn, $dln))"
}

def logThrowable(c: blackbox.Context)(level: c.Tree, t: c.Expr[Throwable]): c.Tree = {
import c.universe._

log(c)(level, c.Expr[String](q"scribe.LogRecord.throwable2String($t)"))
}

def enclosingType(c: blackbox.Context): EnclosingType = {
Expand Down
5 changes: 2 additions & 3 deletions tests/shared/src/test/scala/specs/LoggingSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ class LoggingSpec extends WordSpec with Matchers with Logging {
val message = "Wahoo!"
logger.info(s"It works! $message")
}
// TODO: fix f interpolation support
/*"log using 'f' interpolation" in {
"log using 'f' interpolation" in {
val d = 12.3456
logger.info(f"It works! $d%.0f")
}*/
}
"write a detailed log message" in {
val lineNumber = Some(15)
testingModifier.clear()
Expand Down

0 comments on commit 8a84548

Please sign in to comment.