I want to create log.c file with containing info(), debug() and error() functions. These functions are working without printing file name, line number,..etc. So when I call one of this function I want to dump the file name, line number, and function name of the caller. So how do we exactly trace back? Is there a way to trace back on C or, if we use macros, how can this be done?
(C++11) The predefined identifier __func__ is implicitly defined as a string that contains the unqualified and unadorned name of the enclosing function. __func__ is mandated by the C++ standard and is not a Microsoft extension.
Print Function in C, C++, and PythonPrint function is used to display content on the screen. Approach: Some characters are stored in integer value inside printf function. Printing the value as well as the count of the characters.
__LINE__ is a preprocessor macro that expands to current line number in the source file, as an integer. __LINE__ is useful when generating log statements, error messages intended for programmers, when throwing exceptions, or when writing debugging code.
I'd pass the data to the function through parameters (maybe get the help of a macro)
int info(const char *fname, int lineno, const char *fxname, ...) { /* ... */ } int debug(const char *fname, int lineno, const char *fxname, ...) { /* ... */ } int error(const char *fname, int lineno, const char *fxname, ...) { /* ... */ }
And to call them
info(__FILE__, __LINE__, __func__, ...); debug(__FILE__, __LINE__, __func__, ...); error(__FILE__, __LINE__, __func__, ...);
Note: __func__
is C99; gcc, in mode C89 has __FUNCTION__
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With