Skip to content

Commit

Permalink
Added when***Enabled() methods in LoggerTakingImplicitImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
prateekatknoldus authored and analytically committed Dec 30, 2021
1 parent 25c54e3 commit b0f79a6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class LoggerTakingImplicitImpl[A] private[scalalogging] {

def error(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorMessageArgsMarker[A]

def whenErrorEnabled(body: Unit)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorCode[A]

// Warn

def warn(message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnMessage[A]
Expand All @@ -32,6 +34,8 @@ class LoggerTakingImplicitImpl[A] private[scalalogging] {

def warn(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnMessageArgsMarker[A]

def whenWarnEnabled(body: Unit)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnCode[A]

// Info

def info(message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoMessage[A]
Expand All @@ -46,6 +50,8 @@ class LoggerTakingImplicitImpl[A] private[scalalogging] {

def info(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoMessageArgsMarker[A]

def whenInfoEnabled(body: Unit)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoCode[A]

// Debug

def debug(message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugMessage[A]
Expand All @@ -60,6 +66,8 @@ class LoggerTakingImplicitImpl[A] private[scalalogging] {

def debug(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugMessageArgsMarker[A]

def whenDebugEnabled(body: Unit)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugCode[A]

// Trace

def trace(message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceMessage[A]
Expand All @@ -74,4 +82,5 @@ class LoggerTakingImplicitImpl[A] private[scalalogging] {

def trace(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceMessageArgsMarker[A]

def whenTraceEnabled(body: Unit)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceCode[A]
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ private[scalalogging] object LoggerTakingImplicitMacro {
}
}

def errorCode[A](c: LoggerContext[A])(body: c.Expr[Unit])(a: c.Expr[A]): c.universe.Tree = {
import c.universe._
val underlying = q"${c.prefix}.underlying"
val canLogEv = q"${c.prefix}.canLogEv"
q"""if ($underlying.isErrorEnabled) {
$underlying.error($canLogEv.logMessage($body))
$canLogEv.afterLog($a)
}"""
}

// Warn

def warnMessage[A](c: LoggerContext[A])(message: c.Expr[String])(a: c.Expr[A]): c.universe.Tree = {
Expand Down Expand Up @@ -159,6 +169,16 @@ private[scalalogging] object LoggerTakingImplicitMacro {
}
}

def warnCode[A](c: LoggerContext[A])(body: c.Expr[Unit])(a: c.Expr[A]): c.universe.Tree = {
import c.universe._
val underlying = q"${c.prefix}.underlying"
val canLogEv = q"${c.prefix}.canLogEv"
q"""if ($underlying.isWarnEnabled) {
$underlying.warn($canLogEv.logMessage($body))
$canLogEv.afterLog($a)
}"""
}

// Info

def infoMessage[A](c: LoggerContext[A])(message: c.Expr[String])(a: c.Expr[A]): c.universe.Tree = {
Expand Down Expand Up @@ -235,6 +255,16 @@ private[scalalogging] object LoggerTakingImplicitMacro {
}
}

def infoCode[A](c: LoggerContext[A])(body: c.Expr[Unit])(a: c.Expr[A]): c.universe.Tree = {
import c.universe._
val underlying = q"${c.prefix}.underlying"
val canLogEv = q"${c.prefix}.canLogEv"
q"""if ($underlying.isInfoEnabled) {
$underlying.info($canLogEv.logMessage($body))
$canLogEv.afterLog($a)
}"""
}

// Debug

def debugMessage[A](c: LoggerContext[A])(message: c.Expr[String])(a: c.Expr[A]): c.universe.Tree = {
Expand Down Expand Up @@ -311,6 +341,17 @@ private[scalalogging] object LoggerTakingImplicitMacro {
}
}

def debugCode[A](c: LoggerContext[A])(body: c.Expr[Unit])(a: c.Expr[A]): c.universe.Tree = {
import c.universe._
val underlying = q"${c.prefix}.underlying"
val canLogEv = q"${c.prefix}.canLogEv"
q"""if ($underlying.isDebugEnabled) {
$underlying.debug($canLogEv.logMessage($body))
$canLogEv.afterLog($a)
}"""
}


// Trace

def traceMessage[A](c: LoggerContext[A])(message: c.Expr[String])(a: c.Expr[A]): c.universe.Tree = {
Expand Down Expand Up @@ -386,4 +427,14 @@ private[scalalogging] object LoggerTakingImplicitMacro {
}"""
}
}

def traceCode[A](c: LoggerContext[A])(body: c.Expr[Unit])(a: c.Expr[A]): c.universe.Tree = {
import c.universe._
val underlying = q"${c.prefix}.underlying"
val canLogEv = q"${c.prefix}.canLogEv"
q"""if ($underlying.isTraceEnabled) {
$underlying.trace($canLogEv.logMessage($body))
$canLogEv.afterLog($a)
}"""
}
}

0 comments on commit b0f79a6

Please sign in to comment.