Do all the things like ++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatarSign Up
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple APILearn More
Lensflare606285dDo you mean variadic?
Lensflare606285dThe real pain begins when you try to support multiplayer over network. 😄
Fast-Nop4000085dAbout the only reason I've ever used variable argument lists in C is interfacing some sort of printf for loggers or so.
Outside of that, real function prototypes are the usual solution, and only if that totally doesn't work, using some void pointer along with data length and format indicator, but that's already dirty.
AleCx042814985d@Fast-Nop ding ding ding, that is exactly what I am doing, a custom logger. This + macro programming which are in C completely new things to me are making my head spin. I'll get to it eventually through practice. For right now I have covered all different levels of logging that I could need for what I am doing, might be plenty of room to upgrade, but for right now I am content and will probably go night night
Hazarth659385dThey start making sense when you work with them enough. I wrote several Logging engines for my own game engines back in the day... that being said I remember nothing, but it sure was cool!
I have a logger in my c project and tried this to match the printf syntax but quickly gave up, dont get how those arg lists work
AleCx042814984d@ArtOfBBQ I combined the only way I can realistically think about macros and varargs from a language that is not Lisp into something more modern. Basically, I toyed with the idea of decorators in Python, since they made me think of macros. Then in a header file (log.h) I defined the levels for the log in an enum (fatal, error and debug for now)
I have a function declaration that takes the level, a const char for the message and the varidic arguments.
After that for each one of the log levels, I created a macro function declaration that would "extend"?(dunno the correct word) the actual log function depending on the use casa, FLOG for fatal level ELOG etc etc.
The syntax tripped me out:
#define FLOG(message, ...) logger(LL_FATAL, message, ##__VA_ARGS__)
Then on the actual log.c file I just filled in the rest from where I got the argument lists, the syntax for that is not as un-intutive as I thought and based myself on the specification for fprintf
AleCx042814984d@ArtOfBBQ but to be fair it was quite fucky to figure this out, specially since A) I am no C expert, and B) I have no clue what I am doing when it comes to C development. I approach it with a high level of respect.
But understanding what can be done through macros gives me an idea as to how a lot of custom shit can be done. I even managed to create it's own assertion and detection mechanism that can even point at errors as they appear in specific files per line in which the code is showing.
I can't say I can replicate this in other environments, safe for Lisp in which code introspection is really doable, but that is a different use case.
C is complex, but not complicated, one just really needs to know what they are doing.
@AleCx04 C is very easy actually i think it's the best language and we've been going down the wrong path ever since
but yeah i don't really like the variable argument length syntax thing, I ended up with this
log_append("Variable i is currently: ");
printf("variable i is currently: %u", i);
for me it's fine and i can focus on something else
Fast-Nop4000084d@ArtOfBBQ The approach with your two function calls falls flat as soon as multithreading is involved - unless you also slap a mutex around them, and that becomes bulky if you have to do that in every call.
Here's what I used for a simple logger. It just prints to stdout, or errors to stderr, and relies on a logger-static variable "verbosity" which can be set via another setter function.