added filtering to "logs" plugin - helpful for user reports and for unittests

This commit is contained in:
lars 2006-11-29 13:39:51 +00:00
parent 8cc788ec8f
commit ec73772334
2 changed files with 35 additions and 13 deletions

View file

@ -8,8 +8,24 @@ class logs(cryptobox.plugins.base.CryptoBoxPlugin):
requestAuth = False
rank = 90
def doAction(self):
self.__prepareFormData()
def doAction(self, lines=50, size=3000, pattern=None):
import re
## filter input
try:
lines = int(lines)
if lines < 0: raise(ValueError)
except ValueError:
lines = 50
try:
size = int(size)
if size < 0: raise(ValueError)
except ValueError:
size = 3000
if not pattern is None:
pattern = str(pattern)
if re.search(u'\W', pattern): pattern = None
self.hdf[self.hdf_prefix + "Content"] = self.__getLogContent(lines, size, pattern)
self.hdf[self.hdf_prefix + "StyleSheetFile"] = os.path.abspath(os.path.join(self.pluginDir, "logs.css"))
return "show_log"
@ -20,11 +36,18 @@ class logs(cryptobox.plugins.base.CryptoBoxPlugin):
self.cbox.prefs["Log"]["Details"])
def __prepareFormData(self):
self.hdf[self.hdf_prefix + "Content"] = self.__getLogContent()
self.hdf[self.hdf_prefix + "StyleSheetFile"] = os.path.abspath(os.path.join(self.pluginDir, "logs.css"))
def __getLogContent(self, lines=50, maxSize=3000):
return "<br/>".join(self.cbox.getLogData(lines, maxSize))
def __getLogContent(self, lines, maxSize, pattern):
import re
if pattern:
content = []
current_length = 0
for line in self.cbox.getLogData():
if line.find(pattern) != -1:
content.append(line)
current_length += len(line)
if lines and len(content) >=lines: break
if maxSize and current_length >=maxSize: break
else:
content = self.cbox.getLogData(lines, maxSize)
return "<br/>".join(content)

View file

@ -9,13 +9,12 @@ class unittests(cryptobox.web.testclass.WebInterfaceTestClass):
self.cmd.find('class="console"')
def test_write_logs(self):
## we have to assume two things:
## 1) log level is at least "error"
## 2) the log message does not get lost in a possible stream of log messages
log_text = "unittest - just a marker - please ignore"
self.cbox.log.error(log_text)
log_url = self.URL + "logs"
self.register_auth(log_url)
self.cmd.go(log_url)
self.cmd.go(log_url + "?pattern=ERROR")
self.cmd.find(log_text)
## TODO: check the filtering functions