src/Pure/System/progress.scala
author wenzelm
Sun Dec 10 20:29:00 2017 +0100 (18 months ago)
changeset 67178 70576478bda9
parent 65930 9a28fc03c3fe
child 67880 e59220a075de
permissions -rw-r--r--
avoid println with its extra CR on Windows;
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@65930
    32
    echo: Boolean = false,
wenzelm@65930
    33
    strict: Boolean = true): Process_Result =
wenzelm@64201
    34
  {
wenzelm@64201
    35
    Isabelle_System.bash(script, cwd = cwd, env = env, redirect = redirect,
wenzelm@64201
    36
      progress_stdout = echo_if(echo, _),
wenzelm@65930
    37
      progress_stderr = echo_if(echo, _),
wenzelm@65930
    38
      strict = strict)
wenzelm@64201
    39
  }
wenzelm@61276
    40
}
wenzelm@61276
    41
wenzelm@64909
    42
object No_Progress extends Progress
wenzelm@61276
    43
wenzelm@64117
    44
class Console_Progress(verbose: Boolean = false, stderr: Boolean = false) extends Progress
wenzelm@61276
    45
{
wenzelm@64117
    46
  override def echo(msg: String)
wenzelm@64117
    47
  {
wenzelm@67178
    48
    Output.writeln(msg, stdout = !stderr)
wenzelm@64117
    49
  }
wenzelm@64117
    50
wenzelm@61276
    51
  override def theory(session: String, theory: String): Unit =
wenzelm@61276
    52
    if (verbose) echo(session + ": theory " + theory)
wenzelm@61276
    53
wenzelm@61276
    54
  @volatile private var is_stopped = false
wenzelm@61276
    55
  def interrupt_handler[A](e: => A): A = POSIX_Interrupt.handler { is_stopped = true } { e }
wenzelm@61276
    56
  override def stopped: Boolean =
wenzelm@61276
    57
  {
wenzelm@61276
    58
    if (Thread.interrupted) is_stopped = true
wenzelm@61276
    59
    is_stopped
wenzelm@61276
    60
  }
wenzelm@61276
    61
}
wenzelm@65888
    62
wenzelm@65888
    63
class File_Progress(path: Path, verbose: Boolean = false) extends Progress
wenzelm@65888
    64
{
wenzelm@65888
    65
  override def echo(msg: String): Unit =
wenzelm@65888
    66
    File.append(path, msg + "\n")
wenzelm@65888
    67
wenzelm@65888
    68
  override def theory(session: String, theory: String): Unit =
wenzelm@65888
    69
    if (verbose) echo(session + ": theory " + theory)
wenzelm@65888
    70
wenzelm@65888
    71
  override def toString: String = path.toString
wenzelm@65888
    72
}