src/Pure/General/output.ML
author wenzelm
Mon, 12 Jul 2010 22:17:31 +0200
changeset 37784 1d639d28832c
parent 32966 5b21661fe618
child 38492 d8c7be27e01d
permissions -rw-r--r--
removed impractical tolerate_legacy_features flag;
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@23660
     9
  type output = string
wenzelm@18682
    10
  val writeln: string -> unit
wenzelm@18682
    11
  val priority: string -> unit
wenzelm@18682
    12
  val tracing: string -> unit
wenzelm@18682
    13
  val warning: string -> unit
wenzelm@22826
    14
  val legacy_feature: string -> unit
wenzelm@25686
    15
  val cond_timeit: bool -> string -> (unit -> 'a) -> 'a
wenzelm@14815
    16
  val timeit: (unit -> 'a) -> 'a
wenzelm@14815
    17
  val timeap: ('a -> 'b) -> 'a -> 'b
wenzelm@14815
    18
  val timeap_msg: string -> ('a -> 'b) -> 'a -> 'b
wenzelm@32738
    19
  val timing: bool Unsynchronized.ref
wenzelm@14815
    20
end;
wenzelm@14815
    21
wenzelm@14815
    22
signature OUTPUT =
wenzelm@14815
    23
sig
wenzelm@14815
    24
  include BASIC_OUTPUT
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@23660
    31
  val std_output: output -> unit
wenzelm@23660
    32
  val std_error: output -> unit
wenzelm@23660
    33
  val writeln_default: 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 debug_fn: (output -> unit) Unsynchronized.ref
wenzelm@32738
    40
  val prompt_fn: (output -> unit) Unsynchronized.ref
wenzelm@32738
    41
  val status_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@32738
    45
  val debugging: bool Unsynchronized.ref
wenzelm@32966
    46
  val no_warnings_CRITICAL: ('a -> 'b) -> 'a -> 'b
wenzelm@22130
    47
  val debug: (unit -> string) -> unit
wenzelm@14815
    48
end;
wenzelm@14815
    49
wenzelm@14815
    50
structure Output: OUTPUT =
wenzelm@14815
    51
struct
wenzelm@14815
    52
wenzelm@23616
    53
(** print modes **)
wenzelm@14881
    54
wenzelm@23660
    55
type output = string;  (*raw system output*)
wenzelm@23660
    56
wenzelm@23616
    57
fun default_output s = (s, size s);
wenzelm@23660
    58
fun default_escape (s: output) = s;
wenzelm@14955
    59
wenzelm@23616
    60
local
wenzelm@23616
    61
  val default = {output = default_output, escape = default_escape};
wenzelm@32738
    62
  val modes = Unsynchronized.ref (Symtab.make [("", default)]);
wenzelm@23616
    63
in
wenzelm@23922
    64
  fun add_mode name output escape = CRITICAL (fn () =>
wenzelm@32738
    65
    Unsynchronized.change modes (Symtab.update_new (name, {output = output, escape = escape})));
wenzelm@23616
    66
  fun get_mode () =
wenzelm@24612
    67
    the_default default (Library.get_first (Symtab.lookup (! modes)) (print_mode_value ()));
wenzelm@23616
    68
end;
wenzelm@14815
    69
wenzelm@19265
    70
fun output_width x = #output (get_mode ()) x;
wenzelm@14815
    71
val output = #1 o output_width;
wenzelm@23727
    72
wenzelm@23616
    73
fun escape x = #escape (get_mode ()) x;
wenzelm@14815
    74
wenzelm@14815
    75
wenzelm@14815
    76
wenzelm@14815
    77
(** output channels **)
wenzelm@14815
    78
wenzelm@14984
    79
(* output primitives -- normally NOT used directly!*)
wenzelm@14984
    80
wenzelm@24058
    81
fun std_output s = NAMED_CRITICAL "IO" (fn () =>
wenzelm@23922
    82
  (TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut));
wenzelm@23922
    83
wenzelm@24058
    84
fun std_error s = NAMED_CRITICAL "IO" (fn () =>
wenzelm@23922
    85
  (TextIO.output (TextIO.stdErr, s); TextIO.flushOut TextIO.stdErr));
wenzelm@14815
    86
wenzelm@27605
    87
fun writeln_default "" = ()
wenzelm@27605
    88
  | writeln_default s = std_output (suffix "\n" s);
wenzelm@14815
    89
wenzelm@14984
    90
wenzelm@14984
    91
(* Isabelle output channels *)
wenzelm@14984
    92
wenzelm@32738
    93
val writeln_fn = Unsynchronized.ref writeln_default;
wenzelm@32738
    94
val priority_fn = Unsynchronized.ref (fn s => ! writeln_fn s);
wenzelm@32738
    95
val tracing_fn = Unsynchronized.ref (fn s => ! writeln_fn s);
wenzelm@32738
    96
val warning_fn = Unsynchronized.ref (std_output o suffix "\n" o prefix_lines "### ");
wenzelm@32738
    97
val error_fn = Unsynchronized.ref (std_output o suffix "\n" o prefix_lines "*** ");
wenzelm@32738
    98
val debug_fn = Unsynchronized.ref (std_output o suffix "\n" o prefix_lines "::: ");
wenzelm@32738
    99
val prompt_fn = Unsynchronized.ref std_output;
wenzelm@32738
   100
val status_fn = Unsynchronized.ref (fn _: string => ());
wenzelm@14815
   101
wenzelm@14815
   102
fun writeln s = ! writeln_fn (output s);
wenzelm@14815
   103
fun priority s = ! priority_fn (output s);
wenzelm@14815
   104
fun tracing s = ! tracing_fn (output s);
wenzelm@14815
   105
fun warning s = ! warning_fn (output s);
wenzelm@25845
   106
fun error_msg s = ! error_fn (output s);
wenzelm@25845
   107
fun prompt s = ! prompt_fn (output s);
wenzelm@27605
   108
fun status s = ! status_fn (output s);
aspinall@15190
   109
wenzelm@37784
   110
fun legacy_feature s = warning ("Legacy feature! " ^ s);
wenzelm@22826
   111
wenzelm@32966
   112
fun no_warnings_CRITICAL f = setmp_CRITICAL warning_fn (K ()) f;
wenzelm@16191
   113
wenzelm@32738
   114
val debugging = Unsynchronized.ref false;
wenzelm@22130
   115
fun debug s = if ! debugging then ! debug_fn (output (s ())) else ()
aspinall@15190
   116
wenzelm@14815
   117
wenzelm@14815
   118
wenzelm@14815
   119
(** timing **)
wenzelm@14815
   120
wenzelm@25686
   121
(*conditional timing with message*)
wenzelm@25686
   122
fun cond_timeit flag msg e =
wenzelm@14815
   123
  if flag then
wenzelm@23862
   124
    let
wenzelm@23862
   125
      val start = start_timing ();
wenzelm@25686
   126
      val result = Exn.capture e ();
wenzelm@30187
   127
      val end_msg = #message (end_timing start);
wenzelm@25686
   128
      val _ = warning (if msg = "" then end_msg else msg ^ "\n" ^ end_msg);
wenzelm@25686
   129
    in Exn.release result end
wenzelm@25686
   130
  else e ();
haftmann@25667
   131
wenzelm@23862
   132
(*unconditional timing*)
wenzelm@25686
   133
fun timeit e = cond_timeit true "" e;
wenzelm@14815
   134
wenzelm@14815
   135
(*timed application function*)
wenzelm@14815
   136
fun timeap f x = timeit (fn () => f x);
wenzelm@25686
   137
fun timeap_msg msg f x = cond_timeit true msg (fn () => f x);
wenzelm@25686
   138
wenzelm@25686
   139
(*global timing mode*)
wenzelm@32738
   140
val timing = Unsynchronized.ref false;
wenzelm@14815
   141
wenzelm@14815
   142
end;
wenzelm@14815
   143
wenzelm@32738
   144
structure Basic_Output: BASIC_OUTPUT = Output;
wenzelm@32738
   145
open Basic_Output;