ConsoleHandler.py 3.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

''' 
Created on Apr 14, 2015

:author: Eric C. Pellegrini
'''

import logging

import wx

37
from MDANSE import REGISTRY
38
39
40
41
42
43
44
45
46
47
from MDANSE.Framework.Handlers.IHandler import IHandler
from MDANSE.Logging.Formatters import Formatter
  
class ConsoleHandler(IHandler, logging.Handler):
    """Sets up a GUI handler for the MDANSE logger.
    
    Emits the logging message to the MDANSE GUI console.

    @note: inherits from logging.Handler class that sets a generic handler.
    """
48
        
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    COLORS = {'DEBUG'    : wx.GREEN,
              'INFO'     : wx.BLACK,
              'WARNING'  : wx.BLUE,
              'ERROR'    : wx.RED,
              'CRITICAL' : wx.RED,
              'FATAL'    : wx.RED}
    
    def __init__(self, window):
        '''
        The constructor.
        
        @param console: the parent widget for the textctrl.
        @type console: wx widget
        '''

        logging.Handler.__init__(self)
        
        self.setFormatter(Formatter())
         
        self._window = window

        # Creates a wx text attribute.        
        self.style = wx.TextAttr()

        # Set its font to a non proporiotnal font.
        self.style.SetFont(wx.Font(10, wx.FONTFAMILY_MODERN, wx.NORMAL, wx.NORMAL))
        
            
    def emit(self, record):
        """
        Send the log message to a wx.TextCtrl widget.
        
        @param record: the log message.
        @type record: logging.LogRecord        
        """
        
85
86
87
88
        record.msg = record.msg.strip()
        if not record.msg:
            return
        
89
90
91
92
93
94
95
96
97
        self.style.SetTextColour(ConsoleHandler.COLORS.get(record.levelname,wx.BLACK))
                                    
        # Set the the created text attribute as the default style for the text ctrl.                            
        self._window.SetDefaultStyle(self.style)        
        
        # Append the log message to the text ctrl.
        self._window.AppendText(self.format(record))

        self._window.AppendText("\n")
98
99
100
101
102
103
104
        
    def write(self,message):
        
        record = logging.LogRecord("console",logging.INFO,None,None,message,None,None,None)
        
        self.emit(record)
        
105
REGISTRY["console"] = ConsoleHandler