Does anyone know how to dynamically get all the variables values passed into a function for the sake of logging ?
I’m looking for a simple way (like using a compiler macro) to be able to log the function, and the variable values passed into it (which will then be written to a log file so we can easily find inputs that cause functions to crash)
I’ve been trying
#define NFDebug( s, ... ) NSLog( @"DEBUG: %s: %@", __PRETTY_FUNCTION__, \
[NSString stringWithFormat:(@"%@"), ##__VA_ARGS__] )
,which gives everything, BUT the variables values
I use something like this:
Note that there is no comma before the _str in the NSLog – this means the string you use in your calling code is appended (by the compiler) to the “%s: ” string to become a composite format string for the NSLog. Whatever parameters you want to print can be included in your passed in format string. The %s applies to the _ _ func _ _ and your _str applies to the rest of the passed in variables:
This has the advantage that you can log whatever text and variables you want, conditionally on debug, and with the function name automatically prepended onto the output.