diff --git a/tools/test-networkmanager-service.py b/tools/test-networkmanager-service.py index 9002f26b36..8221804143 100755 --- a/tools/test-networkmanager-service.py +++ b/tools/test-networkmanager-service.py @@ -288,8 +288,15 @@ class Util: return (Util.ip_addr_ntop(a, family), family) @staticmethod - def eprint(*args, **kwargs): - print(*args, file=sys.stderr, **kwargs) + def log(message): + if gl.log_file: + try: + gl.log_file.write(message + "\n") + gl.log_file.flush() + except Exception: + pass + else: + print(message, file=sys.stderr) @staticmethod def variant_from_dbus(val): @@ -308,9 +315,9 @@ class Util: if isinstance(val, dbus.Array): try: if val.signature == "s": - return GLib.Variant("as", [Util.variant_from_dbus(x) for x in val]) + return GLib.Variant("as", [str(x) for x in val]) if val.signature == "b": - return GLib.Variant("ab", [Util.variant_from_dbus(x) for x in val]) + return GLib.Variant("ab", [bool(x) for x in val]) if val.signature == "y": return GLib.Variant("ay", [int(x) for x in val]) if val.signature == "u": @@ -342,8 +349,7 @@ class Util: ) except Exception as e: raise Exception( - "Cannot convert array element to type '%s': %s" - % (val.signature, e.message) + "Cannot convert array element to type '%s': %s" % (val.signature, e) ) if isinstance(val, dbus.Dictionary): if val.signature == "ss": @@ -2884,6 +2890,26 @@ class ObjectManager(dbus.service.Object): return managed_objects +def setup_log_file(): + """ + Get environment variable for the log file , if any + We accept a %p placeholder to replace with the pid of the current + process + """ + try: + log_file_name = os.environ["NM_TEST_NETWORKMANAGER_SERVICE_LOGFILE"] + except KeyError: + return None + if "%p" in log_file_name: + log_file_name = log_file_name.replace("%p", str(os.getpid())) + try: + log_file = open(log_file_name, "w") + except Exception: + log_file = None + + return log_file + + ############################################################################### @@ -2895,6 +2921,7 @@ def main(): global gl gl = Global() + gl.log_file = setup_log_file() gl.mainloop = GLib.MainLoop() gl.bus = dbus.SessionBus() gl.force_activation_failure = {} @@ -2927,7 +2954,8 @@ def main(): gl.settings.unexport() gl.manager.unexport() gl.object_manager.remove_from_connection() - + if gl.log_file: + gl.log_file.close() sys.exit(0)