Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define the scala 2 macros in the scala 3 module #415

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 69 additions & 47 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,55 +1,77 @@
// basics

name := "scala-logging"
crossScalaVersions := Seq("3.3.1", "2.11.12", "2.12.18", "2.13.12")
scalaVersion := crossScalaVersions.value.head
ThisBuild / versionScheme := Some("early-semver")
scalacOptions ++= Seq(
"-unchecked",
"-deprecation",
"-language:_",
"-encoding", "UTF-8",
"-Ywarn-unused"
)
incOptions := incOptions.value.withLogRecompileOnMacro(false)
val scala213 = "2.13.12"
val scala3 = "3.4.0"
val scala2 = Seq("2.11.12", "2.12.18", scala213)
val isScala3 = Def.setting {
CrossVersion.partialVersion(scalaVersion.value).exists(_._1 != 2)
}
libraryDependencies ++= Dependencies.scalaLogging(scalaVersion.value, isScala3.value)
initialCommands := """|import com.typesafe.scalalogging._
|import org.slf4j.{ Logger => Underlying, _ }""".stripMargin

// OSGi

import com.typesafe.sbt.osgi.SbtOsgi
enablePlugins(SbtOsgi)
osgiSettings
OsgiKeys.bundleSymbolicName := "com.typesafe.scala-logging"
OsgiKeys.privatePackage := Seq()
OsgiKeys.exportPackage := Seq("com.typesafe.scalalogging*")

// publishing

organization := "com.typesafe.scala-logging"
sonatypeProfileName := "com.typesafe"
licenses := Seq("Apache 2.0 License" -> url("http://www.apache.org/licenses/LICENSE-2.0.html"))
homepage := Some(url("https://github.com/lightbend/scala-logging"))
Test / publishArtifact := false
pomIncludeRepository := (_ => false)
scmInfo := Some(
ScmInfo(url("https://github.com/lightbend/scala-logging"), "scm:git:[email protected]:lightbend/scala-logging.git")
)
developers := List(
Developer(
id = "hseeberger",
name = "Heiko Seeberger",
email = "",
url = url("http://heikoseeberger.de")
),
Developer(
id = "analytically",
name = "Mathias Bogaert",
email = "",
url = url("http://twitter.com/analytically")
import sbt.url

lazy val root = (project in file(".")).aggregate(main, scala2macros)

lazy val main = (project in file("main"))
.enablePlugins(SbtOsgi)
.settings(
name := "scala-logging",
crossScalaVersions := Seq(scala3) ++ scala2,
scalaVersion := crossScalaVersions.value.head,
ThisBuild / versionScheme := Some("early-semver"),
scalacOptions ++= Seq(
"-unchecked",
"-deprecation",
"-language:_",
"-encoding", "UTF-8",
"-Ywarn-unused"
),
incOptions := incOptions.value.withLogRecompileOnMacro(false),
libraryDependencies ++= Dependencies.scalaLogging(scalaVersion.value, isScala3.value),
initialCommands := """|import com.typesafe.scalalogging._
|import org.slf4j.{ Logger => Underlying, _ }""".stripMargin
).settings(
// OSGi

osgiSettings
).settings(
OsgiKeys.bundleSymbolicName := "com.typesafe.scala-logging",
OsgiKeys.privatePackage := Seq(),
OsgiKeys.exportPackage := Seq("com.typesafe.scalalogging*"),

// publishing

organization := "com.typesafe.scala-logging",
sonatypeProfileName := "com.typesafe",
licenses := Seq("Apache 2.0 License" -> url("http://www.apache.org/licenses/LICENSE-2.0.html")),
homepage := Some(url("https://github.com/lightbend/scala-logging")),
Test / publishArtifact := false,
pomIncludeRepository := (_ => false),
scmInfo := Some(
ScmInfo(url("https://github.com/lightbend/scala-logging"), "scm:git:[email protected]:lightbend/scala-logging.git")
),
developers := List(
Developer(
id = "hseeberger",
name = "Heiko Seeberger",
email = "",
url = url("http://heikoseeberger.de")
),
Developer(
id = "analytically",
name = "Mathias Bogaert",
email = "",
url = url("http://twitter.com/analytically")
)
)

)
.dependsOn(scala2macros)

lazy val scala2macros = project
.settings(
name := "scala2macros",
scalaVersion := scala213,
crossScalaVersions := scala2,
libraryDependencies ++= Dependencies.scalaLogging(scalaVersion.value, false),
)
)
85 changes: 85 additions & 0 deletions main/src/main/scala-2/com/typesafe/scalalogging/LoggerImpl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.typesafe.scalalogging
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is essentially a rename, with minor changes Scala2LoggerMacro to avoid a name clash


import org.slf4j.Marker
class LoggerImpl {

// Error

def error(message: String): Unit = macro Scala2LoggerMacro.errorMessage

def error(message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.errorMessageCause

def error(message: String, args: Any*): Unit = macro Scala2LoggerMacro.errorMessageArgs

def error(marker: Marker, message: String): Unit = macro Scala2LoggerMacro.errorMessageMarker

def error(marker: Marker, message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.errorMessageCauseMarker

def error(marker: Marker, message: String, args: Any*): Unit = macro Scala2LoggerMacro.errorMessageArgsMarker

def whenErrorEnabled(body: Unit): Unit = macro Scala2LoggerMacro.errorCode

// Warn

def warn(message: String): Unit = macro Scala2LoggerMacro.warnMessage

def warn(message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.warnMessageCause

def warn(message: String, args: Any*): Unit = macro Scala2LoggerMacro.warnMessageArgs

def warn(marker: Marker, message: String): Unit = macro Scala2LoggerMacro.warnMessageMarker

def warn(marker: Marker, message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.warnMessageCauseMarker

def warn(marker: Marker, message: String, args: Any*): Unit = macro Scala2LoggerMacro.warnMessageArgsMarker

def whenWarnEnabled(body: Unit): Unit = macro Scala2LoggerMacro.warnCode

// Info

def info(message: String): Unit = macro Scala2LoggerMacro.infoMessage

def info(message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.infoMessageCause

def info(message: String, args: Any*): Unit = macro Scala2LoggerMacro.infoMessageArgs

def info(marker: Marker, message: String): Unit = macro Scala2LoggerMacro.infoMessageMarker

def info(marker: Marker, message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.infoMessageCauseMarker

def info(marker: Marker, message: String, args: Any*): Unit = macro Scala2LoggerMacro.infoMessageArgsMarker

def whenInfoEnabled(body: Unit): Unit = macro Scala2LoggerMacro.infoCode

// Debug

def debug(message: String): Unit = macro Scala2LoggerMacro.debugMessage

def debug(message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.debugMessageCause

def debug(message: String, args: Any*): Unit = macro Scala2LoggerMacro.debugMessageArgs

def debug(marker: Marker, message: String): Unit = macro Scala2LoggerMacro.debugMessageMarker

def debug(marker: Marker, message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.debugMessageCauseMarker

def debug(marker: Marker, message: String, args: Any*): Unit = macro Scala2LoggerMacro.debugMessageArgsMarker

def whenDebugEnabled(body: Unit): Unit = macro Scala2LoggerMacro.debugCode

// Trace

def trace(message: String): Unit = macro Scala2LoggerMacro.traceMessage

def trace(message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.traceMessageCause

def trace(message: String, args: Any*): Unit = macro Scala2LoggerMacro.traceMessageArgs

def trace(marker: Marker, message: String): Unit = macro Scala2LoggerMacro.traceMessageMarker

def trace(marker: Marker, message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.traceMessageCauseMarker

def trace(marker: Marker, message: String, args: Any*): Unit = macro Scala2LoggerMacro.traceMessageArgsMarker

def whenTraceEnabled(body: Unit): Unit = macro Scala2LoggerMacro.traceCode
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.typesafe.scalalogging

import org.slf4j.Marker

class LoggerTakingImplicitImpl[A] private[scalalogging] {

// Error

def error(message: String)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.errorMessage[A]

def error(message: String, cause: Throwable)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.errorMessageCause[A]

def error(message: String, args: Any*)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.errorMessageArgs[A]

def error(marker: Marker, message: String)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.errorMessageMarker[A]

def error(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.errorMessageCauseMarker[A]

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

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

// Warn

def warn(message: String)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.warnMessage[A]

def warn(message: String, cause: Throwable)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.warnMessageCause[A]

def warn(message: String, args: Any*)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.warnMessageArgs[A]

def warn(marker: Marker, message: String)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.warnMessageMarker[A]

def warn(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.warnMessageCauseMarker[A]

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

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

// Info

def info(message: String)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.infoMessage[A]

def info(message: String, cause: Throwable)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.infoMessageCause[A]

def info(message: String, args: Any*)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.infoMessageArgs[A]

def info(marker: Marker, message: String)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.infoMessageMarker[A]

def info(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.infoMessageCauseMarker[A]

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

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

// Debug

def debug(message: String)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.debugMessage[A]

def debug(message: String, cause: Throwable)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.debugMessageCause[A]

def debug(message: String, args: Any*)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.debugMessageArgs[A]

def debug(marker: Marker, message: String)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.debugMessageMarker[A]

def debug(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.debugMessageCauseMarker[A]

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

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

// Trace

def trace(message: String)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.traceMessage[A]

def trace(message: String, cause: Throwable)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.traceMessageCause[A]

def trace(message: String, args: Any*)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.traceMessageArgs[A]

def trace(marker: Marker, message: String)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.traceMessageMarker[A]

def trace(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.traceMessageCauseMarker[A]

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

def whenTraceEnabled(body: Unit)(implicit a: A): Unit = macro Scala2LoggerTakingImplicitMacro.traceCode[A]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.typesafe.scalalogging

import org.slf4j.{Marker, Logger as Underlying }
import scala.language.experimental.macros

trait LoggerImpl {
def underlying: Underlying
Expand Down Expand Up @@ -54,4 +55,87 @@ trait LoggerImpl {
inline def trace(inline marker: Marker, inline message: String, inline cause: Throwable): Unit = ${LoggerMacro.traceMessageCauseMarker('underlying, 'marker, 'message, 'cause)}
inline def trace(inline marker: Marker, inline message: String, inline args: Any*): Unit = ${LoggerMacro.traceMessageArgsMarker('underlying, 'marker, 'message, 'args)}
inline def whenTraceEnabled(inline body: Unit): Unit = ${LoggerMacro.traceCode('underlying, 'body)}

//scala 2
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rest is a copy of the scala 2 macro definitions to be compiled in scala 3. Then these can be used from either scala 3 or 2.


// Error

def error(message: String): Unit = macro Scala2LoggerMacro.errorMessage

def error(message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.errorMessageCause

def error(message: String, args: Any*): Unit = macro Scala2LoggerMacro.errorMessageArgs

def error(marker: Marker, message: String): Unit = macro Scala2LoggerMacro.errorMessageMarker

def error(marker: Marker, message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.errorMessageCauseMarker

def error(marker: Marker, message: String, args: Any*): Unit = macro Scala2LoggerMacro.errorMessageArgsMarker

def whenErrorEnabled(body: Unit): Unit = macro Scala2LoggerMacro.errorCode

// Warn

def warn(message: String): Unit = macro Scala2LoggerMacro.warnMessage

def warn(message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.warnMessageCause

def warn(message: String, args: Any*): Unit = macro Scala2LoggerMacro.warnMessageArgs

def warn(marker: Marker, message: String): Unit = macro Scala2LoggerMacro.warnMessageMarker

def warn(marker: Marker, message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.warnMessageCauseMarker

def warn(marker: Marker, message: String, args: Any*): Unit = macro Scala2LoggerMacro.warnMessageArgsMarker

def whenWarnEnabled(body: Unit): Unit = macro Scala2LoggerMacro.warnCode

// Info

def info(message: String): Unit = macro Scala2LoggerMacro.infoMessage

def info(message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.infoMessageCause

def info(message: String, args: Any*): Unit = macro Scala2LoggerMacro.infoMessageArgs

def info(marker: Marker, message: String): Unit = macro Scala2LoggerMacro.infoMessageMarker

def info(marker: Marker, message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.infoMessageCauseMarker

def info(marker: Marker, message: String, args: Any*): Unit = macro Scala2LoggerMacro.infoMessageArgsMarker

def whenInfoEnabled(body: Unit): Unit = macro Scala2LoggerMacro.infoCode

// Debug

def debug(message: String): Unit = macro Scala2LoggerMacro.debugMessage

def debug(message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.debugMessageCause

def debug(message: String, args: Any*): Unit = macro Scala2LoggerMacro.debugMessageArgs

def debug(marker: Marker, message: String): Unit = macro Scala2LoggerMacro.debugMessageMarker

def debug(marker: Marker, message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.debugMessageCauseMarker

def debug(marker: Marker, message: String, args: Any*): Unit = macro Scala2LoggerMacro.debugMessageArgsMarker

def whenDebugEnabled(body: Unit): Unit = macro Scala2LoggerMacro.debugCode

// Trace

def trace(message: String): Unit = macro Scala2LoggerMacro.traceMessage

def trace(message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.traceMessageCause

def trace(message: String, args: Any*): Unit = macro Scala2LoggerMacro.traceMessageArgs

def trace(marker: Marker, message: String): Unit = macro Scala2LoggerMacro.traceMessageMarker

def trace(marker: Marker, message: String, cause: Throwable): Unit = macro Scala2LoggerMacro.traceMessageCauseMarker

def trace(marker: Marker, message: String, args: Any*): Unit = macro Scala2LoggerMacro.traceMessageArgsMarker

def whenTraceEnabled(body: Unit): Unit = macro Scala2LoggerMacro.traceCode

}
Loading
Loading