2014-11-01 15 views
6

Sono nuovo in ios/swift. Vorrei usare la registrazione delle funzioni c da asl.h in file rapidi. Chiunque? Ho cercato su Google e la gente sembra scrivere le proprie lezioni veloci di registrazione. Nessuna mancanza di rispetto, ma mi piacerebbe usare solo asl. Cioè, veloce non piace #include <asl.h> e non mi piace solo chiamando asl_log(NULL, NULL, ASL_LEVEL_INFO, "Hello World!");Come utilizzare asl.h nei file swift-ios

+0

Non riesco a trovarlo in 'Darwin', che è dove dovrebbe essere. Forse potresti semplicemente scrivere un wrapper in Objective-C e farlo passare attraverso. – CodaFi

risposta

4

Grazie, e con l'aiuto di http://doing-it-wrong.mikeweller.com/2012/07/youre-doing-it-wrong-1-nslogdebug-ios.html ho fatto le seguenti modifiche:

Aggiunto un file BRASL.h al progetto con il seguente contenuto:

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

quindi aggiunto il corrispondente .m file con:

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

E naturalmente il file di collegamento

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

#import "BRASL.h" 

Poi nel mio codice swift posso usare per esempio:

aslInfo("Initializing managed object context") 

Fin qui tutto bene, sembra funzionare come pubblicizzato :)

+0

La soluzione di cui sopra funziona bene per le piccole app che utilizzano solo un singolo thread. Quando inizi a fare il multithreading, una soluzione migliore sarebbe probabilmente iniziare a utilizzare una piccola libreria come CocoaLumberjack: https://github.com/CocoaLumberjack/CocoaLumberjack/blob/master/README.md – Rien

+0

Works! Ma vedo solo gli output da 'ASL_LEVEL_NOTICE' verso l'alto. Nessun 'ASL_LEVEL_INFO' né' ASL_LEVEL_DEBUG' anche se sia 'aslInfo' che' aslDebug' chiamano il metodo 'asl_log' (verificato tramite debugging). Tuttavia le loro uscite non sono elencate sulla console. Qualche suggerimento? –

3

Finora, il modo più semplice che ho trovato è il seguente (funziona per qualsiasi c-librerie):

Step-1: File-New-File Objective-C, es MyBridgeToACLib.h, MyBridgeToACLib.m

Step-2: In 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 

Step-3: In 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 

Step-4: Setup la seguente "MyProjectName -Bridging-header.h". Google "XCode Bridging-Header" per le istruzioni.

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

Vorrei usare questa classe per il debugging all'interno della mia app. Non so come farlo funzionare con il Bridging-Header. Ho appena creato un file chiamato ** MyBridgeToACLib-Bridging-Header.h ** e incluso ** # import MyBridgeToACLib.h **. C'è qualcosa che mi manca? Lavoro su Objective-c e non su swift, solo un po 'di confusione su di me perché non ho mai lavorato su di esso –

1

Ecco alcuni progetti open-source che potrebbe interessarti:

CleanroomASL - Un'API di basso livello, ma con swiftified per la lettura da & scrittura nel registro di sistema Apple

CleanroomLogger - Un alto livello di registrazione Swift API che supporta la scrittura alla ASL

AppleSystemLogSwiftPackage - Una dichiarazione Swift Package Manager (SPM) che permette di 'import ASL' all'interno del codice. (Si noti che SPM solo crea per Mac OS X al momento, quindi per il momento non ti aiuterà con iOS.)

Sulla base di ciò che hai scritto, ho il sospetto che il progetto CleanroomLogger sarebbe il più appropriato per il tuo uso

Spero che troviate questo utile,

E.

Full disclosure: ho contribuito a ciascuno di questi progetti.

Problemi correlati