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;
     1 /*  Title:      Pure/System/progress.scala
     2     Author:     Makarius
     3 
     4 Progress context for system processes.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import java.io.{File => JFile}
    11 
    12 
    13 class Progress
    14 {
    15   def echo(msg: String) {}
    16   def echo_if(cond: Boolean, msg: String) { if (cond) echo(msg) }
    17   def theory(session: String, theory: String) {}
    18 
    19   def echo_warning(msg: String) { echo(Output.warning_text(msg)) }
    20   def echo_error_message(msg: String) { echo(Output.error_message_text(msg)) }
    21 
    22   def timeit[A](message: String = "", enabled: Boolean = true)(e: => A): A =
    23     Timing.timeit(message, enabled, echo(_))(e)
    24 
    25   def stopped: Boolean = false
    26   override def toString: String = if (stopped) "Progress(stopped)" else "Progress"
    27 
    28   def bash(script: String,
    29     cwd: JFile = null,
    30     env: Map[String, String] = Isabelle_System.settings(),
    31     redirect: Boolean = false,
    32     echo: Boolean = false,
    33     strict: Boolean = true): Process_Result =
    34   {
    35     Isabelle_System.bash(script, cwd = cwd, env = env, redirect = redirect,
    36       progress_stdout = echo_if(echo, _),
    37       progress_stderr = echo_if(echo, _),
    38       strict = strict)
    39   }
    40 }
    41 
    42 object No_Progress extends Progress
    43 
    44 class Console_Progress(verbose: Boolean = false, stderr: Boolean = false) extends Progress
    45 {
    46   override def echo(msg: String)
    47   {
    48     Output.writeln(msg, stdout = !stderr)
    49   }
    50 
    51   override def theory(session: String, theory: String): Unit =
    52     if (verbose) echo(session + ": theory " + theory)
    53 
    54   @volatile private var is_stopped = false
    55   def interrupt_handler[A](e: => A): A = POSIX_Interrupt.handler { is_stopped = true } { e }
    56   override def stopped: Boolean =
    57   {
    58     if (Thread.interrupted) is_stopped = true
    59     is_stopped
    60   }
    61 }
    62 
    63 class File_Progress(path: Path, verbose: Boolean = false) extends Progress
    64 {
    65   override def echo(msg: String): Unit =
    66     File.append(path, msg + "\n")
    67 
    68   override def theory(session: String, theory: String): Unit =
    69     if (verbose) echo(session + ": theory " + theory)
    70 
    71   override def toString: String = path.toString
    72 }