[core] Add debugging capabilities

* If an exception is thrown, catch it and show a (somewhat) nice error
  message in the i3bar instead of the normal content
* Add a flag "-d" for debugging into a debug log. Currently, this only
  logs commandline calls as they occur and their return values, as well
  as exceptions.

fixes #58
This commit is contained in:
Tobias Witek 2017-04-02 08:31:23 +02:00
parent 11235d6883
commit 251f8d2e9f
5 changed files with 80 additions and 9 deletions

View file

@ -1,4 +1,5 @@
import shlex
import logging
import subprocess
try:
@ -8,17 +9,21 @@ except ImportError:
pass
def execute(cmd, wait=True):
logging.info("executing command '{}'".format(cmd))
args = shlex.split(cmd)
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
rv = None
if wait:
out, _ = proc.communicate()
if proc.returncode != 0:
raise RuntimeError("{} exited with {}".format(cmd, proc.returncode))
if type(out) == str:
return out
return out.decode("utf-8")
return None
rv = out
else:
rv = out.decode("utf-8")
logging.info("command returned '{}'".format("" if not rv else rv))
return rv
def bytefmt(num):
for unit in [ "", "Ki", "Mi", "Gi" ]: