Achuith Bhandarkar | 66ce09d | 2019-10-01 16:25:43 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Logging via CherryPy.""" |
| 7 | |
Achuith Bhandarkar | 66ce09d | 2019-10-01 16:25:43 +0200 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 10 | import re |
| 11 | |
Achuith Bhandarkar | 66ce09d | 2019-10-01 16:25:43 +0200 | [diff] [blame] | 12 | import cherrypy # pylint: disable=import-error |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 13 | |
| 14 | |
| 15 | class Loggable(object): |
| 16 | """Provides a log method, with automatic log tag generation.""" |
| 17 | _CAMELCASE_RE = re.compile('(?<=.)([A-Z])') |
| 18 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 19 | def _Log(self, message, *args): |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 20 | return LogWithTag( |
| 21 | self._CAMELCASE_RE.sub(r'_\1', self.__class__.__name__).upper(), |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 22 | message, *args) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 23 | |
| 24 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 25 | def LogWithTag(tag, message, *args): |
| 26 | # CherryPy log doesn't seem to take any optional args, so we just handle |
| 27 | # args by formatting them into message. |
| 28 | return cherrypy.log(message % args, context=tag) |