aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/DebugUtils.scala
blob: 01fe4fe4ac6ce64fb1fb9e87c0b7d180a34a49eb (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
60
61
62
63
64
65
66
// Private implicit classes and other utility functions for debugging

package firrtl

import java.io.PrintWriter
import Utils._

private object DebugUtils {


  /** Private class for recording and organizing debug information  */
  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 println(message: => String){
      writer.println(message)
    }
    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 printlnDebug(circuit: Circuit){
      if (circuitEnable) this.println(circuit.serialize(debugFlags))
    }
    // Used if not autoflushing
    def flush() = writer.flush()
    
  }
  /** Factory object for logger
    *
    * Logger records and organizes debug information
    */
  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())
  }
}