diff options
author | Thomas Mangin <thomas.mangin@exa.net.uk> | 2020-04-12 21:46:57 +0100 |
---|---|---|
committer | Thomas Mangin <thomas.mangin@exa.net.uk> | 2020-04-13 15:53:47 +0100 |
commit | 25bf56cb62cc239b28c20512dedd4fa9fa1c8bc5 (patch) | |
tree | 3ecbac75abd8c807aaa21d29e3ce95488ce61107 /python/vyos/util.py | |
parent | 1e6dc1a45dd113dc49d24738d0b324bb67a8d11f (diff) | |
download | vyos-1x-25bf56cb62cc239b28c20512dedd4fa9fa1c8bc5.tar.gz vyos-1x-25bf56cb62cc239b28c20512dedd4fa9fa1c8bc5.zip |
cmd: T2226: improve debugging
allow to setup the debugging from environment variables.
allow to set the name of the file used for logging
change the name of the debug options to be:
- developer: enable pdb of raise
- log: all logging messages are logged to a file
- ifconfig: show on screen action peformed to change intefaces
- command: print all the result of command to screen
also provide a way to setup the debugging using environment
variables.
Diffstat (limited to 'python/vyos/util.py')
-rw-r--r-- | python/vyos/util.py | 55 |
1 files changed, 11 insertions, 44 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py index 14020e2d9..49c47cd85 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -21,48 +21,7 @@ from subprocess import PIPE from subprocess import STDOUT from subprocess import DEVNULL - -def debug(flag): - """ - Check is a debug flag was set by the user. - a flag can be set by touching the file /tmp/vyos.flag.debug - with flag being the flag name, the current flags are: - - developer: the code will drop into PBD on un-handled exception - - ifconfig: prints command and sysfs access on stdout for interface - The function returns an empty string if the flag was not set, - """ - - # this is to force all new flags to be registered here to be documented: - if flag not in ['developer', 'ifconfig']: - return '' - for folder in ('/tmp', '/config'): - if os.path.isfile(f'{folder}/vyos.{flag}.debug'): - return flag - return '' - - -def debug_msg(message, flag=''): - """ - print a debug message line on stdout if debugging is enabled for the flag - """ - - if debug(flag): - print(f'DEBUG/{flag:<6} {message}') - - if not debug('developer'): - return - - logfile = '/tmp/full-log' - existed = os.path.exists(logfile) - - with open(logfile, 'a') as f: - f.write(f'DEBUG/{flag:<6} {message}\n') - if not existed: - # at boot the file is created as root:vyattacfg - # at runtime the file is created as user:vyattacfg - # do not use run/cmd to not have a recursive call to this code - os.system(f'chmod g+w {logfile}') - +from vyos import debug # There is many (too many) ways to run command with python # os.system, subprocess.Popen, subproces.{run,call,check_output} @@ -98,7 +57,14 @@ def popen(command, flag='', shell=None, input=None, timeout=None, env=None, to get both stdout, and stderr: popen('command', stdout=PIPE, stderr=STDOUT) to discard stdout and get stderr: popen('command', stdout=DEVNUL, stderr=PIPE) """ - debug_msg(f"cmd '{command}'", flag) + + # log if the flag is set, otherwise log if command is set + if not debug.enabled(flag): + flag = 'command' + + cmd_msg = f"cmd '{command}'" + debug.message(cmd_msg, flag) + use_shell = shell stdin = None if shell is None: @@ -129,7 +95,8 @@ def popen(command, flag='', shell=None, input=None, timeout=None, env=None, nl = '\n' if decoded1 and decoded2 else '' decoded = decoded1 + nl + decoded2 if decoded: - debug_msg(f"returned:\n{decoded}", flag) + ret_msg = f"returned:\n{decoded}" + debug.message(ret_msg, flag) return decoded, p.returncode |