Example #1
0
    def __clean(self):
        try:
            if self.max_count < 0:
                return
            sorted_names = dict()
            """:type: dict[int, string]"""
            names = os.listdir(self.target)
            for name in names:
                if not re.match(r'^' + self.file_name + r'\.\d{20}$', name):
                    continue
                when = name.split('.')[-1]
                sorted_names[int(when)] = name
            deleted_count = len(sorted_names) - self.max_count

            deleted_list = list()
            for when, name in sorted_names.items():
                if deleted_count <= 0:
                    break
                deleted_list.append(name)
                deleted_count -= 1

            for name in deleted_list:
                full_path = os.path.join(self.target, name)
                os.remove(full_path)
        except:
            SysLog.error(traceback.format_exc())
Example #2
0
 def open(self):
     try:
         self.handler = open(self.file_path, 'ab')
         self.current_size = self.handler.tell()
         return True
     except:
         SysLog.error(traceback.format_exc())
Example #3
0
 def __rename(self):
     try:
         ending = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')
         new_file_path = self.file_path + '.' + ending
         os.rename(self.file_path, new_file_path)
     except:
         SysLog.error(traceback.format_exc())
Example #4
0
    def write(self, content):
        try:
            if self.lock is None:
                return self.__write(content)

            with self.lock:
                return self.__write(content)
        except:
            SysLog.error(traceback.format_exc())
Example #5
0
 def close(self):
     try:
         if self.handler is None:
             return
         if self.current_size <= 0:
             return
         self.handler.flush()
         self.handler.close()
         self.handler = None
     except:
         SysLog.error(traceback.format_exc())
Example #6
0
    def close(cls):
        try:
            if not cls.__process:
                cls.__process.close()
                cls.__process = None

            if not cls.__report:
                cls.__report.close()
                cls.__report = None

            SysLog.close()
        except:
            print traceback.format_exc()
Example #7
0
 def report(cls, msg, vid='', keyword='normal'):
     try:
         if cls.REPORT < cls.__current_log_level:
             return
         content = cls.__join_content(vid=vid,
                                      keyword=keyword,
                                      level='report',
                                      msg=msg)
         if not content:
             return
         cls.__report.write(content)
     except:
         SysLog.error(traceback.format_exc())
Example #8
0
 def fatal(cls, msg, vid='', keyword='normal'):
     try:
         if cls.FATAL < cls.__current_log_level:
             return
         content = cls.__join_content(vid=vid,
                                      keyword=keyword,
                                      level='fatal',
                                      msg=msg)
         if not content:
             return
         cls.__process.write(content)
     except:
         SysLog.error(traceback.format_exc())
Example #9
0
    def __write(self, content):
        try:
            try:
                self.handler.write(content)
                self.current_size = self.handler.tell()
            except Exception, e:
                self.close()
                self.open()
                SysLog.error(e.message)

            if self.current_size >= self.max_size:
                self.close()
                self.__rename()
                self.__clean()
                self.open()
            return True
Example #10
0
 def __join_content(cls, vid, keyword, level, msg):
     try:
         if cls.__thread_local.the_id is None:
             the_pid = os.getpid()
             the_tid = threading.current_thread().ident
             cls.__thread_local.the_id = '%s_%s' % (the_pid, the_tid)
         the_id = cls.__thread_local.the_id
         content = ''
         content += '[%s]%s' % (datetime.datetime.now().strftime(
             '%Y-%m-%d %H:%M:%S.%f'), cls.__sep)
         content += '[%s]%s' % (level, cls.__sep)
         content += '[%s]%s' % (the_id, cls.__sep)
         content += '[%s]%s' % (cls.__env, cls.__sep)
         content += '[%s]%s' % (vid, cls.__sep)
         content += '[%s]%s' % (keyword, cls.__sep)
         frame = inspect.currentframe().f_back.f_back
         info = traceback.extract_stack(f=frame, limit=1)[0]
         content += '[%s:%s:%s]%s' % (info[0], info[1], info[2], cls.__sep)
         content += '[%s]' % msg
         content += '\n'
         return content
     except:
         SysLog.error(traceback.format_exc())
Example #11
0
    def init(cls,
             env,
             target,
             file_name,
             file_size=100 * 1024 * 1024,
             max_file_count=-1,
             multiprocess=False):
        try:
            if multiprocess:
                lock = multiprocessing.RLock()
            else:
                lock = threading.RLock()

            if not os.path.exists(target):
                os.makedirs(target)
            if not os.path.exists(target):
                return False

            cls.__process = Writer(lock, target, file_name + '.process.log',
                                   file_size, max_file_count)
            if not cls.__process.open():
                return False
            cls.__report = Writer(lock, target, file_name + '.report.log',
                                  file_size, max_file_count)
            if not cls.__report.open():
                return False

            cls.__sep = '\x1E'
            cls.__env = env

            cls.__thread_local = threading.local()
            cls.__thread_local.the_id = None

            if not SysLog.open():
                return False
            return True
        except:
            print traceback.format_exc()
Example #12
0
            try:
                self.handler.write(content)
                self.current_size = self.handler.tell()
            except Exception, e:
                self.close()
                self.open()
                SysLog.error(e.message)

            if self.current_size >= self.max_size:
                self.close()
                self.__rename()
                self.__clean()
                self.open()
            return True
        except:
            SysLog.error(traceback.format_exc())

    def close(self):
        try:
            if self.handler is None:
                return
            if self.current_size <= 0:
                return
            self.handler.flush()
            self.handler.close()
            self.handler = None
        except:
            SysLog.error(traceback.format_exc())

    def __rename(self):
        try: