55
55
except ImportError:
58
import openerp.loglevels as loglevels
59
59
from config import config
60
60
from lru import LRU
62
# get_encodings, ustr and exception_to_unicode were originally from tools.misc.
63
# There are moved to loglevels until we refactor tools.
64
from openerp.loglevels import get_encodings, ustr, exception_to_unicode
62
66
_logger = logging.getLogger('tools')
64
68
# List of etree._Element subclasses that we choose to ignore when parsing XML.
435
439
class WriteToLogger(object):
436
440
def __init__(self):
437
self.logger = netsvc.Logger()
441
self.logger = loglevels.Logger()
439
443
def write(self, s):
440
self.logger.notifyChannel('email_send', netsvc.LOG_DEBUG, s)
444
self.logger.notifyChannel('email_send', loglevels.LOG_DEBUG, s)
442
446
if openobject_id:
443
447
message['Message-Id'] = generate_tracking_message_id(openobject_id)
842
846
return s.replace('&','&').replace('<','<').replace('>','>')
844
def get_encodings(hint_encoding='utf-8'):
847
'iso-8859-1': 'iso8859-15',
852
if hint_encoding.lower() in fallbacks:
853
yield fallbacks[hint_encoding.lower()]
855
# some defaults (also taking care of pure ASCII)
856
for charset in ['utf8','latin1']:
857
if not (hint_encoding) or (charset.lower() != hint_encoding.lower()):
860
from locale import getpreferredencoding
861
prefenc = getpreferredencoding()
862
if prefenc and prefenc.lower() != 'utf-8':
864
prefenc = fallbacks.get(prefenc.lower())
869
def ustr(value, hint_encoding='utf-8'):
870
"""This method is similar to the builtin `str` method, except
871
it will return unicode() string.
873
@param value: the value to convert
874
@param hint_encoding: an optional encoding that was detected
875
upstream and should be tried first to
879
@return: unicode string
881
if isinstance(value, Exception):
882
return exception_to_unicode(value)
884
if isinstance(value, unicode):
887
if not isinstance(value, basestring):
889
return unicode(value)
891
raise UnicodeError('unable to convert %r' % (value,))
893
for ln in get_encodings(hint_encoding):
895
return unicode(value, ln)
898
raise UnicodeError('unable to convert %r' % (value,))
901
def exception_to_unicode(e):
902
if (sys.version_info[:2] < (2,6)) and hasattr(e, 'message'):
903
return ustr(e.message)
904
if hasattr(e, 'args'):
905
return "\n".join((ustr(a) for a in e.args))
909
return u"Unknown message"
912
848
# to be compatible with python 2.4
913
849
import __builtin__
914
850
if not hasattr(__builtin__, 'all'):
1095
1031
vector.append(' result: %s' % pformat(res))
1096
1032
vector.append(' time delta: %s' % (time.time() - timeb4))
1097
netsvc.Logger().notifyChannel('logged', netsvc.LOG_DEBUG, '\n'.join(vector))
1033
loglevels.Logger().notifyChannel('logged', loglevels.LOG_DEBUG, '\n'.join(vector))
1302
1238
except Exception:
1303
netsvc.Logger().notifyChannel("detect_server_timezone", netsvc.LOG_WARNING,
1239
loglevels.Logger().notifyChannel("detect_server_timezone", loglevels.LOG_WARNING,
1304
1240
"Python pytz module is not available. Timezone will be set to UTC by default.")
1336
1272
tz = pytz.timezone(value)
1337
netsvc.Logger().notifyChannel("detect_server_timezone", netsvc.LOG_INFO,
1273
loglevels.Logger().notifyChannel("detect_server_timezone", loglevels.LOG_INFO,
1338
1274
"Using timezone %s obtained from %s." % (tz.zone,source))
1340
1276
except pytz.UnknownTimeZoneError:
1341
netsvc.Logger().notifyChannel("detect_server_timezone", netsvc.LOG_WARNING,
1277
loglevels.Logger().notifyChannel("detect_server_timezone", loglevels.LOG_WARNING,
1342
1278
"The timezone specified in %s (%s) is invalid, ignoring it." % (source,value))
1344
netsvc.Logger().notifyChannel("detect_server_timezone", netsvc.LOG_WARNING,
1280
loglevels.Logger().notifyChannel("detect_server_timezone", loglevels.LOG_WARNING,
1345
1281
"No valid timezone could be detected, using default UTC timezone. You can specify it explicitly with option 'timezone' in the server configuration.")