Zamiel

log_watcher.py

Aug 31st, 2018
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. #! C:\Python27\python.exe
  2.  
  3. # Imports
  4. import os
  5. import re
  6. import sys
  7.  
  8. # Configuration
  9. #show_everything = True
  10. show_everything = False
  11. #logFilePath = 'C:/Users/' + os.getenv('username') + '/Documents/My Games/Binding of Isaac Rebirth/log.txt'
  12. #logFilePath = 'C:/Users/' + os.getenv('username') + '/Documents/My Games/Binding of Isaac Afterbirth/log.txt'
  13. logFilePath = 'C:/Users/' + os.getenv('username') + '/Documents/My Games/Binding of Isaac Afterbirth+/log.txt'
  14.  
  15. fileArrayPosition = 0
  16.  
  17. def main():
  18.     while True:
  19.         parseLog()
  20.  
  21. def parseLog():
  22.     global fileArrayPosition
  23.  
  24.     # Read the log into a variable
  25.     try:
  26.         with open(logFilePath, 'r') as f:
  27.             fileContents = f.read()
  28.     except Exception:
  29.         print('I could not open the log file at "' + logFilePath + '".')
  30.         sys.exit(1)
  31.  
  32.     # Convert it to an array
  33.     fileArray = fileContents.splitlines()
  34.  
  35.     # Return to the start if we go past the end of the file (which occurs when the log file is truncated)
  36.     if fileArrayPosition > len(fileArray):
  37.         fileArrayPosition = 0
  38.  
  39.     # Process the log's new output
  40.     for line in fileArray[fileArrayPosition:]:
  41.         # Show everything for debugging purposes
  42.         if show_everything:
  43.             print(line)
  44.  
  45.         # Show errors and Lua debugging messages
  46.         if show_everything == False:
  47.             if line.startswith('[INFO] - ERR'):
  48.                 print(line)
  49.             elif line.startswith('[INFO] - Lua Debug: '):
  50.                 print(line)
  51.             elif "Error" in line or "error" in line or "ERROR" in line:
  52.                 print(line)
  53.             elif "Warn" in line or "warn" in line or "WARN" in line:
  54.                 print(line)
  55.             elif "Fail" in line:
  56.                 print(line)
  57.  
  58.     # Set that we have read the log up to this point
  59.     fileArrayPosition = len(fileArray)
  60.  
  61. # The main program
  62. if __name__ == '__main__':
  63.     main()
Add Comment
Please, Sign In to add comment