3

我想使用 CocoaLumberjack 并尝试将其插入到ddLogLevel const我的 .pch 文件中:

#if DEBUG
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_INFO;
#endif

但是,由于我使用的是 XMPP 框架,并且使用了 CocoaLumberjack,所以我遇到了Redefinition of 'ddLogLevel'错误,因为这些类包含与上面完全相同的const定义。

我绝对不想ddLogLevel在我的每个类中都定义以避免这种情况。我怎样才能解决这个问题?

4

3 回答 3

2

你可以在它周围加一个守卫。像这样的东西:

#ifndef ddLogLevel
#if DEBUG
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_INFO;
#endif //DEBUG
#endif //ddLogLevel

如果您不能使用 ddLogLevel 作为保护:(现在无法测试)

#ifndef DDLOGLEVEL
#if DEBUG
#define DDLOGLEVEL
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_INFO;
#endif //DEBUG
#endif //DDLOGLEVEL

我希望它有效。

于 2013-05-15T05:05:05.217 回答
1

我认为答案是不要将 ddLogLevel 声明为静态(如本指南中指出的那样https://github.com/CocoaLumberjack/CocoaLumberjack/wiki/XcodeTricks

相反,请遵循cocoalumberjack 的此全局日志级别

这类似于 MagicalRecord 遇到Magical Record 获得 ddLogLevel 的所有权

常量.h

extern int const ddLogLevel;

常数.m

#import <CocoaLumberjack/DDLog.h>
#ifdef DEBUG
    int const ddLogLevel = LOG_LEVEL_VERBOSE;
#else
    int const ddLogLevel = LOG_LEVEL_WARN;
#endif

另外,有些人似乎不明static白头文件中关键字的含义,所以请阅读头文件中的变量声明 - 是否静态?

于 2014-08-10T17:23:01.480 回答
0

将定义包装在预处理器指令中:

#ifndef DEFINED_DD_LOG_LEVEL
#define DEFINED_DD_LOG_LEVEL
#  if DEBUG
...
#  endif // DEBUG
#endif // DEFINED_DD_LOG_LEVEL
于 2013-05-15T05:07:56.947 回答