aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/logger
diff options
context:
space:
mode:
authorSchuyler Eldridge2019-04-22 21:20:08 -0400
committerSchuyler Eldridge2019-04-25 16:24:15 -0400
commitef8f06f23b9ee6cf86de2450752dfd0fcd32da80 (patch)
tree79e2e8c5753903ca6d14e9b952c26a07442bd980 /src/main/scala/logger
parent47fe781c4ace38dff7f31da7e78f772e131d689e (diff)
Add ShellOption, DeletedWrapper
Abstracts away option writing such that users no longer have to understand scopt semantics. This adds a ShellOption class and a HasShellOptions trait for something which provides one or more ShellOptions. This refactors the FIRRTL codebase to use this style of option specification. Adds and uses DeletedWrapper to automatically generate DeletedAnnotations. Signed-off-by: Schuyler Eldridge <schuyler.eldridge@ibm.com>
Diffstat (limited to 'src/main/scala/logger')
-rw-r--r--src/main/scala/logger/Logger.scala6
-rw-r--r--src/main/scala/logger/LoggerAnnotations.scala77
2 files changed, 45 insertions, 38 deletions
diff --git a/src/main/scala/logger/Logger.scala b/src/main/scala/logger/Logger.scala
index 1cf7d7ee..00c29b1a 100644
--- a/src/main/scala/logger/Logger.scala
+++ b/src/main/scala/logger/Logger.scala
@@ -363,9 +363,9 @@ object Logger {
* @param inputAnnotations annotation sequence containing logger options
*/
def setOptions(inputAnnotations: AnnotationSeq): Unit = {
- val annotations = Seq( AddDefaults,
- Checks )
- .foldLeft(inputAnnotations)((a, p) => p.runTransform(a))
+ val annotations =
+ Seq( AddDefaults, Checks )
+ .foldLeft(inputAnnotations)((a, p) => p.transform(a))
val lopts = view[LoggerOptions](annotations)
state.globalLevel = (state.globalLevel, lopts.globalLogLevel) match {
diff --git a/src/main/scala/logger/LoggerAnnotations.scala b/src/main/scala/logger/LoggerAnnotations.scala
index 2811cc6c..204fc4ab 100644
--- a/src/main/scala/logger/LoggerAnnotations.scala
+++ b/src/main/scala/logger/LoggerAnnotations.scala
@@ -4,7 +4,7 @@ package logger
import firrtl.AnnotationSeq
import firrtl.annotations.{Annotation, NoTargetAnnotation}
-import firrtl.options.{HasScoptOptions, StageUtils}
+import firrtl.options.{HasShellOptions, ShellOption, StageUtils}
import scopt.OptionParser
@@ -18,17 +18,16 @@ sealed trait LoggerOption { this: Annotation => }
*/
case class LogLevelAnnotation(globalLogLevel: LogLevel.Value = LogLevel.None) extends NoTargetAnnotation with LoggerOption
-object LogLevelAnnotation extends HasScoptOptions {
- def addOptions(p: OptionParser[AnnotationSeq]): Unit = p.opt[String]("log-level")
- .abbr("ll")
- .valueName("<Error|Warn|Info|Debug|Trace>")
- .action( (x, c) => LogLevelAnnotation(LogLevel(x)) +: c )
- .validate{ x =>
- lazy val msg = s"$x bad value must be one of error|warn|info|debug|trace"
- if (Array("error", "warn", "info", "debug", "trace").contains(x.toLowerCase)) { p.success }
- else { p.failure(msg) }}
- .unbounded()
- .text(s"Sets the verbosity level of logging, default is ${new LoggerOptions().globalLogLevel}")
+object LogLevelAnnotation extends HasShellOptions {
+
+ val options = Seq(
+ new ShellOption[String](
+ longOption = "log-level",
+ toAnnotationSeq = (a: String) => Seq(LogLevelAnnotation(LogLevel(a))),
+ helpText = s"Set global logging verbosity (default: ${new LoggerOptions().globalLogLevel}",
+ shortOption = Some("ll"),
+ helpValueName = Some("{error|warn|info|debug|trace}") ) )
+
}
/** Describes a mapping of a class to a specific log level
@@ -38,16 +37,19 @@ object LogLevelAnnotation extends HasScoptOptions {
*/
case class ClassLogLevelAnnotation(className: String, level: LogLevel.Value) extends NoTargetAnnotation with LoggerOption
-object ClassLogLevelAnnotation extends HasScoptOptions {
- def addOptions(p: OptionParser[AnnotationSeq]): Unit = p.opt[Seq[String]]("class-log-level")
- .abbr("cll")
- .valueName("<FullClassName:[Error|Warn|Info|Debug|Trace]>[,...]")
- .action( (x, c) => (x.map { y =>
- val className :: levelName :: _ = y.split(":").toList
- val level = LogLevel(levelName)
- ClassLogLevelAnnotation(className, level) }) ++ c )
- .unbounded()
- .text(s"This defines per-class verbosity of logging")
+object ClassLogLevelAnnotation extends HasShellOptions {
+
+ val options = Seq(
+ new ShellOption[Seq[String]](
+ longOption = "class-log-level",
+ toAnnotationSeq = (a: Seq[String]) => a.map { aa =>
+ val className :: levelName :: _ = aa.split(":").toList
+ val level = LogLevel(levelName)
+ ClassLogLevelAnnotation(className, level) },
+ helpText = "Set per-class logging verbosity",
+ shortOption = Some("cll"),
+ helpValueName = Some("<FullClassName:{error|warn|info|debug|trace}>...") ) )
+
}
/** Enables logging to a file (as opposed to STDOUT)
@@ -56,22 +58,27 @@ object ClassLogLevelAnnotation extends HasScoptOptions {
*/
case class LogFileAnnotation(file: Option[String]) extends NoTargetAnnotation with LoggerOption
-object LogFileAnnotation extends HasScoptOptions {
- def addOptions(p: OptionParser[AnnotationSeq]): Unit = {
- p.opt[String]("log-file")
- .action( (x, c) => LogFileAnnotation(Some(x)) +: c )
- .unbounded()
- .text(s"log to the specified file")
- }
+object LogFileAnnotation extends HasShellOptions {
+
+ val options = Seq(
+ new ShellOption[String](
+ longOption = "log-file",
+ toAnnotationSeq = (a: String) => Seq(LogFileAnnotation(Some(a))),
+ helpText = "Log to a file instead of STDOUT",
+ helpValueName = Some("<file>") ) )
+
}
/** Enables class names in log output
* - enabled with `-lcn/--log-class-names`
*/
-case object LogClassNamesAnnotation extends NoTargetAnnotation with LoggerOption with HasScoptOptions {
- def addOptions(p: OptionParser[AnnotationSeq]): Unit = p.opt[Unit]("log-class-names")
- .abbr("lcn")
- .action( (x, c) => LogClassNamesAnnotation +: c )
- .unbounded()
- .text(s"shows class names and log level in logging output, useful for target --class-log-level")
+case object LogClassNamesAnnotation extends NoTargetAnnotation with LoggerOption with HasShellOptions {
+
+ val options = Seq(
+ new ShellOption[Unit](
+ longOption = "log-class-names",
+ toAnnotationSeq = (a: Unit) => Seq(LogClassNamesAnnotation),
+ helpText = "Show class names and log level in logging output",
+ shortOption = Some("lcn") ) )
+
}