Package gui :: Module logViewer
[hide private]
[frames] | no frames]

Source Code for Module gui.logViewer

  1  """Provides functionality to view the NVDA log. 
  2  """ 
  3   
  4  import codecs 
  5  import wx 
  6  import globalVars 
  7  import gui 
  8   
  9  #: The singleton instance of the log viewer UI. 
 10  logViewer = None 
 11   
12 -class LogViewer(wx.Frame):
13 """The NVDA log viewer GUI. 14 """ 15
16 - def __init__(self, parent):
17 super(LogViewer, self).__init__(parent, wx.ID_ANY, _("NVDA Log Viewer")) 18 self.Bind(wx.EVT_ACTIVATE, self.onActivate) 19 self.Bind(wx.EVT_CLOSE, self.onClose) 20 mainSizer = wx.BoxSizer(wx.VERTICAL) 21 self.outputCtrl = wx.TextCtrl(self, wx.ID_ANY, size=(500, 500), style=wx.TE_MULTILINE | wx.TE_READONLY|wx.TE_RICH) 22 self.outputCtrl.Bind(wx.EVT_CHAR, self.onOutputChar) 23 mainSizer.Add(self.outputCtrl, proportion=1, flag=wx.EXPAND) 24 self.SetSizer(mainSizer) 25 mainSizer.Fit(self) 26 27 menuBar = wx.MenuBar() 28 menu = wx.Menu() 29 item = menu.Append(wx.ID_ANY, _("Refresh F5")) 30 self.Bind(wx.EVT_MENU, self.refresh, item) 31 item = menu.Append(wx.ID_SAVEAS) 32 self.Bind(wx.EVT_MENU, self.onSaveAsCommand, item) 33 menu.AppendSeparator() 34 item = menu.Append(wx.ID_EXIT, _("E&xit")) 35 self.Bind(wx.EVT_MENU, self.onClose, item) 36 menuBar.Append(menu, _("Log")) 37 self.SetMenuBar(menuBar) 38 39 self._lastFilePos = 0 40 41 self.refresh() 42 self.outputCtrl.SetFocus()
43
44 - def refresh(self, evt=None):
45 pos = self.outputCtrl.GetInsertionPoint() 46 # Append new text to the output control which has been written to the log file since the last refresh. 47 try: 48 f = codecs.open(globalVars.appArgs.logFileName, "r", encoding="UTF-8") 49 f.seek(self._lastFilePos) 50 self.outputCtrl.AppendText(f.read()) 51 self._lastFilePos = f.tell() 52 self.outputCtrl.SetInsertionPoint(pos) 53 f.close() 54 except IOError: 55 pass
56
57 - def onActivate(self, evt):
58 if evt.GetActive(): 59 self.refresh() 60 evt.Skip()
61
62 - def onClose(self, evt):
63 self.Destroy()
64
65 - def onSaveAsCommand(self, evt):
66 filename = wx.FileSelector(_("Save As"), default_filename="nvda.log", flags=wx.SAVE | wx.OVERWRITE_PROMPT, parent=self) 67 if not filename: 68 return 69 try: 70 # codecs.open() forces binary mode, which is bad under Windows because line endings won't be converted to crlf automatically. 71 # Therefore, do the encoding manually. 72 file(filename, "w").write(self.outputCtrl.GetValue().encode("UTF-8")) 73 except (IOError, OSError), e: 74 gui.messageBox(_("Error saving log: %s") % e.strerror, _("Error"), style=wx.OK | wx.ICON_ERROR, parent=self)
75
76 - def onOutputChar(self, evt):
77 key = evt.GetKeyCode() 78 if key == wx.WXK_ESCAPE: 79 self.Close() 80 evt.Skip()
81
82 -def activate():
83 """Activate the log viewer. 84 If the log viewer has not already been created and opened, this will create and open it. 85 Otherwise, it will be brought to the foreground if possible. 86 """ 87 global logViewer 88 if globalVars.appArgs.secure: 89 # The log might expose sensitive information and the Save As dialog in the Log Viewer is a security risk. 90 return 91 if not logViewer: 92 logViewer = LogViewer(gui.mainFrame) 93 logViewer.Raise() 94 # There is a MAXIMIZE style which can be used on the frame at construction, but it doesn't seem to work the first time it is shown, 95 # probably because it was in the background. 96 # Therefore, explicitly maximise it here. 97 # This also ensures that it will be maximized whenever it is activated, even if the user restored/minimised it. 98 logViewer.Maximize() 99 logViewer.Show()
100