summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/LogStreamerViewController.swift
blob: fc360781e85608272a1a608964f130e1aa8ddeab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
//  LogStreamerViewController.swift
//  MullvadVPN
//
//  Created by pronebird on 17/08/2020.
//  Copyright © 2020 Mullvad VPN AB. All rights reserved.
//

#if DEBUG

import Foundation
import UIKit
import Logging

class LogStreamerViewController: UIViewController, UITextViewDelegate {

    private let textView = UITextView()
    private let streamer: LogStreamer<UTF8>
    private let logEntryParser = LogEntryParser()
    private var currentTextColor: UIColor?
    private let timestampFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "HH:mm:ss.SSS"
        return formatter
    }()

    private var autoScrollButtonItem: UIBarButtonItem {
        return UIBarButtonItem(barButtonSystemItem: autoScroll ? .pause : .play, target: self, action: #selector(handleToggleAutoscroll(_:)))
    }

    private var dismissButtonItem: UIBarButtonItem {
        return UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(handleDismissButton(_:)))
    }

    var autoScroll: Bool = true {
        didSet {
            updateAutoScrollBarItem()
            handleAutoScroll()
        }
    }

    init(fileURLs: [URL]) {
        streamer = LogStreamer(fileURLs: fileURLs)
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - View lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = NSLocalizedString("App logs", comment: "")
        navigationItem.leftBarButtonItem = autoScrollButtonItem
        navigationItem.rightBarButtonItem = dismissButtonItem

        addSubviews()
        startStreamer()
    }

    // MARK: - UITextViewDelegate

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        let translation = scrollView.panGestureRecognizer.translation(in: scrollView.superview)

        // Disable autoscroll if user scrolled up
        if translation.y > 0 {
            autoScroll = false
        } else if translation.y < 0 {
            // Enable autoscroll if user scrolled to the bottom of the view
            let maxScrollY = scrollView.contentSize.height - scrollView.frame.height

            if targetContentOffset.pointee.y >= maxScrollY {
                autoScroll = true
            }
        }
    }

    func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
        // Disable autoscroll when user requested scroll to top
        autoScroll = false
        return true
    }

    // MARK: - Private

    private func addSubviews() {
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.isEditable = false
        if #available(iOS 13.0, *) {
            textView.font = UIFont.monospacedSystemFont(ofSize: UIFont.systemFontSize, weight: .regular)
        } else {
            textView.font = UIFont(name: "Courier", size: UIFont.systemFontSize)
        }
        textView.delegate = self

        view.addSubview(textView)

        NSLayoutConstraint.activate([
            textView.topAnchor.constraint(equalTo: view.topAnchor),
            textView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            textView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            textView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }

    private func startStreamer() {
        self.streamer.start { [weak self] (str) in
            guard let self = self else { return }

            DispatchQueue.main.async {
                // Try parsing the entry
                let entry = self.logEntryParser.parse(str)

                // Since the log streamer sends the log file line-by-line, it's possible that only a
                // part of a multiline message is captured at first.
                let message = entry.map { (entry) -> String in
                    // Reformat the log entry date
                    let timestamp = self.timestampFormatter.string(from: entry.timestamp)

                    return "\(timestamp) \(entry.module) \(entry.message)\n"
                } ?? "\(str)\n"


                // Compute the range for replacing the text color
                let start = self.textView.text.utf16.count
                let end = start + message.utf16.count
                let textRange = NSRange(start..<end)

                self.textView.insertText(message)
                self.handleAutoScroll()

                // Update the current log entry color
                if let logLevel = entry?.level {
                    self.currentTextColor = self.textColor(for: logLevel)
                }

                // Apply the color attribute to the inserted text
                if let textColor = self.currentTextColor {
                    self.textView.textStorage.addAttributes([.foregroundColor: textColor], range: textRange)
                }
            }
        }
    }

    private func handleAutoScroll() {
        if autoScroll && !textView.isTracking && (!textView.isDragging || textView.isDecelerating) {
            scrollToBottom()
        }
    }

    private func scrollToBottom() {
        let textRange = NSRange(..<textView.text.endIndex, in: textView.text)

        textView.scrollRangeToVisible(textRange)
    }

    private func updateAutoScrollBarItem() {
        navigationItem.leftBarButtonItem = autoScrollButtonItem
    }

    private func textColor(for logLevel: Logger.Level) -> UIColor {
        switch logLevel {
        case .debug, .trace:
            return .lightGray
        case .error, .critical:
            return .red
        case .info, .notice:
            return .blue
        case .warning:
            return .orange
        }
    }

    // MARK: - Actions

    @objc func handleDismissButton(_ sender: Any) {
        dismiss(animated: true)
    }

    @objc func handleToggleAutoscroll(_ sender: Any) {
        autoScroll = !autoScroll
    }
}

#endif