-
Notifications
You must be signed in to change notification settings - Fork 131
/
LoggerMacro.scala
282 lines (219 loc) · 12.7 KB
/
LoggerMacro.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package com.typesafe.scalalogging
import org.slf4j.Marker
import scala.quoted.*
import org.slf4j.{ Logger as Underlying }
private[scalalogging] object LoggerMacro {
// Error
def errorMessage(underlying: Expr[Underlying], message: Expr[String])(using Quotes): Expr[Unit] = {
val (messageFormat, args) = deconstructInterpolatedMessage(message)
errorMessageArgs(underlying, messageFormat, Expr.ofSeq(args))
}
def errorMessageCause(underlying: Expr[Underlying], message: Expr[String], cause: Expr[Throwable]) (using Quotes) =
'{ if ($underlying.isErrorEnabled) $underlying.error($message, $cause) }
def errorMessageArgs(underlying: Expr[Underlying], message: Expr[String], args: Expr[Seq[Any]]) (using Quotes) = {
val anyRefArgs = formatArgs(args)
if(anyRefArgs.isEmpty)
'{ if ($underlying.isErrorEnabled) $underlying.error($message) }
else if(anyRefArgs.lengthIs == 1)
'{ if ($underlying.isErrorEnabled) $underlying.error($message, ${anyRefArgs.head}) }
else
'{ if ($underlying.isErrorEnabled) $underlying.error($message, ${Expr.ofSeq(anyRefArgs)}*) }
}
def errorMessageMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String]) (using Quotes) = {
val (messageFormat, args) = deconstructInterpolatedMessage(message)
errorMessageArgsMarker(underlying, marker, messageFormat, Expr.ofSeq(args))
}
def errorMessageCauseMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String], cause: Expr[Throwable]) (using Quotes) =
'{ if ($underlying.isErrorEnabled($marker)) $underlying.error($marker, $message, $cause) }
def errorMessageArgsMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String], args: Expr[Seq[Any]]) (using Quotes) = {
val anyRefArgs = formatArgs(args)
if(anyRefArgs.isEmpty)
'{ if ($underlying.isErrorEnabled($marker)) $underlying.error($marker, $message) }
else if(anyRefArgs.lengthIs == 1)
'{if ($underlying.isErrorEnabled($marker)) $underlying.error($marker, $message, ${anyRefArgs.head}) }
else
'{ if ($underlying.isErrorEnabled($marker)) $underlying.error($marker, $message, ${Expr.ofSeq(anyRefArgs)}*) }
}
def errorCode(underlying: Expr[Underlying], body: Expr[Unit]) (using Quotes) =
'{ if ($underlying.isErrorEnabled) $body }
// Warn
def warnMessage(underlying: Expr[Underlying], message: Expr[String])(using Quotes): Expr[Unit] = {
val (messageFormat, args) = deconstructInterpolatedMessage(message)
warnMessageArgs(underlying, messageFormat, Expr.ofSeq(args))
}
def warnMessageCause(underlying: Expr[Underlying], message: Expr[String], cause: Expr[Throwable]) (using Quotes) =
'{ if ($underlying.isWarnEnabled) $underlying.warn($message, $cause) }
def warnMessageArgs(underlying: Expr[Underlying], message: Expr[String], args: Expr[Seq[Any]]) (using Quotes) = {
val anyRefArgs = formatArgs(args)
if(anyRefArgs.isEmpty)
'{ if ($underlying.isWarnEnabled) $underlying.warn($message) }
else if(anyRefArgs.lengthIs == 1)
'{ if ($underlying.isWarnEnabled) $underlying.warn($message, ${anyRefArgs.head}) }
else
'{ if ($underlying.isWarnEnabled) $underlying.warn($message, ${Expr.ofSeq(anyRefArgs)}*) }
}
def warnMessageMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String]) (using Quotes) = {
val (messageFormat, args) = deconstructInterpolatedMessage(message)
warnMessageArgsMarker(underlying, marker, messageFormat, Expr.ofSeq(args))
}
def warnMessageCauseMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String], cause: Expr[Throwable]) (using Quotes) =
'{ if ($underlying.isWarnEnabled($marker)) $underlying.warn($marker, $message, $cause) }
def warnMessageArgsMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String], args: Expr[Seq[Any]]) (using Quotes) = {
val anyRefArgs = formatArgs(args)
if(anyRefArgs.isEmpty)
'{ if ($underlying.isWarnEnabled($marker)) $underlying.warn($marker, $message) }
else if(anyRefArgs.lengthIs == 1)
'{if ($underlying.isWarnEnabled($marker)) $underlying.warn($marker, $message, ${anyRefArgs.head}) }
else
'{ if ($underlying.isWarnEnabled($marker)) $underlying.warn($marker, $message, ${Expr.ofSeq(anyRefArgs)}*) }
}
def warnCode(underlying: Expr[Underlying], body: Expr[Unit]) (using Quotes) =
'{ if ($underlying.isWarnEnabled) $body }
// Info
def infoMessage(underlying: Expr[Underlying], message: Expr[String])(using Quotes): Expr[Unit] = {
val (messageFormat, args) = deconstructInterpolatedMessage(message)
infoMessageArgs(underlying, messageFormat, Expr.ofSeq(args))
}
def infoMessageCause(underlying: Expr[Underlying], message: Expr[String], cause: Expr[Throwable]) (using Quotes) =
'{ if ($underlying.isInfoEnabled) $underlying.info($message, $cause) }
def infoMessageArgs(underlying: Expr[Underlying], message: Expr[String], args: Expr[Seq[Any]]) (using Quotes) = {
val anyRefArgs = formatArgs(args)
if(anyRefArgs.isEmpty)
'{ if ($underlying.isInfoEnabled) $underlying.info($message) }
else if(anyRefArgs.lengthIs == 1)
'{ if ($underlying.isInfoEnabled) $underlying.info($message, ${anyRefArgs.head}) }
else
'{ if ($underlying.isInfoEnabled) $underlying.info($message, ${Expr.ofSeq(anyRefArgs)}*) }
}
def infoMessageMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String]) (using Quotes) = {
val (messageFormat, args) = deconstructInterpolatedMessage(message)
infoMessageArgsMarker(underlying, marker, messageFormat, Expr.ofSeq(args))
}
def infoMessageCauseMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String], cause: Expr[Throwable]) (using Quotes) =
'{ if ($underlying.isInfoEnabled($marker)) $underlying.info($marker, $message, $cause) }
def infoMessageArgsMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String], args: Expr[Seq[Any]]) (using Quotes) = {
val anyRefArgs = formatArgs(args)
if(anyRefArgs.isEmpty)
'{ if ($underlying.isInfoEnabled($marker)) $underlying.info($marker, $message) }
else if(anyRefArgs.lengthIs == 1)
'{if ($underlying.isInfoEnabled($marker)) $underlying.info($marker, $message, ${anyRefArgs.head}) }
else
'{ if ($underlying.isInfoEnabled($marker)) $underlying.info($marker, $message, ${Expr.ofSeq(anyRefArgs)}*) }
}
def infoCode(underlying: Expr[Underlying], body: Expr[Unit]) (using Quotes) =
'{ if ($underlying.isInfoEnabled) $body }
// Debug
def debugMessage(underlying: Expr[Underlying], message: Expr[String])(using Quotes): Expr[Unit] = {
val (messageFormat, args) = deconstructInterpolatedMessage(message)
debugMessageArgs(underlying, messageFormat, Expr.ofSeq(args))
}
def debugMessageCause(underlying: Expr[Underlying], message: Expr[String], cause: Expr[Throwable]) (using Quotes) =
'{ if ($underlying.isDebugEnabled) $underlying.debug($message, $cause) }
def debugMessageArgs(underlying: Expr[Underlying], message: Expr[String], args: Expr[Seq[Any]]) (using Quotes) = {
val anyRefArgs = formatArgs(args)
if(anyRefArgs.isEmpty)
'{ if ($underlying.isDebugEnabled) $underlying.debug($message) }
else if(anyRefArgs.lengthIs == 1)
'{ if ($underlying.isDebugEnabled) $underlying.debug($message, ${anyRefArgs.head}) }
else
'{ if ($underlying.isDebugEnabled) $underlying.debug($message, ${Expr.ofSeq(anyRefArgs)}*) }
}
def debugMessageMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String]) (using Quotes) = {
val (messageFormat, args) = deconstructInterpolatedMessage(message)
debugMessageArgsMarker(underlying, marker, messageFormat, Expr.ofSeq(args))
}
def debugMessageCauseMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String], cause: Expr[Throwable]) (using Quotes) =
'{ if ($underlying.isDebugEnabled($marker)) $underlying.debug($marker, $message, $cause) }
def debugMessageArgsMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String], args: Expr[Seq[Any]]) (using Quotes) = {
val anyRefArgs = formatArgs(args)
if(anyRefArgs.isEmpty)
'{ if ($underlying.isDebugEnabled($marker)) $underlying.debug($marker, $message) }
else if(anyRefArgs.lengthIs == 1)
'{if ($underlying.isDebugEnabled($marker)) $underlying.debug($marker, $message, ${anyRefArgs.head}) }
else
'{ if ($underlying.isDebugEnabled($marker)) $underlying.debug($marker, $message, ${Expr.ofSeq(anyRefArgs)}*) }
}
def debugCode(underlying: Expr[Underlying], body: Expr[Unit]) (using Quotes) =
'{ if ($underlying.isDebugEnabled) $body }
// Trace
def traceMessage(underlying: Expr[Underlying], message: Expr[String])(using Quotes): Expr[Unit] = {
val (messageFormat, args) = deconstructInterpolatedMessage(message)
traceMessageArgs(underlying, messageFormat, Expr.ofSeq(args))
}
def traceMessageCause(underlying: Expr[Underlying], message: Expr[String], cause: Expr[Throwable]) (using Quotes) =
'{ if ($underlying.isTraceEnabled) $underlying.trace($message, $cause) }
def traceMessageArgs(underlying: Expr[Underlying], message: Expr[String], args: Expr[Seq[Any]]) (using Quotes) = {
val anyRefArgs = formatArgs(args)
if(anyRefArgs.isEmpty)
'{ if ($underlying.isTraceEnabled) $underlying.trace($message) }
else if(anyRefArgs.lengthIs == 1)
'{ if ($underlying.isTraceEnabled) $underlying.trace($message, ${anyRefArgs.head}) }
else
'{ if ($underlying.isTraceEnabled) $underlying.trace($message, ${Expr.ofSeq(anyRefArgs)}*) }
}
def traceMessageMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String]) (using Quotes) = {
val (messageFormat, args) = deconstructInterpolatedMessage(message)
traceMessageArgsMarker(underlying, marker, messageFormat, Expr.ofSeq(args))
}
def traceMessageCauseMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String], cause: Expr[Throwable]) (using Quotes) =
'{ if ($underlying.isTraceEnabled($marker)) $underlying.trace($marker, $message, $cause) }
def traceMessageArgsMarker(underlying: Expr[Underlying], marker: Expr[Marker], message: Expr[String], args: Expr[Seq[Any]]) (using Quotes) = {
val anyRefArgs = formatArgs(args)
if(anyRefArgs.isEmpty)
'{ if ($underlying.isTraceEnabled($marker)) $underlying.trace($marker, $message) }
else if(anyRefArgs.lengthIs == 1)
'{if ($underlying.isTraceEnabled($marker)) $underlying.trace($marker, $message, ${anyRefArgs.head}) }
else
'{ if ($underlying.isTraceEnabled($marker)) $underlying.trace($marker, $message, ${Expr.ofSeq(anyRefArgs)}*) }
}
def traceCode(underlying: Expr[Underlying], body: Expr[Unit]) (using Quotes) =
'{ if ($underlying.isTraceEnabled) $body }
/** Checks whether `message` is an interpolated string and transforms it into SLF4J string interpolation. */
private def deconstructInterpolatedMessage(message: Expr[String])(using Quotes): (Expr[String], Seq[Expr[Any]]) = {
import quotes.reflect.*
import util.*
message.asTerm match{
case Inlined(_, _, Apply(Select(Apply(Select(Select(_, "StringContext"), _), messageNode), _), argumentsNode)) =>
val messageTextPartsOpt: Option[List[String]] =
messageNode.collectFirst{
case Typed(Repeated(ls, _), _) =>
ls.collect{ case Literal(StringConstant(s)) => s}
}
val argsOpt: Option[List[Term]] =
argumentsNode.collectFirst {
case Typed(Repeated(ls, _), _) => ls
}
(messageTextPartsOpt, argsOpt) match{
case (Some(messageTextParts), Some(args)) =>
val format = messageTextParts.iterator
// Emulate standard interpolator escaping
.map(StringContext.processEscapes)
// Escape literal slf4j format anchors if the resulting call will require a format string
.map(str => if (args.nonEmpty) str.replace("{}", "\\{}") else str)
.mkString("{}")
val formatArgs = args.map(_.asExpr)
(Expr(format), formatArgs)
case _ =>
(message, Seq.empty)
}
case _ => (message, Seq.empty)
}
}
def formatArgs(args: Expr[Seq[Any]])(using q: Quotes): Seq[Expr[AnyRef]] = {
import quotes.reflect.*
import util.*
args.asTerm match {
case p@Inlined(_, _, Typed(Repeated(v, _),_)) =>
v.map{
case t if t.tpe <:< TypeRepr.of[AnyRef] => t.asExprOf[AnyRef]
case t => '{${t.asExpr}.asInstanceOf[AnyRef]}
}
case Repeated(v, _) =>
v.map{
case t if t.tpe <:< TypeRepr.of[AnyRef] => t.asExprOf[AnyRef]
case t => '{${t.asExpr}.asInstanceOf[AnyRef]}
}
case _ => Seq.empty
}
}
}