~ubuntu-branches/debian/experimental/dogtail/experimental

« back to all changes in this revision

Viewing changes to dogtail/logging.py

  • Committer: Package Import Robot
  • Author(s): Alessio Treglia
  • Date: 2013-02-09 16:01:44 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20130209160144-k2yk35tll4eew9wg
Tags: 0.8.1-1
* New maintainer. (Closes: #696136) (Closes: #553898)
* Set packaging format to 3.0 (quilt).
* New upstream release (Closes: #486452):
  - String exceptions are not used anymore. (Closes: #585287)
  - Fix missing check in findChildren(), tree.py (Closes: #485758)
* ACK NMUs:
  - Convert APT's API patch into the quilt format. (Closes: #572087)
  - Convert Ludovico Gardenghi's patch into the quilt
    format. (Closes: #485752)
* Fix desktop file as Freedesktop.org's per-spec.
* Migrate from CDBS + python-support to DH short-form + dh_python2.
* Move to section python.
* Refresh {,Build-}Depends lists.
* Remove xbase-clients from Depends. (Closes: #601486)
* Add Homepage field. (Closes: #572570)
* Add watch file.
* Add gbp config file.
* Refresh debian/copyright to meet copyright format 1.0.
* Install NEWS as upstream changelog.
* Bump Standards.

Show diffs side-by-side

added added

removed removed

Lines of Context:
81
81
                    self.now = self.now + ":" + self.zeroPad(self.timetup[i])
82
82
        return self.now
83
83
 
84
 
class IconLogger:
85
 
    """
86
 
    Writes entries to the tooltip of an icon in the notification area or the desktop.
87
 
    """
88
 
    trayicon = None
89
 
    def __init__(self):
90
 
        if not IconLogger.trayicon:
91
 
            from trayicon import TrayIcon
92
 
            IconLogger.trayicon = TrayIcon()
93
 
            if IconLogger.trayicon.proc: self.works = True
94
 
            else: self.works = False
95
 
            iconName = 'dogtail-tail-48.png'
96
 
            iconPath = '/usr/share/icons/hicolor/48x48/apps/' + iconName
97
 
            if os.path.exists(iconPath):
98
 
                IconLogger.trayicon.set_icon(iconPath)
99
 
            self.message('dogtail running...')
100
 
 
101
 
    def message(self, msg):
102
 
        """
103
 
        Display a message to the user
104
 
        """
105
 
        IconLogger.trayicon.set_tooltip(msg)
106
 
 
107
 
    def __del__(self):
108
 
        IconLogger.trayicon.close()
109
 
 
110
84
class Logger:
111
85
    """
112
 
    Writes entries to standard out, and to an IconLogger if desired.
 
86
    Writes entries to standard out.
113
87
    """
114
 
    iconLogger = None
115
88
    stamper = TimeStamp()
116
89
    def __init__(self, logName, file = False, stdOut = True):
117
90
        """
118
 
        FIXME! make this log to a file based on the name arg.
119
 
 
120
91
        name: the name of the log
121
 
 
122
92
        file: The file object to log to.
123
 
 
124
93
        stdOut: Whether to log to standard out.
125
94
        """
126
95
        self.logName = logName
128
97
        self.file = file # Handle to the logfile
129
98
        if not self.file: return
130
99
 
131
 
        logDir = config.logDir
132
 
        if not os.path.isdir(logDir): os.makedirs(logDir)
133
 
 
134
100
        scriptName = config.scriptName
135
101
        if not scriptName: scriptName = 'log'
136
102
        self.fileName = scriptName
137
103
 
138
104
        # check to see if we can write to the logDir
139
 
        if os.path.isdir(logDir):
140
 
            # generate a logfile name and check if it already exists
141
 
            self.fileName = logDir + self.stamper.fileStamp(self.fileName) + '_' + self.logName
142
 
            i = 0
143
 
            while os.path.exists(self.fileName):
144
 
                # Append the pathname
145
 
                if i == 0:
146
 
                    self.fileName = self.fileName + "." + str(i)
147
 
                else:
148
 
                    logsplit = self.fileName.split(".")
149
 
                    logsplit[-1] = str(i)
150
 
                    self.fileName = ".".join(logsplit)
151
 
                i += 1
 
105
        if os.path.isdir(config.logDir):
 
106
            self.findUniqueName()
152
107
        else:
153
108
            # If path doesn't exist, raise an exception
154
 
            raise IOError, "Log path %s does not exist or is not a directory" % logDir
155
 
 
 
109
            raise IOError, \
 
110
                    "Log path %s does not exist or is not a directory" % logDir
 
111
 
 
112
    def findUniqueName(self):
 
113
        # generate a logfile name and check if it already exists
 
114
        self.fileName = config.logDir + self.stamper.fileStamp(self.fileName) \
 
115
                + '_' + self.logName
 
116
        i = 0
 
117
        while os.path.exists(self.fileName):
 
118
            # Append the pathname
 
119
            if i == 0:
 
120
                self.fileName = self.fileName + "." + str(i)
 
121
            else:
 
122
                logsplit = self.fileName.split(".")
 
123
                logsplit[-1] = str(i)
 
124
                self.fileName = ".".join(logsplit)
 
125
            i += 1
 
126
 
 
127
    def createFile(self):
156
128
        # Try to create the file and write the header info
157
 
        try:
158
 
            print "Creating logfile at %s ..." % self.fileName
159
 
            self.file = codecs.open(self.fileName, mode = 'wb', encoding = 'utf-8')
160
 
            self.file.write("##### " + os.path.basename(self.fileName) + '\n')
161
 
            self.file.flush()
162
 
            #self.file.close()
163
 
        except IOError:
164
 
            print "Could not create and write to " + self.fileName
 
129
        print "Creating logfile at %s ..." % self.fileName
 
130
        self.file = codecs.open(self.fileName, mode = 'wb', encoding = \
 
131
                'utf-8')
 
132
        self.file.write("##### " + os.path.basename(self.fileName) + '\n')
 
133
        self.file.flush()
165
134
 
166
 
    def log(self, message):
 
135
    def log(self, message, newline = True, force = False):
167
136
        """
168
137
        Hook used for logging messages. Might eventually be a virtual
169
138
        function, but nice and simple for now.
 
139
 
 
140
        If force is True, log to a file irrespective of config.logDebugToFile.
170
141
        """
171
142
        message = message.decode('utf-8', 'replace')
172
143
 
173
 
        # Try to use the IconLogger.
174
 
        if self.iconLogger and self.iconLogger.works:
175
 
            self.iconLogger.message(message)
176
 
        
177
 
        # Also write to standard out.
178
 
        if self.stdOut: print message
179
 
 
180
144
        # Try to open and write the result to the log file.
181
 
        if not self.file: return
182
 
        try:
183
 
            #self.file = open(self.fileName, 'a')
184
 
            self.file.write(message + '\n')
 
145
        if isinstance(self.file, bool) and (force or config.logDebugToFile):
 
146
            self.createFile()
 
147
 
 
148
        if force or config.logDebugToFile:
 
149
            if newline: self.file.write(message + '\n')
 
150
            else: self.file.write(message + ' ')
185
151
            self.file.flush()
186
 
            #self.file.close()
187
 
        except IOError:
188
 
            print "Could not write to file " + self.fileName
 
152
 
 
153
        if self.stdOut and config.logDebugToStdOut: 
 
154
            if newline: print message
 
155
            else: print message,
189
156
 
190
157
class ResultsLogger(Logger):
191
158
    """
193
160
    """
194
161
    def __init__(self, stdOut = True):
195
162
        Logger.__init__(self, 'results', file = True, stdOut = stdOut)
196
 
        # Set the logDir - maybe we want to use mktemp(1) for this later.
197
163
 
198
164
    # Writes the result of a test case comparison to the log
199
165
    def log(self, entry):
214
180
            raise ValueError, entry
215
181
            print "Method argument requires a 1 {key: value} dict. Supplied argument not one {key: value}"
216
182
 
217
 
        Logger.log(self, self.stamper.entryStamp() + "      " + entry)
 
183
        Logger.log(self, self.stamper.entryStamp() + "      " + entry, force = True)
218
184
 
219
 
debugLogger = Logger('debug', file = True)
 
185
debugLogger = Logger('debug', config.logDebugToFile)
220
186
 
221
187
import traceback
222
188
def exceptionHook(exc, value, tb):