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.

68 lines
1.6 KiB

  1. #! /usr/bin/env python
  2. import wx
  3. from time import strftime
  4. from .Utils import (
  5. get_filesize,
  6. check_path,
  7. file_exist,
  8. fix_path
  9. )
  10. class LogManager(object):
  11. LOG_FILENAME = 'log'
  12. MAX_FILESIZE = 524288 # 524288B = 512kB
  13. def __init__(self, config_path, add_time=False):
  14. self.config_path = config_path
  15. self.add_time = add_time
  16. self.log_file = self._get_log_file()
  17. self._auto_clear_log()
  18. def size(self):
  19. if not file_exist(self.log_file):
  20. return 0
  21. return get_filesize(self.log_file)
  22. def clear(self):
  23. self._write('', 'w')
  24. def log(self, data):
  25. self._write(data + '\n', 'a')
  26. def _write(self, data, mode):
  27. check_path(self.config_path)
  28. with open(self.log_file, mode) as fl:
  29. if self.add_time:
  30. t = '[%s] ' % strftime('%c')
  31. fl.write(t)
  32. fl.write(data)
  33. def _auto_clear_log(self):
  34. if self.size() > self.MAX_FILESIZE:
  35. self.clear()
  36. def _get_log_file(self):
  37. return fix_path(self.config_path) + self.LOG_FILENAME
  38. class LogGUI(wx.Frame):
  39. TITLE = 'Log Viewer'
  40. def __init__(self, log_file, parent=None, id=-1):
  41. wx.Frame.__init__(self, parent, id, self.TITLE, size=(650, 200))
  42. panel = wx.Panel(self)
  43. text_area = wx.TextCtrl(panel, -1, style=wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL)
  44. sizer = wx.BoxSizer()
  45. sizer.Add(text_area, 1, wx.EXPAND)
  46. panel.SetSizerAndFit(sizer)
  47. text_area.LoadFile(log_file)