Skip to content

Commit

Permalink
[logging] Replace all print functions with logging to be able to co…
Browse files Browse the repository at this point in the history
…ntrol output.
  • Loading branch information
belyalov committed Jul 15, 2018
1 parent 23d9b27 commit 3d35365
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ install:
- export MDST=~/.micropython/lib/
- mkdir -p $MDST
- ln -s `pwd`/micropython-lib/unittest/unittest.py $MDST
- ln -s `pwd`/micropython-lib/logging/logging.py $MDST
- ln -s ../../uasyncio.core/uasyncio/core.py `pwd`/micropython-lib/uasyncio/uasyncio
- ln -s `pwd`/micropython-lib/uasyncio/uasyncio $MDST/uasyncio
- ln -s `pwd`/tinyweb $MDST/tinyweb
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ By itself - *tinyweb* is just simple TCP server which runs in top of **uasyncio*
### Requirements
* [uasyncio](https://github.com/micropython/micropython-lib/tree/master/uasyncio) - micropython version of *async* library for big brother - python3.
* [uasyncio-core](https://github.com/micropython/micropython-lib/tree/master/uasyncio.core)
* [logging](https://github.com/micropython/micropython-lib/tree/master/logging)

### Quickstart
Tinyweb comes as a compiled firmware for ESP8266 / ESP32 as well ("frozen modules"). You don't have to use it - however, it could be easiest way to try it :)
Expand Down
18 changes: 11 additions & 7 deletions tinyweb/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
MIT license
(C) Konstantin Belyalov 2017-2018
"""
import logging
import uasyncio as asyncio
import ujson as json
import gc
Expand All @@ -12,6 +13,9 @@
import usocket as socket


log = logging.getLogger('TINYWEB')


def urldecode_plus(s):
"""Decode urlencoded string (including '+' char).
Expand Down Expand Up @@ -491,16 +495,15 @@ async def _handler(self, reader, writer):
try:
await resp.error(500)
except Exception as e:
sys.print_exception(e)
sys.print_exception(e, logging._stream)
except HTTPException as e:
try:
await resp.error(e.code, e.message)
except Exception as e:
sys.print_exception(e)
sys.print_exception(e, logging._stream)
except Exception as e:
print('-' * 40)
print('Unhandled exception in "{}"'.format(req.path.decode()))
sys.print_exception(e)
log.error('Unhandled exception at "{}"'.format(req.path.decode()))
sys.print_exception(e, logging._stream)
try:
await resp.error(500)
# Send exception info if desired
Expand Down Expand Up @@ -652,12 +655,13 @@ def run(self, host="127.0.0.1", port=8081, loop=None, loop_forever=True, backlog
loop_forever - run loo.loop_forever(), otherwise caller must run it by itself.
"""
if backlog:
print("WARNING: 'backlog' has been moved to __init__() and will be removed in next release")
log.warning("DEPRECATED: 'backlog' has been moved to __init__()")
self.backlog = backlog
if loop:
self.loop = loop
else:
self.loop = asyncio.get_event_loop()
print("* Starting Web Server at {}:{}".format(host, port))
log.debug("* Starting Web Server at {}:{}".format(host, port))
self._server_coro = self._tcp_server(host, port, self.backlog)
self.loop.create_task(self._server_coro)
if loop_forever:
Expand Down

0 comments on commit 3d35365

Please sign in to comment.