Skip to content

Commit b0f79a6

Browse files
prateekatknoldusanalytically
authored andcommitted
Added when***Enabled() methods in LoggerTakingImplicitImpl
1 parent 25c54e3 commit b0f79a6

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/main/scala-2/com/typesafe/scalalogging/LoggerTakingImplicitImpl.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class LoggerTakingImplicitImpl[A] private[scalalogging] {
1818

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

21+
def whenErrorEnabled(body: Unit)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorCode[A]
22+
2123
// Warn
2224

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

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

37+
def whenWarnEnabled(body: Unit)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnCode[A]
38+
3539
// Info
3640

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

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

53+
def whenInfoEnabled(body: Unit)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoCode[A]
54+
4955
// Debug
5056

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

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

69+
def whenDebugEnabled(body: Unit)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugCode[A]
70+
6371
// Trace
6472

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

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

85+
def whenTraceEnabled(body: Unit)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceCode[A]
7786
}

src/main/scala-2/com/typesafe/scalalogging/LoggerTakingImplicitMacro.scala

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ private[scalalogging] object LoggerTakingImplicitMacro {
8383
}
8484
}
8585

86+
def errorCode[A](c: LoggerContext[A])(body: c.Expr[Unit])(a: c.Expr[A]): c.universe.Tree = {
87+
import c.universe._
88+
val underlying = q"${c.prefix}.underlying"
89+
val canLogEv = q"${c.prefix}.canLogEv"
90+
q"""if ($underlying.isErrorEnabled) {
91+
$underlying.error($canLogEv.logMessage($body))
92+
$canLogEv.afterLog($a)
93+
}"""
94+
}
95+
8696
// Warn
8797

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

172+
def warnCode[A](c: LoggerContext[A])(body: c.Expr[Unit])(a: c.Expr[A]): c.universe.Tree = {
173+
import c.universe._
174+
val underlying = q"${c.prefix}.underlying"
175+
val canLogEv = q"${c.prefix}.canLogEv"
176+
q"""if ($underlying.isWarnEnabled) {
177+
$underlying.warn($canLogEv.logMessage($body))
178+
$canLogEv.afterLog($a)
179+
}"""
180+
}
181+
162182
// Info
163183

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

258+
def infoCode[A](c: LoggerContext[A])(body: c.Expr[Unit])(a: c.Expr[A]): c.universe.Tree = {
259+
import c.universe._
260+
val underlying = q"${c.prefix}.underlying"
261+
val canLogEv = q"${c.prefix}.canLogEv"
262+
q"""if ($underlying.isInfoEnabled) {
263+
$underlying.info($canLogEv.logMessage($body))
264+
$canLogEv.afterLog($a)
265+
}"""
266+
}
267+
238268
// Debug
239269

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

344+
def debugCode[A](c: LoggerContext[A])(body: c.Expr[Unit])(a: c.Expr[A]): c.universe.Tree = {
345+
import c.universe._
346+
val underlying = q"${c.prefix}.underlying"
347+
val canLogEv = q"${c.prefix}.canLogEv"
348+
q"""if ($underlying.isDebugEnabled) {
349+
$underlying.debug($canLogEv.logMessage($body))
350+
$canLogEv.afterLog($a)
351+
}"""
352+
}
353+
354+
314355
// Trace
315356

316357
def traceMessage[A](c: LoggerContext[A])(message: c.Expr[String])(a: c.Expr[A]): c.universe.Tree = {
@@ -386,4 +427,14 @@ private[scalalogging] object LoggerTakingImplicitMacro {
386427
}"""
387428
}
388429
}
430+
431+
def traceCode[A](c: LoggerContext[A])(body: c.Expr[Unit])(a: c.Expr[A]): c.universe.Tree = {
432+
import c.universe._
433+
val underlying = q"${c.prefix}.underlying"
434+
val canLogEv = q"${c.prefix}.canLogEv"
435+
q"""if ($underlying.isTraceEnabled) {
436+
$underlying.trace($canLogEv.logMessage($body))
437+
$canLogEv.afterLog($a)
438+
}"""
439+
}
389440
}

0 commit comments

Comments
 (0)