3

I want to use CocoaLumberjack and am trying to insert the ddLogLevel const in my .pch file:

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

However, since I'm using XMPP framework, and that uses CocoaLumberjack, I'm getting Redefinition of 'ddLogLevel' errors since those classes contain the exact same const definitions as above.

I definitely don't want to define ddLogLevel in every one of my classes to avoid this. How can I get around this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
OdieO
  • 6,836
  • 7
  • 56
  • 88

3 Answers3

2

You could add a guard around it. Something like this:

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

If you cannot use ddLogLevel as a guard: (cannot test it right now)

#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

I hope it works.

Burkhard
  • 14,596
  • 22
  • 87
  • 108
  • Hmm, so now I learned the syntax for #ifndef, however, same error. My presumption is that if I put this in my .pch, then ddLogLevel isn't defined yet anywhere. The .pch code goes to the top of each class, right? So when the XMPPFramework files define ddLogLevel, that's where I get the error. And there are A LOT of XMPPFramework classes. I don't want to have to alter them. – OdieO May 15 '13 at 05:13
  • @SmoothAlmonds: why does it need to be a .pch? Can't you just use a normal .h (then it would be inserted via #include in every file)? – Burkhard May 15 '13 at 05:23
  • So if I use a normal .h, the only way to get that automatically into every file would be to put that header into the .pch, right? Thing is, I want to use DDLog in every class, so I'd like it to be automatically available without manually including it. Am I understanding you wrong? – OdieO May 15 '13 at 05:29
  • I think you will need to include the header file for it to be available. You cannot use a varible or constant if it is not declared in the scope of where you want to use it. – Burkhard May 15 '13 at 05:44
1

I think the answer is to not to declare ddLogLevel as static (as pointed out in this guide https://github.com/CocoaLumberjack/CocoaLumberjack/wiki/XcodeTricks)

Instead, follow this Global log level for cocoalumberjack

This is similar to what MagicalRecord encounters Magical Record takes ownership of ddLogLevel

Constant.h

extern int const ddLogLevel;

Constant.m

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

Also, some people seem to not understand what static keyword means in header files, so read this Variable declarations in header files - static or not?

Community
  • 1
  • 1
onmyway133
  • 45,645
  • 31
  • 257
  • 263
0

Wrap the definition in a preprocessor directive:

#ifndef DEFINED_DD_LOG_LEVEL
#define DEFINED_DD_LOG_LEVEL
#  if DEBUG
...
#  endif // DEBUG
#endif // DEFINED_DD_LOG_LEVEL
GoZoner
  • 67,920
  • 20
  • 95
  • 145