src/Pure/General/output.ML
author wenzelm
Mon, 25 Oct 2010 20:24:13 +0200
changeset 40391 7cbebd636e79
parent 40371 da45a2f45870
child 40392 7ee65dbffa31
permissions -rw-r--r--
explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm@14815
     1
(*  Title:      Pure/General/output.ML
wenzelm@14815
     2
    Author:     Makarius, Hagia Maria Sion Abbey (Jerusalem)
wenzelm@14815
     3
wenzelm@22585
     4
Output channels and timing messages.
wenzelm@14815
     5
*)
wenzelm@14815
     6
wenzelm@14815
     7
signature BASIC_OUTPUT =
wenzelm@14815
     8
sig
wenzelm@18682
     9
  val writeln: string -> unit
wenzelm@18682
    10
  val priority: string -> unit
wenzelm@18682
    11
  val tracing: string -> unit
wenzelm@18682
    12
  val warning: string -> unit
wenzelm@22826
    13
  val legacy_feature: string -> unit
wenzelm@25686
    14
  val cond_timeit: bool -> string -> (unit -> 'a) -> 'a
wenzelm@14815
    15
  val timeit: (unit -> 'a) -> 'a
wenzelm@14815
    16
  val timeap: ('a -> 'b) -> 'a -> 'b
wenzelm@14815
    17
  val timeap_msg: string -> ('a -> 'b) -> 'a -> 'b
wenzelm@32738
    18
  val timing: bool Unsynchronized.ref
wenzelm@14815
    19
end;
wenzelm@14815
    20
wenzelm@14815
    21
signature OUTPUT =
wenzelm@14815
    22
sig
wenzelm@14815
    23
  include BASIC_OUTPUT
wenzelm@40391
    24
  type output = string
wenzelm@23660
    25
  val default_output: string -> output * int
wenzelm@23660
    26
  val default_escape: output -> string
wenzelm@23660
    27
  val add_mode: string -> (string -> output * int) -> (output -> string) -> unit
wenzelm@23660
    28
  val output_width: string -> output * int
wenzelm@23660
    29
  val output: string -> output
wenzelm@23660
    30
  val escape: output -> string
wenzelm@40026
    31
  val raw_stdout: output -> unit
wenzelm@40026
    32
  val raw_stderr: output -> unit
wenzelm@40026
    33
  val raw_writeln: output -> unit
wenzelm@32738
    34
  val writeln_fn: (output -> unit) Unsynchronized.ref
wenzelm@32738
    35
  val priority_fn: (output -> unit) Unsynchronized.ref
wenzelm@32738
    36
  val tracing_fn: (output -> unit) Unsynchronized.ref
wenzelm@32738
    37
  val warning_fn: (output -> unit) Unsynchronized.ref
wenzelm@32738
    38
  val error_fn: (output -> unit) Unsynchronized.ref
wenzelm@32738
    39
  val prompt_fn: (output -> unit) Unsynchronized.ref
wenzelm@32738
    40
  val status_fn: (output -> unit) Unsynchronized.ref
wenzelm@38492
    41
  val report_fn: (output -> unit) Unsynchronized.ref
wenzelm@25845
    42
  val error_msg: string -> unit
wenzelm@25845
    43
  val prompt: string -> unit
wenzelm@27605
    44
  val status: string -> unit
wenzelm@38492
    45
  val report: string -> unit
wenzelm@14815
    46
end;
wenzelm@14815
    47
wenzelm@14815
    48
structure Output: OUTPUT =
wenzelm@14815
    49
struct
wenzelm@14815
    50
wenzelm@23616
    51
(** print modes **)
wenzelm@14881
    52
wenzelm@23660
    53
type output = string;  (*raw system output*)
wenzelm@23660
    54
wenzelm@23616
    55
fun default_output s = (s, size s);
wenzelm@23660
    56
fun default_escape (s: output) = s;
wenzelm@14955
    57
wenzelm@23616
    58
local
wenzelm@23616
    59
  val default = {output = default_output, escape = default_escape};
wenzelm@32738
    60
  val modes = Unsynchronized.ref (Symtab.make [("", default)]);
wenzelm@23616
    61
in
wenzelm@23922
    62
  fun add_mode name output escape = CRITICAL (fn () =>
wenzelm@32738
    63
    Unsynchronized.change modes (Symtab.update_new (name, {output = output, escape = escape})));
wenzelm@23616
    64
  fun get_mode () =
wenzelm@24612
    65
    the_default default (Library.get_first (Symtab.lookup (! modes)) (print_mode_value ()));
wenzelm@23616
    66
end;
wenzelm@14815
    67
wenzelm@19265
    68
fun output_width x = #output (get_mode ()) x;
wenzelm@14815
    69
val output = #1 o output_width;
wenzelm@23727
    70
wenzelm@23616
    71
fun escape x = #escape (get_mode ()) x;
wenzelm@14815
    72
wenzelm@14815
    73
wenzelm@14815
    74
wenzelm@14815
    75
(** output channels **)
wenzelm@14815
    76
wenzelm@40026
    77
(* raw output primitives -- not to be used in user-space *)
wenzelm@14984
    78
wenzelm@40026
    79
fun raw_stdout s = (TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut);
wenzelm@40026
    80
fun raw_stderr s = (TextIO.output (TextIO.stdErr, s); TextIO.flushOut TextIO.stdErr);
wenzelm@14815
    81
wenzelm@40026
    82
fun raw_writeln "" = ()
wenzelm@40026
    83
  | raw_writeln s = raw_stdout (suffix "\n" s);  (*atomic output!*)
wenzelm@14815
    84
wenzelm@14984
    85
wenzelm@14984
    86
(* Isabelle output channels *)
wenzelm@14984
    87
wenzelm@40026
    88
val writeln_fn = Unsynchronized.ref raw_writeln;
wenzelm@32738
    89
val priority_fn = Unsynchronized.ref (fn s => ! writeln_fn s);
wenzelm@32738
    90
val tracing_fn = Unsynchronized.ref (fn s => ! writeln_fn s);
wenzelm@40026
    91
val warning_fn = Unsynchronized.ref (raw_stdout o suffix "\n" o prefix_lines "### ");
wenzelm@40026
    92
val error_fn = Unsynchronized.ref (raw_stdout o suffix "\n" o prefix_lines "*** ");
wenzelm@40026
    93
val prompt_fn = Unsynchronized.ref raw_stdout;
wenzelm@32738
    94
val status_fn = Unsynchronized.ref (fn _: string => ());
wenzelm@38492
    95
val report_fn = Unsynchronized.ref (fn _: string => ());
wenzelm@14815
    96
wenzelm@14815
    97
fun writeln s = ! writeln_fn (output s);
wenzelm@14815
    98
fun priority s = ! priority_fn (output s);
wenzelm@14815
    99
fun tracing s = ! tracing_fn (output s);
wenzelm@14815
   100
fun warning s = ! warning_fn (output s);
wenzelm@25845
   101
fun error_msg s = ! error_fn (output s);
wenzelm@25845
   102
fun prompt s = ! prompt_fn (output s);
wenzelm@27605
   103
fun status s = ! status_fn (output s);
wenzelm@38492
   104
fun report s = ! report_fn (output s);
aspinall@15190
   105
wenzelm@37784
   106
fun legacy_feature s = warning ("Legacy feature! " ^ s);
wenzelm@22826
   107
wenzelm@14815
   108
wenzelm@14815
   109
wenzelm@14815
   110
(** timing **)
wenzelm@14815
   111
wenzelm@25686
   112
(*conditional timing with message*)
wenzelm@25686
   113
fun cond_timeit flag msg e =
wenzelm@14815
   114
  if flag then
wenzelm@23862
   115
    let
wenzelm@23862
   116
      val start = start_timing ();
wenzelm@25686
   117
      val result = Exn.capture e ();
wenzelm@30187
   118
      val end_msg = #message (end_timing start);
wenzelm@25686
   119
      val _ = warning (if msg = "" then end_msg else msg ^ "\n" ^ end_msg);
wenzelm@25686
   120
    in Exn.release result end
wenzelm@25686
   121
  else e ();
haftmann@25667
   122
wenzelm@23862
   123
(*unconditional timing*)
wenzelm@25686
   124
fun timeit e = cond_timeit true "" e;
wenzelm@14815
   125
wenzelm@14815
   126
(*timed application function*)
wenzelm@14815
   127
fun timeap f x = timeit (fn () => f x);
wenzelm@25686
   128
fun timeap_msg msg f x = cond_timeit true msg (fn () => f x);
wenzelm@25686
   129
wenzelm@25686
   130
(*global timing mode*)
wenzelm@32738
   131
val timing = Unsynchronized.ref false;
wenzelm@14815
   132
wenzelm@14815
   133
end;
wenzelm@14815
   134
wenzelm@32738
   135
structure Basic_Output: BASIC_OUTPUT = Output;
wenzelm@32738
   136
open Basic_Output;