Skip to content

Commit

Permalink
[core] Add debugging capabilities
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tobi-wan-kenobi committed Apr 2, 2017
1 parent 11235d6 commit 251f8d2
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
39 changes: 33 additions & 6 deletions bumblebee-status
Original file line number Diff line number Diff line change
@@ -1,24 +1,51 @@
#!/usr/bin/env python

import os
import sys
import logging
import bumblebee.theme
import bumblebee.engine
import bumblebee.config
import bumblebee.output
import bumblebee.input

def main():
logging.basicConfig(
level=logging.DEBUG,
format="[%(asctime)s] %(levelname)-8s %(message)s",
filename="{}/debug.log".format(os.path.dirname(os.path.realpath(__file__)))
)
config = bumblebee.config.Config(sys.argv[1:])
theme = bumblebee.theme.Theme(config.theme())
output = bumblebee.output.I3BarOutput(theme=theme)
inp = bumblebee.input.I3BarInput()
engine = bumblebee.engine.Engine(
config=config,
output=output,
inp=inp,
)
engine = None

engine.run()
try:
engine = bumblebee.engine.Engine(
config=config,
output=output,
inp=inp,
)
engine.run()
except BaseException as e:
logging.exception(e)
if output.started():
output.flush()
output.end()
else:
output.start()
import time
while True:
output.begin()
error = bumblebee.modules.error.Module(engine, config)
error.set("exception occurred: {}".format(e))
widget = error.widgets()[0]
widget.link_module(error)
output.draw(widget, error)
output.flush()
output.end()
time.sleep(1)
# try:
# except KeyboardInterrupt as error:
# inp.stop()
Expand Down
8 changes: 8 additions & 0 deletions bumblebee/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
module parameters, etc.) to all other components
"""

import os
import sys
import logging
import argparse
import textwrap
import importlib
Expand All @@ -14,6 +16,7 @@
THEME_HELP = "Specify the theme to use for drawing modules"
PARAMETER_HELP = "Provide configuration parameters in the form of <module>.<key>=<value>"
LIST_HELP = "Display a list of either available themes or available modules along with their parameters."
DEBUG_HELP = "Enable debug log ('debug.log' located in the same directory as the bumblebee-status executable)"

class print_usage(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
Expand Down Expand Up @@ -51,6 +54,8 @@ def create_parser():
help=PARAMETER_HELP)
parser.add_argument("-l", "--list", choices=["modules", "themes"], action=print_usage,
help=LIST_HELP)
parser.add_argument("-d", "--debug", action="store_true",
help=DEBUG_HELP)
return parser

class Config(bumblebee.store.Store):
Expand All @@ -64,6 +69,9 @@ def __init__(self, args=None):
parser = create_parser()
self._args = parser.parse_args(args if args else [])

if not self._args.debug:
logger = logging.getLogger().disabled = True

for param in self._args.parameters:
key, value = param.split("=")
self.set(key, value)
Expand Down
26 changes: 26 additions & 0 deletions bumblebee/modules/error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# pylint: disable=C0111,R0903

"""Draws an error widget.
"""

import bumblebee.input
import bumblebee.output
import bumblebee.engine

class Module(bumblebee.engine.Module):
def __init__(self, engine, config):
super(Module, self).__init__(engine, config,
bumblebee.output.Widget(full_text=self.text)
)
self._text = ""

def set(self, text):
self._text = text

def text(self, widget):
return self._text

def state(self, widget):
return ["critical"]

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
5 changes: 5 additions & 0 deletions bumblebee/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ class I3BarOutput(object):
def __init__(self, theme):
self._theme = theme
self._widgets = []
self._started = False

def started(self):
return self._started

def start(self):
"""Print start preamble for i3bar protocol"""
self._started = True
sys.stdout.write(json.dumps({"version": 1, "click_events": True}) + "[\n")

def stop(self):
Expand Down
11 changes: 8 additions & 3 deletions bumblebee/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import shlex
import logging
import subprocess

try:
Expand All @@ -8,17 +9,21 @@
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" ]:
Expand Down

0 comments on commit 251f8d2

Please sign in to comment.