Swift 中調試狀態下打印日志


https://www.cnblogs.com/peaker-wu/p/5624368.html

 

Swift 中調試狀態下打印日志

首先我們應該知道Swift中真個程序的入口就是在AppDelegate.swift中。所以在打印日志在 AppDelegate.swift中是這樣的

復制代碼
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        DLLog("你好")
        
        return true
    }

}
// 把要打印的日志寫在和AppDelegate同一個等級的方法中,即不從屬於AppDelegate這個類,這樣在真個項目中才能使用這個打印日志,因為這就是程序的入口,
//這里的T表示不指定message參數類型
func DLLog<T>(message: T, fileName: String = __FILE__, funcName: String = __FUNCTION__, lineNum : Int = __LINE__) {
    
    #if DEBUG
        /**
         * 此處還要在項目的build settings中搜索swift flags,找到 Other Swift Flags 找到Debug
         * 添加 -D DEBUG,即可。
         */
         // 1.對文件進行處理
        let file = (fileName as NSString).lastPathComponent
        // 2.打印內容
        print("[\(file)][\(funcName)](\(lineNum))\(message)")
        
    #endif
    
}
復制代碼

打印的類型是:[AppDelegate.swift][application(_:didFinishLaunchingWithOptions:)](19)你好 

分別是文件 方法名 行號  內容

其次,還應該處理的一點是,如圖所示(其實在代碼里已經說明了)

這樣完整的調試狀態下的log日志就完成了

 

func uncaughtExceptionHandler(exception: NSException){

        // 異常的堆棧信息

        let stackArray: NSArray = exception.callStackSymbols as NSArray

        // 出現異常的原因

        let reason: NSString = exception.reason as! NSString

        // 異常名稱

        let name = exception.name

        let exceptionInfo = String(format: "Exception reason:\(name) \n Excepiton name: %s \n Excepiton stack: %s",reason,stackArray)

        print(exceptionInfo)

        var tmpArr = NSMutableArray.init(array: stackArray)

        tmpArr.insert(reason, at: 0)

        //保存到本地  --  當然你可以在下次啟動的時候,上傳這個log

        let strLog = String(format: "%s/Documents/error.log", arguments: [NSHomeDirectory()])

        let url = URL.init(fileURLWithPath: strLog)

        do {

            try exceptionInfo.write(to: url, atomically: true, encoding: String.Encoding.utf8)

        } catch {

            print(exception.description)

        }

    }

 /////可以使用

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Override point for customization after application launch.

   //////尚有問題問解決

        ///let observer = UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque())

       /// NSSetUncaughtExceptionHandler { (exception) in

        ///    let mySelf = Unmanaged<AppDelegate>.fromOpaque(observer).takeUnretainedValue()

         ////   mySelf.uncaughtExceptionHandler(exception: exception)

       // }

        

        return true

    }

 

 

 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Override point for customization after application launch.

 

       

        NSSetUncaughtExceptionHandler { exception in

 

            // 異常的堆棧信息

            let stackArray: NSArray = exception.callStackSymbols as NSArray

            // 出現異常的原因

            let reason: NSString = exception.reason as! NSString

            // 異常名稱

            let name = exception.name

            let exceptionInfo = String(format: "Exception reason:\(name) \n Excepiton name: %s \n Excepiton stack: %s",reason,stackArray)

            print(exceptionInfo)

            var tmpArr = NSMutableArray.init(array: stackArray)

            tmpArr.insert(reason, at: 0)

            //保存到本地  --  當然你可以在下次啟動的時候,上傳這個log

            let strLog = String(format: "%s/Documents/error.log", arguments: [NSHomeDirectory()])

            let url = URL.init(fileURLWithPath: strLog)

            do {

                try exceptionInfo.write(to: url, atomically: true, encoding: String.Encoding.utf8)

            } catch {

                print(exception.description)

            }

            print(exception)

        }

        

        return true

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM