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.

122 lines
3.1 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
  1. #!/usr/bin/env python2
  2. """Youtubedlg module responsible for handling the log stuff. """
  3. import os.path
  4. from time import strftime
  5. import wx
  6. from .utils import check_path
  7. class LogManager(object):
  8. """Simple log manager for youtube-dl.
  9. This class is mainly used for loggin the youtube-dl STDERR.
  10. Attributes:
  11. LOG_FILENAME (string): Filename of the log file.
  12. TIME_TEMPLATE (string): Custom template to log the time.
  13. MAX_LOGSIZE (int): Maximum number (Bytes) of the log file.
  14. Args:
  15. config_path (string): Absolute path where LogManager should
  16. store the log file.
  17. add_time (boolean): If True LogManager will also log the time.
  18. """
  19. LOG_FILENAME = "log"
  20. TIME_TEMPLATE = "[{time}] {error_msg}"
  21. MAX_LOGSIZE = 524288
  22. def __init__(self, config_path, add_time=False):
  23. self.config_path = config_path
  24. self.add_time = add_time
  25. self.log_file = os.path.join(config_path, self.LOG_FILENAME)
  26. self._init_log()
  27. self._auto_clear_log()
  28. def log_size(self):
  29. """Return log file size in Bytes. """
  30. if not os.path.exists(self.log_file):
  31. return 0
  32. return os.path.getsize(self.log_file)
  33. def clear(self):
  34. """Clear log file. """
  35. self._write('', 'w')
  36. def log(self, data):
  37. """Log data to log file. """
  38. self._write(data + '\n', 'a')
  39. def _write(self, data, mode):
  40. """Write data to log file.
  41. That's the main method for writing to the log file.
  42. Args:
  43. data (string): String to write on the log file.
  44. mode (string): Can be any IO mode supported by python.
  45. """
  46. check_path(self.config_path)
  47. with open(self.log_file, mode) as log:
  48. if mode == 'a' and self.add_time:
  49. msg = self.TIME_TEMPLATE.format(time=strftime('%c'), error_msg=data)
  50. else:
  51. msg = data
  52. log.write(msg)
  53. def _init_log(self):
  54. """Init log file if not exist. """
  55. if not os.path.exists(self.log_file):
  56. self._write('', 'w')
  57. def _auto_clear_log(self):
  58. """Auto clear log file. """
  59. if self.log_size() > self.MAX_LOGSIZE:
  60. self.clear()
  61. class LogGUI(wx.Frame):
  62. """Simple GUI for youtube-dlg.
  63. Attributes:
  64. TITLE (string): Frame title.
  65. FRAME_SIZE (tuple): Tuple that holds the frame size (width, height).
  66. Args:
  67. parent (wx.Window): Frame parent.
  68. """
  69. TITLE = "Log Viewer"
  70. FRAME_SIZE = (650, 200)
  71. def __init__(self, parent=None):
  72. wx.Frame.__init__(self, parent, title=self.TITLE, size=self.FRAME_SIZE)
  73. panel = wx.Panel(self)
  74. self._text_area = wx.TextCtrl(
  75. panel,
  76. style=wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL
  77. )
  78. sizer = wx.BoxSizer()
  79. sizer.Add(self._text_area, 1, wx.EXPAND)
  80. panel.SetSizerAndFit(sizer)
  81. def load(self, filename):
  82. """Load file content on text area. """
  83. if os.path.exists(filename):
  84. self._text_area.LoadFile(filename)