]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
tests: py: print_msg refactor
authorMáté Eckl <ecklm94@gmail.com>
Thu, 17 May 2018 07:36:59 +0000 (09:36 +0200)
committerFlorian Westphal <fw@strlen.de>
Thu, 17 May 2018 13:35:22 +0000 (15:35 +0200)
The errstr attribute was hard-coded to "ERROR:"

errstr has been moved in the parameter list. As print_msg is only
used from the other print_* this is not an issue, and as there is a
print_error function, I don't think that strerr should default to
"ERROR:".

Also this kind of messages now get written to stderr. This can be
beneficial if someone wants to redirect output to a file.

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
tests/py/nft-test.py

index 88a3b2181aba29549e23f2671c9d84d50ae96356..2be4700463b7ff3a80d96ab54d88d6c558c7f737 100755 (executable)
@@ -104,23 +104,25 @@ class Obj:
         return self.__dict__ == other.__dict__
 
 
-def print_msg(reason, filename=None, lineno=None, color=None, errstr=None):
+def print_msg(reason, errstr, filename=None, lineno=None, color=None):
     '''
     Prints a message with nice colors, indicating file and line number.
     '''
     if filename and lineno:
-        print filename + ": " + color + "ERROR:" + Colors.ENDC + \
-              " line %d: %s" % (lineno + 1, reason)
+        sys.stderr.write(filename + ": " + color + errstr + Colors.ENDC + \
+              " line %d: %s" % (lineno + 1, reason))
     else:
-        print color + "ERROR:" + Colors.ENDC + " %s" % reason
+        sys.stderr.write(color + errstr + Colors.ENDC + " %s" % reason)
+    sys.stderr.write("\n")
+    sys.stderr.flush() # So that the message stay in the right place.
 
 
 def print_error(reason, filename=None, lineno=None):
-    print_msg(reason, filename, lineno, Colors.RED, "ERROR:")
+    print_msg(reason, "ERROR:", filename, lineno, Colors.RED)
 
 
 def print_warning(reason, filename=None, lineno=None):
-    print_msg(reason, filename, lineno, Colors.YELLOW, "WARNING:")
+    print_msg(reason, "WARNING:", filename, lineno, Colors.YELLOW)
 
 
 def color_differences(rule, other, color):