首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在文件中使用asl.h

如何在文件中使用asl.h
EN

Stack Overflow用户
提问于 2014-11-01 00:40:47
回答 3查看 2.3K关注 0票数 6

我是ios/的新手。我希望在快速文件中使用asl.h中的日志c-函数。有没有人?我在googled上搜索,人们似乎编写了自己的日志快速类。没有不敬之处,但我只想用asl。也就是说,斯威夫特不喜欢#include <asl.h>,也不喜欢我只打电话给asl_log(NULL, NULL, ASL_LEVEL_INFO, "Hello World!");

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-11-14 12:42:32

谢谢,在http://doing-it-wrong.mikeweller.com/2012/07/youre-doing-it-wrong-1-nslogdebug-ios.html的帮助下,我做了以下修改:

向项目中添加了一个BRASL.h文件,其内容如下:

代码语言:javascript
复制
//
//  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文件:

代码语言:javascript
复制
//
//  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, 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, NULL, ASL_LEVEL_ALERT, "%s", [string UTF8String]);
}
#else
void aslAlert(NSString *string) {}
#endif

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

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

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

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

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

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

当然,桥接文件

代码语言:javascript
复制
//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "BRASL.h"

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

代码语言:javascript
复制
aslInfo("Initializing managed object context")

到目前为止,似乎像广告中所说的那样起作用:)

票数 4
EN

Stack Overflow用户

发布于 2014-11-03 22:04:20

到目前为止,我发现的最简单的方法是以下方法(,它适用于任何c库):

步骤1:文件-新-文件目标-C,例如MyBridgeToACLib.h,MyBridgeToACLib.m

步骤2: MyBridgeToACLib.h

代码语言:javascript
复制
#import <Foundation/Foundation.h>
@interface MyBridgeToACLib : NSObject    
  // here you need to declare a function for each c-function you want to call from swift, e.g. : 
  + (void) debug:(NSString*) nsStr;
  + (void) debug:(NSString*) nsStr secondValue:(NSInteger) nsInt;
@end

步骤3: MyBridgeToACLib.m

代码语言:javascript
复制
#include <asl.h>  // or any c-library you need to call from Swift
#import "MyBridgeToACLib.h"

@implementation MyBridgeToACLib

+ (void) debug:(NSString*) nsStr {
  // here you need to convert from Objective-C types to C-types, e.g. NSString to char*
  const char *cStr = [nsStr UTF8String];
  printf("%s\n", cStr);
  // call your c-functions
  asl_log(NULL, NULL, ASL_LEVEL_DEBUG, "%s", cStr);
}

+ (void) debug:(NSString*) nsStr secondValue:(NSInteger) nsInt {
  const char *cStr = [nsStr UTF8String];
  long cInt = nsInt;
  printf("%s%li\n", cStr, cInt);
  asl_log(NULL, NULL, ASL_LEVEL_DEBUG, "%s%li", cStr, cInt);
}
@end

步骤4:设置以下“MyProjectName-桥接-Header.h”。谷歌"XCode桥接-标题“的说明。

代码语言:javascript
复制
    // Swift and Objective-C in the Same Project   
//https://developer.apple.com/library/ios/documentation/swift/conceptual/buildingcocoaapps/MixandMatch.html
    //
    // Here import all of your "Bridge"-headers 
    #import "MyBridgeToACLib.h"
票数 3
EN

Stack Overflow用户

发布于 2015-12-15 17:03:34

下面是一些您可能感兴趣的开源项目:

·CleanroomASL- A低级别但快速的API,用于读取和写入苹果系统日志

·支持向ASL写入的CleanroomLogger- A高级Swift日志记录API

·AppleSystemLogSwiftPackage--一个Swift (SPM)声明,允许您从代码中“导入ASL”。(请注意,SPM目前只为Mac构建,因此目前它无法帮助您使用iOS。)

基于您所写的内容,我怀疑CleanroomLogger项目最适合您使用。

希望你觉得这有帮助,

E.

充分披露:我已经为这些项目中的每一个做出了贡献。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26685219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档