author | wenzelm |
Wed, 08 Jul 2020 14:43:02 +0200 | |
changeset 72002 | 5c4800f6b25a |
parent 71749 | 77232ff6b8f6 |
child 72337 | 4075560b3d5c |
permissions | -rw-r--r-- |
62400 | 1 |
/* Title: Pure/System/process_result.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Result of system process. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
71747 | 9 |
object Process_Result |
10 |
{ |
|
71749 | 11 |
def print_return_code(rc: Int): String = "Return code: " + rc + rc_text(rc) |
12 |
def print_rc(rc: Int): String = "return code " + rc + rc_text(rc) |
|
13 |
||
14 |
def rc_text(rc: Int): String = |
|
15 |
return_code_text.get(rc) match { |
|
16 |
case None => "" |
|
17 |
case Some(text) => " (" + text + ")" |
|
18 |
} |
|
19 |
private val return_code_text = |
|
71748 | 20 |
Map(0 -> "OK", |
21 |
1 -> "ERROR", |
|
22 |
2 -> "FAILURE", |
|
23 |
130 -> "INTERRUPT", |
|
24 |
131 -> "QUIT SIGNAL", |
|
25 |
137 -> "KILL SIGNAL", |
|
26 |
138 -> "BUS ERROR", |
|
27 |
139 -> "SEGMENTATION VIOLATION", |
|
28 |
143 -> "TERMINATION SIGNAL") |
|
71747 | 29 |
} |
30 |
||
62401 | 31 |
final case class Process_Result( |
62402 | 32 |
rc: Int, |
33 |
out_lines: List[String] = Nil, |
|
34 |
err_lines: List[String] = Nil, |
|
62569 | 35 |
timeout: Boolean = false, |
36 |
timing: Timing = Timing.zero) |
|
62400 | 37 |
{ |
38 |
def out: String = cat_lines(out_lines) |
|
39 |
def err: String = cat_lines(err_lines) |
|
71631 | 40 |
|
71646 | 41 |
def output(outs: List[String]): Process_Result = |
42 |
copy(out_lines = out_lines ::: outs.flatMap(split_lines)) |
|
43 |
def errors(errs: List[String]): Process_Result = |
|
44 |
copy(err_lines = err_lines ::: errs.flatMap(split_lines)) |
|
72002
5c4800f6b25a
more robust protocol for "Timing ..." messages, notably for pide_session=true;
wenzelm
parents:
71749
diff
changeset
|
45 |
def error(err: String): Process_Result = |
5c4800f6b25a
more robust protocol for "Timing ..." messages, notably for pide_session=true;
wenzelm
parents:
71749
diff
changeset
|
46 |
if (err.isEmpty) this else errors(List(err)) |
62400 | 47 |
|
62569 | 48 |
def was_timeout: Process_Result = copy(rc = 1, timeout = true) |
62401 | 49 |
|
62400 | 50 |
def ok: Boolean = rc == 0 |
51 |
def interrupted: Boolean = rc == Exn.Interrupt.return_code |
|
52 |
||
68927 | 53 |
def error_rc: Process_Result = if (interrupted) this else copy(rc = rc max 1) |
54 |
||
64408
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
55 |
def check_rc(pred: Int => Boolean): Process_Result = |
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
56 |
if (pred(rc)) this |
62400 | 57 |
else if (interrupted) throw Exn.Interrupt() |
62492 | 58 |
else Exn.error(err) |
62400 | 59 |
|
64408
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
60 |
def check: Process_Result = check_rc(_ == 0) |
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
61 |
|
71747 | 62 |
def print_return_code: String = Process_Result.print_return_code(rc) |
63 |
def print_rc: String = Process_Result.print_rc(rc) |
|
64 |
||
62400 | 65 |
def print: Process_Result = |
66 |
{ |
|
64138
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
67 |
Output.warning(err) |
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
68 |
Output.writeln(out) |
62404
13a0f537e232
retain tail out_lines as printed, but not the whole log content;
wenzelm
parents:
62402
diff
changeset
|
69 |
copy(out_lines = Nil, err_lines = Nil) |
62400 | 70 |
} |
62553 | 71 |
|
72 |
def print_stdout: Process_Result = |
|
73 |
{ |
|
64138
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
74 |
Output.warning(err, stdout = true) |
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
75 |
Output.writeln(out, stdout = true) |
62553 | 76 |
copy(out_lines = Nil, err_lines = Nil) |
77 |
} |
|
64024 | 78 |
|
79 |
def print_if(b: Boolean): Process_Result = if (b) print else this |
|
80 |
def print_stdout_if(b: Boolean): Process_Result = if (b) print_stdout else this |
|
62400 | 81 |
} |