def initFile(fileName=None, postfix=''): dir = Globals.EventDir() if not os.path.isdir(dir): return None if fileName == None: log1 = os.path.join(dir, baseLogFileName(postfix)) else: log1 = os.path.join(dir, fileName) if not os.path.isfile(log1): fp = open(log1, 'w') fp.close() assert os.path.isfile(log1) elif os.stat(log1).st_size < 10 * 1024 * 1024: return log1 else: os.remove(log1) return log1 lines = Basic.readTextFile(log1) n = len(lines) if n > MAX_LINES: print('resize log file ' + log1 + '...') lines.append('resize log file to the last ' + str(MAX_LINES) + ' lines') newLines = lines[n - MAX_LINES:] Basic.writeTextFile(log1, newLines) return log1
def init(runTimeLog=None, errorLog=None, startMsg=None): global g_RuntimeLog, g_ErrorLog # create and open runtime logfile and error log # if runTimeLog == None: postfix1 = datetime.datetime.now().strftime("-%Y-%m-%d-%H-%M") g_RuntimeLog = initFile(postfix=postfix1) else: g_RuntimeLog = initFile(runTimeLog) if errorLog == None: g_ErrorLog = initFile() else: g_ErrorLog = initFile(errorLog) if startMsg: if g_ErrorLog and os.path.isfile(g_ErrorLog): lines = Basic.readTextFile(g_ErrorLog) lines2 = [] found = False for s in lines: if startMsg in s: found = True if found: lines2.append(s) #endfor if len(lines2) > 0: os.remove(g_ErrorLog) Basic.writeTextFile(g_ErrorLog, lines2) if startMsg != None: return setStartMsg(startMsg) else: return True