src/Pure/System/progress.scala
author wenzelm
Thu May 25 17:32:35 2017 +0200 (2017-05-25)
changeset 65921 5b42937d3b2d
parent 65910 5bc7e080b182
child 65930 9a28fc03c3fe
permissions -rw-r--r--
more operations;
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@61276
    26
  override def toString: String = if (stopped) "Progress(stopped)" else "Progress"
wenzelm@64201
    27
wenzelm@64201
    28
  def bash(script: String,
wenzelm@64201
    29
    cwd: JFile = null,
wenzelm@64201
    30
    env: Map[String, String] = Isabelle_System.settings(),
wenzelm@64201
    31
    redirect: Boolean = false,
wenzelm@64201
    32
    echo: Boolean = false): Process_Result =
wenzelm@64201
    33
  {
wenzelm@64201
    34
    Isabelle_System.bash(script, cwd = cwd, env = env, redirect = redirect,
wenzelm@64201
    35
      progress_stdout = echo_if(echo, _),
wenzelm@64201
    36
      progress_stderr = echo_if(echo, _))
wenzelm@64201
    37
  }
wenzelm@61276
    38
}
wenzelm@61276
    39
wenzelm@64909
    40
object No_Progress extends Progress
wenzelm@61276
    41
wenzelm@64117
    42
class Console_Progress(verbose: Boolean = false, stderr: Boolean = false) extends Progress
wenzelm@61276
    43
{
wenzelm@64117
    44
  override def echo(msg: String)
wenzelm@64117
    45
  {
wenzelm@64117
    46
    if (stderr) Console.err.println(msg) else Console.println(msg)
wenzelm@64117
    47
  }
wenzelm@64117
    48
wenzelm@61276
    49
  override def theory(session: String, theory: String): Unit =
wenzelm@61276
    50
    if (verbose) echo(session + ": theory " + theory)
wenzelm@61276
    51
wenzelm@61276
    52
  @volatile private var is_stopped = false
wenzelm@61276
    53
  def interrupt_handler[A](e: => A): A = POSIX_Interrupt.handler { is_stopped = true } { e }
wenzelm@61276
    54
  override def stopped: Boolean =
wenzelm@61276
    55
  {
wenzelm@61276
    56
    if (Thread.interrupted) is_stopped = true
wenzelm@61276
    57
    is_stopped
wenzelm@61276
    58
  }
wenzelm@61276
    59
}
wenzelm@65888
    60
wenzelm@65888
    61
class File_Progress(path: Path, verbose: Boolean = false) extends Progress
wenzelm@65888
    62
{
wenzelm@65888
    63
  override def echo(msg: String): Unit =
wenzelm@65888
    64
    File.append(path, msg + "\n")
wenzelm@65888
    65
wenzelm@65888
    66
  override def theory(session: String, theory: String): Unit =
wenzelm@65888
    67
    if (verbose) echo(session + ": theory " + theory)
wenzelm@65888
    68
wenzelm@65888
    69
  override def toString: String = path.toString
wenzelm@65888
    70
}