LogUtils.scala 1.9 KB
Newer Older
L
LinJiawei 已提交
1 2 3
package xiangshan.utils

import chisel3._
4
import chisel3.util.experimental.BoringUtils
L
LinJiawei 已提交
5 6 7
import xiangshan.HasXSParameter
import xiangshan.utils.XSLogLevel.XSLogLevel

8 9
import scala.io.StdIn

L
LinJiawei 已提交
10 11 12
object XSLogLevel extends Enumeration {
  type XSLogLevel = Value

13
  val ALL   = Value(0, "ALL  ")
L
LinJiawei 已提交
14
  val DEBUG = Value("DEBUG")
Y
Yinan Xu 已提交
15 16
  val INFO  = Value("INFO ")
  val WARN  = Value("WARN ")
L
LinJiawei 已提交
17
  val ERROR = Value("ERROR")
Y
Yinan Xu 已提交
18
  val OFF   = Value("OFF  ")
L
LinJiawei 已提交
19 20
}

21
object XSLog {
22
  var generateLog: Boolean = false
L
LinJiawei 已提交
23
  def apply(debugLevel: XSLogLevel)
Y
Yinan Xu 已提交
24
           (prefix: Boolean, cond: Bool, pable: Printable)
25 26
           (implicit name: String): Any =
  {
27
    val commonInfo = p"[$debugLevel][time=${GTimer()}] $name: "
28 29
    val logEnable = WireInit(false.B)
    BoringUtils.addSink(logEnable, "DISPLAY_LOG_ENABLE")
30 31 32 33
    if(generateLog){
      when (cond && logEnable) {
        printf((if (prefix) commonInfo else p"") + pable)
      }
L
LinJiawei 已提交
34 35 36 37
    }
  }
}

W
William Wang 已提交
38
sealed abstract class LogHelper(val logLevel: XSLogLevel) extends HasXSParameter {
L
LinJiawei 已提交
39

40
  def apply(cond: Bool, fmt: String, data: Bits*)(implicit name: String): Any =
L
LinJiawei 已提交
41
    apply(cond, Printable.pack(fmt, data:_*))
Y
Yinan Xu 已提交
42
  def apply(cond: Bool, pable: Printable)(implicit name: String): Any = apply(true, cond, pable)
43
  def apply(fmt: String, data: Bits*)(implicit name: String): Any =
Y
Yinan Xu 已提交
44 45 46 47 48 49
    apply(Printable.pack(fmt, data:_*))
  def apply(pable: Printable)(implicit name: String): Any = apply(true.B, pable)
  def apply(prefix: Boolean, cond: Bool, fmt: String, data: Bits*)(implicit name: String): Any =
    apply(prefix, cond, Printable.pack(fmt, data:_*))
  def apply(prefix: Boolean, cond: Bool, pable: Printable)(implicit name: String): Any =
    XSLog(logLevel)(prefix, cond, pable)
L
LinJiawei 已提交
50 51 52 53 54 55 56 57 58
}

object XSDebug extends LogHelper(XSLogLevel.DEBUG)

object XSInfo extends LogHelper(XSLogLevel.INFO)

object XSWarn extends LogHelper(XSLogLevel.WARN)

object XSError extends LogHelper(XSLogLevel.ERROR)