Esempio n. 1
0
    def enable_file_logging(self, toolname, num_logs=50, log_dir=None):
        """ Find a tmp location for file logging """
        from c_misc import backup_file
        import c_path

        if log_dir is None:
            log_dir = ''
            for tmp_path in [os.getenv('TEMP'), os.getenv('TMP')]:
                if c_path.validate_dir(tmp_path):
                    log_dir = c_path.join(tmp_path,
                                          '{0}_logs'.format(toolname))
                    break
            else:
                self.warning(
                    'CANNOT FIND A LOCAL TEMP DIRECTORY TO CREATE LOGS')

        # Set Log Folder
        flids = []
        if log_dir:
            log_dir = c_path.normalize(log_dir)
            log_file = c_path.join(log_dir, '{0}_log.txt'.format(toolname))
            log_file_debug = c_path.join(log_dir,
                                         '{0}_log_debug.txt'.format(toolname))

            # Create the logs directory
            retval, reterr = True, ''
            try:
                c_path.create_dir(log_dir)
            except Exception as e:
                retval, reterr = False, str(e)

            if not retval:
                self.warning(
                    'CANNOT CREATE DIRECTORY FOR LOGGING FILE: {0}\nPath: {1}'.
                    format(reterr, log_dir))
            elif num_logs > 1 and c_path.validate_file(
                    log_file) and not backup_file(log_file, num_logs):
                self.warning(
                    'FAILED TO BACKUP LOGGING FILE: \nPath: {0}'.format(
                        log_file))
            elif num_logs > 1 and c_path.validate_file(
                    log_file_debug) and not backup_file(
                        log_file_debug, num_logs):
                self.warning(
                    'FAILED TO BACKUP DEBUG LOGGING FILE: \nPath: {0}'.format(
                        log_file_debug))
            else:
                flids.append(self.add_file_logger(log_file, self.INFO, 'w'))
                self.info('Logging to {0}'.format(log_file))
                # Only create debug file if debug is enabled
                if self.verbosity < self.INFO:
                    flids.append(
                        self.add_file_logger(log_file_debug, self.verbosity,
                                             'w'))
                    self.debug('Debug logging to {0}'.format(log_file_debug))
        return flids
    def enable_file_logging(self, toolname, num_logs=50, log_dir=None):
        """ Find a tmp location for file logging """
        from c_misc import backup_file
        import c_path

        if log_dir is None:
            log_dir = ''
            for tmp_path in [os.getenv('TEMP'), os.getenv('TMP')]:
                if c_path.validate_dir(tmp_path):
                    log_dir = c_path.join(tmp_path, '{0}_logs'.format(toolname))
                    break
            else:
                self.warning('CANNOT FIND A LOCAL TEMP DIRECTORY TO CREATE LOGS')

        # Set Log Folder
        if log_dir:
            log_file = c_path.join(log_dir, '{0}_log.txt'.format(toolname))
            log_file_debug = c_path.join(log_dir, '{0}_log_debug.txt'.format(toolname))

            # Create the logs directory
            retval, reterr = True, ''
            try:
                c_path.create_dir(log_dir);
            except Exception as e:
                retval, reterr = False, str(e)

            if not retval:
                self.warning('CANNOT CREATE DIRECTORY FOR LOGGING FILE: {0}\n	Path: {1}'.format(reterr, log_dir))
            elif num_logs > 1 and c_path.validate_file(log_file) and not backup_file(log_file, num_logs):
                self.warning('FAILED TO BACKUP LOGGING FILE: \n	Path: {0}'.format(log_file))
            elif num_logs > 1 and c_path.validate_file(log_file_debug) and not backup_file(log_file_debug, num_logs):
                self.warning('FAILED TO BACKUP DEBUG LOGGING FILE: \n	Path: {0}'.format(log_file_debug))
            else:
                self.add_file_logger(log_file, self.INFO, 'w')
                self.info('Logging to {0}'.format(log_file))
                # Only create debug file if debug is enabled
                if self.verbosity < self.INFO:
                    self.add_file_logger(log_file_debug, self.verbosity, 'w')
                    self.debug('Debug logging to {0}'.format(log_file_debug))
                return log_file, log_file_debug
        return '', ''
    def __init__(self, clean=False):
        # Imports needed for tracking
        import time
        import tempfile
        import atexit

        # Initialize variables
        self.clean = clean
        self.orig_NamedTemporaryFile = tempfile.NamedTemporaryFile
        self.tempdir = tempfile.mkdtemp(prefix='sectools_' + time.asctime().replace(' ', '_').replace(':', '_') + '_')
        self.trackedfiles = []

        # Create the temporary folder
        c_path.create_dir(self.tempdir)

        # Override the NamedTemporaryFile call and the temp directory
        tempfile.NamedTemporaryFile = self.wrapper_NamedTemporaryFile
        tempfile.tempdir = self.tempdir

        # Register handler for exit
        atexit.register(self.exit_printer)