aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/Logger.scala
blob: 85969307a08b7a7a254db1e167a7e346302e6c28 (plain)
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
package firrtl

import java.io.PrintWriter
import Utils._

class Logger private (
  writer: PrintWriter,
  printMode: Symbol,
  printVars: List[Symbol]){
 
  // Legal printModes: 'none, 'error, 'warn, 'info, 'debug, 'trace
  require(List('none, 'error, 'warn, 'info, 'debug, 'trace) contains printMode) 
  val errorEnable = List('error, 'warn, 'info, 'debug, 'trace) contains printMode
  val warnEnable  = List('warn, 'info, 'debug, 'trace) contains printMode
  val infoEnable  = List('info, 'debug, 'trace) contains printMode
  val debugEnable = List('debug, 'trace) contains printMode
  val traceEnable = List('trace) contains printMode
  val circuitEnable = printVars contains 'circuit
  val debugFlags = printVars.map(_ -> true).toMap.withDefaultValue(false)

  def error(message: => String){
    if (errorEnable) writer.println(message.split("\n").map("[error] " + _).mkString("\n"))
  }
  def warn(message: => String){
    if (warnEnable) writer.println(message.split("\n").map("[warn] " + _).mkString("\n"))
  }
  def info(message: => String){
    if (infoEnable) writer.println(message.split("\n").map("[info] " + _).mkString("\n"))
  }
  def debug(message: => String){
    if (debugEnable) writer.println(message.split("\n").map("[debug] " + _).mkString("\n"))
  }
  def trace(message: => String){
    if (traceEnable) writer.println(message.split("\n").map("[trace] " + _).mkString("\n"))
  }

  def printType(node: => AST){
    val tpe = node.getType
    if( tpe.nonEmpty ) this.debug(s"@<t:${tpe.get.serialize}>")
  }

  def printDebug(circuit: Circuit){
    if (circuitEnable) this.debug(circuit.serialize(debugFlags))
  }
  // Used if not autoflushing
  def flush() = writer.flush()
  
}

object Logger
{
  def apply(writer: PrintWriter): Logger = 
    new Logger(writer, 'warn, List())
  def apply(writer: PrintWriter, printMode: Symbol): Logger = 
    new Logger(writer, printMode, List())
  def apply(writer: PrintWriter, printMode: Symbol, printVars: List[Symbol]): Logger = 
    new Logger(writer, printMode, printVars)
  def apply(): Logger = new Logger(null, 'none, List())
}