Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add app log in Diagnostics screen #234

Merged
merged 7 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor log constants
  • Loading branch information
keeshux committed Oct 15, 2022
commit ca0dd620cb7a8758fca635a6e0e855eec4c013d3
40 changes: 22 additions & 18 deletions Passepartout/App/Constants/Constants+App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,36 +140,40 @@ extension Constants {
}

enum Log {
private static let parentPath = "Library/Caches"
enum App {
static let url = containerURL(filename: "App.log")

private static func containerLogURL(filename: String) -> URL {
Files.containerURL
.appendingPathComponent(parentPath)
.appendingPathComponent(filename)
static let format = "$DHH:mm:ss.SSS$d $C$L$c $N.$F:$l - $M"
}

enum Tunnel {
static let path = containerPath(filename: "Tunnel.log")

private static func containerLogPath(filename: String) -> String {
"\(parentPath)/\(filename)"
static let format = "$DHH:mm:ss$d - $M"
}

private static let parentPath = "Library/Caches"

static let appLogURL = containerLogURL(filename: "App.log")

static let tunnelLogPath = containerLogPath(filename: "Tunnel.log")

static let logLevel: SwiftyBeaver.Level = {
static let level: SwiftyBeaver.Level = {
guard let levelString = ProcessInfo.processInfo.environment["LOG_LEVEL"], let levelNum = Int(levelString) else {
return .info
}
return .init(rawValue: levelNum) ?? .info
}()

static let logFormat = "$DHH:mm:ss.SSS$d $C$L$c $N.$F:$l - $M"
static let maxBytes = 100000

static let tunnelLogFormat = "$DHH:mm:ss$d - $M"

static let tunnelLogMaxBytes = 100000

static let tunnelLogRefreshInterval: TimeInterval = 5.0
static let refreshInterval: TimeInterval = 5.0

private static func containerURL(filename: String) -> URL {
Files.containerURL
.appendingPathComponent(parentPath)
.appendingPathComponent(filename)
}

private static func containerPath(filename: String) -> String {
"\(parentPath)/\(filename)"
}
}

enum URLs {
Expand Down
6 changes: 3 additions & 3 deletions Passepartout/App/Context/AppContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class AppContext {
private var cancellables: Set<AnyCancellable> = []

init(coreContext: CoreContext) {
logManager = LogManager(logFile: Constants.Log.appLogURL)
logManager.logLevel = Constants.Log.logLevel
logManager.logFormat = Constants.Log.logFormat
logManager = LogManager(logFile: Constants.Log.App.url)
logManager.logLevel = Constants.Log.level
logManager.logFormat = Constants.Log.App.format
logManager.configureLogging()
pp_log.info("Logging to: \(logManager.logFile!)")

Expand Down
6 changes: 3 additions & 3 deletions Passepartout/App/Views/DebugLogView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ struct DebugLogView: View {

@State private var isSharing = false

private let maxBytes = UInt64(Constants.Log.tunnelLogMaxBytes)
private let maxBytes = UInt64(Constants.Log.maxBytes)

private let appName = Constants.Global.appName

private let appVersion = Constants.Global.appVersionString

private let shareFilename = Unlocalized.Issues.Filenames.debugLog

init(title: String, url: URL, updateInterval: TimeInterval) {
init(title: String, url: URL, refreshInterval: TimeInterval) {
self.title = title
self.url = url
timer = Timer.TimerPublisher(interval: updateInterval, runLoop: .main, mode: .common)
timer = Timer.TimerPublisher(interval: refreshInterval, runLoop: .main, mode: .common)
.autoconnect()
.eraseToAnyPublisher()
}
Expand Down
10 changes: 5 additions & 5 deletions Passepartout/App/Views/DiagnosticsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extension DiagnosticsView {

let tunnelLogURL: URL?

private let logUpdateInterval = Constants.Log.tunnelLogRefreshInterval
private let refreshInterval = Constants.Log.refreshInterval

var body: some View {
appLink
Expand All @@ -65,25 +65,25 @@ extension DiagnosticsView {
navigationLink(
withTitle: L10n.Diagnostics.Items.AppLog.title,
url: appLogURL,
updateInterval: logUpdateInterval
refreshInterval: refreshInterval
)
}

private var tunnelLink: some View {
navigationLink(
withTitle: L10n.Diagnostics.Items.TunnelLog.title,
url: tunnelLogURL,
updateInterval: logUpdateInterval
refreshInterval: refreshInterval
)
}

private func navigationLink(withTitle title: String, url: URL?, updateInterval: Double) -> some View {
private func navigationLink(withTitle title: String, url: URL?, refreshInterval: TimeInterval) -> some View {
NavigationLink(title) {
url.map {
DebugLogView(
title: title,
url: $0,
updateInterval: updateInterval
refreshInterval: refreshInterval
)
}
}.disabled(url == nil)
Expand Down
4 changes: 2 additions & 2 deletions Passepartout/AppShared/Context/CoreContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ class CoreContext {

private func configureObjects() {
providerManager.rateLimitMilliseconds = Constants.RateLimit.providerManager
vpnManager.tunnelLogPath = Constants.Log.tunnelLogPath
vpnManager.tunnelLogFormat = Constants.Log.tunnelLogFormat
vpnManager.tunnelLogPath = Constants.Log.Tunnel.path
vpnManager.tunnelLogFormat = Constants.Log.Tunnel.format

profileManager.observeUpdates()
vpnManager.observeUpdates()
Expand Down