先來介紹一下需求淑玫,為xlog寫一個(gè)adapter客峭,需要在一個(gè)由Swift寫的Library中使用。
在明確需求后進(jìn)行Tasking
- 根據(jù)mars的官方文檔陋葡,編譯生成Framework
- 通過pod lib create 創(chuàng)建一個(gè) Cocoapods的library
- 編寫adapter,修改podspec文件
編譯
為了支持bitcode彻采,需要修改build_ios.py文件腐缤,將-DENABLE_BITCODE=1
,
運(yùn)行python build_ios.py
肛响,選擇2:Clean & build xlog. 過程中可能會(huì)報(bào)錯(cuò)岭粤,比如缺失某個(gè)工具,我在編譯時(shí)發(fā)現(xiàn)沒有安裝cmake特笋,通過brew安裝一下即可剃浇。
編譯完成后生成的Framework在cmake_build/iOS/Darwin.out/mars.framework
構(gòu)建pod lib
pod lib create xlog
根據(jù)需要?jiǎng)?chuàng)建相應(yīng)的template工程
adapter
創(chuàng)建一個(gè)interface,來隔離實(shí)現(xiàn)
#import <Foundation/Foundation.h>
@protocol XLogger <NSObject>
@required
- (void)info:(NSString *)tag msg:(NSString *)msg;
- (void)warning:(NSString *)tag msg:(NSString *)msg;
- (void)error:(NSString *)tag msg:(NSString *)msg;
- (void)flush;
@end
同時(shí)將第一步編譯好的mars.framwork添加到xlog/目錄下猎物,只是添加了文件虎囚,那么在pod install時(shí)是無法添加進(jìn)來的,那么就要修改一下podspec文件蔫磨,來引用這個(gè)本地的Framework淘讥。同時(shí)也需要添加mars所需要的系統(tǒng)庫,如:SystemConfiguration堤如,CoreTelephony以及l(fā)ibz等蒲列,具體可參考下面的podspec文件
Pod::Spec.new do |s|
s.name = 'xlog'
s.version = '0.1.0'
s.summary = 'A short description of xlog.'
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://github.com/iossocket/xlog'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'iossocket' => 'avx302@gmail.com' }
s.source = { :git => 'https://github.com/iossocket/xlog.git', :tag => s.version.to_s }
s.ios.deployment_target = '10.0'
s.source_files = 'xlog/Classes/**/*.{h,m,mm}'
s.public_header_files = 'xlog/Classes/**/*.h'
s.vendored_frameworks = 'xlog/mars.framework'
s.preserve_path = 'xlog/Classes/xlog.modulemap'
s.module_map = 'xlog/Classes/xlog.modulemap'
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
s.libraries = 'resolv.9', 'z'
s.frameworks = 'SystemConfiguration', 'CoreTelephony'
s.dependency 'SSZipArchive'
end
為了讓項(xiàng)目可以方便的在Swift工程中使用,需要添加modulemap搀罢,并在podspec文件中聲明:preserve_path
蝗岖,module_map
,pod_target_xcconfig
榔至。
framework module xlog {
header "XLogger.h"
header "MarsXLogger.h"
export *
}
具體mars xlog的實(shí)現(xiàn)就相對(duì)簡(jiǎn)單了很多抵赢,參考文檔即可:
#import <Foundation/Foundation.h>
@protocol XLogger <NSObject>
@required
- (void)info:(NSString *)tag msg:(NSString *)msg;
- (void)warning:(NSString *)tag msg:(NSString *)msg;
- (void)error:(NSString *)tag msg:(NSString *)msg;
- (void)flush;
@end
#import "MarsXLogger.h"
#import <mars/xlog/xlogger.h>
#import <mars/xlog/xloggerbase.h>
#import <mars/xlog/appender.h>
#import "Utils/LogUtil.h"
@interface MarsXLogger()
@property(nonatomic, strong) NSString *logFolderPath;
@end
@implementation MarsXLogger
+ (instancetype)sharedInstance {
static MarsXLogger *logger = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
logger = [self new];
});
return logger;
}
- (instancetype)init {
if (self = [super init]) {
self.logFolderPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:@"/marslogger"];
NSLog(@"%@", self.logFolderPath);
xlogger_SetLevel(kLevelInfo);
#ifdef DEBUG
appender_set_console_log(true);
#endif
appender_open(kAppednerAsync, [self.logFolderPath UTF8String], "marslogger", "");
}
return self;
}
- (void)info:(NSString *)tag msg:(NSString *)msg {
LOG_INFO([tag UTF8String], msg);
}
- (void)warning:(NSString *)tag msg:(NSString *)msg {
LOG_WARNING([tag UTF8String], msg);
}
- (void)error:(NSString *)tag msg:(NSString *)msg {
LOG_ERROR([tag UTF8String], msg);
}
- (void)flush {
appender_flush();
}
@end
需要注意的是,當(dāng)使用mars的函數(shù)時(shí),由于mars是用C++寫的铅鲤,OC想要順利調(diào)用划提,需要將其改為mm文件。
具體的代碼可參考https://github.com/iossocket/xlog彩匕。想要把它push到Cocoapods master有點(diǎn)腔剂。媒区。驼仪。所以可以把它放在私有的Cocoapods repo中,供項(xiàng)目?jī)?nèi)部使用袜漩。