Browse Source

打印封装

huzhiqiang 3 years ago
parent
commit
f63093c4c7
1 changed files with 34 additions and 28 deletions
  1. 34 28
      BFNetRequestKit/Classes/SWNetRequest.swift

+ 34 - 28
BFNetRequestKit/Classes/SWNetRequest.swift

@@ -90,7 +90,7 @@ public class SWNetRequest: NSObject {
             response(nil, PQError(msg: "非法地址", code: 0), nil)
             return
         }
-        debugPrint("网络请求-发起:url = \(url),parames = \(parames ?? [:])")
+//        DebugPrint("网络请求-发起:url = \(url),parames = \(parames ?? [:])")
 
         sessionManager?.request(url, method: method, parameters: parames, encoding: encoding, headers: nil, requestModifier: { request in
             request.timeoutInterval = bf_timeoutInterval
@@ -99,7 +99,7 @@ public class SWNetRequest: NSObject {
             case .success:
                 if jsonResponse.data != nil {
                     let jsonData: Any = try! JSONSerialization.jsonObject(with: jsonResponse.data!, options: JSONSerialization.ReadingOptions.mutableContainers)
-                    debugPrint("网络请求-成功:url = \(jsonResponse.request?.url?.absoluteString ?? ""),jsonData = \(jsonData)")
+                    DebugPrint("网络请求-成功:url = \(jsonResponse.request?.url?.absoluteString ?? ""),jsonData = \(jsonData)")
                     response(jsonData, nil, jsonResponse.metrics?.taskInterval.duration)
                 }else {
                     response(nil, PQError(msg: "数据为空", code: 1), jsonResponse.metrics?.taskInterval.duration)
@@ -107,7 +107,7 @@ public class SWNetRequest: NSObject {
             case let .failure(error):
                 let errorResult = dealWithError(error: error)
                 response(nil, PQError(msg: errorResult.msg, code: errorResult.code), jsonResponse.metrics?.taskInterval.duration)
-                debugPrint("网络请求-失败:url = \(jsonResponse.request?.url?.absoluteString ?? ""),error = \(String(describing: jsonResponse.error?.localizedDescription))")
+//                DebugPrint("网络请求-失败:url = \(jsonResponse.request?.url?.absoluteString ?? ""),error = \(String(describing: jsonResponse.error?.localizedDescription))")
             }
         }
     }
@@ -202,67 +202,73 @@ public class SWNetRequest: NSObject {
         if let error = error as? AFError {
             switch error {
             case let .invalidURL(url):
-                print("网球请求-处理错误:Invalid URL: \(url) - \(error.localizedDescription)")
+                DebugPrint("网球请求-处理错误:Invalid URL: \(url) - \(error.localizedDescription)")
                 code = .valideUrl
                 msg = "请求地址检验失败"
             case let .parameterEncodingFailed(reason):
-                print("网球请求-处理错误:parameterEncodingFailed \(error.localizedDescription),Reason: \(reason)")
+                DebugPrint("网球请求-处理错误:parameterEncodingFailed \(error.localizedDescription),Reason: \(reason)")
             case let .multipartEncodingFailed(reason):
-                print("网球请求-处理错误:multipartEncodingFailed: \(error.localizedDescription),\(reason)")
+                DebugPrint("网球请求-处理错误:multipartEncodingFailed: \(error.localizedDescription),\(reason)")
             case let .responseValidationFailed(reason):
-                print("网球请求-处理错误:responseValidationFailed: \(error.localizedDescription),Reason: \(reason)")
+                DebugPrint("网球请求-处理错误:responseValidationFailed: \(error.localizedDescription),Reason: \(reason)")
                 switch reason {
                 case .dataFileNil, .dataFileReadFailed:
-                    print("网球请求-处理错误:Downloaded file could not be read")
+                    DebugPrint("网球请求-处理错误:Downloaded file could not be read")
                 case let .missingContentType(acceptableContentTypes):
-                    print("网球请求-处理错误:Content Type Missing: \(acceptableContentTypes)")
+                    DebugPrint("网球请求-处理错误:Content Type Missing: \(acceptableContentTypes)")
                 case let .unacceptableContentType(acceptableContentTypes, responseContentType):
-                    print("网球请求-处理错误:Response content type: \(responseContentType) was unacceptable: \(acceptableContentTypes)")
+                    DebugPrint("网球请求-处理错误:Response content type: \(responseContentType) was unacceptable: \(acceptableContentTypes)")
                 case let .unacceptableStatusCode(code):
-                    print("网球请求-处理错误:Response status code was unacceptable: \(code)")
+                    DebugPrint("网球请求-处理错误:Response status code was unacceptable: \(code)")
                 case let .customValidationFailed(error: error):
-                    print("网球请求-处理错误:customValidationFailed: \(error)")
+                    DebugPrint("网球请求-处理错误:customValidationFailed: \(error)")
                 }
             case let .responseSerializationFailed(reason):
-                print("网球请求-处理错误:Response serialization failed: \(error.localizedDescription)")
-                print("Failure Reason: \(reason)")
+                DebugPrint("网球请求-处理错误:Response serialization failed: \(error.localizedDescription)")
+                DebugPrint("Failure Reason: \(reason)")
             case let .createUploadableFailed(error: error):
-                print("网球请求-处理错误:createUploadableFailed: \(error.localizedDescription)")
+                DebugPrint("网球请求-处理错误:createUploadableFailed: \(error.localizedDescription)")
             case let .createURLRequestFailed(error: error):
-                print("createURLRequestFailed: \(error.localizedDescription)")
+                DebugPrint("createURLRequestFailed: \(error.localizedDescription)")
             case let .downloadedFileMoveFailed(error: error, source: source, destination: destination):
-                print("网球请求-处理错误:downloadedFileMoveFailed: \(error.localizedDescription)")
+                DebugPrint("网球请求-处理错误:downloadedFileMoveFailed: \(error.localizedDescription)")
             case .explicitlyCancelled:
-                print("网球请求-处理错误:explicitlyCancelled: \(error.localizedDescription)")
+                DebugPrint("网球请求-处理错误:explicitlyCancelled: \(error.localizedDescription)")
             case let .parameterEncoderFailed(reason: reason):
-                print("网球请求-处理错误:parameterEncoderFailed: \(error.localizedDescription)")
+                DebugPrint("网球请求-处理错误:parameterEncoderFailed: \(error.localizedDescription)")
             case let .requestAdaptationFailed(error: error):
-                print("网球请求-处理错误:requestAdaptationFailed: \(error.localizedDescription)")
+                DebugPrint("网球请求-处理错误:requestAdaptationFailed: \(error.localizedDescription)")
             case let .requestRetryFailed(retryError: retryError, originalError: originalError):
-                print("网球请求-处理错误:requestRetryFailed:  retryError = \(retryError),originalError = \(originalError)\(error.localizedDescription)")
+                DebugPrint("网球请求-处理错误:requestRetryFailed:  retryError = \(retryError),originalError = \(originalError)\(error.localizedDescription)")
             case let .serverTrustEvaluationFailed(reason: reason):
-                print("网球请求-处理错误:serverTrustEvaluationFailed: - \(error.localizedDescription)")
+                DebugPrint("网球请求-处理错误:serverTrustEvaluationFailed: - \(error.localizedDescription)")
             case .sessionDeinitialized:
-                print("网球请求-处理错误:sessionDeinitialized:  \(error.localizedDescription)")
+                DebugPrint("网球请求-处理错误:sessionDeinitialized:  \(error.localizedDescription)")
             case let .sessionInvalidated(error: error):
-                print("网球请求-处理错误:sessionInvalidated: \(error?.localizedDescription)")
+                DebugPrint("网球请求-处理错误:sessionInvalidated: \(error?.localizedDescription)")
             case let .sessionTaskFailed(error: error):
-                print("网球请求-处理错误:sessionTaskFailed:error = \(error)  \(error.localizedDescription)")
+                DebugPrint("网球请求-处理错误:sessionTaskFailed:error = \(error)  \(error.localizedDescription)")
                 code = errorCode.init(rawValue: (error as? NSError)?.code ?? 1) ?? .nomal
                 if code == .netLost || code == .tomeOut {
                     msg = "网络不给力"
                 }
             case let .urlRequestValidationFailed(reason: reason):
-                print("网球请求-处理错误:urlRequestValidationFailed: \(error.localizedDescription)")
+                DebugPrint("网球请求-处理错误:urlRequestValidationFailed: \(error.localizedDescription)")
             }
-            print("网球请求-处理错误:Underlying error: \(String(describing: error.underlyingError))")
+            DebugPrint("网球请求-处理错误:Underlying error: \(String(describing: error.underlyingError))")
         } else {
-            print("网球请求-处理错误:Unknown error: \(String(describing: error))")
+            DebugPrint("网球请求-处理错误:Unknown error: \(String(describing: error))")
         }
         return (code.rawValue, msg)
     }
 }
 
+func DebugPrint(_ printString:String){
+    #if DEBUG
+        print(printString)
+    #endif
+}
+
 class CMRequestRetrier: RequestInterceptor {
     func retry(_: Request, for _: Session, dueTo _: Error, completion: @escaping (RetryResult) -> Void) {
         completion(.doNotRetry)