blob: c28c6b96d6c9f46d816db6947ce7a03eb911368f [file] [log] [blame]
Achuith Bhandarkar66ce09d2019-10-01 16:25:43 +02001# -*- coding: utf-8 -*-
Gilad Arnoldc65330c2012-09-20 15:17:48 -07002# 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 Bhandarkar66ce09d2019-10-01 16:25:43 +02008from __future__ import print_function
9
Gilad Arnoldc65330c2012-09-20 15:17:48 -070010import re
11
Achuith Bhandarkar66ce09d2019-10-01 16:25:43 +020012import cherrypy # pylint: disable=import-error
Gilad Arnoldc65330c2012-09-20 15:17:48 -070013
14
15class Loggable(object):
16 """Provides a log method, with automatic log tag generation."""
17 _CAMELCASE_RE = re.compile('(?<=.)([A-Z])')
18
Chris Sosa6a3697f2013-01-29 16:44:43 -080019 def _Log(self, message, *args):
Gilad Arnoldc65330c2012-09-20 15:17:48 -070020 return LogWithTag(
21 self._CAMELCASE_RE.sub(r'_\1', self.__class__.__name__).upper(),
Chris Sosa6a3697f2013-01-29 16:44:43 -080022 message, *args)
Gilad Arnoldc65330c2012-09-20 15:17:48 -070023
24
Chris Sosa6a3697f2013-01-29 16:44:43 -080025def 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)