From c2b319e977ef668c6ba7ce40722202394a890d09 Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Thu, 24 Oct 2013 16:34:16 -0400 Subject: [PATCH] error,warn,info,debug commands to set log level, useful in conjunction with early combinator --. Fixes #806. --- .../main/scala/sbt/BasicCommandStrings.scala | 23 +++++++++++++++++++ main/src/main/scala/sbt/LogManager.scala | 8 +++++++ main/src/main/scala/sbt/Main.scala | 9 +++++--- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/main/command/src/main/scala/sbt/BasicCommandStrings.scala b/main/command/src/main/scala/sbt/BasicCommandStrings.scala index 7e22e3a73..9adfcc192 100644 --- a/main/command/src/main/scala/sbt/BasicCommandStrings.scala +++ b/main/command/src/main/scala/sbt/BasicCommandStrings.scala @@ -37,6 +37,29 @@ object BasicCommandStrings def exitBrief = "Terminates the build." + def logLevelHelp = + { + val levels = Level.values.toSeq + val levelList = levels.mkString(", ") + val brief = ("", "Sets the logging level to 'log-level'. Valid levels: " + levelList) + val detailed = levels.map(l => (l.toString, logLevelDetail(l))).toMap + Help(brief, detailed) + } + private[this] def logLevelDetail(level: Level.Value): String = +s"""$level + + Sets the global logging level to $level. + This will be used as the default level for logging from commands, settings, and tasks. + Any explicit `logLevel` configuration in a project overrides this setting. + +${runEarly(level.toString)} + + Sets the global logging level as described above, but does so before any other commands are executed on startup, including project loading. + This is useful as a startup option: + * it takes effect before any logging occurs + * if no other commands are passed, interactive mode is still entered +""" + def runEarly(command: String) = { val sep = if(command.isEmpty || Character.isLetter(command.charAt(0))) "" else " " s"$EarlyCommand$sep$command" diff --git a/main/src/main/scala/sbt/LogManager.scala b/main/src/main/scala/sbt/LogManager.scala index 5e0eda520..905738319 100644 --- a/main/src/main/scala/sbt/LogManager.scala +++ b/main/src/main/scala/sbt/LogManager.scala @@ -86,6 +86,14 @@ object LogManager s } + def setGlobalLogLevel(s: State, level: Level.Value): State = { + s.globalLogging.full match { + case a: AbstractLogger => a.setLevel(level) + case _ => () + } + s.put(BasicKeys.explicitGlobalLogLevels, true).put(Keys.logLevel.key, level) + } + private[this] def setExplicitGlobalLogLevels(s: State, flag: Boolean): State = s.put(BasicKeys.explicitGlobalLogLevels, flag) private[this] def hasExplicitGlobalLogLevels(s: State): Boolean = diff --git a/main/src/main/scala/sbt/Main.scala b/main/src/main/scala/sbt/Main.scala index be112a4ca..e9e79352a 100644 --- a/main/src/main/scala/sbt/Main.scala +++ b/main/src/main/scala/sbt/Main.scala @@ -85,11 +85,11 @@ object BuiltinCommands { def initialAttributes = AttributeMap.empty - def ConsoleCommands: Seq[Command] = Seq(ignore, exit, IvyConsole.command, early, act, nop) - def ScriptCommands: Seq[Command] = Seq(ignore, exit, Script.command, early, act, nop) + def ConsoleCommands: Seq[Command] = Seq(ignore, exit, IvyConsole.command, setLogLevel, early, act, nop) + def ScriptCommands: Seq[Command] = Seq(ignore, exit, Script.command, setLogLevel, early, act, nop) def DefaultCommands: Seq[Command] = Seq(ignore, help, about, tasks, settingsCommand, loadProject, projects, project, reboot, read, history, set, sessionCommand, inspect, loadProjectImpl, loadFailed, Cross.crossBuild, Cross.switchVersion, - setOnFailure, clearOnFailure, stashOnFailure, popOnFailure, + setOnFailure, clearOnFailure, stashOnFailure, popOnFailure, setLogLevel, ifLast, multi, shell, continuous, eval, alias, append, last, lastGrep, export, boot, nop, call, exit, early, initialize, act) ++ compatCommands def DefaultBootCommands: Seq[String] = LoadProject :: (IfLast + " " + Shell) :: Nil @@ -98,6 +98,9 @@ object BuiltinCommands def about = Command.command(AboutCommand, aboutBrief, aboutDetailed) { s => s.log.info(aboutString(s)); s } + def setLogLevel = Command.arb(const(logLevelParser), logLevelHelp)(LogManager.setGlobalLogLevel) + private[this] def logLevelParser: Parser[Level.Value] = oneOf(Level.values.toSeq.map(v => v.toString ^^^ v)) + // This parser schedules the default boot commands unless overridden by an alias def bootParser(s: State) = {