def gungame_except_hook(tb_type, value, trace_back, mute_console=False): # If this error was called to stop an attribute from being set, do not log # it. if str(value) == "gg_cancel_callback": return tb = traceback.format_exception(tb_type, value, trace_back) # If not a gungame error, send to ES and return if 'gungame51' not in str(tb).lower(): es.excepter(tb_type, value, trace_back) return # Format the traceback for i in range(len(tb)): # Remove long file names ? if tb[i].strip().startswith('File "'): tb[i] = (tb[i].replace(tb[i][(tb[i].find('File "') + 6):tb[i].find('eventscripts')], '../')).replace('\\', '/') tb[-2] = tb[-2] + '\n' # turn tb into a string tb = reduce((lambda a, b: a + b), tb) # Is the length under 255 chars? if len(tb) < 255: db_tb = [tb] # Length over 255 chars else: db_tb = [x.strip() for x in tb.split('\n') if x != ''] # Print traceback to console? if not mute_console: es.dbgmsg(0, ' \n') es.dbgmsg(0, '# ' + '=' * 48) es.dbgmsg(0, '# >>' + 'GunGame 5.1 Exception Caught!'.rjust(50)) es.dbgmsg(0, '# ' + '=' * 48) # Divide up for 255 line limit for db_line in db_tb: es.dbgmsg(0, db_line) es.dbgmsg(0, '# ' + '=' * 48) es.dbgmsg(0, ' \n') # Does the log file exist yet? if not file_created: gamethread.delayed(5, gungame_except_hook, (tb_type, value, trace_back, True)) return # Use Log File with file_name.open('r+') as log_file: # Get contents log_contents = log_file.read() # Look for duplicate error find_error_index = log_contents.find(tb) # File has no duplicate error ? if find_error_index == -1: # Error template error_format = ['-=' * 39 + '-\n', (('LAST EVENT: ' + '%s' % strftime('[%m/%d/%Y @ %H:%M:%S]')) + ' ' * 9 + ' TOTAL OCCURENCES: [0001]').center(79) + '\n', '-=' * 39 + '-\n', '\n', tb, '\n', '\n'] # No duplicate, appending to end of file ''' For some reason we get an error if we do not read again here if someone knows why, please let me know! - Monday ''' log_file.read() log_file.writelines(error_format) else: # Go to the back to the begining of the file log_file.seek(0) # Increase occurence count error_count = (int(log_contents[(find_error_index - 92): (find_error_index - 88)]) + 1) # Write change w/ new date and occurence count log_file.write(log_contents[:(find_error_index - 241)] + log_contents[(find_error_index + len(tb) + 2):] + '-=' * 39 + '-\n' + (('LAST EVENT: ' + '%s' % strftime( '[%m/%d/%Y @ %H:%M:%S]')) + ' ' * 9 + ' TOTAL OCCURENCES:' + ' [%04i]' % error_count).center(79) + '\n' + '-=' * 39 + '-\n\n' + tb + '\n\n')
def gungame_except_hook(tb_type, value, trace_back, mute_console=False): # If this error was called to stop an attribute from being set, do not log # it. if str(value) == "gg_cancel_callback": return tb = traceback.format_exception(tb_type, value, trace_back) # If not a gungame error, send to ES and return if 'gungame51' not in str(tb).lower(): es.excepter(tb_type, value, trace_back) return # Format the traceback for i in range(len(tb)): # Remove long file names ? if tb[i].strip().startswith('File "'): tb[i] = (tb[i].replace( tb[i][(tb[i].find('File "') + 6):tb[i].find('eventscripts')], '../')).replace('\\', '/') tb[-2] = tb[-2] + '\n' # turn tb into a string tb = reduce((lambda a, b: a + b), tb) # Is the length under 255 chars? if len(tb) < 255: db_tb = [tb] # Length over 255 chars else: db_tb = [x.strip() for x in tb.split('\n') if x != ''] # Print traceback to console? if not mute_console: es.dbgmsg(0, ' \n') es.dbgmsg(0, '# ' + '=' * 48) es.dbgmsg(0, '# >>' + 'GunGame 5.1 Exception Caught!'.rjust(50)) es.dbgmsg(0, '# ' + '=' * 48) # Divide up for 255 line limit for db_line in db_tb: es.dbgmsg(0, db_line) es.dbgmsg(0, '# ' + '=' * 48) es.dbgmsg(0, ' \n') # Does the log file exist yet? if not file_created: gamethread.delayed(5, gungame_except_hook, (tb_type, value, trace_back, True)) return # Use Log File with file_name.open('r+') as log_file: # Get contents log_contents = log_file.read() # Look for duplicate error find_error_index = log_contents.find(tb) # File has no duplicate error ? if find_error_index == -1: # Error template error_format = [ '-=' * 39 + '-\n', (('LAST EVENT: ' + '%s' % strftime('[%m/%d/%Y @ %H:%M:%S]')) + ' ' * 9 + ' TOTAL OCCURENCES: [0001]').center(79) + '\n', '-=' * 39 + '-\n', '\n', tb, '\n', '\n' ] # No duplicate, appending to end of file ''' For some reason we get an error if we do not read again here if someone knows why, please let me know! - Monday ''' log_file.read() log_file.writelines(error_format) else: # Go to the back to the begining of the file log_file.seek(0) # Increase occurence count error_count = (int(log_contents[(find_error_index - 92):(find_error_index - 88)]) + 1) # Write change w/ new date and occurence count log_file.write( log_contents[:(find_error_index - 241)] + log_contents[(find_error_index + len(tb) + 2):] + '-=' * 39 + '-\n' + (('LAST EVENT: ' + '%s' % strftime('[%m/%d/%Y @ %H:%M:%S]')) + ' ' * 9 + ' TOTAL OCCURENCES:' + ' [%04i]' % error_count).center(79) + '\n' + '-=' * 39 + '-\n\n' + tb + '\n\n')
def unload_on_error(): es.dbgmsg(0, '[GunGame51] %s' % ('=' * 79)) es.excepter(*sys.exc_info()) es.dbgmsg(0, '[GunGame51] %s' % ('=' * 79)) es.unload('gungame51')