Commits

German Larrain committed 595bf7a

Starting 'dev-logging' branch: use the logging module of the python standard library for an appropriate and customizable logging of events in ARS

  • Participants
  • Parent commits 51e0e35
  • Branches dev-logging

Comments (0)

Files changed (4)

ars/app/__init__.py

 """
 
 from abc import abstractmethod
+import logging
+import logging.config
 import math
 import sys
 
 import ars.graphics.adapters as gp
 from ars.model.simulator import Simulation
+import ars.utilities.logging_help as lut
 
 class Program():
 
 		class constants
 		-create the simulation
 		"""
+		self.settings = SettingsManager(
+			'/home/german/Prog/WS/Eclipse/ARS/bin/logging.conf')
+
 		self.do_create_window = True
 		self.data_files_names = None # TODO
 		self.data_files = None # TODO
 
 	def on_action_selection(self, key):
 		"""Method called after an actions is selected by pressing a key"""
-		if self.PRINT_KEY_INFO:
-			print(key)
+		logging.getLogger('ars.app').info(str(key))
 		try:
 			if self.key_press_functions.has_key(key):
 				if self.key_press_functions.is_repeat(key):
 		except Exception as e:
 			raise Exception('Could not record frame', e)
 
+class SettingsManager:
+	def __init__(self, logging_config_file):
+		self.setup_logging('ars', logging_config_file)
+
+	def setup_logging(self, top_level_logger, config_file):
+
+		# according to ~/doc/python2.6/html/library/logging.html#configuring-logging-for-a-library
+		h = lut.NullHandler()
+		logging.getLogger(top_level_logger).addFilter(h)
+
+		try:
+			logging.config.fileConfig(config_file)
+		except:
+			lut.set_default_logging_config()
+
 class ActionMap:
 	def __init__(self):
 		self._map = {}

ars/utilities/logging_help.py

+# Created on 2012.04.08
+#
+# @author: german
+
+#===============================================================================
+# It is named this way to avoid name clashing
+#===============================================================================
+
+import logging as lgg
+
+def set_default_logging_config():
+	#TODO: implement
+	pass
+
+class NullHandler(lgg.Handler):
+	"""A do-nothing handler. Code from:
+	~/doc/python2.6/html/library/logging.html#configuring-logging-for-a-library"""
+	def emit(self, record):
+		pass
+
+# based on ~/doc/python2.6/html/library/logging.html
+
+[loggers]
+keys=root,ars
+
+[handlers]
+keys=consoleHandler
+
+[formatters]
+keys=SimpleFormatter
+
+[logger_root]
+#level=DEBUG
+level=INFO
+handlers=consoleHandler
+
+[logger_ars]
+#level=DEBUG
+level=INFO
+handlers=consoleHandler
+qualName=ars
+propagate=0
+
+[handler_consoleHandler]
+class=StreamHandler
+#level=DEBUG
+level=INFO
+formatter=simpleFormatter
+args=(sys.stdout,)
+
+#===============================================================================
+# ~/doc/python2.6/html/library/logging.html#rotating-file-handler
+# [handler_fileHandler]
+# class=RotatingFileHandler
+# level=DEBUG
+# formatter=simpleFormatter
+# args=(sys.stdout,)
+#===============================================================================
+
+[formatter_simpleFormatter]
+format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
+datefmt=
+
+
+# Created on 2012.04.08
+#
+# @author: german
+
+
+#===============================================================================
+# Logging
+#===============================================================================
+
+TOP_LEVEL_LOGGER = 'ars'
+LOGGING_CONFIG_FILE = 'logging.conf'