You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.9 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. #!/usr/bin/env python2
  2. ''' Simple log system for youtube-dlG. '''
  3. import os.path
  4. from time import strftime
  5. import wx
  6. from .utils import check_path
  7. class LogManager(object):
  8. '''
  9. OUT_OF_DATE
  10. Simple log manager for youtube-dlG.
  11. Params
  12. config_path: Absolute path where LogManager should store the log file.
  13. add_time: If True LogManager will also log the time.
  14. Accessible Methods
  15. log_size()
  16. Params: None
  17. Return: Log file size in Bytes
  18. clear()
  19. Params: None
  20. Return: None
  21. log()
  22. Params: Data to log
  23. Return: None
  24. Accessible Variables
  25. log_file: Absolute path to log file.
  26. '''
  27. LOG_FILENAME = "log"
  28. TIME_TEMPLATE = "[{time}] {error_msg}"
  29. MAX_LOGSIZE = 524288 # 524288B = 512kB
  30. def __init__(self, config_path, add_time=False):
  31. self.config_path = config_path
  32. self.add_time = add_time
  33. self.log_file = os.path.join(config_path, self.LOG_FILENAME)
  34. self._init_log()
  35. self._auto_clear_log()
  36. def log_size(self):
  37. ''' Return log file size in Bytes. '''
  38. if not os.path.exists(self.log_file):
  39. return 0
  40. return os.path.getsize(self.log_file)
  41. def clear(self):
  42. ''' Clear log file. '''
  43. self._write('', 'w')
  44. def log(self, data):
  45. ''' Log data to log file. '''
  46. self._write(data + '\n', 'a')
  47. def _write(self, data, mode):
  48. ''' Write data to log file using mode. '''
  49. check_path(self.config_path)
  50. with open(self.log_file, mode) as log:
  51. if mode == 'a' and self.add_time:
  52. msg = self.TIME_TEMPLATE.format(time=strftime('%c'), error_msg=data)
  53. else:
  54. msg = data
  55. log.write(msg)
  56. def _init_log(self):
  57. ''' Init log file if not exist. '''
  58. if not os.path.exists(self.log_file):
  59. self._write('', 'w')
  60. def _auto_clear_log(self):
  61. ''' Auto clear log file. '''
  62. if self.log_size() > self.MAX_LOGSIZE:
  63. self.clear()
  64. class LogGUI(wx.Frame):
  65. '''
  66. Simple GUI for LogManager
  67. Accessible Methods
  68. load()
  69. Params: File to load
  70. Return: None
  71. '''
  72. TITLE = "Log Viewer"
  73. FRAME_SIZE = (650, 200)
  74. def __init__(self, parent=None):
  75. wx.Frame.__init__(self, parent, title=self.TITLE, size=self.FRAME_SIZE)
  76. panel = wx.Panel(self)
  77. self._text_area = wx.TextCtrl(
  78. panel,
  79. style=wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL
  80. )
  81. sizer = wx.BoxSizer()
  82. sizer.Add(self._text_area, 1, wx.EXPAND)
  83. panel.SetSizerAndFit(sizer)
  84. def load(self, filename):
  85. ''' Load file on text area if file exists. '''
  86. if os.path.exists(filename):
  87. self._text_area.LoadFile(filename)