1. Shuge Lee
  2. libsipc

Commits

Shuge Lee  committed 03c3447

fixed fetch sytem configuration file failed; improved log module

  • Participants
  • Parent commits 1dddb91
  • Branches master

Comments (0)

Files changed (4)

File demos/demo_get_sys_config.py

View file
 
 def load_from_sip_c_server():
     sys_config_obj = sipc.SysConfig()
-    sys_config_obj.auto_update()
+    sys_config_obj.auto_update(ignore_local = True)
 
     pprint(sys_config_obj.to_dict())
 
 
 if __name__ == "__main__":
-    load_from_local_latest_sys_config()
-
-    load_from_specify_server()
+#    load_from_local_latest_sys_config()
+#
+#    load_from_specify_server()
 
     load_from_sip_c_server()

File sipc/config.py

View file
+
+APP_NAME = 'iBlah'
+
+redirect_stdout = False
+redirect_stderr = False

File sipc/log.py

View file
 #!/usr/bin/env python
 import logging
+import os
+import sys
+import config
+import commons
 
 logging.getLogger().setLevel(logging.DEBUG)
 LOGGING_FORMAT = '[%(asctime)10s] %(message)s'
 logging.basicConfig(format = LOGGING_FORMAT, datefmt = "%Y-%m-%d %H:%M:%S")
 
 
+app_path_prefix = os.path.join(commons.get_app_data_path(config.APP_NAME))
+log_file_path = os.path.join(app_path_prefix, "debug.log")
+file_ch = logging.FileHandler(log_file_path)
+file_ch.setLevel(logging.DEBUG)
+formatter = logging.Formatter(LOGGING_FORMAT, datefmt = "%Y-%m-%d %H:%M:%S")
+file_ch.setFormatter(formatter)
+logging.getLogger().addHandler(file_ch)
+
+stderr_log_file_path = os.path.join(app_path_prefix, "stderr.log")
+if config.redirect_stderr:
+    sys.stderr = file(stderr_log_file_path, "a+")
+
+stdout_log_file_path = os.path.join(app_path_prefix, "stdout.log")
+if config.redirect_stdout:
+    sys.stdout = file(stdout_log_file_path, "a+")
+
+
 if __name__ == "__main__":
     logging.info("this is info")
     logging.debug("this is debug")
-    logging.error("this is error")
+    logging.error("this is error")

File sipc/sys_config.py

View file
 
 import commons
 from http import HTTPResponse
-from log import logging
+import config
+import logging
 
 
 __all__ = [
 
 socket.setdefaulttimeout(5.0)
 
-APP_NAME = 'iBlah'
 
 _GET_SYS_CONFIG_BODY_TPL = '''<?xml version="1.0"?>
 <config>
     FETION_PROTOCOL_VERSION = "4.0.2510"
     URL = "http://nav.fetion.com.cn/nav/getsystemconfig.aspx"
 
-    LATEST_SYS_CONFIG_FILE_PATH = os.path.join(commons.get_app_data_path(APP_NAME), "latest_sys_config.json")
+    LATEST_SYS_CONFIG_FILE_PATH = os.path.join(commons.get_app_data_path(config.APP_NAME), "latest_sys_config.json")
     
     def __init__(self):
         self.servers_version = None
                     http_url = None,
                     http_method = None,
                     latest_config_file_path = None,
-                    ignore_local = False):
+                    ignore_local = False,
+                    debug = False):
+        self.debug = debug
 
         if not latest_config_file_path:
             latest_config_file_path = SysConfig.LATEST_SYS_CONFIG_FILE_PATH
 
         if not ignore_local:
             self.load_from_local_file_path(file_path = latest_config_file_path)
+            logging.info("local system configuration loaded")
+        else:
+            logging.info("local system configuration skipped")
 
         resp = self.download_from_server(http_url = http_url, http_method = http_method, ignore_local = ignore_local)
 
         if resp.status != httplib.OK:
-            msg = "auto update response status code is %d" % resp.status
-            logging.warn(msg)
+            msg = "response of fetching system configuration status code is %d" % resp.status
+            logging.error(msg)
         else:
             self.load_from_string(resp.body)
+            logging.info("system configuration parsed successfully")
             
             parent_path = os.path.dirname(latest_config_file_path)
             if not os.path.exists(parent_path):
                 os.makedirs(parent_path)
             self.save(file_path = latest_config_file_path)
+            logging.info("system configuration saved")
 
         return resp
 
 
 if __name__ == "__main__":
     import doctest
-    doctest.testmod()
+    doctest.testmod()