加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在swift-ios文件中使用asl.h

发布时间:2020-12-14 05:39:26 所属栏目:百科 来源:网络整理
导读:我是ios / swift的新手.我想使用asl.h中的日志记录c函数 在swift文件中.任何人?我用Google搜索,人们似乎都在编写自己的日志快速类.没有不尊重,但我想使用asl. 也就是说,斯威夫特不喜欢 #include asl.h并且它不喜欢我只是调用asl_log(NULL,NULL,ASL_LEVEL_IN
我是ios / swift的新手.我想使用asl.h中的日志记录c函数
在swift文件中.任何人?我用Google搜索,人们似乎都在编写自己的日志快速类.没有不尊重,但我想使用asl.
也就是说,斯威夫特不喜欢
#include< asl.h>并且它不喜欢我只是调用asl_log(NULL,NULL,ASL_LEVEL_INFO,“Hello World!”);
谢谢,在 http://doing-it-wrong.mikeweller.com/2012/07/youre-doing-it-wrong-1-nslogdebug-ios.html的帮助下,我做了以下修改:

使用以下内容向项目添加了BRASL.h文件:

//
//  BRASL.h
//

#ifndef BRASL_h
#define BRASL_h

#import <asl.h>
#import <Foundation/Foundation.h>


// Define which loglevel is necessary for deployment and development
// =================================================================
// Used to conditionally implement the log functions. All log
// functions are defined so the compiler does not complain. But only
// those logfunctions that are used will contain code.
// =================================================================
#ifndef BRASL_LOG_LEVEL
    // DEBUG is set in the project build-settings
    #if DEBUG == 1
        // Set logging level for development
        #define BRASL_LOG_LEVEL ASL_LEVEL_DEBUG
    #else
        // Set logging level for deployment
        #define BRASL_LOG_LEVEL ASL_LEVEL_NOTICE
    #endif
#endif


// Define the log functions
// ========================
void aslEmergency(NSString *string);
void aslAlert(NSString *string);
void aslCritical(NSString *string);
void aslError(NSString *string);
void aslWarning(NSString *string);
void aslNotice(NSString *string);
void aslInfo(NSString *string);
void aslDebug(NSString *string);


#endif

然后添加相应的.m文件:

//
//  BRASL.h
//

#import "BRASL.h"


// We need this to set asl up to also write the information to the debugger
// ========================================================================
static void AddStderrOnce() {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        asl_add_log_file(NULL,STDERR_FILENO);
    });
}


// Implement the log functions where necessary
// ===========================================
#if BRASL_LOG_LEVEL >= ASL_LEVEL_EMERG
void aslEmergency(NSString *string) {
    AddStderrOnce();
    asl_log(NULL,ASL_LEVEL_EMERG,"%s",[string UTF8String]);
}
#else
void aslEmergency(NSString *string) {}
#endif

#if BRASL_LOG_LEVEL >= ASL_LEVEL_ALERT
void aslAlert(NSString *string) {
    AddStderrOnce();
    asl_log(NULL,ASL_LEVEL_ALERT,[string UTF8String]);
}
#else
void aslAlert(NSString *string) {}
#endif

#if BRASL_LOG_LEVEL >= ASL_LEVEL_CRIT
void aslCritical(NSString *string) {
    AddStderrOnce();
    asl_log(NULL,ASL_LEVEL_CRIT,[string UTF8String]);
}
#else
void aslCritical(NSString *string) {}
#endif

#if BRASL_LOG_LEVEL >= ASL_LEVEL_ERR
void aslError(NSString *string) {
    AddStderrOnce();
    asl_log(NULL,ASL_LEVEL_ERR,[string UTF8String]);
}
#else
void aslError(NSString *string) {}
#endif

#if BRASL_LOG_LEVEL >= ASL_LEVEL_WARNING
void aslWarning(NSString *string) {
    AddStderrOnce();
    asl_log(NULL,ASL_LEVEL_WARNING,[string UTF8String]);
}
#else
void aslWarning(NSString *string) {}
#endif

#if BRASL_LOG_LEVEL >= ASL_LEVEL_NOTICE
void aslNotice(NSString *string) {
    AddStderrOnce();
    asl_log(NULL,ASL_LEVEL_NOTICE,[string UTF8String]);
}
#else
void aslNotice(NSString *string) {}
#endif

#if BRASL_LOG_LEVEL >= ASL_LEVEL_INFO
void aslInfo(NSString *string) {
    AddStderrOnce();
    asl_log(NULL,[string UTF8String]);
}
#else
void aslInfo(NSString *string) {}
#endif

#if BRASL_LOG_LEVEL >= ASL_LEVEL_DEBUG
void aslDebug(NSString *string) {
    AddStderrOnce();
    asl_log(NULL,ASL_LEVEL_DEBUG,[string UTF8String]);
}
#else
void aslDebug(NSString *string) {}
#endif

当然还有桥接文件

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "BRASL.h"

然后在我的快速代码中我可以使用例如:

aslInfo("Initializing managed object context")

到目前为止这么好,似乎像宣传的那样工作:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读