| author | wenzelm |
| Mon, 06 Feb 2023 15:35:18 +0100 | |
| changeset 77210 | 1ffee8893b12 |
| parent 77174 | 1eb55d6809b3 |
| child 77498 | 2d12cb7ff829 |
| 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 |
||
| 73897 | 10 |
import java.util.{Map => JMap}
|
| 64201 | 11 |
import java.io.{File => JFile}
|
12 |
||
13 |
||
| 75393 | 14 |
object Progress {
|
15 |
sealed case class Theory(theory: String, session: String = "", percentage: Option[Int] = None) {
|
|
| 68987 | 16 |
def message: String = print_session + print_theory + print_percentage |
17 |
||
18 |
def print_session: String = if (session == "") "" else session + ": " |
|
19 |
def print_theory: String = "theory " + theory |
|
20 |
def print_percentage: String = |
|
21 |
percentage match { case None => "" case Some(p) => " " + p + "%" }
|
|
| 68957 | 22 |
} |
|
68410
4e27f5c361d2
clarified signature: more uniform theory_message (see also d7920eb7de54);
wenzelm
parents:
68330
diff
changeset
|
23 |
} |
|
4e27f5c361d2
clarified signature: more uniform theory_message (see also d7920eb7de54);
wenzelm
parents:
68330
diff
changeset
|
24 |
|
| 75393 | 25 |
class Progress {
|
| 73340 | 26 |
def echo(msg: String): Unit = {}
|
27 |
def echo_if(cond: Boolean, msg: String): Unit = { if (cond) echo(msg) }
|
|
28 |
def theory(theory: Progress.Theory): Unit = {}
|
|
29 |
def nodes_status(nodes_status: Document_Status.Nodes_Status): Unit = {}
|
|
| 64909 | 30 |
|
| 73340 | 31 |
def echo_warning(msg: String): Unit = echo(Output.warning_text(msg)) |
32 |
def echo_error_message(msg: String): Unit = echo(Output.error_message_text(msg)) |
|
| 65826 | 33 |
|
|
76593
badb5264f7b9
clarified signature: just one level of arguments to avoid type-inference problems;
wenzelm
parents:
76592
diff
changeset
|
34 |
def timeit[A](body: => A, message: Exn.Result[A] => String = null, enabled: Boolean = true): A = |
|
badb5264f7b9
clarified signature: just one level of arguments to avoid type-inference problems;
wenzelm
parents:
76592
diff
changeset
|
35 |
Timing.timeit(body, message = message, enabled = enabled, output = echo) |
| 65921 | 36 |
|
|
71726
a5fda30edae2
clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents:
71601
diff
changeset
|
37 |
@volatile protected var is_stopped = false |
| 73367 | 38 |
def stop(): Unit = { is_stopped = true }
|
| 75393 | 39 |
def stopped: Boolean = {
|
| 73367 | 40 |
if (Thread.interrupted()) is_stopped = true |
|
71726
a5fda30edae2
clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents:
71601
diff
changeset
|
41 |
is_stopped |
|
a5fda30edae2
clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents:
71601
diff
changeset
|
42 |
} |
|
a5fda30edae2
clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents:
71601
diff
changeset
|
43 |
|
| 73367 | 44 |
def interrupt_handler[A](e: => A): A = POSIX_Interrupt.handler { stop() } { e }
|
| 73340 | 45 |
def expose_interrupt(): Unit = if (stopped) throw Exn.Interrupt() |
| 61276 | 46 |
override def toString: String = if (stopped) "Progress(stopped)" else "Progress" |
| 64201 | 47 |
|
48 |
def bash(script: String, |
|
49 |
cwd: JFile = null, |
|
| 73897 | 50 |
env: JMap[String, String] = Isabelle_System.settings(), |
| 64201 | 51 |
redirect: Boolean = false, |
| 65930 | 52 |
echo: Boolean = false, |
| 72599 | 53 |
watchdog: Time = Time.zero, |
| 75393 | 54 |
strict: Boolean = true |
55 |
): Process_Result = {
|
|
| 72599 | 56 |
val result = |
57 |
Isabelle_System.bash(script, cwd = cwd, env = env, redirect = redirect, |
|
58 |
progress_stdout = echo_if(echo, _), |
|
59 |
progress_stderr = echo_if(echo, _), |
|
60 |
watchdog = if (watchdog.is_zero) None else Some((watchdog, _ => stopped)), |
|
61 |
strict = strict) |
|
62 |
if (strict && stopped) throw Exn.Interrupt() else result |
|
| 64201 | 63 |
} |
| 61276 | 64 |
} |
65 |
||
| 75393 | 66 |
class Console_Progress(verbose: Boolean = false, stderr: Boolean = false) extends Progress {
|
|
68951
a7b1fe2d30ad
more uniform Progress, with theory() for batch-build and theory_percentage() for PIDE session;
wenzelm
parents:
68903
diff
changeset
|
67 |
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
|
68 |
Output.writeln(msg, stdout = !stderr, include_empty = true) |
| 64117 | 69 |
|
| 68957 | 70 |
override def theory(theory: Progress.Theory): Unit = |
71 |
if (verbose) echo(theory.message) |
|
| 61276 | 72 |
} |
| 65888 | 73 |
|
| 75393 | 74 |
class File_Progress(path: Path, verbose: Boolean = false) extends Progress {
|
| 65888 | 75 |
override def echo(msg: String): Unit = |
76 |
File.append(path, msg + "\n") |
|
77 |
||
| 68957 | 78 |
override def theory(theory: Progress.Theory): Unit = |
79 |
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
|
80 |
|
| 65888 | 81 |
override def toString: String = path.toString |
82 |
} |
|
|
76994
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
83 |
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
84 |
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
85 |
/* structured program progress */ |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
86 |
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
87 |
object Program_Progress {
|
| 77174 | 88 |
class Program private[Program_Progress](heading: String, title: String) {
|
|
76994
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
89 |
private val output_buffer = new StringBuffer(256) // synchronized |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
90 |
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
91 |
def echo(msg: String): Unit = synchronized {
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
92 |
if (output_buffer.length() > 0) output_buffer.append('\n')
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
93 |
output_buffer.append(msg) |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
94 |
} |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
95 |
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
96 |
val start_time: Time = Time.now() |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
97 |
private var stop_time: Option[Time] = None |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
98 |
def stop_now(): Unit = synchronized { stop_time = Some(Time.now()) }
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
99 |
|
| 77174 | 100 |
def output(): (Command.Results, XML.Body) = synchronized {
|
|
76994
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
101 |
val output_text = output_buffer.toString |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
102 |
val elapsed_time = stop_time.map(t => t - start_time) |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
103 |
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
104 |
val message_prefix = heading + " " |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
105 |
val message_suffix = |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
106 |
elapsed_time match {
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
107 |
case None => " ..." |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
108 |
case Some(t) => " ... (" + t.message + " elapsed time)"
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
109 |
} |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
110 |
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
111 |
val (results, message) = |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
112 |
if (output_text.isEmpty) {
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
113 |
(Command.Results.empty, XML.string(message_prefix + title + message_suffix)) |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
114 |
} |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
115 |
else {
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
116 |
val i = Document_ID.make() |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
117 |
val results = Command.Results.make(List(i -> Protocol.writeln_message(output_text))) |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
118 |
val message = |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
119 |
XML.string(message_prefix) ::: |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
120 |
List(XML.Elem(Markup(Markup.WRITELN, Markup.Serial(i)), XML.string(title))) ::: |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
121 |
XML.string(message_suffix) |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
122 |
(results, message) |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
123 |
} |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
124 |
|
| 76997 | 125 |
(results, List(XML.elem(Markup.TRACING_MESSAGE, message))) |
|
76994
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
126 |
} |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
127 |
} |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
128 |
} |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
129 |
|
| 77174 | 130 |
abstract class Program_Progress( |
131 |
default_heading: String = "Running", |
|
132 |
default_title: String = "program" |
|
133 |
) extends Progress {
|
|
|
76994
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
134 |
private var _finished_programs: List[Program_Progress.Program] = Nil |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
135 |
private var _running_program: Option[Program_Progress.Program] = None |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
136 |
|
| 77174 | 137 |
def output(): (Command.Results, XML.Body) = synchronized {
|
|
76994
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
138 |
val programs = (_running_program.toList ::: _finished_programs).reverse |
| 77174 | 139 |
val programs_output = programs.map(_.output()) |
|
76994
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
140 |
val results = Command.Results.merge(programs_output.map(_._1)) |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
141 |
val body = Library.separate(Pretty.Separator, programs_output.map(_._2)).flatten |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
142 |
(results, body) |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
143 |
} |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
144 |
|
| 77174 | 145 |
private def start_program(heading: String, title: String): Unit = synchronized {
|
146 |
_running_program = Some(new Program_Progress.Program(heading, title)) |
|
|
76994
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
147 |
} |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
148 |
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
149 |
def stop_program(): Unit = synchronized {
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
150 |
_running_program match {
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
151 |
case Some(program) => |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
152 |
program.stop_now() |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
153 |
_finished_programs ::= program |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
154 |
_running_program = None |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
155 |
case None => |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
156 |
} |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
157 |
} |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
158 |
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
159 |
def detect_program(s: String): Option[String] |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
160 |
|
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
161 |
override def echo(msg: String): Unit = synchronized {
|
| 77174 | 162 |
detect_program(msg).map(Word.explode) match {
|
163 |
case Some(a :: bs) => |
|
|
76994
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
164 |
stop_program() |
| 77174 | 165 |
start_program(a, Word.implode(bs)) |
166 |
case _ => |
|
167 |
if (_running_program.isEmpty) start_program(default_heading, default_title) |
|
|
76994
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
168 |
_running_program.get.echo(msg) |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
169 |
} |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
170 |
} |
|
7c23db6b857b
more detailed Program_Progress / Log_Progress: each program gets its own log output, which is attached to the document via markup;
wenzelm
parents:
76593
diff
changeset
|
171 |
} |