logger.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import os
  2. import sys
  3. try:
  4. import pil
  5. import helpers
  6. from constants import Constants
  7. except ImportError:
  8. from . import pil
  9. from . import helpers
  10. from .constants import Constants
  11. def supports_color():
  12. try:
  13. """
  14. from https://github.com/django/django/blob/master/django/core/management/color.py
  15. Return True if the running system's terminal supports color,
  16. and False otherwise.
  17. """
  18. plat = sys.platform
  19. supported_platform = plat != 'Pocket PC' and (plat != 'win32' or 'ANSICON' in os.environ)
  20. # isatty is not always implemented, #6223.
  21. is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
  22. if not supported_platform or not is_a_tty:
  23. return False
  24. return True
  25. except Exception:
  26. return False
  27. PREFIX_ERROR = '\x1B[1;31;49m[E]\x1B[0m'
  28. PREFIX_INFO = '\x1B[1;32;49m[I]\x1B[0m'
  29. PREFIX_WARN = '\x1B[1;33;49m[W]\x1B[0m'
  30. PREFIX_BINFO = '\x1B[1;34;49m[I]\x1B[0m'
  31. PRINT_SEP = '-' * 75
  32. SUPP_COLOR = supports_color()
  33. def info(log_str, force_plain=False):
  34. if SUPP_COLOR and not force_plain:
  35. to_print = "{:s} {:s}".format(PREFIX_INFO, log_str)
  36. else:
  37. to_print = "[I] {:s}".format(log_str)
  38. print(to_print)
  39. if pil.log_to_file:
  40. _log_to_file(log_str)
  41. def binfo(log_str, force_plain=False):
  42. if SUPP_COLOR and not force_plain:
  43. to_print = "{:s} {:s}".format(PREFIX_BINFO, log_str)
  44. else:
  45. to_print = "[I] {:s}".format(log_str)
  46. print(to_print)
  47. if pil.log_to_file:
  48. _log_to_file(log_str)
  49. def warn(log_str, force_plain=False):
  50. if SUPP_COLOR and not force_plain:
  51. to_print = "{:s} {:s}".format(PREFIX_WARN, log_str)
  52. else:
  53. to_print = "[W] {:s}".format(log_str)
  54. print(to_print)
  55. if pil.log_to_file:
  56. _log_to_file(log_str)
  57. def error(log_str, force_plain=False):
  58. if SUPP_COLOR and not force_plain:
  59. to_print = "{:s} {:s}".format(PREFIX_ERROR, log_str)
  60. else:
  61. to_print = "[E] {:s}".format(log_str)
  62. print(to_print)
  63. if pil.log_to_file:
  64. _log_to_file(log_str)
  65. def plain(log_str):
  66. print("{:s}".format(log_str))
  67. if pil.log_to_file:
  68. _log_to_file("{:s}".format(log_str))
  69. def whiteline():
  70. print("")
  71. if pil.log_to_file:
  72. _log_to_file("")
  73. def separator():
  74. print(PRINT_SEP)
  75. if pil.log_to_file:
  76. _log_to_file(PRINT_SEP)
  77. def banner():
  78. separator()
  79. binfo("PYINSTALIVE (SCRIPT V{:s} - PYTHON V{:s}) - {:s}".format(Constants.SCRIPT_VER, Constants.PYTHON_VER,
  80. helpers.strdatetime()))
  81. separator()
  82. def _log_to_file(log_str):
  83. try:
  84. with open("pyinstalive{:s}.log".format(
  85. "_" + pil.dl_user if len(pil.dl_user) > 0 else ".default"), "a+") as f:
  86. f.write(log_str + '\n')
  87. f.close()
  88. except Exception:
  89. pass