gui: add arg for saving log to a file

This commit is contained in:
linuswck 2024-09-04 17:01:27 +08:00
parent 1896e2534b
commit 1111967a7b
1 changed files with 13 additions and 4 deletions

View File

@ -39,10 +39,8 @@ FLOAT_REGEX = re.compile(r'(?P<number>[+-]?((((\d+(\.\d*)?)|(\d*\.\d+))([eE][+-]
def get_argparser(): def get_argparser():
parser = argparse.ArgumentParser(description="ARTIQ master") parser = argparse.ArgumentParser(description="ARTIQ master")
parser.add_argument("--connect", default=None, action="store_true", parser.add_argument("--log-file", default="", nargs="?",
help="Automatically connect to the specified Thermostat in IP:port format") help="Store logs into a file; Set the base filename")
parser.add_argument('IP', metavar="ip", default=None, nargs='?')
parser.add_argument('PORT', metavar="port", default=None, nargs='?')
parser.add_argument("-l", "--log", dest="logLevel", choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], parser.add_argument("-l", "--log", dest="logLevel", choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
help="Set the logging level") help="Set the logging level")
@ -951,6 +949,17 @@ class MainWindow(QtWidgets.QMainWindow):
async def coro_main(): async def coro_main():
args = get_argparser().parse_args() args = get_argparser().parse_args()
if args.log_file or args.log_file is None:
filename = args.log_file
if filename is None:
time = datetime.now()
filename = f"{time.year}-{time.day}-{time.month}-{time.hour}:{time.minute}:{time.second}.log"
logging.basicConfig(filename=filename,
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%Y/%d/%m %H:%M:%S',
level=logging.INFO)
if args.logLevel: if args.logLevel:
logging.basicConfig(level=getattr(logging, args.logLevel)) logging.basicConfig(level=getattr(logging, args.logLevel))