diff options
| author | chick | 2020-08-14 19:47:53 -0700 |
|---|---|---|
| committer | Jack Koenig | 2020-08-14 19:47:53 -0700 |
| commit | 6fc742bfaf5ee508a34189400a1a7dbffe3f1cac (patch) | |
| tree | 2ed103ee80b0fba613c88a66af854ae9952610ce /src/main/scala/logger | |
| parent | b516293f703c4de86397862fee1897aded2ae140 (diff) | |
All of src/ formatted with scalafmt
Diffstat (limited to 'src/main/scala/logger')
| -rw-r--r-- | src/main/scala/logger/Logger.scala | 105 | ||||
| -rw-r--r-- | src/main/scala/logger/LoggerAnnotations.scala | 35 | ||||
| -rw-r--r-- | src/main/scala/logger/LoggerOptions.scala | 20 | ||||
| -rw-r--r-- | src/main/scala/logger/phases/AddDefaults.scala | 10 | ||||
| -rw-r--r-- | src/main/scala/logger/phases/Checks.scala | 27 |
5 files changed, 111 insertions, 86 deletions
diff --git a/src/main/scala/logger/Logger.scala b/src/main/scala/logger/Logger.scala index 9cf645fa..e002db92 100644 --- a/src/main/scala/logger/Logger.scala +++ b/src/main/scala/logger/Logger.scala @@ -4,7 +4,7 @@ package logger import java.io.{ByteArrayOutputStream, File, FileOutputStream, PrintStream} -import firrtl.{ExecutionOptionsManager, AnnotationSeq} +import firrtl.{AnnotationSeq, ExecutionOptionsManager} import firrtl.options.Viewer.view import logger.phases.{AddDefaults, Checks} @@ -38,7 +38,7 @@ object LogLevel extends Enumeration { case "info" => LogLevel.Info case "debug" => LogLevel.Debug case "trace" => LogLevel.Trace - case level => throw new Exception(s"Unknown LogLevel '$level'") + case level => throw new Exception(s"Unknown LogLevel '$level'") } } @@ -58,8 +58,8 @@ private class LoggerState { val classLevels = new scala.collection.mutable.HashMap[String, LogLevel.Value] val classToLevelCache = new scala.collection.mutable.HashMap[String, LogLevel.Value] var logClassNames = false - var stream: PrintStream = System.out - var fromInvoke: Boolean = false // this is used to not have invokes re-create run-state + var stream: PrintStream = System.out + var fromInvoke: Boolean = false // this is used to not have invokes re-create run-state var stringBufferOption: Option[Logger.OutputCaptor] = None override def toString: String = { @@ -137,10 +137,9 @@ object Logger { @deprecated("Use makescope(opts: FirrtlOptions)", "1.2") def makeScope[A](args: Array[String] = Array.empty)(codeBlock: => A): A = { val executionOptionsManager = new ExecutionOptionsManager("logger") - if(executionOptionsManager.parse(args)) { + if (executionOptionsManager.parse(args)) { makeScope(executionOptionsManager)(codeBlock) - } - else { + } else { throw new Exception(s"logger invoke failed to parse args ${args.mkString(", ")}") } } @@ -154,10 +153,9 @@ object Logger { def makeScope[A](options: AnnotationSeq)(codeBlock: => A): A = { val runState: LoggerState = { val newRunState = updatableLoggerState.value.getOrElse(new LoggerState) - if(newRunState.fromInvoke) { + if (newRunState.fromInvoke) { newRunState - } - else { + } else { val forcedNewRunState = new LoggerState forcedNewRunState.fromInvoke = true forcedNewRunState @@ -179,39 +177,41 @@ object Logger { */ private def testPackageNameMatch(className: String, level: LogLevel.Value): Option[Boolean] = { val classLevels = state.classLevels - if(classLevels.isEmpty) return None + if (classLevels.isEmpty) return None // If this class name in cache just use that value - val levelForThisClassName = state.classToLevelCache.getOrElse(className, { - // otherwise break up the class name in to full package path as list and find most specific entry you can - val packageNameList = className.split("""\.""").toList - /* - * start with full class path, lopping off from the tail until nothing left - */ - def matchPathToFindLevel(packageList: List[String]): LogLevel.Value = { - if(packageList.isEmpty) { - LogLevel.None + val levelForThisClassName = state.classToLevelCache.getOrElse( + className, { + // otherwise break up the class name in to full package path as list and find most specific entry you can + val packageNameList = className.split("""\.""").toList + /* + * start with full class path, lopping off from the tail until nothing left + */ + def matchPathToFindLevel(packageList: List[String]): LogLevel.Value = { + if (packageList.isEmpty) { + LogLevel.None + } else { + val partialName = packageList.mkString(".") + val level = classLevels.getOrElse( + partialName, { + matchPathToFindLevel(packageList.reverse.tail.reverse) + } + ) + level + } } - else { - val partialName = packageList.mkString(".") - val level = classLevels.getOrElse(partialName, { - matchPathToFindLevel(packageList.reverse.tail.reverse) - }) - level - } - } - val levelSpecified = matchPathToFindLevel(packageNameList) - if(levelSpecified != LogLevel.None) { - state.classToLevelCache(className) = levelSpecified + val levelSpecified = matchPathToFindLevel(packageNameList) + if (levelSpecified != LogLevel.None) { + state.classToLevelCache(className) = levelSpecified + } + levelSpecified } - levelSpecified - }) + ) - if(levelForThisClassName != LogLevel.None) { + if (levelForThisClassName != LogLevel.None) { Some(levelForThisClassName >= level) - } - else { + } else { None } } @@ -226,19 +226,20 @@ object Logger { */ private def showMessage(level: LogLevel.Value, className: String, message: => String): Unit = { def logIt(): Unit = { - if(state.logClassNames) { + if (state.logClassNames) { state.stream.println(s"[$level:$className] $message") - } - else { + } else { state.stream.println(message) } } testPackageNameMatch(className, level) match { - case Some(true) => logIt() + case Some(true) => logIt() case Some(false) => case None => - if((state.globalLevel == LogLevel.None && level == LogLevel.Error) || - (state.globalLevel != LogLevel.None && state.globalLevel >= level)) { + if ( + (state.globalLevel == LogLevel.None && level == LogLevel.Error) || + (state.globalLevel != LogLevel.None && state.globalLevel >= level) + ) { logIt() } } @@ -247,6 +248,7 @@ object Logger { def getGlobalLevel: LogLevel.Value = { state.globalLevel } + /** * This resets everything in the current Logger environment, including the destination * use this with caution. Unexpected things can happen @@ -309,7 +311,7 @@ object Logger { def clearStringBuffer(): Unit = { state.stringBufferOption match { case Some(x) => x.byteArrayOutputStream.reset() - case None => + case None => } } @@ -360,16 +362,16 @@ object Logger { */ def setOptions(inputAnnotations: AnnotationSeq): Unit = { val annotations = - Seq( new AddDefaults, Checks ) - .foldLeft(inputAnnotations)((a, p) => p.transform(a)) + Seq(new AddDefaults, Checks) + .foldLeft(inputAnnotations)((a, p) => p.transform(a)) val lopts = view[LoggerOptions](annotations) state.globalLevel = (state.globalLevel, lopts.globalLogLevel) match { case (LogLevel.None, LogLevel.None) => LogLevel.None - case (x, LogLevel.None) => x - case (LogLevel.None, x) => x - case (_, x) => x - case _ => LogLevel.Error + case (x, LogLevel.None) => x + case (LogLevel.None, x) => x + case (_, x) => x + case _ => LogLevel.Error } setClassLogLevels(lopts.classLogLevels) @@ -386,6 +388,7 @@ object Logger { * @param containerClass passed in from the LazyLogging trait in order to provide class level logging granularity */ class Logger(containerClass: String) { + /** * Log message at Error level * @param message message generator to be invoked if level is right @@ -393,6 +396,7 @@ class Logger(containerClass: String) { def error(message: => String): Unit = { Logger.showMessage(LogLevel.Error, containerClass, message) } + /** * Log message at Warn level * @param message message generator to be invoked if level is right @@ -400,6 +404,7 @@ class Logger(containerClass: String) { def warn(message: => String): Unit = { Logger.showMessage(LogLevel.Warn, containerClass, message) } + /** * Log message at Inof level * @param message message generator to be invoked if level is right @@ -407,6 +412,7 @@ class Logger(containerClass: String) { def info(message: => String): Unit = { Logger.showMessage(LogLevel.Info, containerClass, message) } + /** * Log message at Debug level * @param message message generator to be invoked if level is right @@ -414,6 +420,7 @@ class Logger(containerClass: String) { def debug(message: => String): Unit = { Logger.showMessage(LogLevel.Debug, containerClass, message) } + /** * Log message at Trace level * @param message message generator to be invoked if level is right diff --git a/src/main/scala/logger/LoggerAnnotations.scala b/src/main/scala/logger/LoggerAnnotations.scala index f4dc6b38..b345d617 100644 --- a/src/main/scala/logger/LoggerAnnotations.scala +++ b/src/main/scala/logger/LoggerAnnotations.scala @@ -5,7 +5,6 @@ package logger import firrtl.annotations.{Annotation, NoTargetAnnotation} import firrtl.options.{HasShellOptions, ShellOption} - /** An annotation associated with a Logger command line option */ sealed trait LoggerOption { this: Annotation => } @@ -14,7 +13,9 @@ sealed trait LoggerOption { this: Annotation => } * - if unset, a [[LogLevelAnnotation]] with the default log level will be emitted * @param level the level of logging */ -case class LogLevelAnnotation(globalLogLevel: LogLevel.Value = LogLevel.Warn) extends NoTargetAnnotation with LoggerOption +case class LogLevelAnnotation(globalLogLevel: LogLevel.Value = LogLevel.Warn) + extends NoTargetAnnotation + with LoggerOption object LogLevelAnnotation extends HasShellOptions { @@ -24,7 +25,9 @@ object LogLevelAnnotation extends HasShellOptions { 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}") ) ) + helpValueName = Some("{error|warn|info|debug|trace}") + ) + ) } @@ -33,20 +36,26 @@ object LogLevelAnnotation extends HasShellOptions { * @param name the class name to log * @param level the verbosity level */ -case class ClassLogLevelAnnotation(className: String, level: LogLevel.Value) extends NoTargetAnnotation with LoggerOption +case class ClassLogLevelAnnotation(className: String, level: LogLevel.Value) + extends NoTargetAnnotation + with LoggerOption 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) }, + 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}>...") ) ) + helpValueName = Some("<FullClassName:{error|warn|info|debug|trace}>...") + ) + ) } @@ -63,7 +72,9 @@ object LogFileAnnotation extends HasShellOptions { longOption = "log-file", toAnnotationSeq = (a: String) => Seq(LogFileAnnotation(Some(a))), helpText = "Log to a file instead of STDOUT", - helpValueName = Some("<file>") ) ) + helpValueName = Some("<file>") + ) + ) } @@ -77,6 +88,8 @@ case object LogClassNamesAnnotation extends NoTargetAnnotation with LoggerOption longOption = "log-class-names", toAnnotationSeq = (a: Unit) => Seq(LogClassNamesAnnotation), helpText = "Show class names and log level in logging output", - shortOption = Some("lcn") ) ) + shortOption = Some("lcn") + ) + ) } diff --git a/src/main/scala/logger/LoggerOptions.scala b/src/main/scala/logger/LoggerOptions.scala index 299382f0..6cc745b9 100644 --- a/src/main/scala/logger/LoggerOptions.scala +++ b/src/main/scala/logger/LoggerOptions.scala @@ -9,23 +9,25 @@ package logger * @param logToFile if true, log to a file * @param logClassNames indicates logging verbosity on a class-by-class basis */ -class LoggerOptions private [logger] ( - val globalLogLevel: LogLevel.Value = LogLevelAnnotation().globalLogLevel, +class LoggerOptions private[logger] ( + val globalLogLevel: LogLevel.Value = LogLevelAnnotation().globalLogLevel, val classLogLevels: Map[String, LogLevel.Value] = Map.empty, - val logClassNames: Boolean = false, - val logFileName: Option[String] = None) { + val logClassNames: Boolean = false, + val logFileName: Option[String] = None) { - private [logger] def copy( - globalLogLevel: LogLevel.Value = globalLogLevel, + private[logger] def copy( + globalLogLevel: LogLevel.Value = globalLogLevel, classLogLevels: Map[String, LogLevel.Value] = classLogLevels, - logClassNames: Boolean = logClassNames, - logFileName: Option[String] = logFileName): LoggerOptions = { + logClassNames: Boolean = logClassNames, + logFileName: Option[String] = logFileName + ): LoggerOptions = { new LoggerOptions( globalLogLevel = globalLogLevel, classLogLevels = classLogLevels, logClassNames = logClassNames, - logFileName = logFileName) + logFileName = logFileName + ) } diff --git a/src/main/scala/logger/phases/AddDefaults.scala b/src/main/scala/logger/phases/AddDefaults.scala index 660de579..ec673637 100644 --- a/src/main/scala/logger/phases/AddDefaults.scala +++ b/src/main/scala/logger/phases/AddDefaults.scala @@ -5,10 +5,10 @@ package logger.phases import firrtl.AnnotationSeq import firrtl.options.Phase -import logger.{LoggerOption, LogLevelAnnotation} +import logger.{LogLevelAnnotation, LoggerOption} /** Add default logger [[Annotation]]s */ -private [logger] class AddDefaults extends Phase { +private[logger] class AddDefaults extends Phase { override def prerequisites = Seq.empty override def optionalPrerequisiteOf = Seq.empty @@ -20,12 +20,12 @@ private [logger] class AddDefaults extends Phase { */ def transform(annotations: AnnotationSeq): AnnotationSeq = { var ll = true - annotations.collect{ case a: LoggerOption => a }.map{ + annotations.collect { case a: LoggerOption => a }.map { case _: LogLevelAnnotation => ll = false - case _ => + case _ => } annotations ++ - (if (ll) Seq(LogLevelAnnotation()) else Seq() ) + (if (ll) Seq(LogLevelAnnotation()) else Seq()) } } diff --git a/src/main/scala/logger/phases/Checks.scala b/src/main/scala/logger/phases/Checks.scala index e945fa98..0109c7ad 100644 --- a/src/main/scala/logger/phases/Checks.scala +++ b/src/main/scala/logger/phases/Checks.scala @@ -6,12 +6,13 @@ import firrtl.AnnotationSeq import firrtl.annotations.Annotation import firrtl.options.{Dependency, Phase} -import logger.{LogLevelAnnotation, LogFileAnnotation, LoggerException} +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]] */ + * for a [[Logger]] + */ object Checks extends Phase { override def prerequisites = Seq(Dependency[AddDefaults]) @@ -26,20 +27,22 @@ object Checks extends Phase { */ 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 _ => }) + 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 } + 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 )} + | - 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 )} + 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 } |
