src/Pure/System/progress.scala
author wenzelm
Fri Mar 16 16:38:46 2018 +0100 (16 months ago)
changeset 67880 e59220a075de
parent 67178 70576478bda9
child 68305 5321218147d3
permissions -rw-r--r--
tuned signature;
wenzelm@61276
     1
/*  Title:      Pure/System/progress.scala
wenzelm@61276
     2
    Author:     Makarius
wenzelm@61276
     3
wenzelm@61276
     4
Progress context for system processes.
wenzelm@61276
     5
*/
wenzelm@61276
     6
wenzelm@61276
     7
package isabelle
wenzelm@61276
     8
wenzelm@61276
     9
wenzelm@64201
    10
import java.io.{File => JFile}
wenzelm@64201
    11
wenzelm@64201
    12
wenzelm@61276
    13
class Progress
wenzelm@61276
    14
{
wenzelm@61276
    15
  def echo(msg: String) {}
wenzelm@64049
    16
  def echo_if(cond: Boolean, msg: String) { if (cond) echo(msg) }
wenzelm@61276
    17
  def theory(session: String, theory: String) {}
wenzelm@64909
    18
wenzelm@65826
    19
  def echo_warning(msg: String) { echo(Output.warning_text(msg)) }
wenzelm@65828
    20
  def echo_error_message(msg: String) { echo(Output.error_message_text(msg)) }
wenzelm@65826
    21
wenzelm@65921
    22
  def timeit[A](message: String = "", enabled: Boolean = true)(e: => A): A =
wenzelm@65921
    23
    Timing.timeit(message, enabled, echo(_))(e)
wenzelm@65921
    24
wenzelm@61276
    25
  def stopped: Boolean = false
wenzelm@67880
    26
  def expose_interrupt() { if (stopped) throw Exn.Interrupt() }
wenzelm@61276
    27
  override def toString: String = if (stopped) "Progress(stopped)" else "Progress"
wenzelm@64201
    28
wenzelm@64201
    29
  def bash(script: String,
wenzelm@64201
    30
    cwd: JFile = null,
wenzelm@64201
    31
    env: Map[String, String] = Isabelle_System.settings(),
wenzelm@64201
    32
    redirect: Boolean = false,
wenzelm@65930
    33
    echo: Boolean = false,
wenzelm@65930
    34
    strict: Boolean = true): Process_Result =
wenzelm@64201
    35
  {
wenzelm@64201
    36
    Isabelle_System.bash(script, cwd = cwd, env = env, redirect = redirect,
wenzelm@64201
    37
      progress_stdout = echo_if(echo, _),
wenzelm@65930
    38
      progress_stderr = echo_if(echo, _),
wenzelm@65930
    39
      strict = strict)
wenzelm@64201
    40
  }
wenzelm@61276
    41
}
wenzelm@61276
    42
wenzelm@64909
    43
object No_Progress extends Progress
wenzelm@61276
    44
wenzelm@64117
    45
class Console_Progress(verbose: Boolean = false, stderr: Boolean = false) extends Progress
wenzelm@61276
    46
{
wenzelm@64117
    47
  override def echo(msg: String)
wenzelm@64117
    48
  {
wenzelm@67178
    49
    Output.writeln(msg, stdout = !stderr)
wenzelm@64117
    50
  }
wenzelm@64117
    51
wenzelm@61276
    52
  override def theory(session: String, theory: String): Unit =
wenzelm@61276
    53
    if (verbose) echo(session + ": theory " + theory)
wenzelm@61276
    54
wenzelm@61276
    55
  @volatile private var is_stopped = false
wenzelm@61276
    56
  def interrupt_handler[A](e: => A): A = POSIX_Interrupt.handler { is_stopped = true } { e }
wenzelm@61276
    57
  override def stopped: Boolean =
wenzelm@61276
    58
  {
wenzelm@61276
    59
    if (Thread.interrupted) is_stopped = true
wenzelm@61276
    60
    is_stopped
wenzelm@61276
    61
  }
wenzelm@61276
    62
}
wenzelm@65888
    63
wenzelm@65888
    64
class File_Progress(path: Path, verbose: Boolean = false) extends Progress
wenzelm@65888
    65
{
wenzelm@65888
    66
  override def echo(msg: String): Unit =
wenzelm@65888
    67
    File.append(path, msg + "\n")
wenzelm@65888
    68
wenzelm@65888
    69
  override def theory(session: String, theory: String): Unit =
wenzelm@65888
    70
    if (verbose) echo(session + ": theory " + theory)
wenzelm@65888
    71
wenzelm@65888
    72
  override def toString: String = path.toString
wenzelm@65888
    73
}