do create log files at defined places
authorOliver Maurhart <oliver.maurhart@ait.ac.at>
Wed, 19 Feb 2014 11:30:28 +0100
changeset 710ca25608ed0f
parent 70 e1db93adbebf
child 72 9c747741aff2
do create log files at defined places
OpenSecurity/bin/cygwin.py
OpenSecurity/bin/environment.py
OpenSecurity/bin/notification.py
OpenSecurity/bin/opensecurity_util.py
OpenSecurity/bin/opensecurityd.pyw
OpenSecurity/bin/vmmanager.pyw
     1.1 --- a/OpenSecurity/bin/cygwin.py	Wed Feb 19 10:40:39 2014 +0100
     1.2 +++ b/OpenSecurity/bin/cygwin.py	Wed Feb 19 11:30:28 2014 +0100
     1.3 @@ -38,9 +38,11 @@
     1.4  import sys
     1.5  import _winreg
     1.6  from subprocess import Popen, PIPE, call, STARTUPINFO, _subprocess
     1.7 +
     1.8  # local
     1.9  from environment import Environment
    1.10  from opensecurity_util import logger, setupLogger, OpenSecurityException
    1.11 +
    1.12  # ------------------------------------------------------------
    1.13  # code
    1.14  
     2.1 --- a/OpenSecurity/bin/environment.py	Wed Feb 19 10:40:39 2014 +0100
     2.2 +++ b/OpenSecurity/bin/environment.py	Wed Feb 19 11:30:28 2014 +0100
     2.3 @@ -74,6 +74,8 @@
     2.4                  self._data_path = os.path.join(self._prefix_path, 'share')
     2.5          elif sys.platform == 'win32' or sys.platform == 'cygwin':
     2.6              self._data_path = self._prefix_path
     2.7 +        else:
     2.8 +            raise OSError()
     2.9  
    2.10              
    2.11      def data_path_get(self):
    2.12 @@ -88,14 +90,45 @@
    2.13          return self._prefix_path
    2.14          
    2.15      prefix_path = property(prefix_path_get)
    2.16 +    
    2.17 +    
    2.18 +    def log_path_get(self):
    2.19 +        
    2.20 +        """the path where log files should be stored"""
    2.21 +        
    2.22 +        user_log_path = os.path.join(os.environ['HOME'], '.log')
    2.23 +        
    2.24 +        if sys.platform == 'linux2':
    2.25              
    2.26 -# test method			
    2.27 +            if os.access('/var/log', os.W_OK):
    2.28 +                return '/var/log'
    2.29 +            
    2.30 +            print('no permissions to write log files in /var/log, switching to ~/.log')
    2.31 +            
    2.32 +            if not os.path.exists(user_log_path):
    2.33 +                os.mkdir(user_log_path)
    2.34 +            elif not os.path.isdir(user_log_path):
    2.35 +                raise IOError(user_log_path + ': not a folder')
    2.36 +            
    2.37 +            return user_log_path
    2.38 +        
    2.39 +        elif sys.platform == 'win32' or sys.platform == 'cygwin':
    2.40 +            return user_log_path
    2.41 +        
    2.42 +        else:
    2.43 +            raise OSError()
    2.44 +            
    2.45 +    log_path = property(log_path_get)
    2.46 +            
    2.47 +            
    2.48 +# test method
    2.49  def test():
    2.50  
    2.51      """Test: class Environment"""
    2.52      e = Environment('My Application')
    2.53      print('prefix_path: "{0}"'.format(e.prefix_path))
    2.54      print('  data_path: "{0}"'.format(e.data_path))
    2.55 +    print('   log_path: "{0}"'.format(e.log_path))
    2.56  
    2.57  
    2.58  # test the module			
     3.1 --- a/OpenSecurity/bin/opensecurity_util.py	Wed Feb 19 10:40:39 2014 +0100
     3.2 +++ b/OpenSecurity/bin/opensecurity_util.py	Wed Feb 19 11:30:28 2014 +0100
     3.3 @@ -1,26 +1,45 @@
     3.4 -import logging
     3.5 -
     3.6 -class OpenSecurityException(Exception):
     3.7 -    def __init__(self, value):
     3.8 -        self.value = value
     3.9 -    def __str__(self):
    3.10 -        return repr(self.value)
    3.11 -    
    3.12 -def setupLogger(name='OpenSecurity'):
    3.13 -    logger = logging.getLogger(name)
    3.14 -    logger.setLevel(logging.DEBUG)
    3.15 -    # create formatter and add it to the handlers
    3.16 -    formatter = logging.Formatter('%(asctime)-15s - %(name)s - %(levelname)s - %(message)s')
    3.17 -    # create file handler which logs even debug messages
    3.18 -    fh = logging.FileHandler(name+'.log')
    3.19 -    fh.setLevel(logging.DEBUG)
    3.20 -    fh.setFormatter(formatter)
    3.21 -    logger.addHandler(fh)
    3.22 -    # create console handler with a higher log level
    3.23 -    ch = logging.StreamHandler()
    3.24 -    ch.setLevel(logging.DEBUG)
    3.25 -    ch.setFormatter(formatter)
    3.26 -    logger.addHandler(ch)
    3.27 -    return logger
    3.28 -
    3.29 -logger = setupLogger()
    3.30 \ No newline at end of file
    3.31 +#!/bin/env python
    3.32 +# -*- coding: utf-8 -*-
    3.33 +
    3.34 +import logging
    3.35 +import os
    3.36 +
    3.37 +
    3.38 +# local
    3.39 +from environment import Environment
    3.40 +
    3.41 +class OpenSecurityException(Exception):
    3.42 +    def __init__(self, value):
    3.43 +        self.value = value
    3.44 +    def __str__(self):
    3.45 +        return repr(self.value)
    3.46 +    
    3.47 +def setupLogger(name='OpenSecurity'):
    3.48 +    logger = logging.getLogger(name)
    3.49 +    logger.setLevel(logging.DEBUG)
    3.50 +    # create formatter and add it to the handlers
    3.51 +    formatter = logging.Formatter('%(asctime)-15s - %(name)s - %(levelname)s - %(message)s')
    3.52 +    # create file handler which logs even debug messages
    3.53 +    fh = logging.FileHandler(os.path.join(Environment('OpenSecurity').log_path, name + '.log'))
    3.54 +    fh.setLevel(logging.DEBUG)
    3.55 +    fh.setFormatter(formatter)
    3.56 +    logger.addHandler(fh)
    3.57 +    # create console handler with a higher log level
    3.58 +    ch = logging.StreamHandler()
    3.59 +    ch.setLevel(logging.DEBUG)
    3.60 +    ch.setFormatter(formatter)
    3.61 +    logger.addHandler(ch)
    3.62 +    return logger
    3.63 +
    3.64 +logger = setupLogger()
    3.65 +
    3.66 +# test method
    3.67 +def test():
    3.68 +
    3.69 +    """Test: OpenSecurity logging"""
    3.70 +    logger.info('test logging')
    3.71 +
    3.72 +
    3.73 +# test the module           
    3.74 +if __name__ == '__main__':
    3.75 +    test()