src/Pure/System/progress.scala
author wenzelm
Mon May 28 13:35:43 2018 +0200 (13 months ago)
changeset 68305 5321218147d3
parent 67880 e59220a075de
child 68330 d7920eb7de54
permissions -rw-r--r--
clarified 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@68305
    26
  def interrupt_handler[A](e: => A): A = e
wenzelm@67880
    27
  def expose_interrupt() { if (stopped) throw Exn.Interrupt() }
wenzelm@61276
    28
  override def toString: String = if (stopped) "Progress(stopped)" else "Progress"
wenzelm@64201
    29
wenzelm@64201
    30
  def bash(script: String,
wenzelm@64201
    31
    cwd: JFile = null,
wenzelm@64201
    32
    env: Map[String, String] = Isabelle_System.settings(),
wenzelm@64201
    33
    redirect: Boolean = false,
wenzelm@65930
    34
    echo: Boolean = false,
wenzelm@65930
    35
    strict: Boolean = true): Process_Result =
wenzelm@64201
    36
  {
wenzelm@64201
    37
    Isabelle_System.bash(script, cwd = cwd, env = env, redirect = redirect,
wenzelm@64201
    38
      progress_stdout = echo_if(echo, _),
wenzelm@65930
    39
      progress_stderr = echo_if(echo, _),
wenzelm@65930
    40
      strict = strict)
wenzelm@64201
    41
  }
wenzelm@61276
    42
}
wenzelm@61276
    43
wenzelm@64909
    44
object No_Progress extends Progress
wenzelm@61276
    45
wenzelm@64117
    46
class Console_Progress(verbose: Boolean = false, stderr: Boolean = false) extends Progress
wenzelm@61276
    47
{
wenzelm@64117
    48
  override def echo(msg: String)
wenzelm@64117
    49
  {
wenzelm@67178
    50
    Output.writeln(msg, stdout = !stderr)
wenzelm@64117
    51
  }
wenzelm@64117
    52
wenzelm@61276
    53
  override def theory(session: String, theory: String): Unit =
wenzelm@61276
    54
    if (verbose) echo(session + ": theory " + theory)
wenzelm@61276
    55
wenzelm@61276
    56
  @volatile private var is_stopped = false
wenzelm@68305
    57
  override def interrupt_handler[A](e: => A): A =
wenzelm@68305
    58
    POSIX_Interrupt.handler { is_stopped = true } { e }
wenzelm@61276
    59
  override def stopped: Boolean =
wenzelm@61276
    60
  {
wenzelm@61276
    61
    if (Thread.interrupted) is_stopped = true
wenzelm@61276
    62
    is_stopped
wenzelm@61276
    63
  }
wenzelm@61276
    64
}
wenzelm@65888
    65
wenzelm@65888
    66
class File_Progress(path: Path, verbose: Boolean = false) extends Progress
wenzelm@65888
    67
{
wenzelm@65888
    68
  override def echo(msg: String): Unit =
wenzelm@65888
    69
    File.append(path, msg + "\n")
wenzelm@65888
    70
wenzelm@65888
    71
  override def theory(session: String, theory: String): Unit =
wenzelm@65888
    72
    if (verbose) echo(session + ": theory " + theory)
wenzelm@65888
    73
wenzelm@65888
    74
  override def toString: String = path.toString
wenzelm@65888
    75
}