Add log level filtering
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import SwiftUI
|
|
||||||
import Library
|
import Library
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
public struct LogView: View {
|
public struct LogView: View {
|
||||||
@Environment(\.selection) private var selection
|
@Environment(\.selection) private var selection
|
||||||
@@ -14,51 +14,29 @@ public struct LogView: View {
|
|||||||
private struct LogView0: View {
|
private struct LogView0: View {
|
||||||
@EnvironmentObject private var environments: ExtensionEnvironments
|
@EnvironmentObject private var environments: ExtensionEnvironments
|
||||||
@EnvironmentObject private var commandClient: CommandClient
|
@EnvironmentObject private var commandClient: CommandClient
|
||||||
|
@State private var selectedLogLevel: Int?
|
||||||
private let logFont = Font.system(.caption2, design: .monospaced)
|
private let logFont = Font.system(.caption2, design: .monospaced)
|
||||||
|
|
||||||
|
private var filteredLogList: [LogEntry] {
|
||||||
|
let effectiveLevel = selectedLogLevel ?? commandClient.defaultLogLevel
|
||||||
|
return commandClient.logList.filter { $0.level <= effectiveLevel }
|
||||||
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
if ApplicationLibrary.inPreview {
|
Group {
|
||||||
let logList = [
|
if ApplicationLibrary.inPreview {
|
||||||
"(packet-tunnel) log server started",
|
let logList = [
|
||||||
"INFO[0000] router: loaded geoip database: 250 codes",
|
"(packet-tunnel) log server started",
|
||||||
"INFO[0000] router: loaded geosite database: 1400 codes",
|
"INFO[0000] router: loaded geoip database: 250 codes",
|
||||||
"INFO[0000] router: updated default interface en0, index 11",
|
"INFO[0000] router: loaded geosite database: 1400 codes",
|
||||||
"inbound/tun[0]: started at utun3",
|
"INFO[0000] router: updated default interface en0, index 11",
|
||||||
"sing-box started (1.666s)",
|
"inbound/tun[0]: started at utun3",
|
||||||
]
|
"sing-box started (1.666s)",
|
||||||
ScrollView {
|
]
|
||||||
LazyVStack(alignment: .leading, spacing: 8) {
|
|
||||||
ForEach(logList.indices, id: \.self) { index in
|
|
||||||
Text(ANSIColors.parseAnsiString(logList[index]))
|
|
||||||
.font(logFont)
|
|
||||||
#if os(tvOS)
|
|
||||||
.focusable()
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
|
||||||
.padding()
|
|
||||||
}
|
|
||||||
#if os(tvOS)
|
|
||||||
.focusEffectDisabled()
|
|
||||||
.focusSection()
|
|
||||||
#endif
|
|
||||||
} else if commandClient.logList.isEmpty {
|
|
||||||
VStack {
|
|
||||||
if commandClient.isConnected {
|
|
||||||
Text("Empty logs")
|
|
||||||
} else {
|
|
||||||
Text("Service not started").onAppear {
|
|
||||||
environments.connect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ScrollViewReader { reader in
|
|
||||||
ScrollView {
|
ScrollView {
|
||||||
LazyVStack(alignment: .leading, spacing: 8) {
|
LazyVStack(alignment: .leading, spacing: 8) {
|
||||||
ForEach(commandClient.logList.indices, id: \.self) { index in
|
ForEach(logList.indices, id: \.self) { index in
|
||||||
Text(ANSIColors.parseAnsiString(commandClient.logList[index]))
|
Text(ANSIColors.parseAnsiString(logList[index]))
|
||||||
.font(logFont)
|
.font(logFont)
|
||||||
#if os(tvOS)
|
#if os(tvOS)
|
||||||
.focusable()
|
.focusable()
|
||||||
@@ -72,21 +50,67 @@ public struct LogView: View {
|
|||||||
.focusEffectDisabled()
|
.focusEffectDisabled()
|
||||||
.focusSection()
|
.focusSection()
|
||||||
#endif
|
#endif
|
||||||
.onAppear {
|
} else if commandClient.logList.isEmpty {
|
||||||
if let lastIndex = commandClient.logList.indices.last {
|
VStack {
|
||||||
reader.scrollTo(lastIndex, anchor: .bottom)
|
if commandClient.isConnected {
|
||||||
|
Text("Empty logs")
|
||||||
|
} else {
|
||||||
|
Text("Service not started").onAppear {
|
||||||
|
environments.connect()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onChangeCompat(of: commandClient.logList.count) { _ in
|
} else {
|
||||||
guard let lastIndex = commandClient.logList.indices.last else {
|
ScrollViewReader { reader in
|
||||||
return
|
ScrollView {
|
||||||
|
LazyVStack(alignment: .leading, spacing: 8) {
|
||||||
|
ForEach(filteredLogList) { logEntry in
|
||||||
|
Text(ANSIColors.parseAnsiString(logEntry.message))
|
||||||
|
.font(logFont)
|
||||||
|
#if os(tvOS)
|
||||||
|
.focusable()
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
||||||
|
.padding()
|
||||||
}
|
}
|
||||||
withAnimation {
|
#if os(tvOS)
|
||||||
reader.scrollTo(lastIndex, anchor: .bottom)
|
.focusEffectDisabled()
|
||||||
|
.focusSection()
|
||||||
|
#endif
|
||||||
|
.onAppear {
|
||||||
|
if let lastEntry = filteredLogList.last {
|
||||||
|
reader.scrollTo(lastEntry.id, anchor: .bottom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.onChangeCompat(of: filteredLogList.count) { _ in
|
||||||
|
guard let lastEntry = filteredLogList.last else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
withAnimation {
|
||||||
|
reader.scrollTo(lastEntry.id, anchor: .bottom)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#if !os(tvOS)
|
||||||
|
.toolbar {
|
||||||
|
ToolbarItem {
|
||||||
|
Menu {
|
||||||
|
Picker("Log Level", selection: $selectedLogLevel) {
|
||||||
|
Text(NSLocalizedString("Default", comment: "Log level filter default option")).tag(Int?.none)
|
||||||
|
ForEach(LogLevel.allCases) { level in
|
||||||
|
Text(level.name).tag(Int?.some(level.rawValue))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} label: {
|
||||||
|
Label("Filter", systemImage: "line.3.horizontal.circle")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,39 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import Libbox
|
import Libbox
|
||||||
|
|
||||||
|
public struct LogEntry: Identifiable {
|
||||||
|
public let id = UUID()
|
||||||
|
public let level: Int
|
||||||
|
public let message: String
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum LogLevel: Int, CaseIterable, Identifiable {
|
||||||
|
public var id: Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
case error = 2
|
||||||
|
case warn = 3
|
||||||
|
case info = 4
|
||||||
|
case debug = 5
|
||||||
|
case trace = 6
|
||||||
|
|
||||||
|
public var name: String {
|
||||||
|
switch self {
|
||||||
|
case .error:
|
||||||
|
return "Error"
|
||||||
|
case .warn:
|
||||||
|
return "Warn"
|
||||||
|
case .info:
|
||||||
|
return "Info"
|
||||||
|
case .debug:
|
||||||
|
return "Debug"
|
||||||
|
case .trace:
|
||||||
|
return "Trace"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class CommandClient: ObservableObject {
|
public class CommandClient: ObservableObject {
|
||||||
public enum ConnectionType {
|
public enum ConnectionType {
|
||||||
case status
|
case status
|
||||||
@@ -17,8 +50,9 @@ public class CommandClient: ObservableObject {
|
|||||||
@Published public var isConnected: Bool
|
@Published public var isConnected: Bool
|
||||||
@Published public var status: LibboxStatusMessage?
|
@Published public var status: LibboxStatusMessage?
|
||||||
@Published public var groups: [LibboxOutboundGroup]?
|
@Published public var groups: [LibboxOutboundGroup]?
|
||||||
@Published public var logList: [String]
|
@Published public var logList: [LogEntry]
|
||||||
@Published public var defaultLogLevel: Int32 = 0
|
@Published public var defaultLogLevel = 0
|
||||||
|
@Published public var selectedLogLevel: Int?
|
||||||
@Published public var clashModeList: [String]
|
@Published public var clashModeList: [String]
|
||||||
@Published public var clashMode: String
|
@Published public var clashMode: String
|
||||||
|
|
||||||
@@ -164,7 +198,7 @@ public class CommandClient: ObservableObject {
|
|||||||
|
|
||||||
func setDefaultLogLevel(_ level: Int32) {
|
func setDefaultLogLevel(_ level: Int32) {
|
||||||
DispatchQueue.main.async { [self] in
|
DispatchQueue.main.async { [self] in
|
||||||
commandClient.defaultLogLevel = level
|
commandClient.defaultLogLevel = Int(level)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,17 +213,13 @@ public class CommandClient: ObservableObject {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
DispatchQueue.main.async { [self] in
|
DispatchQueue.main.async { [self] in
|
||||||
var newLogList = commandClient.logList
|
|
||||||
while messageList.hasNext() {
|
while messageList.hasNext() {
|
||||||
let logEntry = messageList.next()!
|
let logEntry = messageList.next()!
|
||||||
if logEntry.level <= commandClient.defaultLogLevel {
|
commandClient.logList.append(LogEntry(level: Int(logEntry.level), message: logEntry.message))
|
||||||
newLogList.append(logEntry.message)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if newLogList.count >= commandClient.logMaxLines {
|
if commandClient.logList.count >= commandClient.logMaxLines {
|
||||||
newLogList.removeSubrange(0 ... newLogList.count - commandClient.logMaxLines)
|
commandClient.logList.removeSubrange(0 ... commandClient.logList.count - commandClient.logMaxLines)
|
||||||
}
|
}
|
||||||
commandClient.logList = newLogList
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -375,6 +375,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"Default" : {
|
||||||
|
|
||||||
},
|
},
|
||||||
"Delete" : {
|
"Delete" : {
|
||||||
"localizations" : {
|
"localizations" : {
|
||||||
@@ -931,6 +934,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"Log Level" : {
|
||||||
|
|
||||||
},
|
},
|
||||||
"Logs" : {
|
"Logs" : {
|
||||||
"localizations" : {
|
"localizations" : {
|
||||||
|
|||||||
Reference in New Issue
Block a user