blob: 241fcfad577c0d9d507e96f81850aa97a33eaed5 [file] [log] [blame]
Gilad Arnoldc65330c2012-09-20 15:17:48 -07001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Logging via CherryPy."""
6
7import re
8
9import cherrypy
10
11
12class Loggable(object):
13 """Provides a log method, with automatic log tag generation."""
14 _CAMELCASE_RE = re.compile('(?<=.)([A-Z])')
15
Chris Sosa6a3697f2013-01-29 16:44:43 -080016 def _Log(self, message, *args):
Gilad Arnoldc65330c2012-09-20 15:17:48 -070017 return LogWithTag(
18 self._CAMELCASE_RE.sub(r'_\1', self.__class__.__name__).upper(),
Chris Sosa6a3697f2013-01-29 16:44:43 -080019 message, *args)
Gilad Arnoldc65330c2012-09-20 15:17:48 -070020
21
Chris Sosa6a3697f2013-01-29 16:44:43 -080022def LogWithTag(tag, message, *args):
23 # CherryPy log doesn't seem to take any optional args, so we just handle
24 # args by formatting them into message.
25 return cherrypy.log(message % args, context=tag)