gpt4 book ai didi

ios - 动态开启 NSLogs

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:19:52 24 4
gpt4 key购买 nike

我目前正在使用以下 DebugLog 宏

#if defined(DEBUG) && defined(useDebugLogs)
#define DebugLog( s, ... ) NSLog( @"<%s:(%d)> %@", __PRETTY_FUNCTION__, __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#else
#define DebugLog( s, ... )
#endif

但是,现在试飞允许远程记录,这有点过时了。

基本上我想要做的是在我的 settings.plist 中添加一个开关,以允许用户打开远程日志记录。

我看过这个教程 http://jomnius.blogspot.com/2011/09/how-to-do-dynamic-debug-logging-in.html

但是,这是一个非常糟糕的解释,而且实际上似乎不起作用。

最佳答案

好吧,IMO 最好的方法是始终发送日志并调整您的实现,以便您准确地看到您想要的。您要么想要分析,要么不会使用它们。有错误,或者没有。通过它们并提高信噪比。反正……

您可以为此创建一个专用函数。它可以根据您实现中的某些变量有条件地记录或不记录。请注意,您可以通过调用 NSLogv 而不是 NSLog 来转发您的 va_list,或者您可以使用 -[NSString initWithFormat:arguments:] 来转发消息+别处的参数:

MONDebugLog.h

extern void MONDebugLog(NSString * format, ... ) NS_FORMAT_FUNCTION(1,2);

MONDebugLog.m

void MONDebugLog(NSString * format, ... ) {

enum {
MONLog_Undefined,
MONLog_Enable,
MONLog_Disable
};

static int DoLog = MONLog_Undefined;
if (MONLog_Undefined == DoLog) {
DoLog = ...load from plist or defaults and set to Log_Enable or Log_Disable...;
}

if (MONLog_Disable == DoLog) {
return;
}
va_list arguments;
va_start(arguments, format);
NSString * str = [[NSString alloc] initWithFormat:format arguments:arguments];
va_end(arguments);
...now log or xmit str...
}

关于ios - 动态开启 NSLogs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10183087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com