-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraft-i-1.py
126 lines (100 loc) · 4.82 KB
/
draft-i-1.py
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
import tkinter as tk
from collections import OrderedDict
from tkinter import ttk, filedialog, messagebox
class LogViewerApp:
def __init__(self, root):
self.root = root
self.root.title("Log Viewer")
self.root.geometry("950x740") # Set default size
self.filter_options_rand = {
'okhttp': 'green',
'CrashlyticsHelper': 'red',
'Stream': 'blue',
'ViewModel': 'yellow',
'Firebase': 'orange',
'Network': 'gray',
'Camera': 'cyan'
}
self.filter_options = OrderedDict(sorted(self.filter_options_rand.items()))
self.log_entries = []
self.filter_var = tk.StringVar()
self.filter_var.set('Select Filter')
self.filter_dropdown = tk.OptionMenu(root, self.filter_var, *self.filter_options.keys(), 'Select Filter')
self.filter_dropdown.pack(pady=10)
self.filter_var.trace_add('write', self.apply_filter)
self.new_filter_entry = tk.Entry(root, width=20)
self.new_filter_entry.pack(pady=10)
self.apply_new_filter_button = tk.Button(root, text="Apply New Filter", command=self.add_new_filter)
self.apply_new_filter_button.pack(pady=10)
# Frame for Treeview
self.tree_frame = tk.Frame(root)
self.tree_frame.pack(expand=True, fill=tk.BOTH, padx=10, pady=10)
# Treeview and Scrollbars
self.tree = ttk.Treeview(self.tree_frame, columns=('Log',), show='tree', height=20)
self.tree.heading('#0', text='Log Entries')
self.tree.column('#0', width=900, stretch=tk.YES)
# Add scrollbars
self.y_scrollbar = ttk.Scrollbar(self.tree_frame, orient='vertical', command=self.tree.yview)
self.x_scrollbar = ttk.Scrollbar(self.tree_frame, orient='horizontal', command=self.tree.xview)
self.tree.configure(yscrollcommand=self.y_scrollbar.set, xscrollcommand=self.x_scrollbar.set)
self.y_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.x_scrollbar.pack(side=tk.BOTTOM, fill=tk.X)
self.tree.pack(expand=True, fill=tk.BOTH)
self.load_button = tk.Button(root, text="Browse Log File", command=self.browse_file)
self.load_button.pack(pady=10)
def browse_file(self):
file_path = filedialog.askopenfilename(filetypes=[("Log Files", "*.log")])
if file_path:
self.log_entries = self.load_log_file(file_path)
self.display_log()
def load_log_file(self, file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
return file.readlines()
except Exception as e:
return [f"Error loading file: {str(e)}"]
def apply_filter(self, *args):
selected_filter = self.filter_var.get()
if selected_filter != 'Select Filter':
self.display_log(selected_filter)
else:
self.display_log()
def add_new_filter(self):
new_filter = self.new_filter_entry.get()
if new_filter and new_filter not in self.filter_options:
confirmation = messagebox.askquestion("Add Filter", f"Do you want to add '{new_filter}' as a filter?")
if confirmation == 'yes':
txt_color = self.get_unused_color()
self.filter_options[new_filter] = txt_color
self.filter_dropdown['menu'].add_command(label=new_filter, command=tk._setit(self.filter_var, new_filter))
self.filter_var.set(new_filter)
else:
self.display_log(new_filter)
def get_unused_color(self):
used_colors = set(self.filter_options.values())
available_colors = ['green', 'red', 'blue', 'gray', 'yellow', 'cyan', 'orange']
for color in available_colors:
if color not in used_colors:
return color
return 'white'
def display_log(self, filter_keyword=None):
self.tree.delete(*self.tree.get_children())
filter_logs = {}
for line in self.log_entries:
line_lower = line.lower()
for keyword, color in self.filter_options.items():
if keyword.lower() in line_lower and (filter_keyword is None or keyword.lower() == filter_keyword.lower()):
if keyword not in filter_logs:
filter_logs[keyword] = []
filter_logs[keyword].append(line)
break
for keyword, logs in filter_logs.items():
section_id = self.tree.insert('', 'end', text=keyword.title(), open=True)
for log in logs:
self.tree.insert(section_id, tk.END, text=log, tags=(keyword.lower(),))
for color in self.filter_options.values():
self.tree.tag_configure(color, foreground=color)
if __name__ == "__main__":
root = tk.Tk()
app = LogViewerApp(root)
root.mainloop()