src/Pure/System/progress.scala
author wenzelm
Sat Jun 09 21:52:16 2018 +0200 (12 months ago)
changeset 68410 4e27f5c361d2
parent 68330 d7920eb7de54
child 68770 add44e2b8cb0
permissions -rw-r--r--
clarified signature: more uniform theory_message (see also d7920eb7de54);
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@68410
    13
object Progress
wenzelm@68410
    14
{
wenzelm@68410
    15
  def theory_message(session: String, theory: String): String =
wenzelm@68410
    16
    if (session == "") "theory " + theory else session + ": theory " + theory
wenzelm@68410
    17
}
wenzelm@68410
    18
wenzelm@61276
    19
class Progress
wenzelm@61276
    20
{
wenzelm@61276
    21
  def echo(msg: String) {}
wenzelm@64049
    22
  def echo_if(cond: Boolean, msg: String) { if (cond) echo(msg) }
wenzelm@61276
    23
  def theory(session: String, theory: String) {}
wenzelm@64909
    24
wenzelm@65826
    25
  def echo_warning(msg: String) { echo(Output.warning_text(msg)) }
wenzelm@65828
    26
  def echo_error_message(msg: String) { echo(Output.error_message_text(msg)) }
wenzelm@65826
    27
wenzelm@65921
    28
  def timeit[A](message: String = "", enabled: Boolean = true)(e: => A): A =
wenzelm@65921
    29
    Timing.timeit(message, enabled, echo(_))(e)
wenzelm@65921
    30
wenzelm@61276
    31
  def stopped: Boolean = false
wenzelm@68305
    32
  def interrupt_handler[A](e: => A): A = e
wenzelm@67880
    33
  def expose_interrupt() { if (stopped) throw Exn.Interrupt() }
wenzelm@61276
    34
  override def toString: String = if (stopped) "Progress(stopped)" else "Progress"
wenzelm@64201
    35
wenzelm@64201
    36
  def bash(script: String,
wenzelm@64201
    37
    cwd: JFile = null,
wenzelm@64201
    38
    env: Map[String, String] = Isabelle_System.settings(),
wenzelm@64201
    39
    redirect: Boolean = false,
wenzelm@65930
    40
    echo: Boolean = false,
wenzelm@65930
    41
    strict: Boolean = true): Process_Result =
wenzelm@64201
    42
  {
wenzelm@64201
    43
    Isabelle_System.bash(script, cwd = cwd, env = env, redirect = redirect,
wenzelm@64201
    44
      progress_stdout = echo_if(echo, _),
wenzelm@65930
    45
      progress_stderr = echo_if(echo, _),
wenzelm@65930
    46
      strict = strict)
wenzelm@64201
    47
  }
wenzelm@61276
    48
}
wenzelm@61276
    49
wenzelm@64909
    50
object No_Progress extends Progress
wenzelm@61276
    51
wenzelm@64117
    52
class Console_Progress(verbose: Boolean = false, stderr: Boolean = false) extends Progress
wenzelm@61276
    53
{
wenzelm@64117
    54
  override def echo(msg: String)
wenzelm@64117
    55
  {
wenzelm@67178
    56
    Output.writeln(msg, stdout = !stderr)
wenzelm@64117
    57
  }
wenzelm@64117
    58
wenzelm@61276
    59
  override def theory(session: String, theory: String): Unit =
wenzelm@68410
    60
    if (verbose) echo(Progress.theory_message(session, theory))
wenzelm@61276
    61
wenzelm@61276
    62
  @volatile private var is_stopped = false
wenzelm@68305
    63
  override def interrupt_handler[A](e: => A): A =
wenzelm@68305
    64
    POSIX_Interrupt.handler { is_stopped = true } { e }
wenzelm@61276
    65
  override def stopped: Boolean =
wenzelm@61276
    66
  {
wenzelm@61276
    67
    if (Thread.interrupted) is_stopped = true
wenzelm@61276
    68
    is_stopped
wenzelm@61276
    69
  }
wenzelm@61276
    70
}
wenzelm@65888
    71
wenzelm@65888
    72
class File_Progress(path: Path, verbose: Boolean = false) extends Progress
wenzelm@65888
    73
{
wenzelm@65888
    74
  override def echo(msg: String): Unit =
wenzelm@65888
    75
    File.append(path, msg + "\n")
wenzelm@65888
    76
wenzelm@65888
    77
  override def theory(session: String, theory: String): Unit =
wenzelm@68410
    78
    if (verbose) echo(Progress.theory_message(session, theory))
wenzelm@65888
    79
wenzelm@65888
    80
  override def toString: String = path.toString
wenzelm@65888
    81
}