diff --git a/plugins/logs/logs.py b/plugins/logs/logs.py index 6bb6b6b..36cd14a 100644 --- a/plugins/logs/logs.py +++ b/plugins/logs/logs.py @@ -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 "
".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 "
".join(content) diff --git a/plugins/logs/unittests.py b/plugins/logs/unittests.py index 68cb8ef..d63f515 100644 --- a/plugins/logs/unittests.py +++ b/plugins/logs/unittests.py @@ -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 +