Skip to content

Commit

Permalink
removed unused code.
Browse files Browse the repository at this point in the history
  • Loading branch information
gciuloaica committed Mar 9, 2022
1 parent 3177ff3 commit fd4cfc0
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 137 deletions.
3 changes: 0 additions & 3 deletions zio-http-logging/src/main/scala-2/zhttp/logging/Logger.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zhttp.logging

import zhttp.logging.frontend.ConsoleLogger
import zhttp.logging.macros.LoggerMacro
import zhttp.logging.macros.LoggerMacro._

final class Logger(configuration: Configuration) {
Expand Down Expand Up @@ -34,8 +33,6 @@ final class Logger(configuration: Configuration) {
}

object Logger {
import scala.language.experimental.macros
final def getLogger: Logger = macro LoggerMacro.getLoggerImpl
final def getLogger(logLevel: LogLevel) =
new Logger(configuration = Configuration(getClass.getSimpleName, logLevel, LogFormat.default))
final def getLogger(loggerName: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,93 +3,13 @@ package zhttp.logging.macros
import zhttp.logging.LogLevel.{DEBUG, ERROR, INFO, TRACE, WARN}
import zhttp.logging.{LogLevel, Logger}

import scala.annotation.tailrec
import scala.reflect.macros.{blackbox, whitebox}
import scala.reflect.macros.whitebox

/**
* Macro inspired from log4s.
*/
private[zhttp] object LoggerMacro {

/** Get a logger by reflecting the enclosing class name. */
final def getLoggerImpl(c: blackbox.Context) = {
import c.universe._

@tailrec def findEnclosingClass(sym: c.universe.Symbol): c.universe.Symbol = {
sym match {
case NoSymbol =>
c.abort(c.enclosingPosition, s"Couldn't find an enclosing class or module for the logger")
case s if s.isModule || s.isClass =>
s
case other =>
/* We're not in a module or a class, so we're probably inside a member definition. Recurse upward. */
findEnclosingClass(other.owner)
}
}

val cls = findEnclosingClass(c.internal.enclosingOwner)

assert(cls.isModule || cls.isClass, "Enclosing class is always either a module or a class")

def loggerByParam(param: c.Tree) = {
q"_root_.zhttp.logging.Logger.getLogger(...${List(param)})"
}

def loggerBySymbolName(s: Symbol) = {
def fullName(s: Symbol): String = {
@inline def isPackageObject = (
(s.isModule || s.isModuleClass)
&& s.owner.isPackage
&& s.name.decodedName.toString == termNames.PACKAGE.decodedName.toString
)
if (s.isModule || s.isClass) {
if (isPackageObject) {
s.owner.fullName
} else if (s.owner.isStatic) {
s.fullName
} else {
fullName(s.owner) + "." + s.name.encodedName.toString
}
} else {
fullName(s.owner)
}
}
loggerByParam(q"${fullName(s)}")
}

def loggerByType(s: Symbol) = {
val typeSymbol: ClassSymbol = (if (s.isModule) s.asModule.moduleClass else s).asClass
val typeParams = typeSymbol.typeParams

if (typeParams.isEmpty) {
loggerByParam(q"_root_.scala.Predef.classOf[$typeSymbol]")
} else {
if (typeParams.exists(_.asType.typeParams.nonEmpty)) {
/* We have at least one higher-kinded type: fall back to by-name logger construction, as
* there's no simple way to declare a higher-kinded type with an "any" parameter. */
loggerBySymbolName(s)
} else {
val typeArgs = List.fill(typeParams.length)(WildcardType)
val typeConstructor = tq"$typeSymbol[..${typeArgs}]"
loggerByParam(q"_root_.scala.Predef.classOf[$typeConstructor]")
}
}
}

@inline def isInnerClass(s: Symbol) = {
s.isClass && !(s.owner.isPackage)
}

val instanceByName = (cls.isModule || cls.isModuleClass) || cls.isClass && isInnerClass(cls)

if (instanceByName) {
loggerBySymbolName(cls)
} else {
loggerByType(cls)
}

}

/** A macro context that represents a method call on a Logger instance. */
private[this] type LogCtx = whitebox.Context { type PrefixType = Logger }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ final class Logger(configuration: Configuration) {

object Logger {
import scala.language.experimental.macros
inline def getLogger: Logger = ${LoggerMacro.getLoggerImpl}
final def getLogger(logLevel: LogLevel) =
new Logger(configuration = Configuration(getClass.getSimpleName, logLevel, LogFormat.default))
final def getLogger(loggerName: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,6 @@ import scala.quoted._
*/
private[zhttp] object LoggerMacro {

/** Get a logger by reflecting the enclosing class name. */
final def getLoggerImpl(using qctx: Quotes): Expr[Logger] = {
import qctx.reflect._

@tailrec def findEnclosingClass(sym: Symbol): Symbol = {
sym match {
case s if s.isNoSymbol =>
report.errorAndAbort("Couldn't find an enclosing class or module for the logger")
case s if s.isClassDef =>
s
case other =>
/* We're not in a module or a class, so we're probably inside a member definition. Recurse upward. */
findEnclosingClass(other.owner)
}
}

def logger(s: Symbol): Expr[Logger] = {
def fullName(s: Symbol): String = {
val flags = s.flags
if (flags.is(Flags.Package)) {
s.fullName
}
else if (s.isClassDef) {
if (flags.is(Flags.Module)) {
if (s.name == "package$") {
fullName(s.owner)
}
else {
val chomped = s.name.stripSuffix("$")
fullName(s.owner) + "." + chomped
}
}
else {
fullName(s.owner) + "." + s.name
}
}
else {
fullName(s.owner)
}
}

val name = Expr(fullName(s))
'{ Logger.getLogger($name) }
}

val cls = findEnclosingClass(Symbol.spliceOwner)
logger(cls)
}

def traceTM(logger: Expr[Logger])(t: Expr[Throwable])(msg: Expr[String])(using qctx: Quotes) =
'{ if ($logger.isTraceEnabled) $logger.logger.trace($msg, $t) }
def traceM(logger: Expr[Logger])(msg: Expr[String])(using qctx: Quotes) =
Expand Down
4 changes: 1 addition & 3 deletions zio-http/src/main/scala/zhttp/service/Handler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,10 @@ private[zhttp] final case class Handler[R](
/**
* Executes program
*/
private def unsafeRunZIO(program: ZIO[R, Throwable, Any])(implicit ctx: Ctx): Unit = {
log.info(s"Unsafe run $program")
private def unsafeRunZIO(program: ZIO[R, Throwable, Any])(implicit ctx: Ctx): Unit =
rt.unsafeRun(ctx) {
program
}
}

override def serverTime: ServerTime = serverTimeGenerator

Expand Down

1 comment on commit fd4cfc0

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 993753.62

Please sign in to comment.