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