logger.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import os
  2. import sys
  3. from .settings import settings
  4. sep = "-" * 70
  5. def colors(state):
  6. color = ''
  7. if (state == 'BLUE'):
  8. color = '\033[94m'
  9. if (state == 'GREEN'):
  10. color = '\033[92m'
  11. if (state == 'YELLOW'):
  12. color = '\033[93m'
  13. if (state == 'RED'):
  14. color = '\033[91m'
  15. if (state == 'ENDC'):
  16. color = '\033[0m'
  17. if (state == 'WHITE'):
  18. color = '\033[0m'
  19. return color
  20. def supports_color():
  21. """
  22. from https://github.com/django/django/blob/master/django/core/management/color.py
  23. Return True if the running system's terminal supports color,
  24. and False otherwise.
  25. """
  26. plat = sys.platform
  27. supported_platform = plat != 'Pocket PC' and (plat != 'win32' or 'ANSICON' in os.environ)
  28. # isatty is not always implemented, #6223.
  29. is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
  30. if not supported_platform or not is_a_tty:
  31. return "No"
  32. return "Yes"
  33. def log(string, color):
  34. if not supports_color():
  35. print(string)
  36. else:
  37. print('\033[1m' + colors(color) + string + colors("ENDC"))
  38. if settings.log_to_file == 'True':
  39. try:
  40. with open("pyinstalive_{:s}.log".format(settings.user_to_record),"a+") as f:
  41. f.write(string + "\n")
  42. f.close()
  43. except:
  44. pass
  45. def seperator(color):
  46. if not supports_color():
  47. print(sep)
  48. else:
  49. print('\033[1m' + colors(color) + (sep) + colors("ENDC"))
  50. if settings.log_to_file == 'True':
  51. try:
  52. with open("pyinstalive_{:s}.log".format(settings.user_to_record),"a+") as f:
  53. f.write(sep + "\n")
  54. f.close()
  55. except:
  56. pass