Skip to content

Commit

Permalink
main: update logging
Browse files Browse the repository at this point in the history
  • Loading branch information
back-to committed Dec 23, 2020
1 parent 1870f05 commit 7964ff8
Showing 1 changed file with 28 additions and 49 deletions.
77 changes: 28 additions & 49 deletions liveproxy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,61 @@
import platform
import sys

import streamlink.logger as logger
from requests import __version__ as requests_version
from streamlink import __version__ as streamlink_version

from liveproxy import __version__ as liveproxy_version
from liveproxy.argparser import parser

log = logging.getLogger('streamlink.liveproxy-main')


def log_current_versions():
'''Show current installed versions'''

# MAC OS X
if sys.platform == 'darwin':
os_version = 'macOS {0}'.format(platform.mac_ver()[0])
# Windows
elif sys.platform.startswith('win'):
os_version = '{0} {1}'.format(platform.system(), platform.release())
# linux / other
else:
os_version = platform.platform()
from liveproxy.files import create_file
from liveproxy.server import HTTPRequest, ThreadedHTTPServer

log.info('For LiveProxy support visit https://github.com/back-to/liveproxy')
log.debug('OS: {0}'.format(os_version))
log.debug('Python: {0}'.format(platform.python_version()))
log.debug('LiveProxy: {0}'.format(liveproxy_version))
log.debug('Streamlink: {0}'.format(streamlink_version))
log.debug('Requests: {0}'.format(requests_version))


def setup_logging(stream=sys.stdout, level="info"):
logger.basicConfig(
stream=stream,
level=level,
style="{",
format=("[{asctime}]" if level == "trace" else "") + "[{name}][{levelname}] {message}",
datefmt="%H:%M:%S" + (".%f" if level == "trace" else "")
)
log = logging.getLogger(__name__.replace('liveproxy.', ''))


def main():
error_code = 0

args = parser.parse_args(sys.argv[1:])
if args.help:
parser.print_help()
return

setup_logging()
logging.basicConfig(
stream=sys.stdout,
level='DEBUG',
format='[%(name)s][%(levelname)s] %(message)s',
)

if hasattr(os, 'getuid'):
if os.geteuid() == 0:
log.info('LiveProxy is running as root! Be careful!')

log_current_versions()

HOST = args.host
PORT = int(args.port)

if args.help:
parser.print_help()
# MAC OS X
if sys.platform == 'darwin':
os_version = f'macOS {platform.mac_ver()[0]}'
# Windows
elif sys.platform.startswith('win'):
os_version = f'{platform.system()} {platform.release()}'
# Linux / other
else:
os_version = platform.platform()

log.info('For LiveProxy support visit https://github.com/back-to/liveproxy')
log.debug(f'OS: {os_version}')
log.debug(f'Python: {platform.python_version()}')
log.debug(f'LiveProxy: {liveproxy_version}')

HOST = str(args.host)
PORT = int(args.port)

if args.file:
create_file(args)
else:
log.info('Starting server: {0} on port {1}'.format(HOST, PORT))
log.info(f'Starting server: {HOST} on port {PORT}')

try:
httpd = ThreadedHTTPServer((HOST, PORT), HTTPRequest)
except OSError as err:
if err.errno == errno.EADDRINUSE:
log.error('Could not listen on port {0}! Exiting...'.format(PORT))
log.error(f'Could not listen on port {PORT}! Exiting...')
sys.exit(errno.EADDRINUSE)
log.error('Error {0}! Exiting...'.format(err.errno))
log.error(f'Error {err.errno}! Exiting...')
sys.exit(err.errno)
try:
httpd.serve_forever()
Expand All @@ -94,7 +73,7 @@ def main():
finally:
if httpd:
try:
log.info('Closing server {0} on port {1} ...'.format(HOST, PORT))
log.info(f'Closing server {HOST} on port {PORT} ...')
httpd.shutdown()
httpd.server_close()
except KeyboardInterrupt:
Expand Down

0 comments on commit 7964ff8

Please sign in to comment.