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.

60 lines
1.3 KiB

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