美文网首页
集成mars的xlog

集成mars的xlog

作者: 西西的一天 | 来源:发表于2019-12-08 13:57 被阅读0次

    先来介绍一下需求,为xlog写一个adapter,需要在一个由Swift写的Library中使用。

    在明确需求后进行Tasking

    1. 根据mars的官方文档,编译生成Framework
    2. 通过pod lib create 创建一个 Cocoapods的library
    3. 编写adapter,修改podspec文件

    编译

    为了支持bitcode,需要修改build_ios.py文件,将-DENABLE_BITCODE=1
    运行python build_ios.py,选择2:Clean & build xlog. 过程中可能会报错,比如缺失某个工具,我在编译时发现没有安装cmake,通过brew安装一下即可。
    编译完成后生成的Framework在cmake_build/iOS/Darwin.out/mars.framework

    构建pod lib

    pod lib create xlog
    

    根据需要创建相应的template工程

    adapter

    创建一个interface,来隔离实现

    #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
    

    同时将第一步编译好的mars.framwork添加到xlog/目录下,只是添加了文件,那么在pod install时是无法添加进来的,那么就要修改一下podspec文件,来引用这个本地的Framework。同时也需要添加mars所需要的系统库,如:SystemConfiguration,CoreTelephony以及libz等,具体可参考下面的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
    

    为了让项目可以方便的在Swift工程中使用,需要添加modulemap,并在podspec文件中声明:preserve_pathmodule_mappod_target_xcconfig

    framework module xlog {
        header "XLogger.h"
        header "MarsXLogger.h"
        export *
    }
    

    具体mars xlog的实现就相对简单了很多,参考文档即可:

    #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
    

    需要注意的是,当使用mars的函数时,由于mars是用C++写的,OC想要顺利调用,需要将其改为mm文件。
    具体的代码可参考https://github.com/iossocket/xlog。想要把它push到Cocoapods master有点。。。所以可以把它放在私有的Cocoapods repo中,供项目内部使用。

    相关文章

      网友评论

          本文标题:集成mars的xlog

          本文链接:https://www.haomeiwen.com/subject/lmypgctx.html