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