logger.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import os
  2. import sys
  3. import re
  4. from .settings import settings
  5. sep = "-" * 70
  6. def supports_color():
  7. try:
  8. """
  9. from https://github.com/django/django/blob/master/django/core/management/color.py
  10. Return True if the running system's terminal supports color,
  11. and False otherwise.
  12. """
  13. plat = sys.platform
  14. supported_platform = plat != 'Pocket PC' and (plat != 'win32' or 'ANSICON' in os.environ)
  15. # isatty is not always implemented, #6223.
  16. is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
  17. if not supported_platform or not is_a_tty:
  18. return "No", False
  19. return "Yes", True
  20. except Exception as e:
  21. print("Error while logging: {}" + str(e))
  22. def log_seperator():
  23. try:
  24. print(sep)
  25. if settings.log_to_file == 'True':
  26. try:
  27. with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
  28. f.write(sep + '\n')
  29. f.close()
  30. except:
  31. pass
  32. sys.stdout.flush()
  33. except Exception as e:
  34. print("Error while logging: {}" + str(e))
  35. def log_info_green(string):
  36. try:
  37. if supports_color()[1] == False:
  38. print(string)
  39. else:
  40. print('[\x1B[1;32;40mI\x1B[0m] {:s}\x1B[0m'.format(string))
  41. if settings.log_to_file == 'True':
  42. try:
  43. with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
  44. f.write("[I] {:s}\n".format(string))
  45. f.close()
  46. except:
  47. pass
  48. sys.stdout.flush()
  49. except Exception as e:
  50. print("Error while logging: {}" + str(e))
  51. def log_info_blue(string):
  52. try:
  53. if supports_color()[1] == False:
  54. print(string)
  55. else:
  56. print('[\x1B[1;34;40mI\x1B[0m] {:s}\x1B[0m'.format(string))
  57. if settings.log_to_file == 'True':
  58. try:
  59. with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
  60. f.write("[I] {:s}\n".format(string))
  61. f.close()
  62. except:
  63. pass
  64. sys.stdout.flush()
  65. except Exception as e:
  66. print("Error while logging: {}" + str(e))
  67. def log_warn(string):
  68. try:
  69. if supports_color()[1] == False:
  70. print(string)
  71. else:
  72. print('[\x1B[1;33;40mW\x1B[0m] {:s}\x1B[0m'.format(string))
  73. if settings.log_to_file == 'True':
  74. try:
  75. with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
  76. f.write("[W] {:s}\n".format(string))
  77. f.close()
  78. except:
  79. pass
  80. sys.stdout.flush()
  81. except Exception as e:
  82. print("Error while logging: {}" + str(e))
  83. def log_error(string):
  84. try:
  85. if supports_color()[1] == False:
  86. print(string)
  87. else:
  88. print('[\x1B[1;31;40mE\x1B[0m] {:s}\x1B[0m'.format(string))
  89. if settings.log_to_file == 'True':
  90. try:
  91. with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
  92. f.write("[E] {:s}\n".format(string))
  93. f.close()
  94. except:
  95. pass
  96. sys.stdout.flush()
  97. except Exception as e:
  98. print("Error while logging: {}" + str(e))
  99. def log_whiteline():
  100. try:
  101. print("")
  102. if settings.log_to_file == 'True':
  103. try:
  104. with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
  105. f.write("\n")
  106. f.close()
  107. except:
  108. pass
  109. sys.stdout.flush()
  110. except Exception as e:
  111. print("Error while logging: {}" + str(e))
  112. def log_plain(string):
  113. try:
  114. print(string)
  115. if settings.log_to_file == 'True':
  116. try:
  117. with open("pyinstalive{:s}.log".format("_" + settings.user_to_download if len(settings.user_to_download) > 0 else ".default"),"a+") as f:
  118. f.write("{:s}\n".format(string))
  119. f.close()
  120. except:
  121. pass
  122. sys.stdout.flush()
  123. except Exception as e:
  124. print("Error while logging: {}" + str(e))