From ba675b38ad095dba6fa15af04b158ca04667fa8b Mon Sep 17 00:00:00 2001
From: Seth Hitefield <sdhitefield@gmail.com>
Date: Thu, 22 Sep 2016 14:58:31 -0400
Subject: grc: refactor: Added logging

---
 grc/main.py | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

(limited to 'grc/main.py')

diff --git a/grc/main.py b/grc/main.py
index e0f2b26d05..305e8b8f78 100755
--- a/grc/main.py
+++ b/grc/main.py
@@ -15,7 +15,7 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 
-import argparse, sys
+import argparse, logging, sys
 
 import gi
 gi.require_version('Gtk', '3.0')
@@ -35,11 +35,20 @@ GRC comes with ABSOLUTELY NO WARRANTY.
 This is free software, and you are welcome to redistribute it.
 """
 
+LOG_LEVELS = {
+    'debug': logging.DEBUG,
+    'info': logging.INFO,
+    'warning': logging.WARNING,
+    'error': logging.ERROR,
+    'critical': logging.CRITICAL,
+}
+
 
 def main():
     parser = argparse.ArgumentParser(
         description=VERSION_AND_DISCLAIMER_TEMPLATE % gr.version())
     parser.add_argument('flow_graphs', nargs='*')
+    parser.add_argument('--log', choices=['debug', 'info', 'warning', 'error', 'critical'], default='warning')
     args = parser.parse_args()
 
     try:
@@ -47,6 +56,25 @@ def main():
     except:
         pass
 
+    # Enable logging
+    # Note: All other modules need to use the 'grc.<module>' convention
+    log = logging.getLogger('grc')
+    log.setLevel(logging.DEBUG)
+
+    # Console formatting
+    console = logging.StreamHandler()
+    console.setLevel(LOG_LEVELS[args.log])
+
+    msg_format = '[%(asctime)s - %(levelname)8s] --- %(message)s (%(filename)s:%(lineno)s)'
+    date_format = '%I:%M'
+    formatter = logging.Formatter(msg_format, datefmt=date_format)
+
+    #formatter = utils.log.ConsoleFormatter()
+    console.setFormatter(formatter)
+    log.addHandler(console)
+
+    log.debug("Loading platform")
+
     platform = Platform(
         version=gr.version(),
         version_parts=(gr.major_version(), gr.api_version(), gr.minor_version()),
@@ -54,5 +82,7 @@ def main():
         install_prefix=gr.prefix()
     )
 
+    log.debug("Loading application")
     app = Application(args.flow_graphs, platform)
+    log.debug("Running")
     sys.exit(app.run())
-- 
cgit v1.2.3