From 2ec220c1de0f2b92adb7546d50d528134deda19e Mon Sep 17 00:00:00 2001 From: Arpad Ryszka Date: Sat, 18 Apr 2026 04:09:12 +0200 Subject: [PATCH] support printing to a logger --- go.mod | 2 +- notation.go | 126 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 83 insertions(+), 45 deletions(-) diff --git a/go.mod b/go.mod index 92220e8..220a2cc 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module code.squareroundforest.org/arpio/notation -go 1.15 +go 1.26.0 diff --git a/notation.go b/notation.go index 3173353..859d16e 100644 --- a/notation.go +++ b/notation.go @@ -41,7 +41,7 @@ type pending struct { } type node struct { - parts []interface{} + parts []any len int wrapLen wrapLen fullWrap wrapLen @@ -77,7 +77,7 @@ type writer struct { var stderr io.Writer = os.Stderr -func nodeOf(parts ...interface{}) node { +func nodeOf(parts ...any) node { return node{parts: parts} } @@ -97,7 +97,7 @@ func (s str) String() string { return s.val } -func (w *writer) write(o interface{}) { +func (w *writer) write(o any) { if w.err != nil { return } @@ -140,7 +140,7 @@ func config(name string, dflt int) int { return v } -func fprintValues(w io.Writer, o opts, v []interface{}) (int, error) { +func fprintValues(w io.Writer, o opts, v []any) (int, error) { tab := config("TABWIDTH", 8) cols0 := config("LINEWIDTH", 80-tab) cols1 := config("LINEWIDTH1", (cols0+tab)*3/2-tab) @@ -181,7 +181,7 @@ func fprintValues(w io.Writer, o opts, v []interface{}) (int, error) { return wr.n, wr.err } -func fprintlnValues(w io.Writer, o opts, v []interface{}) (int, error) { +func fprintlnValues(w io.Writer, o opts, v []any) (int, error) { n, err := fprintValues(w, o, v) if err != nil { return n, err @@ -191,11 +191,11 @@ func fprintlnValues(w io.Writer, o opts, v []interface{}) (int, error) { return n + nn, err } -func printValues(o opts, v []interface{}) (int, error) { +func printValues(o opts, v []any) (int, error) { return fprintValues(stderr, o, v) } -func printlnValues(o opts, v []interface{}) (int, error) { +func printlnValues(o opts, v []any) (int, error) { n, err := fprintValues(stderr, o, v) if err != nil { return n, err @@ -205,7 +205,7 @@ func printlnValues(o opts, v []interface{}) (int, error) { return n + nn, err } -func sprintValues(o opts, v []interface{}) string { +func sprintValues(o opts, v []any) string { var b bytes.Buffer fprintValues(&b, o, v) return b.String() @@ -213,182 +213,220 @@ func sprintValues(o opts, v []interface{}) string { // Fprint prints the provided objects to the provided writer. When multiple objects are printed, they'll be // separated by a space. -func Fprint(w io.Writer, v ...interface{}) (int, error) { +func Fprint(w io.Writer, v ...any) (int, error) { return fprintValues(w, none, v) } // Fprintw prints the provided objects to the provided writer, with wrapping (and indentation) where necessary. // When multiple objects are printed, they'll be separated by a newline. -func Fprintw(w io.Writer, v ...interface{}) (int, error) { +func Fprintw(w io.Writer, v ...any) (int, error) { return fprintValues(w, wrap, v) } // Fprintt prints the provided objects to the provided writer with moderate type information. When multiple // objects are printed, they'll be separated by a space. -func Fprintt(w io.Writer, v ...interface{}) (int, error) { +func Fprintt(w io.Writer, v ...any) (int, error) { return fprintValues(w, types, v) } // Fprintwt prints the provided objects to the provided writer, with wrapping (and indentation) where necessary, // and with moderate type information. When multiple objects are printed, they'll be separated by a newline. -func Fprintwt(w io.Writer, v ...interface{}) (int, error) { +func Fprintwt(w io.Writer, v ...any) (int, error) { return fprintValues(w, wrap|types, v) } // Fprintv prints the provided objects to the provided writer with verbose type information. When multiple // objects are printed, they'll be separated by a space. -func Fprintv(w io.Writer, v ...interface{}) (int, error) { +func Fprintv(w io.Writer, v ...any) (int, error) { return fprintValues(w, allTypes, v) } // Fprintwv prints the provided objects to the provided writer, with wrapping (and indentation) where necessary, // and with verbose type information. When multiple objects are printed, they'll be separated by a newline. -func Fprintwv(w io.Writer, v ...interface{}) (int, error) { +func Fprintwv(w io.Writer, v ...any) (int, error) { return fprintValues(w, wrap|allTypes, v) } // Fprintln prints the provided objects to the provided writer with a closing newline. When multiple objects are printed, they'll be // separated by a space. -func Fprintln(w io.Writer, v ...interface{}) (int, error) { +func Fprintln(w io.Writer, v ...any) (int, error) { return fprintlnValues(w, none, v) } // Fprintlnw prints the provided objects to the provided writer with a closing newline, with wrapping (and indentation) where necessary. // When multiple objects are printed, they'll be separated by a newline. -func Fprintlnw(w io.Writer, v ...interface{}) (int, error) { +func Fprintlnw(w io.Writer, v ...any) (int, error) { return fprintlnValues(w, wrap, v) } // Fprintlnt prints the provided objects to the provided writer with a closing newline, with moderate type information. When multiple // objects are printed, they'll be separated by a space. -func Fprintlnt(w io.Writer, v ...interface{}) (int, error) { +func Fprintlnt(w io.Writer, v ...any) (int, error) { return fprintlnValues(w, types, v) } // Fprintlnwt prints the provided objects to the provided writer with a closing newline, with wrapping (and indentation) where necessary, // and with moderate type information. When multiple objects are printed, they'll be separated by a newline. -func Fprintlnwt(w io.Writer, v ...interface{}) (int, error) { +func Fprintlnwt(w io.Writer, v ...any) (int, error) { return fprintlnValues(w, wrap|types, v) } // Fprintv prints the provided objects to the provided writer with a closing newline, with verbose type information. When multiple // objects are printed, they'll be separated by a space. -func Fprintlnv(w io.Writer, v ...interface{}) (int, error) { +func Fprintlnv(w io.Writer, v ...any) (int, error) { return fprintlnValues(w, allTypes, v) } // Fprintlnwv prints the provided objects to the provided writer with a closing newline, with wrapping (and indentation) where necessary, // and with verbose type information. When multiple objects are printed, they'll be separated by a newline. -func Fprintlnwv(w io.Writer, v ...interface{}) (int, error) { +func Fprintlnwv(w io.Writer, v ...any) (int, error) { return fprintlnValues(w, wrap|allTypes, v) } // Print prints the provided objects to stderr. When multiple objects are printed, they'll be separated by a // space. -func Print(v ...interface{}) (int, error) { +func Print(v ...any) (int, error) { return printValues(none, v) } // Printw prints the provided objects to stderr, with wrapping (and indentation) where necessary. When multiple // objects are printed, they'll be separated by a newline. -func Printw(v ...interface{}) (int, error) { +func Printw(v ...any) (int, error) { return printValues(wrap, v) } // Printt prints the provided objects to stderr with moderate type information. When multiple objects are // printed, they'll be separated by a space. -func Printt(v ...interface{}) (int, error) { +func Printt(v ...any) (int, error) { return printValues(types, v) } // Printwt prints the provided objects to stderr, with wrapping (and indentation) where necessary, and with // moderate type information. When multiple objects are printed, they'll be separated by a newline. -func Printwt(v ...interface{}) (int, error) { +func Printwt(v ...any) (int, error) { return printValues(wrap|types, v) } // Printv prints the provided objects to stderr with verbose type information. When multiple objects are // printed, they'll be separated by a space. -func Printv(v ...interface{}) (int, error) { +func Printv(v ...any) (int, error) { return printValues(allTypes, v) } // Printwv prints the provided objects to stderr, with wrapping (and indentation) where necessary, and with // verbose type information. When multiple objects are printed, they'll be separated by a newline. -func Printwv(v ...interface{}) (int, error) { +func Printwv(v ...any) (int, error) { return printValues(wrap|allTypes, v) } // Println prints the provided objects to stderr with a closing newline. When multiple objects are printed, // they'll be separated by a space. -func Println(v ...interface{}) (int, error) { +func Println(v ...any) (int, error) { return printlnValues(none, v) } // Printlnw prints the provided objects to stderr with a closing newline, with wrapping (and indentation) where // necessary. When multiple objects are printed, they'll be separated by a newline. -func Printlnw(v ...interface{}) (int, error) { +func Printlnw(v ...any) (int, error) { return printlnValues(wrap, v) } // Printlnt prints the provided objects to stderr with a closing newline, and with moderate type information. When // multiple objects are printed, they'll be separated by a space. -func Printlnt(v ...interface{}) (int, error) { +func Printlnt(v ...any) (int, error) { return printlnValues(types, v) } // Printlnwt prints the provided objects to stderr with a closing newline, with wrapping (and indentation) where // necessary, and with moderate type information. When multiple objects are printed, they'll be separated by a // newline. -func Printlnwt(v ...interface{}) (int, error) { +func Printlnwt(v ...any) (int, error) { return printlnValues(wrap|types, v) } // Printlnv prints the provided objects to stderr with a closing newline, and with verbose type information. When // multiple objects are printed, they'll be separated by a space. -func Printlnv(v ...interface{}) (int, error) { +func Printlnv(v ...any) (int, error) { return printlnValues(allTypes, v) } // Printlnwv prints the provided objects to stderr with a closing newline, with wrapping (and indentation) where // necessary, and with verbose type information. When multiple objects are printed, they'll be separated by a // newline. -func Printlnwv(v ...interface{}) (int, error) { +func Printlnwv(v ...any) (int, error) { return printlnValues(wrap|allTypes, v) } // Sprint returns the string representation of the Go objects. When multiple objects are provided, they'll be -// seprated by a space. -func Sprint(v ...interface{}) string { +// separated by a space. +func Sprint(v ...any) string { return sprintValues(none, v) } // Sprintw returns the string representation of the Go objects, with wrapping (and indentation) where necessary. -// When multiple objects are provided, they'll be seprated by a newline. -func Sprintw(v ...interface{}) string { +// When multiple objects are provided, they'll be separated by a newline. +func Sprintw(v ...any) string { return sprintValues(wrap, v) } // Sprintt returns the string representation of the Go objects, with moderate type information. When multiple -// objects are provided, they'll be seprated by a space. -func Sprintt(v ...interface{}) string { +// objects are provided, they'll be separated by a space. +func Sprintt(v ...any) string { return sprintValues(types, v) } // Sprintwt returns the string representation of the Go objects, with wrapping (and indentation) where necessary, -// and with moderate type information. When multiple objects are provided, they'll be seprated by a newline. -func Sprintwt(v ...interface{}) string { +// and with moderate type information. When multiple objects are provided, they'll be separated by a newline. +func Sprintwt(v ...any) string { return sprintValues(wrap|types, v) } // Sprintv returns the string representation of the Go objects, with verbose type information. When multiple -// objects are provided, they'll be seprated by a space. -func Sprintv(v ...interface{}) string { +// objects are provided, they'll be separated by a space. +func Sprintv(v ...any) string { return sprintValues(allTypes, v) } // Sprintwv returns the string representation of the Go objects, with wrapping (and indentation) where necessary, -// and with verbose type information. When multiple objects are provided, they'll be seprated by a newline. -func Sprintwv(v ...interface{}) string { +// and with verbose type information. When multiple objects are provided, they'll be separated by a newline. +func Sprintwv(v ...any) string { return sprintValues(wrap|allTypes, v) } + +// Lprint prints the string representation of the Go objects onto the provided logging object. When multiple +// objects are provided, they'll be separated by a space. +func Lprint(t interface{ Log(v ...any) }, v ...any) { + t.Log(Sprint(v...)) +} + +// Lprintw prints the string representation of the Go objects onto the provided logging object, with wrapping +// (and indentation) where necessary. When multiple objects are provided, they'll be separated by a newline. +func Lprintw(t interface{ Log(v ...any) }, v ...any) { + t.Log(Sprintw(v...)) +} + +// Lprintt prints the string representation of the Go objects onto the provided logging object, with moderate +// type information. When multiple objects are provided, they'll be separated by a space. +func Lprintt(t interface{ Log(v ...any) }, v ...any) { + t.Log(Sprintt(v...)) +} + +// Lprintwt prints the string representation of the Go objects onto the provided logging object, with wrapping +// (and indentation) where necessary, and with moderate type information. When multiple objects are provided, +// they'll be separated by a newline. +func Lprintwt(t interface{ Log(v ...any) }, v ...any) { + t.Log(Sprintwt(v...)) +} + +// Lprintv prints the string representation of the Go objects onto the provided logging object, with verbose +// type information. When multiple objects are provided, they'll be separated by a space. +func Lprintv(t interface{ Log(v ...any) }, v ...any) { + t.Log(Sprintv(v...)) +} + +// Lprintwv prints the string representation of the Go objects onto the provided logging object, with wrapping +// (and indentation) where necessary, and with verbose type information. When multiple objects are provided, +// they'll be separated by a newline. +func Lprintwv(t interface{ Log(v ...any) }, v ...any) { + t.Log(Sprintwv(v...)) +}