-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
207 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
ducktapeNext/src/main/scala/io/github/arainko/ducktape/internal/Logger.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.github.arainko.ducktape.internal | ||
|
||
import scala.compiletime.* | ||
import scala.quoted.* | ||
import io.github.arainko.ducktape.Transformer | ||
import io.github.arainko.tooling.FullName | ||
|
||
private[ducktape] object Logger { | ||
|
||
transparent inline given Level = Level.Info | ||
given Output = Output.StdOut | ||
|
||
enum Level { | ||
case Debug, Info, Off | ||
} | ||
|
||
enum Output { | ||
case StdOut, Report | ||
|
||
final def print(msg: String)(using Quotes) = | ||
this match { | ||
case StdOut => println(msg) | ||
case Report => quotes.reflect.report.info(msg) | ||
} | ||
} | ||
|
||
private val infoTag = s"${Console.GREEN}[INFO]${Console.RESET}" | ||
private val debugTag = s"${Console.GREEN}[DEBUG]${Console.RESET}" | ||
private def blue(msg: String) = s"${Console.BLUE}$msg${Console.RESET}" | ||
|
||
inline def loggedInfo[A](using Level, Output, FullName, Debug[A], Quotes)( | ||
inline msg: String | ||
)(value: A) = { | ||
info(msg, value) | ||
value | ||
} | ||
|
||
inline def info(inline msg: String)(using level: Level, output: Output, name: FullName, quotes: Quotes): Unit = | ||
inline level match { | ||
case Level.Debug => () | ||
case Level.Info => output.print(s"$infoTag $msg ${blue(s"[$name]")}") | ||
case Level.Off => () | ||
} | ||
|
||
inline def info[A]( | ||
inline msg: String, | ||
value: A | ||
)(using Level, Output, FullName, Debug[A], Quotes): Unit = | ||
info(s"$msg: ${Debug.show(value)}") | ||
|
||
inline def loggedDebug[A](using Level, Output, FullName, Debug[A], Quotes)( | ||
inline msg: String | ||
)(value: A) = { | ||
debug(msg, value) | ||
value | ||
} | ||
|
||
inline def debug(inline msg: String)(using level: Level, output: Output, name: FullName, quotes: Quotes): Unit = | ||
inline level match { | ||
case Level.Debug => output.print(s"$debugTag $msg ${blue(s"[$name]")}") | ||
case Level.Info => output.print(s"$infoTag $msg ${blue(s"[$name]")}") | ||
case Level.Off => () | ||
} | ||
|
||
inline def debug[A](inline msg: String, value: A)(using level: Level, name: FullName, _debug: Debug[A], quotes: Quotes): Unit = | ||
debug(s"$msg: ${Debug.show(value)}") | ||
} |
30 changes: 30 additions & 0 deletions
30
tooling/src/main/scala/io/github/arainko/tooling/FullName.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.github.arainko.tooling | ||
|
||
import scala.quoted.* | ||
|
||
private[arainko] opaque type FullName <: String = String | ||
|
||
private[arainko] object FullName { | ||
|
||
inline given derived(using DummyImplicit): FullName = ${ ownerMacro } | ||
|
||
private def ownerMacro(using Quotes) = { | ||
import quotes.reflect.* | ||
|
||
val pos = Position.ofMacroExpansion | ||
|
||
val sourceFile = s"${pos.sourceFile.name}:${pos.startLine + 1}" | ||
|
||
val rendered = | ||
List | ||
.unfold(Symbol.spliceOwner)(sym => Option.when(!sym.isNoSymbol)(sym -> sym.maybeOwner)) | ||
.collect { | ||
case sym if !sym.flags.is(Flags.Synthetic) && !sym.flags.is(Flags.Package) && !sym.isLocalDummy => sym.name | ||
} | ||
.reverse | ||
.mkString(".") | ||
.replace("$", "") | ||
|
||
'{ ${ Expr(s"$rendered @ $sourceFile") }: FullName } | ||
} | ||
} |