src/Pure/General/output.ML
author wenzelm
Wed, 06 Jul 2011 20:46:06 +0200
changeset 44563 85388f5570c4
parent 42883 2c3fe3cbebae
child 44622 a41f618c641d
permissions -rw-r--r--
prefer Synchronized.var;
wenzelm@14815
     1
(*  Title:      Pure/General/output.ML
wenzelm@14815
     2
    Author:     Makarius, Hagia Maria Sion Abbey (Jerusalem)
wenzelm@14815
     3
wenzelm@42883
     4
Isabelle output channels.
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 tracing: string -> unit
wenzelm@18682
    11
  val warning: string -> unit
wenzelm@22826
    12
  val legacy_feature: string -> unit
wenzelm@14815
    13
end;
wenzelm@14815
    14
wenzelm@14815
    15
signature OUTPUT =
wenzelm@14815
    16
sig
wenzelm@14815
    17
  include BASIC_OUTPUT
wenzelm@40391
    18
  type output = string
wenzelm@23660
    19
  val default_output: string -> output * int
wenzelm@23660
    20
  val default_escape: output -> string
wenzelm@23660
    21
  val add_mode: string -> (string -> output * int) -> (output -> string) -> unit
wenzelm@23660
    22
  val output_width: string -> output * int
wenzelm@23660
    23
  val output: string -> output
wenzelm@23660
    24
  val escape: output -> string
wenzelm@40026
    25
  val raw_stdout: output -> unit
wenzelm@40026
    26
  val raw_stderr: output -> unit
wenzelm@40026
    27
  val raw_writeln: output -> unit
wenzelm@40393
    28
  structure Private_Hooks:
wenzelm@40393
    29
  sig
wenzelm@40393
    30
    val writeln_fn: (output -> unit) Unsynchronized.ref
wenzelm@40393
    31
    val urgent_message_fn: (output -> unit) Unsynchronized.ref
wenzelm@40393
    32
    val tracing_fn: (output -> unit) Unsynchronized.ref
wenzelm@40393
    33
    val warning_fn: (output -> unit) Unsynchronized.ref
wenzelm@40393
    34
    val error_fn: (output -> unit) Unsynchronized.ref
wenzelm@40393
    35
    val prompt_fn: (output -> unit) Unsynchronized.ref
wenzelm@40393
    36
    val status_fn: (output -> unit) Unsynchronized.ref
wenzelm@40393
    37
    val report_fn: (output -> unit) Unsynchronized.ref
wenzelm@40393
    38
  end
wenzelm@40392
    39
  val urgent_message: string -> unit
wenzelm@25845
    40
  val error_msg: string -> unit
wenzelm@25845
    41
  val prompt: string -> unit
wenzelm@27605
    42
  val status: string -> unit
wenzelm@38492
    43
  val report: string -> unit
wenzelm@14815
    44
end;
wenzelm@14815
    45
wenzelm@14815
    46
structure Output: OUTPUT =
wenzelm@14815
    47
struct
wenzelm@14815
    48
wenzelm@23616
    49
(** print modes **)
wenzelm@14881
    50
wenzelm@23660
    51
type output = string;  (*raw system output*)
wenzelm@23660
    52
wenzelm@23616
    53
fun default_output s = (s, size s);
wenzelm@23660
    54
fun default_escape (s: output) = s;
wenzelm@14955
    55
wenzelm@23616
    56
local
wenzelm@23616
    57
  val default = {output = default_output, escape = default_escape};
wenzelm@44563
    58
  val modes = Synchronized.var "Output.modes" (Symtab.make [("", default)]);
wenzelm@23616
    59
in
wenzelm@44563
    60
  fun add_mode name output escape =
wenzelm@44563
    61
    Synchronized.change modes (Symtab.update_new (name, {output = output, escape = escape}));
wenzelm@23616
    62
  fun get_mode () =
wenzelm@44563
    63
    the_default default
wenzelm@44563
    64
      (Library.get_first (Symtab.lookup (Synchronized.value modes)) (print_mode_value ()));
wenzelm@23616
    65
end;
wenzelm@14815
    66
wenzelm@19265
    67
fun output_width x = #output (get_mode ()) x;
wenzelm@14815
    68
val output = #1 o output_width;
wenzelm@23727
    69
wenzelm@23616
    70
fun escape x = #escape (get_mode ()) x;
wenzelm@14815
    71
wenzelm@14815
    72
wenzelm@14815
    73
wenzelm@14815
    74
(** output channels **)
wenzelm@14815
    75
wenzelm@40026
    76
(* raw output primitives -- not to be used in user-space *)
wenzelm@14984
    77
wenzelm@40026
    78
fun raw_stdout s = (TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut);
wenzelm@40026
    79
fun raw_stderr s = (TextIO.output (TextIO.stdErr, s); TextIO.flushOut TextIO.stdErr);
wenzelm@14815
    80
wenzelm@40026
    81
fun raw_writeln "" = ()
wenzelm@40026
    82
  | raw_writeln s = raw_stdout (suffix "\n" s);  (*atomic output!*)
wenzelm@14815
    83
wenzelm@14984
    84
wenzelm@14984
    85
(* Isabelle output channels *)
wenzelm@14984
    86
wenzelm@40393
    87
structure Private_Hooks =
wenzelm@40393
    88
struct
wenzelm@40393
    89
  val writeln_fn = Unsynchronized.ref raw_writeln;
wenzelm@40393
    90
  val urgent_message_fn = Unsynchronized.ref (fn s => ! writeln_fn s);
wenzelm@40393
    91
  val tracing_fn = Unsynchronized.ref (fn s => ! writeln_fn s);
wenzelm@40393
    92
  val warning_fn = Unsynchronized.ref (raw_stdout o suffix "\n" o prefix_lines "### ");
wenzelm@40393
    93
  val error_fn = Unsynchronized.ref (raw_stdout o suffix "\n" o prefix_lines "*** ");
wenzelm@40393
    94
  val prompt_fn = Unsynchronized.ref raw_stdout;
wenzelm@40393
    95
  val status_fn = Unsynchronized.ref (fn _: string => ());
wenzelm@40393
    96
  val report_fn = Unsynchronized.ref (fn _: string => ());
wenzelm@40393
    97
end;
wenzelm@14815
    98
wenzelm@40393
    99
fun writeln s = ! Private_Hooks.writeln_fn (output s);
wenzelm@40393
   100
fun urgent_message s = ! Private_Hooks.urgent_message_fn (output s);
wenzelm@40393
   101
fun tracing s = ! Private_Hooks.tracing_fn (output s);
wenzelm@40393
   102
fun warning s = ! Private_Hooks.warning_fn (output s);
wenzelm@40393
   103
fun error_msg s = ! Private_Hooks.error_fn (output s);
wenzelm@40393
   104
fun prompt s = ! Private_Hooks.prompt_fn (output s);
wenzelm@40393
   105
fun status s = ! Private_Hooks.status_fn (output s);
wenzelm@40393
   106
fun report s = ! Private_Hooks.report_fn (output s);
aspinall@15190
   107
wenzelm@37784
   108
fun legacy_feature s = warning ("Legacy feature! " ^ s);
wenzelm@22826
   109
wenzelm@14815
   110
end;
wenzelm@14815
   111
wenzelm@32738
   112
structure Basic_Output: BASIC_OUTPUT = Output;
wenzelm@32738
   113
open Basic_Output;