Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 1 | # 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 | |
| 7 | import re |
| 8 | |
| 9 | import cherrypy |
| 10 | |
| 11 | |
| 12 | class Loggable(object): |
| 13 | """Provides a log method, with automatic log tag generation.""" |
| 14 | _CAMELCASE_RE = re.compile('(?<=.)([A-Z])') |
| 15 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 16 | def _Log(self, message, *args): |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 17 | return LogWithTag( |
| 18 | self._CAMELCASE_RE.sub(r'_\1', self.__class__.__name__).upper(), |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 19 | message, *args) |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 20 | |
| 21 | |
Chris Sosa | 6a3697f | 2013-01-30 00:44:43 | [diff] [blame] | 22 | def 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) |