logger.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 supports_color() == "No":
  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. sys.stdout.flush()
  46. def seperator(color):
  47. if supports_color() == "No":
  48. print(sep)
  49. else:
  50. print('\033[1m' + colors(color) + (sep) + colors("ENDC"))
  51. if settings.log_to_file == 'True':
  52. try:
  53. with open("pyinstalive_{:s}.log".format(settings.user_to_record),"a+") as f:
  54. f.write(sep + "\n")
  55. f.close()
  56. except:
  57. pass
  58. sys.stdout.flush()