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.
| @@ -583,23 +583,6 @@ | ||
| 583 | 583 | |
| 584 | 584 | Met in rrem's parser, documenting `(?>x*)`. |
| 585 | 585 | |
| 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 | - | |
| 603 | 586 | ## The text of an errno |
| 604 | 587 | |
| 605 | 588 | `Sys` returns minus an errno, and a tool that tells the user what |
| @@ -22,7 +22,7 @@ | ||
| 22 | 22 | | `Str` | operations on `[]U8` strings: comparing, hashing, finding, trimming and cutting, case, joining and replacing, UTF-8, numbers to and from text | |
| 23 | 23 | | `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 | |
| 24 | 24 | | `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` | | |
| 26 | 26 | | `List` | a generic singly linked list, `List(T)` | |
| 27 | 27 | | `Map` | a generic hash map, `Map(K, V; hash, eq)` | |
| 28 | 28 | | `Vec` | a generic growable array, `Vec(T)`, indexed as a slice | |
| @@ -591,6 +591,7 @@ | ||
| 591 | 591 | real(r: F64; places: INT); |
| 592 | 592 | end; |
| 593 | 593 | proc print*(f: []U8; args: []Arg); (* to standard output *) |
| 594 | +proc eprint*(f: []U8; args: []Arg); (* to standard error, once Out is flushed *) | |
| 594 | 595 | proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); (* added to b *) |
| 595 | 596 | |
| 596 | 597 | module List(T); |
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | {{ and }} stand for { and }. A {} with no argument left prints |
| 4 | 4 | nothing, and arguments left over are not printed. *) |
| 5 | 5 | module Fmt; |
| 6 | -import Out, Buf, Arena, Rt; | |
| 6 | +import Out, Buf, Arena, Rt, Sys; | |
| 7 | 7 | |
| 8 | 8 | type |
| 9 | 9 | (* An argument, and how it is printed. A real has places digits |
| @@ -121,6 +121,30 @@ | ||
| 121 | 121 | proc print*(f: []U8; args: []Arg); |
| 122 | 122 | begin |
| 123 | 123 | 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)); | |
| 124 | 148 | end; |
| 125 | 149 | |
| 126 | 150 | (* Appends f to b, each {} replaced by the next of args. *) |
| @@ -0,0 +1 @@ | ||
| 1 | +printed first, then 2 |
| @@ -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. |