Skip to content

Commit

Permalink
Merge pull request #15 from stellarbit/pr-8-logging
Browse files Browse the repository at this point in the history
Pr 8 logging
  • Loading branch information
anton-belousov authored Jul 4, 2020
2 parents 7a6a2f6 + 8cf99ac commit c4f87e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Using aioping

There are 2 ways to use the library.

First one is interactive, which prints results to the stdout.
First one is interactive, which sends results to standard Python logger.
Please make sure you are running this code under root, as only
root is allowed to send ICMP packets:

Expand Down Expand Up @@ -73,6 +73,7 @@ Credits
- https://github.com/harriv
- https://github.com/asantoni
- https://github.com/eddebc
- https://github.com/wise0wl


License
Expand Down
17 changes: 10 additions & 7 deletions aioping/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
$Author: $
"""

import logging
import asyncio
import async_timeout
import sys
Expand All @@ -86,6 +87,8 @@
import uuid
import random

logger = logging.getLogger("aioping")


if sys.platform == "win32":
# On Windows, the best timer is time.clock()
Expand Down Expand Up @@ -275,19 +278,19 @@ async def verbose_ping(dest_addr, timeout=2, count=3):
:param count:
"""
for i in range(count):
delay = None

try:
delay = await ping(dest_addr, timeout)
except TimeoutError as e:
logger.error("%s timed out after %ss" % (dest_addr, timeout))
except Exception as e:
print("%s failed: %s" % (dest_addr, str(e)))
logger.error("%s failed: %s" % (dest_addr, str(e)))
break

if delay is None:
print('%s timed out after %ss' % (dest_addr, timeout))
else:
if delay is not None:
delay *= 1000
print("%s get ping in %0.4fms" % (dest_addr, delay))

print()
logger.warning("%s get ping in %0.4fms" % (dest_addr, delay))


if __name__ == "__main__":
Expand Down

0 comments on commit c4f87e4

Please sign in to comment.