From e80de34a6ebca50bc44980a53ade202a1ef502ee Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Wed, 8 Sep 2010 14:45:22 -0400 Subject: [PATCH] split out extra tasks, implement logging --- main/LogManager.scala | 35 +++++++++++++ main/OutputTasks.scala | 46 +++++++++++++++++ main/SingleProject.scala | 107 ++------------------------------------- 3 files changed, 86 insertions(+), 102 deletions(-) create mode 100644 main/LogManager.scala create mode 100644 main/OutputTasks.scala diff --git a/main/LogManager.scala b/main/LogManager.scala new file mode 100644 index 000000000..97dd3e105 --- /dev/null +++ b/main/LogManager.scala @@ -0,0 +1,35 @@ +/* sbt -- Simple Build Tool + * Copyright 2010 Mark Harrah + */ +package sbt + + import java.io.PrintWriter + import LogManager._ + import std.Transform + +object LogManager +{ + def construct(context: Transform.Context[Project], settings: Settings) = (task: Task[_], to: PrintWriter) => + { + val owner = context owner task + val ownerName = owner flatMap ( context ownerName _ ) getOrElse "" + val taskPath = (context staticName task).toList ::: ownerName :: Nil + def level(key: AttributeKey[Level.Value], default: Level.Value): Level.Value = settings.get(key, taskPath) getOrElse default + val screenLevel = level(ScreenLogLevel, Level.Info) + val backingLevel = level(PersistLogLevel, Level.Debug) + + val console = ConsoleLogger() + val backed = ConsoleLogger(ConsoleLogger.printWriterOut(to), useColor = false) // TODO: wrap this with a filter that strips ANSI codes + + val multi = new MultiLogger(console :: backed :: Nil) + // sets multi to the most verbose for clients that inspect the current level + multi setLevel Level.union(backingLevel, screenLevel) + // set the specific levels + console setLevel screenLevel + backed setLevel backingLevel + multi: Logger + } + + val ScreenLogLevel = AttributeKey[Level.Value]("screen log level") + val PersistLogLevel = AttributeKey[Level.Value]("persist log level") +} \ No newline at end of file diff --git a/main/OutputTasks.scala b/main/OutputTasks.scala new file mode 100644 index 000000000..7a3ab0eb0 --- /dev/null +++ b/main/OutputTasks.scala @@ -0,0 +1,46 @@ +/* sbt -- Simple Build Tool + * Copyright 2010 Mark Harrah + */ +package sbt + + import std._ + import Path._ + import TaskExtra._ + +trait PrintTask +{ + def input: Task[Input] + lazy val show = input flatMap { in => + val m = ReflectUtilities.allVals[Task[_]](this) + val taskStrings = in.splitArgs map { name => + m(name).merge.map { + case Seq() => "No result for " + name + case Seq( (conf, v) ) => name + ": " + v.toString + case confs => confs map { case (conf, v) => conf + ": " + v } mkString(name + ":\n\t", "\n\t", "\n") + } + } + taskStrings.join.map { _ foreach println } + } +} + +trait LastOutput +{ + def input: Task[Input] + def streams: Task[TaskStreams] + lazy val last = (streams, input) flatMap { (s: TaskStreams, i: Input) => + val tasks = ReflectUtilities.allVals[Task[_]](this) + (s.readText( tasks(i.arguments) , update = false ) map { reader => + val out = s.text() + def readL() + { + val line = reader.readLine() + if(line ne null) { + readL() + out.println(line) + println(line) + } + } + readL() + }).merge + } +} diff --git a/main/SingleProject.scala b/main/SingleProject.scala index 98a88c966..bc8755eb6 100644 --- a/main/SingleProject.scala +++ b/main/SingleProject.scala @@ -3,12 +3,12 @@ */ package sbt -import std._ -import Path._ -import TaskExtra._ -import scala.collection.{mutable, JavaConversions} + import std._ + import Path._ + import TaskExtra._ + import scala.collection.{mutable, JavaConversions} -import java.io.File + import java.io.File trait SingleProject extends Tasked with PrintTask with TaskExtra with Types { @@ -44,103 +44,6 @@ object Dummy val Streams = dummy[TaskStreams](StreamsName) } -trait PrintTask -{ - def input: Task[Input] - lazy val show = input flatMap { in => - val m = ReflectUtilities.allVals[Task[_]](this) - val taskStrings = in.splitArgs map { name => - m(name).merge.map { - case Seq() => "No result for " + name - case Seq( (conf, v) ) => name + ": " + v.toString - case confs => confs map { case (conf, v) => conf + ": " + v } mkString(name + ":\n\t", "\n\t", "\n") - } - } - taskStrings.join.map { _ foreach println } - } -} -/* -trait LogManager -{ - def makeLogger(context: Context): (Task[_], PrintWriter) => Logger -} -sealed trait Properties -{ - def parent: Option[Properties] - def get[T](key: AttributeKey[T]): Option[T] - def sub(name: String): Properties - def set[T](key: AttributeKey[T], value: T): Properties - def s -} -sealed trait Attribute[T] -{ - def default: Option[T] - def sub(s: String): Option[Attribute[T]] - def value(s: String): Option[T] - - def get(path: List[String]): Option[T] - def set(path: List[String], value: T): Attribute[T] - def setDefault(path: List[String], default: T): Attribute[T] -} -final class MapBackedAttribute[T](val default: Option[T], subs: Map[String, MapBackedAttribute[T]], values: Map[String, T]) extends Attribute[T] -{ - def sub(s: String) = subs get s - def value(s: String) = values get s - - def get(path: List[String]): Option[T] = - path match { - case Nil => None - case x :: Nil => values get x - case h :: t => (subs get h) flatMap (_ get t ) orElse default - } - def set(path: List[String], value: T): Attribute[T] = - path match { - case Nil => this - case x :: Nil => new MapBackedAttribute(default, subs, values updated( x, value) ) - case h :: t => - val (newSubs, sub) = (subs get h) match { - case Some(s) => (subs, s) - case None => (subs.updated(h, Attribute.empty[T]), Attribute.empty[T]) - } - newSubs put (h, sub.set(t, value) - } - def setDefault(path: List[String], default: T): Attribute[T] = - - def lastComponent(path: List[String]): Option[MapBackedAttribute[T]] = - path match { - case Nil => this - case x :: Nil => new MapBackedAttribute(default, subs, values updated( x, value) ) - case h :: t => - val (newSubs, sub) = (subs get h) match { - case Some(s) => (subs, s) - case None => (subs.updated(h, Attribute.empty[T]), Attribute.empty[T]) - } - newSubs put (h, su - -} -trait ConsoleLogManager extends LogManager -{ - def makeLogger(context: Context, configuration: Configuration) = (task: Task[_], to: PrintWriter) => - { - val owner = context owner task - val taskPath = (context ownerName owner).getOrElse("") :: (context staticName task).getOrElse("") ::: Nil - def level(key: AttributeKey[Level.Value], default: Level.Value): Level.Value = select(key) get taskPath getOrElse default - val screenLevel = level(ScreenLogLevel, Level.Info) - val backingLevel = select(PersistLogLevel, Level.Debug) - - val console = ConsoleLogger() - val backed = ConsoleLogger(to, useColor = false) // TODO: wrap this with a filter that strips ANSI codes - - val multi = new MultiLogger(console :: backed :: Nil) - // sets multi to the most verbose for clients that inspect the current level - multi setLevel Level.union(backingLevel, screenLevel) - // set the specific levels - console setLevel screenLevel - backed setLevel backingLevel - multi: Logger - } -} -*/ object ReflectiveContext { import Transform.Context