Change in osmo-gsm-tester[master]: process: add get_output_mark() and grep_output()

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

neels gerrit-no-reply at lists.osmocom.org
Thu Dec 3 23:56:22 UTC 2020


neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-gsm-tester/+/21512 )


Change subject: process: add get_output_mark() and grep_output()
......................................................................

process: add get_output_mark() and grep_output()

Allow showing log lines matching specific regexes, from a specific start
point of a log.

My use case is to echo the handover related logging after an expected
handover failed, so that the reason is visible already in the console
output of a jenkins run. So far I would need to open the endless bsc log
and look up the matching place in it to get a conclusion about why a
handover failed.

Change-Id: Ib6569f7486e9d961bd79a5f24232e58d053667a1
---
M src/osmo_gsm_tester/core/process.py
1 file changed, 46 insertions(+), 5 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-gsm-tester refs/changes/12/21512/1

diff --git a/src/osmo_gsm_tester/core/process.py b/src/osmo_gsm_tester/core/process.py
index 0b96f2e..7e8641a 100644
--- a/src/osmo_gsm_tester/core/process.py
+++ b/src/osmo_gsm_tester/core/process.py
@@ -23,6 +23,7 @@
 import signal
 from abc import ABCMeta, abstractmethod
 from datetime import datetime
+import re
 
 from . import log
 from .event_loop import MainLoop
@@ -320,12 +321,23 @@
             self.poll()
         return self.process_obj is not None and self.result is None
 
-    def get_output(self, which):
+    @staticmethod
+    def end_ansi_colors(txt):
+        '''Make sure no ANSI colors leak out of logging output'''
+        color_off = '\033[0;m'
+        color_any = '\033['
+        if txt.rfind(color_any) > txt.rfind(color_off):
+            return txt + color_off
+        return txt
+
+    def get_output(self, which, since_mark=0):
         ''' Read process output '''
         path = self.get_output_file(which)
         if path is None:
             return None
         with open(path, 'r') as f2:
+            if since_mark > 0:
+                f2.seek(since_mark)
             return f2.read()
 
     def get_output_file(self, which):
@@ -344,11 +356,40 @@
         tail = min(len(out), tail)
         return prefix + ('\n' + prefix).join(out[-tail:])
 
-    def get_stdout(self):
-        return self.get_output('stdout')
+    def get_output_mark(self, which):
+        out = self.get_output(which)
+        if not out:
+            return None
+        return len(out)
 
-    def get_stderr(self):
-        return self.get_output('stderr')
+    def grep_output(self, which, regex, since_mark=0, line_nrs=False):
+        lines = self.get_output(which, since_mark=since_mark).splitlines()
+        if not lines:
+            return None
+        matches = []
+        r = re.compile(regex)
+        line_nr = since_mark
+        for line in lines:
+            line_nr += 1
+            if r.search(line):
+                line = self.end_ansi_colors(line)
+                if line_nrs:
+                    matches.append((line_nr, line))
+                else:
+                    matches.append(line)
+        return matches
+
+    def get_stdout(self, since_mark=0):
+        return self.get_output('stdout', since_mark=since_mark)
+
+    def get_stderr(self, since_mark=0):
+        return self.get_output('stderr', since_mark=since_mark)
+
+    def get_stdout_mark(self):
+        return self.get_output_mark('stdout')
+
+    def get_stderr_mark(self):
+        return self.get_output_mark('stderr')
 
     def get_stdout_tail(self, tail=10, prefix=''):
         return self.get_output_tail('stdout', tail, prefix)

-- 
To view, visit https://gerrit.osmocom.org/c/osmo-gsm-tester/+/21512
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-gsm-tester
Gerrit-Branch: master
Gerrit-Change-Id: Ib6569f7486e9d961bd79a5f24232e58d053667a1
Gerrit-Change-Number: 21512
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20201203/f8e94a5e/attachment.htm>


More information about the gerrit-log mailing list