added iOS source code
[wl-app.git] / iOS / Pods / AlamofireActivityLogger / alamofire_activity_logger / ActivityLogger / Tools.swift
1 //
2 //  Tools.swift
3 //  alamofire_activity_logger
4 //
5 //  Created by Manuel García-Estañ on 2/11/16.
6 //  Copyright © 2016 manuege. All rights reserved.
7 //
8
9 import Foundation
10
11 internal let appIsDebugMode = _isDebugAssertConfiguration()
12
13 // MARK: - Levels
14
15 /**
16  Log levels
17  
18  `none`
19  Do not log requests or responses.
20  
21  `all`
22  Logs HTTP method, URL, header fields, & request body for requests, and status code, URL, header fields, response string, & elapsed time for responses.
23  
24  `info`
25  Logs HTTP method & URL for requests, and status code, URL, & elapsed time for responses.
26  
27  `error`
28  Logs HTTP method & URL for requests, and status code, URL, & elapsed time for responses, but only for failed requests.
29  */
30 public enum LogLevel {
31     case none
32     case all
33     case info
34     case error
35 }
36
37 // MARK: - Options
38
39 /**
40  Login options
41  
42  `onlyDebug`
43  Only logs if the app is in Debug mode
44  
45  `jsonPrettyPrint`
46  Prints the JSON body on request and response
47  
48  `includeSeparator`
49  Include a separator string at the begining and end of each section
50  */
51 public enum LogOption {
52     case onlyDebug
53     case jsonPrettyPrint
54     case includeSeparator
55     
56     public static var defaultOptions: [LogOption] {
57         return [.onlyDebug, .jsonPrettyPrint, .includeSeparator]
58     }
59 }
60
61 // MARK: - Printer
62
63 /**
64  The different phases of a request that can be printed
65  
66  `request`
67  The request when it is sent
68  
69  `response`
70  The response when it is received; includes a parameter `success` that inform if the response has finished succesfully
71 */
72 public enum Phase {
73     case request
74     case response(success: Bool)
75     
76     /// Tells if there is an error in the phase
77     public var isError: Bool {
78         switch self {
79         case let .response(success):
80             return !success
81         case .request:
82             return false
83         }
84     }
85 }
86
87 /// Instances that conforms with `Printer` protocol are able to print the information from a given request
88 public protocol Printer {
89     
90     /**
91      This method is called when the printer is requested to print a string. Use it to print the information in the way you need.
92      - parameter string: The string to be printed.
93      - parameter phase: The phase of the request that needs to be printed
94      */
95     func print(_ string: String, phase: Phase)
96 }
97
98 /// A printer that just use the native `Swift.print` function to print the string.
99 public struct NativePrinter: Printer {
100     
101     /// Creates a new instance
102     public init() {}
103     
104     public func print(_ string: String, phase: Phase) {
105         Swift.print(string)
106     }
107 }