raz / commit bc0ed42c

commit
bc0ed42c36983cc62ad359a0a0ec1bec27122488
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
d3185b32
Fmt: eprint, to standard error once Out is flushed

What every tool wrote for its errors: a Buf, Fmt.write, Out.flush and
Sys.write. The buffer is an arena's, so that Fmt brings in no Heap: a
program that doesn't call it is the same size. Library growth that
breaks no program, met in rwalk and rrem.

patch · log

5 files changed, +41 -19

PROPOSALS.md modified +0 -17

@@ -583,23 +583,6 @@
583583
584584 Met in rrem's parser, documenting `(?>x*)`.
585585
586-## A line to standard error
587-
588-A message to standard error takes a `Buf`, `Fmt.write` into it,
589-`Out.flush` (so what was printed before comes first) and `Sys.write`
590-to `Sys.stderr`. Every tool writes its errors so, and each writes the
591-same few lines.
592-
593-The proposal, in `Fmt`, beside `print`:
594-
595-```
596-proc eprint*(f: []U8; args: []Arg); (* as print, to standard error, after Out is flushed *)
597-```
598-
599-It breaks no program.
600-
601-Met in rwalk's `Rwfiles` and rrem's `rre`.
602-
603586 ## The text of an errno
604587
605588 `Sys` returns minus an errno, and a tool that tells the user what

doc/raz-lib.md (history) modified +2 -1

@@ -22,7 +22,7 @@
2222 | `Str` | operations on `[]U8` strings: comparing, hashing, finding, trimming and cutting, case, joining and replacing, UTF-8, numbers to and from text |
2323 | `Bytes` | searches of bytes, many at a time: `index`, `last`, `count` and `find` in a `[]U8`; one file per target where the target has instructions for it |
2424 | `Buf` | a growable byte buffer |
25-| `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)])`, or into a `Buf` with `Fmt.write` |
25+| `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)])`, into a `Buf` with `Fmt.write`, or to standard error with `Fmt.eprint` |
2626 | `List` | a generic singly linked list, `List(T)` |
2727 | `Map` | a generic hash map, `Map(K, V; hash, eq)` |
2828 | `Vec` | a generic growable array, `Vec(T)`, indexed as a slice |
@@ -591,6 +591,7 @@
591591 real(r: F64; places: INT);
592592 end;
593593 proc print*(f: []U8; args: []Arg); (* to standard output *)
594+proc eprint*(f: []U8; args: []Arg); (* to standard error, once Out is flushed *)
594595 proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); (* added to b *)
595596
596597 module List(T);

lib/Fmt.raz (history) modified +25 -1

@@ -3,7 +3,7 @@
33 {{ and }} stand for { and }. A {} with no argument left prints
44 nothing, and arguments left over are not printed. *)
55 module Fmt;
6-import Out, Buf, Arena, Rt;
6+import Out, Buf, Arena, Rt, Sys;
77
88 type
99 (* An argument, and how it is printed. A real has places digits
@@ -121,6 +121,30 @@
121121 proc print*(f: []U8; args: []Arg);
122122 begin
123123 format(nil, f, args);
124+end;
125+
126+(* Writes f to standard error as print writes it, after flushing Out,
127+ so that what was printed before comes first. The text is made whole
128+ in a buffer and written at once, to stay together when threads
129+ write at once. The buffer is an arena's, not the heap's, so that a
130+ program that doesn't call eprint carries no heap for it; its pages
131+ are mapped as they are written, and a text of more than a megabyte
132+ stops the program, as a Buf does when it can't grow. *)
133+proc eprint*(f: []U8; args: []Arg);
134+var ar: Arena.Arena; b: Buf.Buf; s: []U8; n: INT;
135+begin
136+ Out.flush;
137+ if not Arena.init(ADR(ar), 1 shl 20) then Rt.stop("Fmt.eprint: no arena"); end;
138+ Buf.init(ADR(b), ADR(ar.base));
139+ format(ADR(b), f, args);
140+ s := Buf.bytes(ADR(b));
141+ while LEN(s) > 0 do
142+ n := Sys.write(Sys.stderr, s);
143+ if n > 0 then s := s[n..LEN(s)];
144+ elsif n # -4 then break; (* -EINTR: try again *)
145+ end;
146+ end;
147+ Arena.done(ADR(ar));
124148 end;
125149
126150 (* Appends f to b, each {} replaced by the next of args. *)

test/run/Eprint.expect (history) added +1 -0

@@ -0,0 +1 @@
1+printed first, then 2

test/run/Eprint.raz (history) added +13 -0

@@ -0,0 +1,13 @@
1+(* Fmt.eprint flushes Out first: with standard error made standard
2+ output, what was printed before comes before it. *)
3+module Eprint;
4+import Fmt, Out, Sys;
5+
6+proc main*;
7+begin
8+ _ := Sys.dupTo(Sys.stdout, Sys.stderr);
9+ Out.str("printed first, ");
10+ Fmt.eprint("{} {}\n", [Fmt.str("then"), Fmt.int(2)]);
11+end;
12+
13+end Eprint.