rembo10/headphones — pull request #1519 by basilfx (2014-04-04 13:32:38+00:00)
Title: Rewrite of logging.py

I did a rewrite of `logging.py` to be more flexible, and improved error detection and handling. I'll try to summarize the improvements:

#### Better logging
The default Python logger accepts multiple arguments for string formatting. This allows lazy string formatting, and should be faster! It is now possible to pass placeholders as arguments:

```
# New
logger.error("Caused by %s", object_with_expensive_to_string)

# Old, but still compatible!
logger.error("Caused by %s" % object_with_expensive_to_string)
```

This means that any debug messages are only evaluated if necessary. I built a special handler to add messages to the WebUI.

#### Logging of exceptions
The code contains a lot of `try: .. except Exception: ..` blocks. I consider this the lazy approach (no offense :P), and I think Headphones can be improved if we can handle errors more specific, like catching only specific exception from which we can recover. It can also help finding bugs! With the exposure of new logger methods, we can now do this:

```
try:
..
except Exception:
logger.exception("Unhandled exception")
```

Python will automatically grab the exception traceback from the stack, resulting in a more-meaningful errors in the logs.

#### Catching uncaught exceptions
With the use of `sys.excepthook` you can handle uncaught exceptions. As a default handler, the traceback will be added to the log file (of level ERROR). Threads don't seem to respect the handler, so I added a workaround for this.

So why is this useful? Consider Headphones to be started as daemon in quiet mode. Exceptions are the NOT printed to stderr or log file! To mimic an exception, I added `1/0` to the postprocessor. Then I started manual postprocessing for with and without exception handler in place.

This is without the exception handler:
![schermafbeelding 2014-04-04 om 13 27 10](https://cloud.githubusercontent.com/assets/815976/2615603/31f2053e-bbfc-11e3-9ae3-cafb191f3a8a.png)

And this is with the exception handler:
![schermafbeelding 2014-04-04 om 13 27 47](https://cloud.githubusercontent.com/assets/815976/2615617/48a907e6-bbfc-11e3-91cc-00cb150e5c7d.png)

I hope this allows for better debugging in the future.

#### The rest
Most of the commits are replacements of logging in the code. Since a lot of logging occurs, I did not replace all yet, but consider it a start :-)

I tested this code for +/- three days, and did not noticed any big problems. That said, there are a lot of edge cases that I cannot test in only three days.