Revision c87d99e9 vncauthproxy/proxy.py

b/vncauthproxy/proxy.py
133 133
        # the port pool, if applicable.
134 134
        if not self.pool is None:
135 135
            self.pool.append(self.sport)
136
            self.log.debug(("Returned port %d to port pool, contains %d ports",
137
                            self.sport, len(self.pool)))
136
            self.debug("Returned port %d to port pool, contains %d ports",
137
                       self.sport, len(self.pool))
138 138

  
139 139
        while self.listeners:
140 140
            self.listeners.pop().close()
......
145 145

  
146 146
        raise gevent.GreenletExit
147 147

  
148
    def info(self, msg):
149
        self.log.info("[C%d] %s" % (self.id, msg))
150

  
151
    def debug(self, msg):
152
        self.log.debug("[C%d] %s" % (self.id, msg))
153

  
154
    def warn(self, msg):
155
        self.log.warn("[C%d] %s" % (self.id, msg))
156

  
157
    def error(self, msg):
158
        self.log.error("[C%d] %s" % (self.id, msg))
159

  
160
    def critical(self, msg):
161
        self.log.critical("[C%d] %s" % (self.id, msg))
162

  
163 148
    def __str__(self):
164 149
        return "VncAuthProxy: %d -> %s:%d" % (self.sport, self.daddr,
165 150
                                              self.dport)
......
207 192
        client_version_str = self.client.recv(1024)
208 193
        client_version = rfb.check_version(client_version_str)
209 194
        if not client_version:
210
            self.error("Invalid version: %s" % client_version_str)
195
            self.error("Invalid version: %s", client_version_str)
211 196
            raise gevent.GreenletExit
212 197

  
213 198
        # Both for RFB 3.3 and 3.8
......
221 206
            res = self.client.recv(1024)
222 207
            type = rfb.parse_client_authtype(res)
223 208
            if type == rfb.RFB_AUTHTYPE_ERROR:
224
                self.warn("Client refused authentication: %s" % res[1:])
209
                self.warn("Client refused authentication: %s", res[1:])
225 210
            else:
226
                self.debug("Client requested authtype %x" % type)
211
                self.debug("Client requested authtype %x", type)
227 212

  
228 213
            if type != rfb.RFB_AUTHTYPE_VNC:
229
                self.error("Wrong auth type: %d" % type)
214
                self.error("Wrong auth type: %d", type)
230 215
                self.client.send(rfb.to_u32(rfb.RFB_AUTH_ERROR))
231 216
                raise gevent.GreenletExit
232 217

  
......
235 220
        self.client.send(challenge)
236 221
        response = self.client.recv(1024)
237 222
        if len(response) != 16:
238
            self.error(("Wrong response length %d, should be 16" %
239
                        len(response)))
223
            self.error("Wrong response length %d, should be 16", len(response))
240 224
            raise gevent.GreenletExit
241 225

  
242 226
        if rfb.check_password(challenge, response, self.password):
243
            self.debug("Authentication successful!")
227
            self.debug("Authentication successful")
244 228
        else:
245 229
            self.warn("Authentication failed")
246 230
            self.client.send(rfb.to_u32(rfb.RFB_AUTH_ERROR))
......
251 235

  
252 236
    def _run(self):
253 237
        try:
254
            self.info(("Waiting for a client to connect at %s" %
255
                       ", ".join(["%s:%d" % s.getsockname()[:2]
256
                                  for s in self.listeners])))
238
            self.info("Waiting for a client to connect at %s",
239
                      ", ".join(["%s:%d" % s.getsockname()[:2]
240
                                 for s in self.listeners]))
257 241
            rlist, _, _ = select(self.listeners, [], [], timeout=self.timeout)
258 242

  
259 243
            if not rlist:
260
                self.info(("Timed out, no connection after %d sec" %
261
                           self.timeout))
244
                self.info("Timed out, no connection after %d sec",
245
                          self.timeout)
262 246
                raise gevent.GreenletExit
263 247

  
264 248
            for sock in rlist:
265 249
                self.client, addrinfo = sock.accept()
266
                self.info("Connection from %s:%d" % addrinfo[:2])
250
                self.info("Connection from %s:%d", addrinfo[:2])
267 251

  
268 252
                # Close all listening sockets, we only want a one-shot
269 253
                # connection from a single client.
......
290 274
            # Any unhandled exception in the previous block
291 275
            # is an error and must be logged accordingly
292 276
            if not isinstance(e, gevent.GreenletExit):
293
                self.log.exception(e)
277
                self.exception(e)
294 278
            raise e
295 279
        finally:
296 280
            self._cleanup()
297 281

  
282
# Logging support inside VncAuthproxy
283
# Wrap all common logging functions in logging-specific methods
284
for funcname in ["info", "debug", "warn", "error", "critical",
285
                 "exception"]:
286
    def gen(funcname):
287
        def wrapped_log_func(self, *args, **kwargs):
288
            func = getattr(self.log, funcname)
289
            func("[C%d] %s" % (self.id, args[0]), *args[1:], **kwargs)
290
        return wrapped_log_func
291
    setattr(VncAuthProxy, funcname, gen(funcname))
292

  
298 293

  
299 294
def fatal_signal_handler(signame):
300
    logger.info("Caught %s, will raise SystemExit" % signame)
295
    logger.info("Caught %s, will raise SystemExit", signame)
301 296
    raise SystemExit
302 297

  
303 298

  
......
320 315
            s.bind(sa)
321 316
            s.listen(1)
322 317
            sockets.append(s)
323
            logger.debug("Listening on %s:%d" % sa[:2])
318
            logger.debug("Listening on %s:%d", sa[:2])
324 319
        except socket.error, msg:
325
            logger.error(("Error binding to %s:%d: %s" %
326
                          (sa[0], sa[1], msg[1])))
320
            logger.error("Error binding to %s:%d: %s", sa[0], sa[1], msg[1])
327 321
            if s:
328 322
                s.close()
329 323
            while sockets:
......
360 354
                continue
361 355

  
362 356
            try:
363
                logger.debug("Connecting to %s:%s" % sa[:2])
357
                logger.debug("Connecting to %s:%s", sa[:2])
364 358
                server.connect(sa)
365
                logger.debug("Connection to %s:%s successful" % sa[:2])
359
                logger.debug("Connection to %s:%s successful", sa[:2])
366 360
            except socket.error:
367 361
                server.close()
368 362
                server = None
......
390 384
        raise Exception("Error handshaking with the server")
391 385

  
392 386
    else:
393
        logger.debug(("Supported authentication types: %s" %
394
                      " ".join([str(x) for x in types])))
387
        logger.debug("Supported authentication types: %s",
388
                     " ".join([str(x) for x in types]))
395 389

  
396 390
    if rfb.RFB_AUTHTYPE_NONE not in types:
397 391
        raise Exception("Error, server demands authentication")

Also available in: Unified diff