Merge lp:~javier.collado/utah/battery-measurements-logging into lp:utah

Proposed by Javier Collado
Status: Merged
Approved by: Javier Collado
Approved revision: 832
Merged at revision: 832
Proposed branch: lp:~javier.collado/utah/battery-measurements-logging
Merge into: lp:utah
Diff against target: 22 lines (+5/-1)
1 file modified
utah/client/battery.py (+5/-1)
To merge this branch: bzr merge lp:~javier.collado/utah/battery-measurements-logging
Reviewer Review Type Date Requested Status
UTAH Dev Pending
Review via email: mp+153123@code.launchpad.net

Description of the change

As explained in the commit message:

-----
The battery module creates a singleton object at import time to take care of
gathering the data related to all battery measurements. Unfortunately, if a
logging method is used, logging.basicConfig is called with default arguments.
Later, UTAHs configure logging function believes logging is already configured
and as a results logging ends up being wrongly configured.

To fix this, this change delays the computation done at import time and does
them the first time the object is used which is later than logging has been
configured.
----

Note that this merge request is needed by the changes in:
https://code.launchpad.net/~javier.collado/utah/reboot-support/+merge/151256

for them to work properly with regard to logging.

To post a comment you must log in.
Revision history for this message
Andy Doan (doanac) wrote :

Here's my concern (and I'm not sure if its worth considering):

If someone ever did something innocent like add a "logging.info" to _Battery.__init__, then this bug will come back. It seems like the real bug is at the end of the file:

  battery = _Battery()

I wonder if we should change that to be something like:

_inst = None
def get_battery():
   if not inst:
      _inst = _Battery()
    return _inst

then you'd have to refactor who uses this, so maybe its overkill

Revision history for this message
Javier Collado (javier.collado) wrote :

@Andy

Yes, I though about having a class method to cache the instance instead of
caching the file names. However, from the client code perspective, I though
this looked cleaner:
battery.get_data()

than this:
_Battery.get_battery().get_data()

The risk of potentially writing a logging message before it's been configured
it's all over the code, so probably the best way to address is to do something
like detect when current logging configuration isn't the intended one and fail
to force the developer to fix problems like the one in this merge proposal.

Anyway, this is out of the scope of the merge proposal, but we can open a bug
if you wish and work on a bettter solution.

Revision history for this message
Andy Doan (doanac) wrote :

On 03/14/2013 07:21 AM, Javier Collado wrote:
> @Andy
> Anyway, this is out of the scope of the merge proposal, but we can open a bug
> if you wish and work on a bettter solution.

no bug needed. just wanted to get your thoughts.
+1

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'utah/client/battery.py'
2--- utah/client/battery.py 2013-02-08 14:19:29 +0000
3+++ utah/client/battery.py 2013-03-13 12:19:21 +0000
4@@ -11,7 +11,7 @@
5 BATTERY_FILE_REGEX = re.compile(r'(.*_now|capacity|status)')
6
7 def __init__(self):
8- self.filenames = self._get_files()
9+ self.filenames = None
10
11 def get_data(self):
12 """Get data from the battery information files
13@@ -38,6 +38,10 @@
14 except ValueError:
15 return data_str
16
17+ # Cache file names for later use
18+ if self.filenames is None:
19+ self.filenames = self._get_files()
20+
21 return {os.path.basename(filename): read_file(filename)
22 for filename in self.filenames}
23

Subscribers

People subscribed via source and target branches