added iOS source code
[wl-app.git] / iOS / Pods / AlamofireActivityLogger / alamofire_activity_logger / ActivityLogger / Logger.swift
1 //
2 //  Logger.swift
3 //  alamofire_activity_logger
4 //
5 //  Created by Manuel García-Estañ on 14/9/16.
6 //  Copyright © 2016 manuege. All rights reserved.
7 //
8
9 import Foundation
10
11 private let nullString = "(null)"
12 private let separatorString = "*******************************"
13
14 /**
15  A set of static methods which logs request and responses
16  */
17 internal struct Logger {
18     
19     internal static func logRequest(request: URLRequest?, level: LogLevel, options: [LogOption], printer: Printer) {
20         
21         guard let request = request else {
22             return
23         }
24         
25         let method = request.httpMethod!
26         let url = request.url?.absoluteString ?? nullString
27         let headers = prettyPrintedString(from: request.allHTTPHeaderFields) ?? nullString
28         
29         // separator
30         let openSeparator = options.contains(.includeSeparator) ? "\(separatorString)\n" : ""
31         let closeSeparator = options.contains(.includeSeparator) ? "\n\(separatorString)" : ""
32         
33         switch (level) {
34         case .all:
35             let prettyPrint = options.contains(.jsonPrettyPrint)
36             let body = string(from: request.httpBody, prettyPrint: prettyPrint) ?? nullString
37             printer.print("\(openSeparator)[Request] \(method) '\(url)':\n\n[Headers]\n\(headers)\n\n[Body]\n\(body)\(closeSeparator)", phase: .request)
38             
39         case .info:
40             printer.print("\(openSeparator)[Request] \(method) '\(url)'\(closeSeparator)", phase: .request)
41             
42         default:
43             break
44         }
45     }
46     
47     internal static func logResponse(request: URLRequest?, response: ResponseInfo, level: LogLevel, options: [LogOption], printer: Printer) {
48         
49         guard level != .none else {
50             return
51         }
52         
53         let httpResponse = response.httpResponse
54         let data = response.data
55         let elapsedTime = response.elapsedTime
56         let error = response.error
57         
58         if request == nil && response.httpResponse == nil {
59             return
60         }
61         
62         // options
63         let prettyPrint = options.contains(.jsonPrettyPrint)
64         
65         // request
66         let requestMethod = request?.httpMethod ?? nullString
67         let requestUrl = request?.url?.absoluteString ?? nullString
68         
69         // response
70         let responseStatusCode = httpResponse?.statusCode ?? 0
71         let responseHeaders = prettyPrintedString(from: httpResponse?.allHeaderFields) ?? nullString
72         let responseData = string(from: data, prettyPrint: prettyPrint) ?? nullString
73         
74         // time
75         let elapsedTimeString = String(format: "[%.4f s]", elapsedTime)
76         
77         // separator
78         let openSeparator = options.contains(.includeSeparator) ? "\(separatorString)\n" : ""
79         let closeSeparator = options.contains(.includeSeparator) ? "\n\(separatorString)" : ""
80         
81         // log
82         let success = (error == nil)
83         let responseTitle = success ? "Response" : "Response Error"
84         switch level {
85         case .all:
86             printer.print("\(openSeparator)[\(responseTitle)] \(responseStatusCode) '\(requestUrl)' \(elapsedTimeString):\n\n[Headers]:\n\(responseHeaders)\n\n[Body]\n\(responseData)\(closeSeparator)", phase: .response(success: success))
87         case .info:
88             printer.print("\(openSeparator)[\(responseTitle)] \(responseStatusCode) '\(requestUrl)' \(elapsedTimeString)\(closeSeparator)", phase: .response(success: success))
89         case .error:
90             if let error = error {
91                 printer.print("\(openSeparator)[\(responseTitle)] \(requestMethod) '\(requestUrl)' \(elapsedTimeString) s: \(error)\(closeSeparator)", phase: .response(success: success))
92             }
93         default:
94             break
95         }
96     }
97     
98     // MARK: - Private helpers
99     private static func string(from data: Data?, prettyPrint: Bool) -> String? {
100         
101         guard let data = data else {
102             return nil
103         }
104         
105         var response: String? = nil
106         
107         if prettyPrint,
108             let json = try? JSONSerialization.jsonObject(with: data, options: []),
109             let prettyString = prettyPrintedString(from: json) {
110             response = prettyString
111         }
112             
113         else if let dataString = String.init(data: data, encoding: .utf8) {
114             response = dataString
115         }
116         
117         return response
118     }
119     
120     private static func prettyPrintedString(from json: Any?) -> String? {
121         guard let json = json else {
122             return nil
123         }
124         
125         var response: String? = nil
126         
127         if let data = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
128             let dataString = String.init(data: data, encoding: .utf8) {
129             response = dataString
130         }
131         
132         return response
133     }
134
135 }