我是ios/swift的新手。我想在swift文件中使用來自asl.h 的日誌c函數。任何人?我搜索了一下,人們似乎寫了他們自己的日誌快速課程。沒有不敬,但我想只用asl。 也就是說,SWIFT不喜歡 #include <asl.h>
,它不喜歡我只是打電話asl_log(NULL, NULL, ASL_LEVEL_INFO, "Hello World!");
如何在swift-ios文件中使用asl.h
回答
感謝,並與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, 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
當然橋接文件
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "BRASL.h"
然後在我的SWIFT代碼我可以使用,例如:
aslInfo("Initializing managed object context")
到目前爲止好,看起來像廣告:)
上述解決方案對於只使用單個線程的小應用程序工作正常。當你開始做多線程時,更好的解決方案可能是開始使用像CocoaLumberjack這樣的小型庫:https://github.com/CocoaLumberjack/CocoaLumberjack/blob/master/README.md – Rien 2014-11-24 10:53:15
工程!但是我只能看到'ASL_LEVEL_NOTICE'的輸出向上。即使'aslInfo'和'aslDebug'都調用'asl_log'方法(通過調試驗證),也不會有'ASL_LEVEL_INFO'和'ASL_LEVEL_DEBUG'。但是他們的輸出不在控制檯上列出。任何提示? – 2015-09-30 14:04:17
到目前爲止,我發現最簡單的方法是以下(它可以用於任何C-庫):
第一步:File-New-File Objective-C,例如MyBridgeToACLib.h,MyBridgeToACLib.m
步驟2:在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
步驟3:在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
步驟4:設置以下「MyProjectName -Bridging-Header.h」。谷歌「XCode橋接頭」的指示。
// 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"
我想使用這個類來調試我的應用程序。只是不知道如何使它與Bridging-Header一起工作。剛剛創建了一個名爲** MyBridgeToACLib-Bridging-Header.h **的文件,並且包含**#import MyBridgeToACLib.h **。有什麼我失蹤?我在Objective-c上工作,而不是在swift上,只是對我有點混淆,因爲我從來沒有對它做過工作 – 2015-05-20 10:32:41
這裏有幾個開源項目的工作,可能會引起您的興趣:
•CleanroomASL - 用於從&寫入Apple系統日誌的低級別但Swiftified API日誌
•CleanroomLogger - 一個高層次的斯威夫特記錄的API,支持寫入ASL
•AppleSystemLogSwiftPackage - 迅速軟件包管理器(SPM)的聲明,允許你「進口翔升」從你的代碼中。 (請注意,SPM目前僅適用於Mac OS X,因此目前無法幫助您使用iOS。)
根據您撰寫的內容,我懷疑CleanroomLogger項目是最合適的你的使用。
希望對您有所幫助,
E.
全面披露:我已經給每個項目的貢獻。
- 1. 如何在C中使用tlb文件?
- 2. 如何在hosts文件中使用blocksites?
- 3. 如何在node.js中使用xml文件?
- 4. 如何在django中使用css文件?
- 5. 如何在NetBeans中使用.jar文件?
- 6. 如何在java文件中使用html:label
- 7. 如何在android中使用.csv文件?
- 8. 如何在VBS中使用文件名
- 9. 如何在Java中使用.aiff文件?
- 10. 如何在CGAL中使用.obj文件
- 11. 如何在class.php文件中使用FirePHP?
- 12. 如何在javascript文件中使用php
- 13. 如何在.xaml文件中使用stylecop
- 14. 如何在eclipse中使用AIDL文件
- 15. 如何在JSP文件中使用Hibernate?
- 16. 如何在php中使用md5文件?
- 17. 如何在C++中使用dll文件?
- 18. 如何在nginx中使用.htaccess文件
- 19. 如何在類文件中使用ScriptManager?
- 20. 如何在csproj文件中使用Msbuild.Community.Tasks.Version
- 21. 如何在opengl中使用.raw文件
- 22. 如何在js.erb文件中使用require.js
- 23. 如何在emscripten中使用preamble.js文件?
- 24. 如何在豬文件中使用.jar
- 25. 如何在sails.js中使用.less文件?
- 26. 如何在as3中使用.jar文件
- 27. 如何在.env文件中使用nodemon?
- 28. 如何在html中使用.vue文件?
- 29. 如何在android中使用.shp文件
- 30. 如何使用GZIP文件在Asp.net中?
我似乎無法在「達爾文」下面找到它,它應該在哪裏。也許你可以在Objective-C中編寫一個包裝器並將其連接起來。 – CodaFi 2014-11-01 01:12:11