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/gui/Application.py |  8 ++++++++
 grc/gui/MainWindow.py  |  5 +++++
 grc/main.py            | 32 +++++++++++++++++++++++++++++++-
 3 files changed, 44 insertions(+), 1 deletion(-)

(limited to 'grc')

diff --git a/grc/gui/Application.py b/grc/gui/Application.py
index c68b38bbaf..5c643eafa1 100644
--- a/grc/gui/Application.py
+++ b/grc/gui/Application.py
@@ -22,6 +22,7 @@ from __future__ import absolute_import, print_function
 
 import os
 import subprocess
+import logging
 
 from gi.repository import Gtk, GObject
 
@@ -33,6 +34,9 @@ from .PropsDialog import PropsDialog
 from ..core import ParseXML, Messages
 
 
+log = logging.getLogger(__name__)
+
+
 class Application(Gtk.Application):
     """
     The action handler will setup all the major window components,
@@ -57,15 +61,19 @@ class Application(Gtk.Application):
         self.platform = platform
         self.config = platform.config
 
+        log.debug("__init__()")
+
         #initialize
         self.init_file_paths = [os.path.abspath(file_path) for file_path in file_paths]
         self.init = False
 
     def do_startup(self):
         Gtk.Application.do_startup(self)
+        log.debug("do_startup()")
 
     def do_activate(self):
         Gtk.Application.do_activate(self)
+        log.debug("do_activate()")
 
         self.main_window = MainWindow(self, self.platform, self._handle_action)
         self.main_window.connect('delete-event', self._quit)
diff --git a/grc/gui/MainWindow.py b/grc/gui/MainWindow.py
index c371601098..5bb4c52a07 100644
--- a/grc/gui/MainWindow.py
+++ b/grc/gui/MainWindow.py
@@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 from __future__ import absolute_import
 
 import os
+import logging
 
 from gi.repository import Gtk, Gdk, GObject
 
@@ -34,6 +35,9 @@ from .Notebook import Notebook, Page
 
 from ..core import Messages
 
+log = logging.getLogger(__name__)
+
+
 ############################################################
 # Main window
 ############################################################
@@ -51,6 +55,7 @@ class MainWindow(Gtk.ApplicationWindow):
         Setup the menu, toolbar, flow graph editor notebook, block selection window...
         """
         Gtk.ApplicationWindow.__init__(self, title="GNU Radio Companion", application=app)
+        log.debug("__init__()")
 
         self._platform = platform
         self.config = platform.config
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