novastone-media / MQTT-Client-Framework

iOS, macOS, tvOS native ObjectiveC MQTT Client Framework
Other
1.84k stars 463 forks source link

Conflicting definitions with CocoaLumberjack (using Carthage) #531

Open silverhammermba opened 5 years ago

silverhammermba commented 5 years ago

Short description

I can't get this to work with an app that is already using CocoaLumberjack 3.5.3 via Carthage because it causes conflicting definitions.

I set up my logging like this in Logging.h

#define LOG_LEVEL_DEF ddLogLevel
@import CocoaLumberjack;
static const DDLogLevel ddLogLevel = DDLogLevelError;

Then in my view controller

#import "Logging.h"
@import MQTTClient;
//...
- (void)viewDidLoad {
    [super viewDidLoad];
    DDLogError(@"error!");
}

That DDLogError gives me two errors:

  1. "Reference to 'ddLogLevel' is ambiguous". It sees two definitions: one is the constant I defined, the other one is extern DDLogLevel ddLogLevel; from MQTTLog.h
  2. "Ambiguous expansion of macro 'DDLogError'". It sees two definitions: one is the correct one from CocoaLumberjack, the other one is this shim from MQTTLog.h #define DDLogError if (ddLogLevel & DDLogFlagError) NSLog

If I remove the @import MQTTClient; it builds and logging works. If I remove the #import "Logging.h" it also builds, but I can see that it isn't using CocoaLumberjack because it no longer respects my logging level.

Environment

Steps to reproduce

  1. Create a single view app
  2. Add CocoaLumberjack and MQTTClient via Carthage
  3. Add some DDLog calls
  4. In the same file where you are using DDLog, @import MQTTClient;

Expected behaviour

There should be some way to import CocoaLumberjack and MQTTClient via Carthage and have MQTTClient use my CocoaLumberjack config.

Actual behaviour

Currently I can only find a way to use one or the other, not both.

Other information

I see that there's a #ifdef LUMBERJACK in MQTTLog.h. I tried adding #define LUMBERJACK before I import MQTTClient, but it makes no difference. What does work is if I actually delete the contents of MQTTLog.h, then it builds fine.

Also, not sure if related, even when I import MQTTClient on its own, I can't get it to log anything. For example, I see that it's supposed to log an error if I try to open an base MQTTTransport instance, but I don't see those logs appearing.

jcavar commented 5 years ago

Hello, thanks for your feedback, can you try this:

#undef LOG_LEVEL_DEF
#define LOG_LEVEL_DEF myLogLevel
static const int myLogLevel = DDLogLevelVerbose;

[MQTTLog setLogLevel:myLogLevel];
VincentSit commented 5 years ago

This approach does not work for me. I have my ddLogLevel in the pch file.

VincentSit commented 5 years ago

Temporary workaround:

Remove #import <MQTTClient/MQTTLog.h> from MQTTClient.h.

If you need to set the LogLevel:

[NSClassFromString(@"MQTTLog") performSelector:@selector(setLogLevel:) withObject:@(ddLogLevel)];
silverhammermba commented 5 years ago

@jcavar Your suggestion doesn't work. That does enable logging, but it isn't using CocoaLumberjack. I verified by setting breakpoints that it's just using MQTT's shim macros and never hitting any DDLog method.

ackratos commented 4 years ago

Temporary workaround:

Remove #import <MQTTClient/MQTTLog.h> from MQTTClient.h.

If you need to set the LogLevel:

[NSClassFromString(@"MQTTLog") performSelector:@selector(setLogLevel:) withObject:@(ddLogLevel)];

thx