author | wenzelm |
Sat, 07 Nov 2020 16:36:50 +0100 | |
changeset 72556 | 7abd365058e9 |
parent 72337 | 4075560b3d5c |
child 72726 | ec6a27bbdab8 |
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", |
|
72337 | 23 |
127 -> "COMMAND NOT FOUND", |
71748 | 24 |
130 -> "INTERRUPT", |
25 |
131 -> "QUIT SIGNAL", |
|
26 |
137 -> "KILL SIGNAL", |
|
27 |
138 -> "BUS ERROR", |
|
28 |
139 -> "SEGMENTATION VIOLATION", |
|
29 |
143 -> "TERMINATION SIGNAL") |
|
71747 | 30 |
} |
31 |
||
62401 | 32 |
final case class Process_Result( |
62402 | 33 |
rc: Int, |
34 |
out_lines: List[String] = Nil, |
|
35 |
err_lines: List[String] = Nil, |
|
62569 | 36 |
timeout: Boolean = false, |
37 |
timing: Timing = Timing.zero) |
|
62400 | 38 |
{ |
39 |
def out: String = cat_lines(out_lines) |
|
40 |
def err: String = cat_lines(err_lines) |
|
71631 | 41 |
|
71646 | 42 |
def output(outs: List[String]): Process_Result = |
43 |
copy(out_lines = out_lines ::: outs.flatMap(split_lines)) |
|
44 |
def errors(errs: List[String]): Process_Result = |
|
45 |
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
|
46 |
def error(err: String): Process_Result = |
5c4800f6b25a
more robust protocol for "Timing ..." messages, notably for pide_session=true;
wenzelm
parents:
71749
diff
changeset
|
47 |
if (err.isEmpty) this else errors(List(err)) |
62400 | 48 |
|
62569 | 49 |
def was_timeout: Process_Result = copy(rc = 1, timeout = true) |
62401 | 50 |
|
62400 | 51 |
def ok: Boolean = rc == 0 |
52 |
def interrupted: Boolean = rc == Exn.Interrupt.return_code |
|
53 |
||
68927 | 54 |
def error_rc: Process_Result = if (interrupted) this else copy(rc = rc max 1) |
55 |
||
64408
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
56 |
def check_rc(pred: Int => Boolean): Process_Result = |
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
57 |
if (pred(rc)) this |
62400 | 58 |
else if (interrupted) throw Exn.Interrupt() |
62492 | 59 |
else Exn.error(err) |
62400 | 60 |
|
64408
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
61 |
def check: Process_Result = check_rc(_ == 0) |
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
62 |
|
71747 | 63 |
def print_return_code: String = Process_Result.print_return_code(rc) |
64 |
def print_rc: String = Process_Result.print_rc(rc) |
|
65 |
||
62400 | 66 |
def print: Process_Result = |
67 |
{ |
|
64138
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
68 |
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
|
69 |
Output.writeln(out) |
62404
13a0f537e232
retain tail out_lines as printed, but not the whole log content;
wenzelm
parents:
62402
diff
changeset
|
70 |
copy(out_lines = Nil, err_lines = Nil) |
62400 | 71 |
} |
62553 | 72 |
|
73 |
def print_stdout: Process_Result = |
|
74 |
{ |
|
64138
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
75 |
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
|
76 |
Output.writeln(out, stdout = true) |
62553 | 77 |
copy(out_lines = Nil, err_lines = Nil) |
78 |
} |
|
62400 | 79 |
} |