From 9ac0bd0710336e4aa23da61eec22630ebbd84573 Mon Sep 17 00:00:00 2001 From: Pzaff Date: Mon, 16 Oct 2017 17:40:07 -0400 Subject: [PATCH] Fixed logging so that it prints to the file as well as the console. Added log formatting. Added log files to be ignorned in git. Updated the run script to call the application correctly. --- .gitignore | 2 +- cli_application.py | 13 +++++++++---- logger.py | 11 ++++++++++- run.sh | 2 +- 4 files changed, 21 insertions(+), 7 deletions(-) mode change 100644 => 100755 cli_application.py diff --git a/.gitignore b/.gitignore index 6c25327..b3e3458 100644 --- a/.gitignore +++ b/.gitignore @@ -60,4 +60,4 @@ target/ *.idea #Subdivision Log Generator log file -logs/ +logs/* diff --git a/cli_application.py b/cli_application.py old mode 100644 new mode 100755 index d71a642..237f1d4 --- a/cli_application.py +++ b/cli_application.py @@ -4,16 +4,17 @@ """ import argparse import cmd -import logging.config import os import sys + from user_interface.curve_parser import parse_curve_file +from logger import get_logger class CliApplication(cmd.Cmd): def __init__(self, application_path=None, application_name="generator", args=None): - self.logger = logging.getLogger(__name__) + self.logger = get_logger(__name__) self.curve_paths = os.path.join(application_path, "curves") self.arguments = args self.parse_cmd_args(self.arguments) @@ -25,8 +26,11 @@ def parse_cmd_args(self, args): if os.path.exists(args.generate_m_value): self.generate_m_value(args.generate_m_value) else: - self.generate_m_value(os.path.join(self.curve_paths, args.generate_m_value)) - + curve_path = os.path.join(self.curve_paths, args.generate_m_value) + if os.path.exists(curve_path): + self.generate_m_value(curve_path) + else: + self.logger.info("The curve file doesn't exist. Please choose an appropriate file.") else: raise Exception("There were no arguments when running the application") @@ -48,6 +52,7 @@ def main(): stick knot's Bezier cure to match the stick knot", default=None, action="store") arguments = argument_parser.parse_args() + get_logger().debug("Subdivision-Generator Starting Up") application = CliApplication(application_path, application_name, arguments) diff --git a/logger.py b/logger.py index cc3c6a3..3d3bfaf 100644 --- a/logger.py +++ b/logger.py @@ -13,9 +13,18 @@ def get_logger(classname=None): if not os.path.exists(log_dir): os.makedirs(log_dir) - logging.basicConfig(filename="logs/generator_log.txt", level="DEBUG") + root = logging.getLogger() + if root.handlers: + for handler in root.handlers: + root.removeHandler(handler) + + logging.basicConfig(format='%(asctime)s: %(filename)s: %(funcName)s: %(levelname)s:\t %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', filename="logs/generator_log.txt") if classname is None: classname = "Subdivision Generator" + logger = logging.getLogger(classname) + logger.setLevel(logging.INFO) + logger.addHandler(logging.StreamHandler()) + return logging.getLogger(classname) diff --git a/run.sh b/run.sh index 2089cea..48fffd9 100755 --- a/run.sh +++ b/run.sh @@ -1,3 +1,3 @@ #!/bin/bash SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)" -"$SCRIPT_DIR/user_interface/cli_application.py" $* +"$SCRIPT_DIR/cli_application.py" $*