From 1ca2e910fbc82a35c5b70b67e020b79c985982f3 Mon Sep 17 00:00:00 2001 From: Shin'ichiro Kawasaki Date: Fri, 1 May 2020 11:45:18 +0900 Subject: [PATCH] scratch_link.py: Add debug print option In the GitHub issue "Scratux support #9", debug message log was required for analysis. However, script edit by users was required to enable debug print. This is not handy. To avoid that the chore by users, add -d debug print option. Signed-off-by: Shin'ichiro Kawasaki --- scratch_link.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/scratch_link.py b/scratch_link.py index bbc5867..9cb541a 100755 --- a/scratch_link.py +++ b/scratch_link.py @@ -10,6 +10,8 @@ import ssl import websockets import json import base64 +import logging +import sys # for Bluetooth (e.g. Lego EV3) import bluetooth @@ -21,12 +23,26 @@ from bluepy.btle import BTLEDisconnectError, BTLEManagementError import threading import time +logLevel = logging.INFO + +# handle command line options +if __name__ == "__main__": + opts = [opt for opt in sys.argv[1:] if opt.startswith("-")] + if "-h" in opts: + print((f"Usage: {sys.argv[0]} [OPTS]\n" + "OPTS:\t-h Show this help.\n" + "\t-d Print debug messages." + )) + sys.exit(1) + elif "-d" in opts: + print("Print debug messages") + logLevel = logging.DEBUG + # for logging -import logging logger = logging.getLogger(__name__) handler = logging.StreamHandler() -handler.setLevel(logging.INFO) -logger.setLevel(logging.INFO) +handler.setLevel(logLevel) +logger.setLevel(logLevel) logger.addHandler(handler) logger.propagate = False