-
Notifications
You must be signed in to change notification settings - Fork 149
Description
I often find it tedious to type a lot just for simple logging while debugging, especially when dealing with structs.
So I thought it would be cool to have a custom printf in nob.h. What do you think?
However, it uses things like X Macros, so the code isn't exactly pretty.
The implementation looks like this, and if added to nob.h, it could be used like this:
main.c
typedef struct {
const char* name;
size_t age;
} Person;
typedef struct {
Person* items;
size_t count;
size_t capacity;
} People;
#define NOB_CFMT_FORMATS \
NOB_CFMT_X('p', Person, "{name: %s, age: %zu}", NOB_CFMT_VAR.name, NOB_CFMT_VAR.age)
#define NOB_IMPLEMENTATION
#include "nob.h"
int main(int argc, char** argv) {
NOB_GO_REBUILD_URSELF(argc, argv);
People people = { 0, };
nob_da_append(&people, ((Person){ .name = "John Doe", .age = 26 }));
nob_da_append(&people, ((Person){ .name = "Jane Doe", .age = 24 }));
printf("Here's our people:\n");
nob_da_foreach(Person, it, &people) {
nob_cprintf(" $p\n", *it);
}
return 0;
}output
Here's our people:
{name: John Doe, age: 26}
{name: Jane Doe, age: 24}
Since it relies on macros to substitute formats, the necessary definitions need to be present before including nob.h, which makes it look a bit messy.
Also, because there are many internal macro implementations, I'm not quite sure how to handle NOB_STRIP_PREFIX properly.
(and yes it is because of my skill issue)
P.S. Thank you for your wonderful streaming. I really have learned a lot from it.