author | wenzelm |
Tue, 04 May 2021 20:02:08 +0200 | |
changeset 73625 | f8f065e20837 |
parent 73367 | 77ef8bef0593 |
child 73897 | 0ddb5de0506e |
permissions | -rw-r--r-- |
61276 | 1 |
/* Title: Pure/System/progress.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Progress context for system processes. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
64201 | 10 |
import java.io.{File => JFile} |
11 |
||
12 |
||
68410
4e27f5c361d2
clarified signature: more uniform theory_message (see also d7920eb7de54);
wenzelm
parents:
68330
diff
changeset
|
13 |
object Progress |
4e27f5c361d2
clarified signature: more uniform theory_message (see also d7920eb7de54);
wenzelm
parents:
68330
diff
changeset
|
14 |
{ |
68957 | 15 |
sealed case class Theory(theory: String, session: String = "", percentage: Option[Int] = None) |
16 |
{ |
|
68987 | 17 |
def message: String = print_session + print_theory + print_percentage |
18 |
||
19 |
def print_session: String = if (session == "") "" else session + ": " |
|
20 |
def print_theory: String = "theory " + theory |
|
21 |
def print_percentage: String = |
|
22 |
percentage match { case None => "" case Some(p) => " " + p + "%" } |
|
68957 | 23 |
} |
68410
4e27f5c361d2
clarified signature: more uniform theory_message (see also d7920eb7de54);
wenzelm
parents:
68330
diff
changeset
|
24 |
} |
4e27f5c361d2
clarified signature: more uniform theory_message (see also d7920eb7de54);
wenzelm
parents:
68330
diff
changeset
|
25 |
|
61276 | 26 |
class Progress |
27 |
{ |
|
73340 | 28 |
def echo(msg: String): Unit = {} |
29 |
def echo_if(cond: Boolean, msg: String): Unit = { if (cond) echo(msg) } |
|
30 |
def theory(theory: Progress.Theory): Unit = {} |
|
31 |
def nodes_status(nodes_status: Document_Status.Nodes_Status): Unit = {} |
|
64909 | 32 |
|
73340 | 33 |
def echo_warning(msg: String): Unit = echo(Output.warning_text(msg)) |
34 |
def echo_error_message(msg: String): Unit = echo(Output.error_message_text(msg)) |
|
65826 | 35 |
|
65921 | 36 |
def timeit[A](message: String = "", enabled: Boolean = true)(e: => A): A = |
71601 | 37 |
Timing.timeit(message, enabled, echo)(e) |
65921 | 38 |
|
71726
a5fda30edae2
clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents:
71601
diff
changeset
|
39 |
@volatile protected var is_stopped = false |
73367 | 40 |
def stop(): Unit = { is_stopped = true } |
71726
a5fda30edae2
clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents:
71601
diff
changeset
|
41 |
def stopped: Boolean = |
a5fda30edae2
clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents:
71601
diff
changeset
|
42 |
{ |
73367 | 43 |
if (Thread.interrupted()) is_stopped = true |
71726
a5fda30edae2
clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents:
71601
diff
changeset
|
44 |
is_stopped |
a5fda30edae2
clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents:
71601
diff
changeset
|
45 |
} |
a5fda30edae2
clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents:
71601
diff
changeset
|
46 |
|
73367 | 47 |
def interrupt_handler[A](e: => A): A = POSIX_Interrupt.handler { stop() } { e } |
73340 | 48 |
def expose_interrupt(): Unit = if (stopped) throw Exn.Interrupt() |
61276 | 49 |
override def toString: String = if (stopped) "Progress(stopped)" else "Progress" |
64201 | 50 |
|
51 |
def bash(script: String, |
|
52 |
cwd: JFile = null, |
|
53 |
env: Map[String, String] = Isabelle_System.settings(), |
|
54 |
redirect: Boolean = false, |
|
65930 | 55 |
echo: Boolean = false, |
72599 | 56 |
watchdog: Time = Time.zero, |
65930 | 57 |
strict: Boolean = true): Process_Result = |
64201 | 58 |
{ |
72599 | 59 |
val result = |
60 |
Isabelle_System.bash(script, cwd = cwd, env = env, redirect = redirect, |
|
61 |
progress_stdout = echo_if(echo, _), |
|
62 |
progress_stderr = echo_if(echo, _), |
|
63 |
watchdog = if (watchdog.is_zero) None else Some((watchdog, _ => stopped)), |
|
64 |
strict = strict) |
|
65 |
if (strict && stopped) throw Exn.Interrupt() else result |
|
64201 | 66 |
} |
61276 | 67 |
} |
68 |
||
64117 | 69 |
class Console_Progress(verbose: Boolean = false, stderr: Boolean = false) extends Progress |
61276 | 70 |
{ |
68951
a7b1fe2d30ad
more uniform Progress, with theory() for batch-build and theory_percentage() for PIDE session;
wenzelm
parents:
68903
diff
changeset
|
71 |
override def echo(msg: String): Unit = |
71100
f31903cc57b0
clarified Console_Progress.echo: include empty lines as in other Progress instances, especially relevant for Progress.bash (e.g. "isabelle phabricator ./bin/config help");
wenzelm
parents:
69818
diff
changeset
|
72 |
Output.writeln(msg, stdout = !stderr, include_empty = true) |
64117 | 73 |
|
68957 | 74 |
override def theory(theory: Progress.Theory): Unit = |
75 |
if (verbose) echo(theory.message) |
|
61276 | 76 |
} |
65888 | 77 |
|
78 |
class File_Progress(path: Path, verbose: Boolean = false) extends Progress |
|
79 |
{ |
|
80 |
override def echo(msg: String): Unit = |
|
81 |
File.append(path, msg + "\n") |
|
82 |
||
68957 | 83 |
override def theory(theory: Progress.Theory): Unit = |
84 |
if (verbose) echo(theory.message) |
|
68951
a7b1fe2d30ad
more uniform Progress, with theory() for batch-build and theory_percentage() for PIDE session;
wenzelm
parents:
68903
diff
changeset
|
85 |
|
65888 | 86 |
override def toString: String = path.toString |
87 |
} |