Milan Zamazal has uploaded a new change for review.

Change subject: logging: Don't crash on non-ASCII in SimpleLogAdapter
......................................................................

logging: Don't crash on non-ASCII in SimpleLogAdapter

The values and messages passed to SimpleLogAdapter can be mixtures of
strings and unicodes containing various non-ASCII characters.  If they
are not properly handled, concatenation of incompatible strings raises
UnicodeDecodeError.

We avoid that problem in SimpleLogAdapter by converting all the
processed texts to unicodes before joining them.  We assume non-ASCII
strings are in UTF-8 but if they are not then we shouldn't crash at
least.

Change-Id: I1930817ade1799b218913348ffa099aab2211ef7
Bug-Url: https://bugzilla.redhat.com/1276906
Bug-Url: https://bugzilla.redhat.com/1260131
Signed-off-by: Milan Zamazal <mzama...@redhat.com>
---
M tests/utilsTests.py
M vdsm/logUtils.py
2 files changed, 28 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/42/48542/1

diff --git a/tests/utilsTests.py b/tests/utilsTests.py
index 68547ce..f4f1a58 100644
--- a/tests/utilsTests.py
+++ b/tests/utilsTests.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
 #
 # Copyright 2012-2013 Red Hat, Inc.
 #
@@ -41,6 +42,7 @@
 from vdsm import taskset
 from vdsm import utils
 from vdsm import cmdutils
+import logUtils
 
 from monkeypatch import MonkeyPatch
 from vmTestsData import VM_STATUS_DUMP
@@ -1070,3 +1072,22 @@
         proc.start()
         self._noIntrWatchFd(myPipe, isEpoll=False, mask=select.POLLIN)
         proc.join()
+
+
+class LoggingTests(TestCaseBase):
+
+    def setUp(self):
+        self._log = FakeLogger(logging.DEBUG)
+
+    def test_nonascii(self):
+        # Just test it doesn't crash with mixture of string and unicodes in
+        # various codings.
+        invalid = chr(254) + chr(0)
+        u_invalid = unichr(253) + unichr(254)
+        extra = dict(e1=u'Hello', e2='Здра́вствуйте', e3=u'שלום', e4='你好',
+                     e5=invalid, e6=u_invalid)
+        log = logUtils.SimpleLogAdapter(self._log, extra)
+        log.debug("Dobrý večer")
+        log.debug(u"Dobrý večer")
+        log.debug(invalid)
+        log.debug(u_invalid)
diff --git a/vdsm/logUtils.py b/vdsm/logUtils.py
index 5caa167..49bb2cf 100644
--- a/vdsm/logUtils.py
+++ b/vdsm/logUtils.py
@@ -90,10 +90,14 @@
     warn = logging.LoggerAdapter.warning
 
     def process(self, msg, kwargs):
-        result = ''
+        result = u''
         for key, value in self.extra.iteritems():
-            result += '%s=`%s`' % (key, value)
-        result += '::%s' % msg
+            if isinstance(value, str):
+                value = value.decode('utf-8', 'replace')
+            result += u'%s=`%s`' % (key, value)
+        if isinstance(msg, str):
+            msg = msg.decode('utf-8', 'replace')
+        result += u'::%s' % msg
         return (result, kwargs)
 
 


-- 
To view, visit https://gerrit.ovirt.org/48542
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1930817ade1799b218913348ffa099aab2211ef7
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Milan Zamazal <mzama...@redhat.com>
_______________________________________________
vdsm-patches mailing list
vdsm-patches@lists.fedorahosted.org
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to