passepartout-apple/Passepartout/App/iOS/Scenes/DebugLogViewController.swift

149 lines
4.7 KiB
Swift
Raw Normal View History

2018-10-11 07:13:19 +00:00
//
// DebugLogViewController.swift
2021-01-01 16:53:28 +00:00
// Passepartout
2018-10-11 07:13:19 +00:00
//
// Created by Davide De Rosa on 6/12/18.
2020-12-27 16:30:25 +00:00
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
2018-10-11 07:13:19 +00:00
//
2018-11-03 21:33:30 +00:00
// https://github.com/passepartoutvpn
2018-10-11 07:13:19 +00:00
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
import UIKit
import SwiftyBeaver
import PassepartoutCore
2020-06-12 21:30:57 +00:00
import TunnelKit
2018-10-11 07:13:19 +00:00
private let log = SwiftyBeaver.self
class DebugLogViewController: UIViewController {
2019-04-29 15:49:48 +00:00
@IBOutlet private weak var textLog: UITextView?
2018-10-11 07:13:19 +00:00
private let vpn = VPN.shared
deinit {
NotificationCenter.default.removeObserver(self)
}
2019-04-29 15:49:48 +00:00
// MARK: UIViewController
2018-10-11 07:13:19 +00:00
override func viewDidLoad() {
super.viewDidLoad()
title = L10n.Core.Service.Cells.DebugLog.caption
2019-04-29 15:49:48 +00:00
textLog?.contentInsetAdjustmentBehavior = .never
2018-10-11 07:13:19 +00:00
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(toggleBars))
textLog?.addGestureRecognizer(tapGestureRecognizer)
2020-06-12 21:30:57 +00:00
NotificationCenter.default.addObserver(self, selector: #selector(vpnDidPrepare), name: VPN.didPrepare, object: nil)
2018-10-11 07:13:19 +00:00
if vpn.isPrepared {
startRefreshingLog()
}
}
2019-04-29 15:50:03 +00:00
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.isToolbarHidden = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.isToolbarHidden = true
}
2019-04-29 15:49:48 +00:00
// MARK: Actions
@objc private func toggleBars() {
let nav = navigationController
let isHidden = nav?.isToolbarHidden ?? true
// nav?.setNavigationBarHidden(!isHidden, animated: true)
nav?.setToolbarHidden(!isHidden, animated: true)
}
2018-10-11 07:13:19 +00:00
@IBAction private func share(_ sender: Any?) {
2019-04-29 15:49:48 +00:00
guard let raw = textLog?.text, !raw.isEmpty else {
let alert = UIAlertController.asAlert(title, L10n.Core.DebugLog.Alerts.EmptyLog.message)
alert.addCancelAction(L10n.Core.Global.ok)
2018-10-11 07:13:19 +00:00
present(alert, animated: true, completion: nil)
return
}
let data = DebugLog(raw: raw).decoratedData()
let path = NSTemporaryDirectory().appending(AppConstants.IssueReporter.Filenames.debugLog)
2018-10-11 07:13:19 +00:00
let url = URL(fileURLWithPath: path)
do {
try data.write(to: url)
} catch let e {
log.error("Failed saving temporary debug log file: \(e)")
return
}
let vc = UIActivityViewController(activityItems: [url], applicationActivities: nil)
vc.popoverPresentationController?.barButtonItem = sender as? UIBarButtonItem
vc.completionWithItemsHandler = { (type, completed, items, error) in
try? FileManager.default.removeItem(at: url)
}
present(vc, animated: true, completion: nil)
}
@IBAction private func previousSession() {
2019-04-29 15:49:48 +00:00
textLog?.findPrevious(string: GroupConstants.VPN.sessionMarker)
2018-10-11 07:13:19 +00:00
}
@IBAction private func nextSession() {
2019-04-29 15:49:48 +00:00
textLog?.findNext(string: GroupConstants.VPN.sessionMarker)
2018-10-11 07:13:19 +00:00
}
2019-04-29 15:49:48 +00:00
2018-10-11 07:13:19 +00:00
private func startRefreshingLog() {
vpn.requestDebugLog(fallback: AppConstants.Log.debugSnapshot) {
2019-04-29 15:49:48 +00:00
self.textLog?.text = $0
2018-10-11 07:13:19 +00:00
DispatchQueue.main.async {
2019-04-29 15:49:48 +00:00
self.textLog?.scrollToEnd()
2018-10-11 07:13:19 +00:00
self.refreshLogInBackground()
}
}
}
private func refreshLogInBackground() {
let updateBlock = {
DispatchQueue.main.asyncAfter(deadline: .now() + AppConstants.Log.viewerRefreshInterval) { [weak self] in
self?.refreshLogInBackground()
}
}
// only update if screen is visible
guard let _ = viewIfLoaded?.window else {
updateBlock()
return
}
vpn.requestDebugLog(fallback: AppConstants.Log.debugSnapshot) {
2019-04-29 15:49:48 +00:00
self.textLog?.text = $0
2018-10-11 07:13:19 +00:00
updateBlock()
}
}
// MARK: Notifications
@objc private func vpnDidPrepare() {
startRefreshingLog()
}
}