问题描述
我开始用新的编程语言Swift构建一个IOS应用程序。我设法用的CocoaPods,并能在我AppDelegate.swift成功创建与我CustomLoggerFormatter的DDTTYLogger(Objective-C的),并追加到记录器。
I started to build an IOS app with the new programming language Swift. I managed to use CocoaPods and was able to successfully create the DDTTYLogger with my CustomLoggerFormatter (Objective-C) in my AppDelegate.swift and append it to the loggers.
var customLoggerFormatter = CustomLoggerFormatter()
var consoleLogger: DDTTYLogger = DDTTYLogger.sharedInstance()
consoleLogger.setLogFormatter(customLoggerFormatter)
DDLog.addLogger(consoleLogger)
但问题是,CocoaLumberjack库正在使用预处理器宏对于记录器方法,如 DDLogVerbose(@..)
But the problem is, that the CocoaLumberjack Library is using preprocessor macros for the logger methods like DDLogVerbose(@"..")
这是在DDLog.h中定义的:
Which is defined in the DDLog.h:
#define DDLogVerbose(frmt, ...) LOG_OBJC_MAYBE(LOG_ASYNC_VERBOSE, LOG_LEVEL_DEF, LOG_FLAG_VERBOSE, 0, frmt, ##__VA_ARGS__)
是否有任何解决方法可以使预处理器定义在Swift中工作?或者有没有人尝试类似的东西并取得更大成功?
Is there any workaround to make preprocessor defines work in Swift? Or did anyone try something similar with more success?
推荐答案
好的,我刚刚找到了解决方案。编写一个Objective-C Wrapper类,调用预处理器并提供调用它的方法。
Okay, I just found a solution. Writing an Objective-C Wrapper class calling the preprocessors and offering methods to call it.
希望这有助于其他人面临同样的问题。
Hopefully this will help other people facing the same issues.
我首先创建了一个头文件:
I first created a header file:
@interface DDLogWrapper : NSObject
+ (void) logVerbose:(NSString *)message;
+ (void) logError:(NSString *)message;
+ (void) logInfo:(NSString *)message;
@end
具有相应的实施:
#import <Foundation/Foundation.h>
#import "DDLogWrapper.h"
// Logging Framework Lumberjack
#import "DDLog.h"
#import "DDASLLogger.h"
#import "DDTTYLogger.h"
// Definition of the current log level
#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_ERROR;
#endif
@implementation DDLogWrapper
+ (void) logVerbose:(NSString *)message {
DDLogVerbose(message);
}
+ (void) logError:(NSString *)message {
DDLogError(message);
}
+ (void) logInfo:(NSString *)message {
DDLogInfo(message);
}
@end
重要的是添加DDLogWrapper .h文件到ProjectName-Bridging-Header.h文件,然后你就可以在Swift中实例化DDLogWrapper并调用方法 logVerbose,logError,logInfo。
。
Important is to add the DDLogWrapper.h File to the ProjectName-Bridging-Header.h file and then you are able to instantiate in Swift the DDLogWrapper and call the methods logVerbose, logError, logInfo.
.
使用以下代码我能够做出日志声明:
With the following code I was able to make a log statement:
DDLogWrapper.logVerbose("TEST");
这篇关于CocoaLumberjack与Swift - 调用预处理器宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!