Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何使用宏将变量传递到函数中(目标c)_Objective C_Logging_Macros_Nslog - Fatal编程技术网

Objective c 如何使用宏将变量传递到函数中(目标c)

Objective c 如何使用宏将变量传递到函数中(目标c),objective-c,logging,macros,nslog,Objective C,Logging,Macros,Nslog,有人知道如何动态地将所有变量值传递到函数中,以便记录日志吗 我正在寻找一种简单的方法(比如使用编译器宏)来记录函数以及传递给它的变量值(然后将其写入日志文件,以便我们可以轻松找到导致函数崩溃的输入) 我一直在努力 #define NFDebug( s, ... ) NSLog( @"DEBUG: %s: %@", __PRETTY_FUNCTION__, \ [NSString stringWithFormat:(@"%@"), ##__VA_ARGS__] ) ,它提供了所有内容,但变量值

有人知道如何动态地将所有变量值传递到函数中,以便记录日志吗

我正在寻找一种简单的方法(比如使用编译器宏)来记录函数以及传递给它的变量值(然后将其写入日志文件,以便我们可以轻松找到导致函数崩溃的输入)

我一直在努力

#define NFDebug( s, ... ) NSLog( @"DEBUG: %s: %@", __PRETTY_FUNCTION__, \
[NSString stringWithFormat:(@"%@"), ##__VA_ARGS__] )

,它提供了所有内容,但变量值

#ifdef YOUR_DEBUG_ENABLER_SYMBOL_ONLY_SET_IN_DEBUG_BUILDS
#define DEBUG_ONLY(_code_) _code_
#else
#define DEBUG_ONLY(_code_)
#endif

#define DebugLog(_str, ...) DEBUG_ONLY(NSLog(@"%s: " _str, __func__, ## __VA_ARGS__))
请注意,NSLog中的_str前面没有逗号-这意味着您在调用代码中使用的字符串将(由编译器)附加到“%s:”字符串,成为NSLog的复合格式字符串。要打印的任何参数都可以包含在传入的格式字符串中。%s应用于函数,而您的str应用于其余传入变量:

float f;
int i;
NSString* s;
// Initialise f, i, and s to something
...
// Log the values only when in debug mode, with function name auto-prepended
DebugLog(@"float is: %f - int is: %d - string is: %@", f, i, s);

这样做的好处是,您可以在调试时有条件地记录所需的任何文本和变量,并在输出前自动添加函数名。

为了简单起见,我在上面省略了它,但实际上我有不同的日志级别(DebugLog0、DbugLog1等)因此,您可以设置所需的日志级别,并根据需要进行更多/更少的日志记录。这可以通过两种方式完成:在编译时设置日志级别;或者在运行时设置日志级别。您可以轻松地在此基础上添加/扩展。拥有DebugAssert、DebugWarn、ReleaseLog和releasererror宏也是有意义的。