import UIKit

class LogViewController: UIViewController, UITextViewDelegate {
    
    @IBOutlet weak var logTextView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.logTextView.clipsToBounds = true
        self.logTextView.layer.cornerRadius = 10
        self.logTextView.layer.borderColor = UIColor.white.cgColor
        self.logTextView.layer.borderWidth = 1.0
        self.logTextView.isScrollEnabled = true
        
        self.logTextView.delegate = self
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    // g_logString 로그 기록용 String
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.logTextView.text = g_logString
        let bottom = NSMakeRange(self.logTextView.text.count - 1, 1)
        self.logTextView.scrollRangeToVisible(bottom)
    }
    
    @IBAction func didTouchCloseButton(_ sender: UIButton) {
        self.dismiss(animated: true)
    }
    
    @IBAction func didTouchTrashButton(_ sender: UIButton) {
        self.logTextView.text = nil
        g_logString = ""
    }
}

/// 콘솔 로그
enum eDirection: Int {
    case UI,SEND,RECEIVE,WEBVIEW,WEB_CONSOL,SCHEME,JAVASCRIPT,OBSERVER,PUSH,ETC,ERROR,DOWNLOAD,UPLOAD
}

var g_logString: String = ""

func log<T>(direction: eDirection,
                        ofType value: T,
                        file: String = #file,
                        function: String = #function,
                        line: Int = #line,
                    datas: Any?...){
#if DEBUG     
    
    let now = Date()
    let formatter = DateFormatter()
    formatter.locale = Locale(identifier:"ko_KR")
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    formatter.timeZone = TimeZone(abbreviation: "KST")
    let time = formatter.string(from: now)
    
    print("\n===============================================================================\n")
    switch direction {
    case .UI:
        print("💚💚💚[ \(direction) LOG : \(String(describing: time)) ]💚💚💚")
    case .SEND:
        print("🔺🔺🔺[ \(direction) LOG : \(String(describing: time)) ]🔺🔺🔺")
    case .RECEIVE:
        print("🔻🔻🔻[ \(direction) LOG : \(String(describing: time)) ]🔻🔻🔻")
    case .WEBVIEW:
        print("💜💜💜[ \(direction) LOG : \(String(describing: time)) ]💜💜💜")
    case .WEB_CONSOL:
        print("👾👾👾[ \(direction) LOG : \(String(describing: time)) ]👾👾👾")
    case .SCHEME:
        print("⚡️⚡️⚡️[ \(direction) LOG : \(String(describing: time)) ]⚡️⚡️⚡️")
    case .JAVASCRIPT:
        print("☕️☕️☕️[ \(direction) LOG : \(String(describing: time)) ]☕️☕️☕️")
    case .OBSERVER:
        print("📩📩📩[ \(direction) LOG : \(String(describing: time)) ]📩📩📩")
    case .PUSH:
        print("📬📬📬[ \(direction) LOG : \(String(describing: time)) ]📬📬📬")
    case .ETC:
        print("💛💛💛[ \(direction) LOG : \(String(describing: time)) ]💛💛💛")
    case .DOWNLOAD:
        print("👎👎👎[ \(direction) LOG : \(String(describing: time)) ]👎👎👎")
    case .UPLOAD:
        print("👍👍👍[ \(direction) LOG : \(String(describing: time)) ]👍👍👍")
    case .ERROR:
        print("☠️☠️☠️[ \(direction) LOG : \(String(describing: time)) ]☠️☠️☠️")
    }
    
    
    g_logString += "\n\n[ \(direction) LOG : \(String(describing: time)) ]"
//    logString += "\nLog File : \(file)"
    g_logString += "\n[Log ClassName] \(type(of: value as Any))"
    g_logString += "\n[Log Function ( \(line) Line )] \(function)"
    
    print("Log File : \(file)")
    print("\n[Log ClassName] \(type(of: value as Any))")
    print("[Log Function ( \(line) Line )] \(function)")
    
    if datas[0] != nil {
        var index = 1
        print("\n[Log Data]")
        g_logString += "\n[Log Data]"
        for data in datas {
            print("Data Value[\(index)] : \(String(describing: data ?? "NULL"))")
            g_logString += "\nData Value[\(index)] : \(String(describing: data ?? "NULL"))"
            index += 1
        }
    }
    
    print("\n===============================================================================")
    
#endif
}

 

+ Recent posts