alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Tracer.scala)

This example Scala source code file (Tracer.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

any, any, anyref, boolean, io, lbrace, printstream, product, some, string, string, t, t, tracer, tracer

The Scala Tracer.scala source code

/* NSC -- new Scala compiler
 * Copyright 2005-2011 LAMP/EPFL
 * @author  Paul Phillips
 */

package scala.tools.nsc
package util

import java.io.PrintStream

class Tracer(enabled: () => Boolean) {
  def out: PrintStream = System.out
  def intoString(x: Any): String = "" + x
  def stringify(x: Any): String = x match {
    case null                   => "null"
    case x: TraversableOnce[_]  => x map stringify mkString ", "
    case x: Product             => stringify(x.productIterator)
    case x: AnyRef              => intoString(x)
  }

  private val LBRACE = "{"
  private val RBRACE = "}"
  private var indentLevel = 0
  private def ind(s: String) = (" " * (indentLevel * 2)) + s
  private def indented[T](body: => T): T = {
    indentLevel += 1
    try body
    finally indentLevel -= 1
  }
  private def p(s: String) = {
    out.print(s)
    out.flush()
  }
  private def pin[T](x: T): T = {
    p(ind("" + x))
    x
  }
  def apply[T](name: String, args: => Any)(body: => T): T = {
    val result = body
    if (enabled()) {
      // concise output optimization
      val boolResult = result match {
        case x: Boolean => Some(x)
        case _          => None
      }
      p(ind("%s(%s) = %s\n".format(
        name,
        stringify(args),
        boolResult getOrElse LBRACE))
      )
      if (boolResult.isEmpty) {
        indented(pin(result))
        p("\n" + ind(RBRACE))
      }
      result
    }
    else result
  }
}

object Tracer {
  def apply(enabled: => Boolean): Tracer = new Tracer(() => enabled)
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala Tracer.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.