aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/logger/phases/Checks.scala
blob: 96df4a14cd8bf0e107dea1dd984cbb3aa794dd07 (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
// SPDX-License-Identifier: Apache-2.0

package logger.phases

import firrtl.AnnotationSeq
import firrtl.annotations.Annotation
import firrtl.options.{Dependency, Phase}

import logger.{LogFileAnnotation, LogLevelAnnotation, LoggerException}

import scala.collection.mutable

/** Check that an [[firrtl.AnnotationSeq AnnotationSeq]] has all necessary [[firrtl.annotations.Annotation Annotation]]s
  * for a [[Logger]]
  */
object Checks extends Phase {

  override def prerequisites = Seq(Dependency[AddDefaults])
  override def optionalPrerequisiteOf = Seq.empty
  override def invalidates(a: Phase) = false

  /** Ensure that an [[firrtl.AnnotationSeq AnnotationSeq]] has necessary [[Logger]] [[firrtl.annotations.Annotation
    * Annotation]]s
    * @param annotations input annotations
    * @return input annotations unmodified
    * @throws logger.LoggerException
    */
  def transform(annotations: AnnotationSeq): AnnotationSeq = {
    val ll, lf = mutable.ListBuffer[Annotation]()
    annotations.foreach(_ match {
      case a: LogLevelAnnotation => ll += a
      case a: LogFileAnnotation  => lf += a
      case _ =>
    })
    if (ll.size > 1) {
      val l = ll.map { case LogLevelAnnotation(x) => x }
      throw new LoggerException(
        s"""|At most one log level can be specified, but found '${l.mkString(", ")}' specified via:
            |    - an option or annotation: -ll, --log-level, LogLevelAnnotation""".stripMargin
      )
    }
    if (lf.size > 1) {
      throw new LoggerException(
        s"""|At most one log file can be specified, but found ${lf.size} combinations of:
            |    - an options or annotation: -ltf, --log-to-file, --log-file, LogFileAnnotation""".stripMargin
      )
    }
    annotations
  }

}