-
Notifications
You must be signed in to change notification settings - Fork 4
/
LogViewer.py
251 lines (236 loc) · 10.4 KB
/
LogViewer.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QPlainTextEdit, QVBoxLayout, QHBoxLayout
from PyQt5 import QtGui, QtCore,QtWidgets
import gzip
import re
from rdsLoglib import rbktimetodate
class LogViewer(QWidget):
hiddened = QtCore.pyqtSignal('PyQt_PyObject')
moveHereSignal = QtCore.pyqtSignal('PyQt_PyObject')
def __init__(self):
super().__init__()
self.lines = []
self.title = "LogViewer"
self.InitWindow()
self.resize(600,800)
self.moveHere_flag = False
self.less_fig=[]
def InitWindow(self):
self.setWindowTitle(self.title)
vbox = QVBoxLayout()
self.plainText = QPlainTextEdit()
self.plainText.setPlaceholderText("This is LogViewer")
self.plainText.setReadOnly(True)
self.plainText.setUndoRedoEnabled(False)
self.plainText.setLineWrapMode(QPlainTextEdit.NoWrap)
self.plainText.setBackgroundVisible(True)
self.plainText.ensureCursorVisible()
self.plainText.contextMenuEvent = self.contextMenuEvent
hbox = QHBoxLayout()
self.find_edit = QtWidgets.QLineEdit()
self.find_up = QtWidgets.QPushButton("Up")
self.find_up.clicked.connect(self.findUp)
self.find_down = QtWidgets.QPushButton("Down")
self.find_down.clicked.connect(self.findDown)
self.less_btn = QtWidgets.QPushButton("Less")
self.less_btn.clicked.connect(self.less)
self.case_btn = QtWidgets.QPushButton("Ignore \n Case")
self.case_btn.setCheckable(True)
self.case_btn.clicked.connect(self.caseChange)
self.reg_btn = QtWidgets.QPushButton("Disable \n Reg")
self.reg_btn.setCheckable(True)
self.reg_btn.clicked.connect(self.regChange)
hbox.addWidget(self.find_edit)
hbox.addWidget(self.find_up)
hbox.addWidget(self.find_down)
hbox.addWidget(self.less_btn)
hbox.addWidget(self.case_btn)
hbox.addWidget(self.reg_btn)
vbox.addWidget(self.plainText)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.find_cursor = None
self.find_set_cursor = None
self.highlightFormat = QtGui.QTextCharFormat()
self.highlightFormat.setForeground(QtGui.QColor("red"))
self.plainText.cursorPositionChanged.connect(self.cursorChanged)
self.last_cursor = None
def setText(self, lines):
self.plainText.setPlainText(''.join(lines))
def setLineNum(self, ln):
print("moveHere_flag", self.moveHere_flag)
if not self.moveHere_flag:
cursor = QtGui.QTextCursor(self.plainText.document().findBlockByLineNumber(ln))
self.plainText.setTextCursor(cursor)
else:
self.moveHere_flag = False
def closeEvent(self,event):
self.hide()
self.hiddened.emit(True)
def readFilies(self,files):
for file in files:
if os.path.exists(file):
if file.endswith(".log"):
try:
with open(file,'rb') as f:
self.readData(f,file)
except:
continue
else:
try:
with gzip.open(file,'rb') as f:
self.readData(f, file)
except:
continue
self.setText(self.lines)
# def mousePressEvent(self, event):
# self.popMenu = self.plainText.createStandardContextMenu()
# self.popMenu.addAction('&Move Here',self.moveHere)
# cursor = QtGui.QCursor()
# self.popMenu.exec_(cursor.pos())
def contextMenuEvent(self, event):
popMenu = self.plainText.createStandardContextMenu()
popMenu.addAction('&Move Here',self.moveHere)
cursor = QtGui.QCursor()
popMenu.exec_(cursor.pos())
def moveHere(self):
cur_cursor = self.plainText.textCursor()
cur_cursor.select(QtGui.QTextCursor.LineUnderCursor)
line = cur_cursor.selectedText()
regex = re.compile("\[(.*?)\].*")
out = regex.match(line)
if out:
self.moveHere_flag = True
mtime = rbktimetodate(out.group(1))
self.moveHereSignal.emit(mtime)
def moveHereWithTime(self, mtime):
self.moveHereSignal.emit(mtime)
def readData(self, f, file):
for line in f.readlines():
try:
line = line.decode('utf-8')
except UnicodeDecodeError:
try:
line = line.decode('gbk')
except UnicodeDecodeError:
print("{} {}:{}".format(file,"Skipped due to decoding failure!", line))
continue
self.lines.append(line)
def findUp(self):
searchStr = self.find_edit.text()
if searchStr != "":
if self.reg_btn.isChecked():
searchStr = QtCore.QRegularExpression(searchStr)
doc = self.plainText.document()
cur_highlightCursor = self.plainText.textCursor()
if self.find_cursor:
if self.find_set_cursor and \
self.find_set_cursor.position() == cur_highlightCursor.position():
cur_highlightCursor = QtGui.QTextCursor(self.find_cursor)
cur_highlightCursor.setPosition(cur_highlightCursor.anchor())
if self.case_btn.isChecked() == False:
cur_highlightCursor = doc.find(searchStr, cur_highlightCursor, QtGui.QTextDocument.FindBackward)
else:
cur_highlightCursor = doc.find(searchStr, cur_highlightCursor, QtGui.QTextDocument.FindBackward|QtGui.QTextDocument.FindCaseSensitively)
if cur_highlightCursor.position() >= 0:
if self.find_cursor:
fmt = QtGui.QTextCharFormat()
self.find_cursor.setCharFormat(fmt)
cur_highlightCursor.movePosition(QtGui.QTextCursor.NoMove,QtGui.QTextCursor.KeepAnchor)
cur_highlightCursor.mergeCharFormat(self.highlightFormat)
self.find_cursor = QtGui.QTextCursor(cur_highlightCursor)
cur_highlightCursor.setPosition(cur_highlightCursor.anchor())
self.find_set_cursor = cur_highlightCursor
self.plainText.setTextCursor(cur_highlightCursor)
def findDown(self):
searchStr = self.find_edit.text()
if searchStr != "":
if self.reg_btn.isChecked():
searchStr = QtCore.QRegularExpression(searchStr)
doc = self.plainText.document()
cur_highlightCursor = self.plainText.textCursor()
if self.find_cursor:
if self.find_set_cursor and \
cur_highlightCursor.position() == self.find_set_cursor.position():
cur_highlightCursor = QtGui.QTextCursor(self.find_cursor)
cur_highlightCursor.clearSelection()
if self.case_btn.isChecked() == False:
cur_highlightCursor = doc.find(searchStr, cur_highlightCursor)
else:
cur_highlightCursor = doc.find(searchStr, cur_highlightCursor, QtGui.QTextDocument.FindCaseSensitively)
if cur_highlightCursor.position()>=0:
if self.find_cursor:
fmt = QtGui.QTextCharFormat()
self.find_cursor.setCharFormat(fmt)
cur_highlightCursor.movePosition(QtGui.QTextCursor.NoMove,QtGui.QTextCursor.KeepAnchor)
cur_highlightCursor.setCharFormat(self.highlightFormat)
self.find_cursor = QtGui.QTextCursor(cur_highlightCursor)
cur_highlightCursor.clearSelection()
self.find_set_cursor = cur_highlightCursor
self.plainText.setTextCursor(cur_highlightCursor)
def cursorChanged(self):
fmt= QtGui.QTextBlockFormat()
fmt.setBackground(QtGui.QColor("light blue"))
cur_cursor = self.plainText.textCursor()
cur_cursor.select(QtGui.QTextCursor.LineUnderCursor)
cur_cursor.setBlockFormat(fmt)
if self.last_cursor:
if cur_cursor.blockNumber() != self.last_cursor.blockNumber():
fmt = QtGui.QTextBlockFormat()
self.last_cursor.select(QtGui.QTextCursor.LineUnderCursor)
self.last_cursor.setBlockFormat(fmt)
self.last_cursor = self.plainText.textCursor()
def caseChange(self):
if self.case_btn.isChecked():
self.case_btn.setText("Match \n Case")
else:
self.case_btn.setText("Ignore \n Case")
def regChange(self):
if self.reg_btn.isChecked():
self.reg_btn.setText("Enable \n Reg")
else:
self.reg_btn.setText("Disable \n Reg")
def less(self):
searchStr = self.find_edit.text()
if searchStr == "":
return
lines = []
doc = self.plainText.document()
if searchStr != "":
if self.reg_btn.isChecked():
searchStr = QtCore.QRegularExpression(searchStr)
if self.case_btn.isChecked() == False:
cursor = doc.find(searchStr, 0)
else:
cursor = doc.find(searchStr, 0, QtGui.QTextDocument.FindCaseSensitively)
while cursor.blockNumber() > 0:
if self.case_btn.isChecked() == False:
cursor = doc.find(searchStr, cursor)
else:
cursor = doc.find(searchStr, cursor, QtGui.QTextDocument.FindCaseSensitively)
if cursor.blockNumber() < 1:
break
next_pos = cursor.block().next().position()
if next_pos < 1:
break
lines.append(cursor.block().text()+'\n')
cursor.setPosition(next_pos)
print("line size:",len(lines))
if len(lines) < 1:
return
new_lg = LogViewer()
new_lg.setWindowTitle(self.find_edit.text())
new_lg.setWindowIcon(QtGui.QIcon('rds.ico'))
new_lg.moveHereSignal.connect(self.moveHereWithTime)
new_lg.setText(lines)
self.less_fig.append(new_lg)
new_lg.show()
if __name__ == "__main__":
import sys
import os
app = QApplication(sys.argv)
view = LogViewer()
filenames = ["test1.log"]
view.readFilies(filenames)
view.show()
app.exec_()