Merge lp:~harlowja/cloud-init/patch-handler into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Joshua Harlow
Status: Merged
Merged at revision: 659
Proposed branch: lp:~harlowja/cloud-init/patch-handler
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 74 lines (+59/-0)
2 files modified
bin/cloud-init (+3/-0)
cloudinit/patcher.py (+56/-0)
To merge this branch: bzr merge lp:~harlowja/cloud-init/patch-handler
Reviewer Review Type Date Requested Status
cloud-init Commiters Pending
Review via email: mp+125327@code.launchpad.net

Description of the change

WIP!

To post a comment you must log in.
655. By Joshua Harlow

Get the fallback working.

656. By Joshua Harlow

First thing that we do after we can start importing
is to patch the functionality before it gets reimported.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'bin/cloud-init'
--- bin/cloud-init 2012-09-07 01:12:21 +0000
+++ bin/cloud-init 2012-09-19 23:12:20 +0000
@@ -33,6 +33,9 @@
33if os.path.exists(os.path.join(possible_topdir, "cloudinit", "__init__.py")):33if os.path.exists(os.path.join(possible_topdir, "cloudinit", "__init__.py")):
34 sys.path.insert(0, possible_topdir)34 sys.path.insert(0, possible_topdir)
3535
36from cloudinit import patcher
37patcher.patch()
38
36from cloudinit import log as logging39from cloudinit import log as logging
37from cloudinit import netinfo40from cloudinit import netinfo
38from cloudinit import signal_handler41from cloudinit import signal_handler
3942
=== added file 'cloudinit/patcher.py'
--- cloudinit/patcher.py 1970-01-01 00:00:00 +0000
+++ cloudinit/patcher.py 2012-09-19 23:12:20 +0000
@@ -0,0 +1,56 @@
1# vi: ts=4 expandtab
2#
3# Copyright (C) 2012 Canonical Ltd.
4# Copyright (C) 2012 Yahoo! Inc.
5#
6# Author: Scott Moser <scott.moser@canonical.com>
7# Author: Joshua Harlow <harlowja@yahoo-inc.com>
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License version 3, as
11# published by the Free Software Foundation.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21import imp
22import logging
23import sys
24
25# Default fallback format
26FALL_FORMAT = 'FALLBACK: %(asctime)s - %(filename)s[%(levelname)s]: %(message)s'
27
28
29class QuietStreamHandler(logging.StreamHandler):
30 def handleError(self, record):
31 pass
32
33
34def _patch_logging():
35 # Replace 'handleError' with one that will be more
36 # tolerant of errors in that it can avoid
37 # re-notifying on exceptions and when errors
38 # do occur, it can at least try to write to
39 # sys.stderr using a fallback logger
40 fallback_handler = QuietStreamHandler(sys.stderr)
41 fallback_handler.setFormatter(logging.Formatter(FALL_FORMAT))
42 def handleError(self, record):
43 try:
44 fallback_handler.handle(record)
45 fallback_handler.flush()
46 except IOError:
47 pass
48 setattr(logging.Handler, 'handleError', handleError)
49
50
51def patch():
52 imp.acquire_lock()
53 try:
54 _patch_logging()
55 finally:
56 imp.release_lock()