[FIX] locale problems (again)
bzr revid: christophe@tinyerp.com-20090205131812-btihwg9g1qweyln3
This commit is contained in:
parent
56a68ebf4a
commit
47a658c475
|
@ -88,29 +88,31 @@ LOG_CRITICAL = 'critical'
|
||||||
logging.DEBUG_RPC = logging.DEBUG - 1
|
logging.DEBUG_RPC = logging.DEBUG - 1
|
||||||
|
|
||||||
class UnicodeFormatter(logging.Formatter):
|
class UnicodeFormatter(logging.Formatter):
|
||||||
def __init__(self, fmt=None, datefmt=None):
|
def __init__(self, enc, fmt=None, datefmt=None):
|
||||||
|
self._enc = enc
|
||||||
logging.Formatter.__init__(self, fmt, datefmt)
|
logging.Formatter.__init__(self, fmt, datefmt)
|
||||||
self._fmt = tools.ustr(self._fmt)
|
self._fmt = tools.ustr(self._fmt)
|
||||||
if self.datefmt is not None:
|
if self.datefmt is not None:
|
||||||
self.datefmt = tools.ustr(self.datefmt)
|
self.datefmt = tools.ustr(self.datefmt)
|
||||||
|
|
||||||
def formatTime(self, *args, **kwargs):
|
def formatTime(self, *args, **kwargs):
|
||||||
return tools.ustr(logging.Formatter.formatTime(self, *args, **kwargs))
|
return tools.ustr(logging.Formatter.formatTime(self, *args, **kwargs), self._enc)
|
||||||
|
|
||||||
def formatException(self, *args, **kwargs):
|
def formatException(self, *args, **kwargs):
|
||||||
return tools.ustr(logging.Formatter.formatException(self, *args, **kwargs))
|
return tools.ustr(logging.Formatter.formatException(self, *args, **kwargs), self._enc)
|
||||||
|
|
||||||
def format(self, *args, **kwargs):
|
def format(self, *args, **kwargs):
|
||||||
return tools.ustr(logging.Formatter.format(self, *args, **kwargs))
|
return tools.ustr(logging.Formatter.format(self, *args, **kwargs), self._enc)
|
||||||
|
|
||||||
def init_logger():
|
def init_logger():
|
||||||
import os
|
import os
|
||||||
from tools.translate import resetlocale
|
from tools.translate import resetlocale
|
||||||
resetlocale()
|
l = resetlocale() or ''
|
||||||
|
enc = '.' in l and l.split('.')[1] or 'utf-8'
|
||||||
|
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
# create a format for log messages and dates
|
# create a format for log messages and dates
|
||||||
formatter = UnicodeFormatter('[%(asctime)s] %(levelname)s:%(name)s:%(message)s', '%a %b %d %Y %H:%M:%S')
|
formatter = UnicodeFormatter(enc, '[%(asctime)s] %(levelname)s:%(name)s:%(message)s', '%a %b %d %Y %H:%M:%S')
|
||||||
|
|
||||||
logging_to_stdout = False
|
logging_to_stdout = False
|
||||||
if tools.config['syslog']:
|
if tools.config['syslog']:
|
||||||
|
@ -121,7 +123,7 @@ def init_logger():
|
||||||
release.version))
|
release.version))
|
||||||
else:
|
else:
|
||||||
handler = logging.handlers.SysLogHandler('/dev/log')
|
handler = logging.handlers.SysLogHandler('/dev/log')
|
||||||
formatter = UnicodeFormatter("%s %s" % (release.description, release.version) + ':%(levelname)s:%(name)s:%(message)s')
|
formatter = UnicodeFormatter(enc, "%s %s" % (release.description, release.version) + ':%(levelname)s:%(name)s:%(message)s')
|
||||||
|
|
||||||
elif tools.config['logfile']:
|
elif tools.config['logfile']:
|
||||||
# LogFile Handler
|
# LogFile Handler
|
||||||
|
|
|
@ -658,7 +658,7 @@ class cache(object):
|
||||||
def to_xml(s):
|
def to_xml(s):
|
||||||
return s.replace('&','&').replace('<','<').replace('>','>')
|
return s.replace('&','&').replace('<','<').replace('>','>')
|
||||||
|
|
||||||
def ustr(value):
|
def ustr(value, fromenc='utf-8'):
|
||||||
"""This method is similar to the builtin `str` method, except
|
"""This method is similar to the builtin `str` method, except
|
||||||
it will return Unicode string.
|
it will return Unicode string.
|
||||||
|
|
||||||
|
@ -677,7 +677,7 @@ def ustr(value):
|
||||||
if not isinstance(value, str):
|
if not isinstance(value, str):
|
||||||
value = str(value)
|
value = str(value)
|
||||||
|
|
||||||
return unicode(value, 'utf-8')
|
return unicode(value, fromenc)
|
||||||
|
|
||||||
def exception_to_unicode(e):
|
def exception_to_unicode(e):
|
||||||
if hasattr(e, 'message'):
|
if hasattr(e, 'message'):
|
||||||
|
|
|
@ -683,8 +683,7 @@ def resetlocale():
|
||||||
# locale.resetlocale is bugged with some locales.
|
# locale.resetlocale is bugged with some locales.
|
||||||
for ln in get_locales():
|
for ln in get_locales():
|
||||||
try:
|
try:
|
||||||
locale.setlocale(locale.LC_ALL, ln)
|
return locale.setlocale(locale.LC_ALL, ln)
|
||||||
break
|
|
||||||
except locale.Error:
|
except locale.Error:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue