想在Xcode中整一個彩色日誌顯示,按照GettingStarted.md 一文中的步驟將CocoaLumberjack 2.x整合進個人項目中來,遇到一些問題,固然不乏一些坑,做個記錄。ios
整合步驟:git
Drag CocoaLumberjack/Framework/{Desktop/Mobile}/Lumberjack.xcodeproj
into your projectgithub
In your App target Build Settingsxcode
Add to 'User Header Search Paths' $(BUILD_ROOT)/../IntermediateBuildFilesPath/UninstalledProducts/include
架構
Set 'Always Search User Paths' to YESapp
In your App target Build Phases框架
Add CocoaLumberjack static library target to 'Target Dependencies'ide
Add libCocoaLumberjack.a
to 'Link Binary With Libraries'post
Include the framework in your source files with測試
#import <CocoaLumberjack/CocoaLumberjack.h>
一、首先是編譯提示Use of undeclared identifier 'LOG_LEVEL_VERBOSE'問題
這個我是按照文檔XcodeTricks.md 在pch文件中加了下面的代碼:
#ifdef DEBUG static const int ddLogLevel = LOG_LEVEL_VERBOSE; #else static const int ddLogLevel = LOG_LEVEL_WARN; #endif
踩坑1,把LOG_LEVEL_VERBOSE和LOG_LEVEL_WARN換成DDLogLevelVerbose和DDLogLevelError就行了。
修改後的代碼應該是:
#ifdef DEBUG static const int ddLogLevel = DDLogLevelVerbose; #else static const int ddLogLevel = DDLogLevelError; #endif
而後在方法application:didFinishLaunchingWithOptions:中添加如下代碼設置顏色顯示:
[DDLog addLogger:[DDASLLogger sharedInstance]]; [DDLog addLogger:[DDTTYLogger sharedInstance]]; [[DDTTYLogger sharedInstance] setColorsEnabled:YES];
測試顏色顯示的代碼:
DDLogError(@"Paper jam"); DDLogWarn(@"Toner is low"); DDLogInfo(@"Warming up printer (pre-customization)"); DDLogVerbose(@"Intializing protcol x26 (pre-customization)");
二、彩色日誌顯示須要插件XcodeColors插件支持,這個插件我下載的是https://github.com/rvi/XcodeColors裏面的,由於它支持了Xcode6.3。
三、用CocoaLumberjack Demo裏自帶的TextXcodeColors工程測試,pod install後打開TextXcodeColors.xcodeproj,編譯提示ld: library not found for -lPods-TXC_ios-CocoaLumberjack
又是一個坑。通常來講,pod install後應該生成xcworkspace文件,可是沒有生成TextXcodeColors.xcworkspace文件,我就奇怪了。後來才發現,應該是打開Demos.xcworkspace,而後在裏面選擇TextXcodeColors這個target,固然前提是先要進入TextXcodeColors文件夾執行pod install才行。儘可能使用「pod install --verbose --no-repo-update」
四、Demo裏測試日誌顏色正常,在本身的項目裏就不會顯示顏色
坑3。奧祕在於要對Project的Scheme做了以下調整:
In Xcode bring up the Scheme Editor (Product -> Edit Scheme...)
Select "Run" (on the left), and then the "Arguments" tab
Add a new Environment Variable named "XcodeColors", with a value of "YES"
五、內存暴漲問題
加入CocoaLumberjack後,模擬器測試正常,真機測試內存暴漲,致使xcode自動終結聯機調試,不調用DDLog相關的代碼就沒有問題。最終縮小範圍,發現把上面的第4步中的Environment Variable給刪掉就沒有問題,我真是暈了。
而另外一個位置的同名項目,我測試是沒有這個問題,真是奇哉怪也。
實在是沒有辦法,把DerivedData裏的全部app文件夾所有刪掉,再從新運行,就沒有問題了。猜想是由於同名項目的緣故?
六、使用靜態庫
原本解決了上面的問題後使用CocoaLumberjack基本就沒什麼問題了,但是不幸又被我發現了一個問題,那就是當我要修改app圖標和啓動圖片的時候,即在General--App Icons and Launch Images中點向右箭頭時,進入的倒是項目Lumberjack.xcodeproj的啓動圖片設置界面。這說明嵌入Lumberjack.xcodeproj後App Icons and Launch Images混亂了。
因而我想到了使用靜態庫,在網上找到一個現成的CocoaLumberjack靜態庫工程,連接是https://github.com/NachoMan/CocoaLumberjack-Static。下載後先執行git submodule update --init --recursive命令更新CocoaLumberjack庫,再執行build.sh腳本便可生成一個zip包,裏面包含了.a文件和頭文件,複製到本身的工程中添加就好了。
能夠用lipo -info xxxxx.a命令來檢查生成的.a文件的CPU架構。
參考:
iOS開源項目之日誌框架CocoaLumberjack
利用 CocoaLumberjack 搭建本身的 Log 系統