From bc0ed42c36983cc62ad359a0a0ec1bec27122488 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 12:02:22 +0100 Subject: [PATCH] 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. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 8ca650a..748ea34 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -583,23 +583,6 @@ Met in rrem's parser, documenting `(?>x*)`. -## A line to standard error - -A message to standard error takes a `Buf`, `Fmt.write` into it, -`Out.flush` (so what was printed before comes first) and `Sys.write` -to `Sys.stderr`. Every tool writes its errors so, and each writes the -same few lines. - -The proposal, in `Fmt`, beside `print`: - -``` -proc eprint*(f: []U8; args: []Arg); (* as print, to standard error, after Out is flushed *) -``` - -It breaks no program. - -Met in rwalk's `Rwfiles` and rrem's `rre`. - ## The text of an errno `Sys` returns minus an errno, and a tool that tells the user what diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 0b389a8..71d688f 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -22,7 +22,7 @@ | `Str` | operations on `[]U8` strings: comparing, hashing, finding, trimming and cutting, case, joining and replacing, UTF-8, numbers to and from text | | `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 | | `Buf` | a growable byte buffer | -| `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)])`, or into a `Buf` with `Fmt.write` | +| `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` | | `List` | a generic singly linked list, `List(T)` | | `Map` | a generic hash map, `Map(K, V; hash, eq)` | | `Vec` | a generic growable array, `Vec(T)`, indexed as a slice | @@ -591,6 +591,7 @@ real(r: F64; places: INT); end; proc print*(f: []U8; args: []Arg); (* to standard output *) +proc eprint*(f: []U8; args: []Arg); (* to standard error, once Out is flushed *) proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); (* added to b *) module List(T); diff --git a/lib/Fmt.raz b/lib/Fmt.raz index 3e8f24f..516fd71 100644 --- a/lib/Fmt.raz +++ b/lib/Fmt.raz @@ -3,7 +3,7 @@ {{ and }} stand for { and }. A {} with no argument left prints nothing, and arguments left over are not printed. *) module Fmt; -import Out, Buf, Arena, Rt; +import Out, Buf, Arena, Rt, Sys; type (* An argument, and how it is printed. A real has places digits @@ -121,6 +121,30 @@ proc print*(f: []U8; args: []Arg); begin format(nil, f, args); +end; + +(* Writes f to standard error as print writes it, after flushing Out, + so that what was printed before comes first. The text is made whole + in a buffer and written at once, to stay together when threads + write at once. The buffer is an arena's, not the heap's, so that a + program that doesn't call eprint carries no heap for it; its pages + are mapped as they are written, and a text of more than a megabyte + stops the program, as a Buf does when it can't grow. *) +proc eprint*(f: []U8; args: []Arg); +var ar: Arena.Arena; b: Buf.Buf; s: []U8; n: INT; +begin + Out.flush; + if not Arena.init(ADR(ar), 1 shl 20) then Rt.stop("Fmt.eprint: no arena"); end; + Buf.init(ADR(b), ADR(ar.base)); + format(ADR(b), f, args); + s := Buf.bytes(ADR(b)); + while LEN(s) > 0 do + n := Sys.write(Sys.stderr, s); + if n > 0 then s := s[n..LEN(s)]; + elsif n # -4 then break; (* -EINTR: try again *) + end; + end; + Arena.done(ADR(ar)); end; (* Appends f to b, each {} replaced by the next of args. *) diff --git a/test/run/Eprint.expect b/test/run/Eprint.expect new file mode 100644 index 0000000..08ca661 --- /dev/null +++ b/test/run/Eprint.expect @@ -0,0 +1 @@ +printed first, then 2 diff --git a/test/run/Eprint.raz b/test/run/Eprint.raz new file mode 100644 index 0000000..a05d4b9 --- /dev/null +++ b/test/run/Eprint.raz @@ -0,0 +1,13 @@ +(* Fmt.eprint flushes Out first: with standard error made standard + output, what was printed before comes before it. *) +module Eprint; +import Fmt, Out, Sys; + +proc main*; +begin + _ := Sys.dupTo(Sys.stdout, Sys.stderr); + Out.str("printed first, "); + Fmt.eprint("{} {}\n", [Fmt.str("then"), Fmt.int(2)]); +end; + +end Eprint.