2014-11-01 21 views
6

Tôi mới sử dụng ios/swift. Tôi muốn sử dụng chức năng ghi nhật ký c từ asl.h trong các tệp nhanh. Bất kỳ ai? Tôi googled và mọi người dường như viết riêng của họ đăng nhập nhanh chóng các lớp học. Không thiếu tôn trọng, nhưng tôi muốn sử dụng asl. Đó là, nhanh chóng không thích #include <asl.h> và nó không thích tôi chỉ gọi asl_log(NULL, NULL, ASL_LEVEL_INFO, "Hello World!");Cách sử dụng asl.h trong các tập tin nhanh chóng

+0

Tôi dường như không thể tìm thấy nó dưới 'Darwin' là nơi cần thiết. Có lẽ bạn chỉ có thể viết một wrapper trong Objective-C và kết nối nó. – CodaFi

Trả lời

4

Cảm ơn, và với sự giúp đỡ từ http://doing-it-wrong.mikeweller.com/2012/07/youre-doing-it-wrong-1-nslogdebug-ios.html tôi đã thực hiện những sửa đổi như sau:

Added một file BRASL.h cho dự án với các nội dung sau:

// 
// 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 

Sau đó, thêm tương ứng .m tệp với:

// 
// 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 

Và tất nhiên tệp cầu nối

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

#import "BRASL.h" 

Sau đó trong mã nhanh chóng của tôi, tôi có thể sử dụng ví dụ:

aslInfo("Initializing managed object context") 

Cho đến nay rất tốt, dường như làm việc như đã quảng cáo :)

+0

Giải pháp trên hoạt động tốt cho các ứng dụng nhỏ chỉ sử dụng một chuỗi duy nhất. Khi bạn bắt đầu thực hiện đa luồng, một giải pháp tốt hơn có thể là bắt đầu sử dụng một thư viện nhỏ như CocoaLumberjack: https://github.com/CocoaLumberjack/CocoaLumberjack/blob/master/README.md – Rien

+0

Hoạt động! Nhưng tôi chỉ thấy kết quả đầu ra từ 'ASL_LEVEL_NOTICE' trở lên. Không có 'ASL_LEVEL_INFO' hoặc' ASL_LEVEL_DEBUG' mặc dù cả hai 'aslInfo' và 'aslDebug' đều gọi phương thức' asl_log' (được xác minh qua gỡ lỗi). Tuy nhiên đầu ra của chúng không được liệt kê trên bảng điều khiển. Bất kỳ gợi ý nào? –

3

Cho đến nay, cách dễ nhất tôi thấy là sau (nó hoạt động cho bất kỳ c-thư viện):

Bước 1: File-New-File Objective-C, vd MyBridgeToACLib.h, MyBridgeToACLib.m

Bước 2: Trong MyBridgeToACLib.h

#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 

Bước-3: Trong MyBridgeToACLib.m

#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 

Bước-4: Thiết lập như sau "MyProjectName -Bridging-Header.h ". Google "XCode Bridging-Header" để được hướng dẫn.

// 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" 
+0

Tôi muốn sử dụng lớp này để gỡ lỗi bên trong ứng dụng của mình. Chỉ cần không biết làm thế nào để làm cho nó làm việc với Bridging-Header. Chỉ cần tạo một tệp được gọi là ** MyBridgeToACLib-Bridging-Header.h ** và bao gồm ** # nhập MyBridgeToACLib.h **. Có điều gì tôi đang thiếu? Tôi làm việc trên Objective-c và không phải nhanh chóng, chỉ cần một chút bối rối cho tôi bởi vì tôi không bao giờ làm việc trên nó –

1

Dưới đây là một vài dự án mã nguồn mở mà có thể bạn quan tâm:

CleanroomASL - API mức thấp nhưng được làm nhanh để đọc từ & ghi vào Nhật ký hệ thống Apple

CleanroomLogger - A-level cao Swift logging API hỗ trợ bằng văn bản cho ASL

AppleSystemLogSwiftPackage - Tờ khai Swift Package Manager (SPM) cho phép bạn 'nhập ASL' từ bên trong mã của bạn. (Lưu ý rằng SPM chỉ xây dựng cho Mac OS X vào lúc này, vì vậy nó sẽ không giúp bạn với iOS ngay bây giờ.)

Dựa trên những gì bạn đã viết, tôi nghi ngờ dự án CleanroomLogger là thích hợp nhất cho sử dụng của bạn.

Hy vọng bạn tìm thấy điều này hữu ích,

E.

Họ và tiết lộ: Tôi đã đóng góp cho mỗi người trong số các dự án này.

Các vấn đề liên quan