-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Tprintf to simplify the current log format
Hypcast previously had a bunch of individual "logf" methods to dump out a log prefixed by a type name and pointer address. This change uses a bit of reflection and a bit of rework on the existing type names to extract that into one shared function. There's an interesting mix of generics and reflection here. I'm using the type parameter to enforce that src is a pointer, which guarantees that "%p" will work right and simplifies getting the type name compared to digging through layers of Elem() that will panic with a wrong move. I'm considering moving Hypcast to log/slog. Even if I throw this away soon, it's nice that I'll be able to search for "log." across the whole codebase to see what needs to change.
- Loading branch information
1 parent
3eb419d
commit 3b669fd
Showing
4 changed files
with
51 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Package log provides special log formatting features for Hypcast. | ||
package log | ||
|
||
import ( | ||
"log" | ||
"reflect" | ||
) | ||
|
||
// Tprintf prints its arguments in the manner of [log.Printf], with a prefix of | ||
// the form "Type(0xabcd...)" indicating the element type and address of the src | ||
// pointer. This can be a convenient way to differentiate instances of the same | ||
// type, though addresses are somewhat opaque as identifiers, and revealing them | ||
// might even be a security concern (albeit a far-fetched one). | ||
func Tprintf[T any](src *T, fmt string, v ...any) { | ||
tfmt := "%s(%p): " + fmt | ||
tval := make([]any, len(v)+2) | ||
tval[0], tval[1] = reflect.TypeFor[T]().Name(), src | ||
copy(tval[2:], v) | ||
log.Printf(tfmt, tval...) | ||
} |