Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [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 | |
| 16 | def _Log(self, message, *args, **kwargs): |
| 17 | return LogWithTag( |
| 18 | self._CAMELCASE_RE.sub(r'_\1', self.__class__.__name__).upper(), |
| 19 | message, *args, **kwargs) |
| 20 | |
| 21 | |
| 22 | def LogWithTag(tag, message, *args, **kwargs): |
| 23 | # CherryPy log doesn't seem to take any optional args, so we'll just join |
| 24 | # them into a single string, if any are provided. |
| 25 | return cherrypy.log(message + ((' ' + ' '.join(args)) if args else ''), tag) |