2009-12-02 10:06:58 +00:00
|
|
|
#!/usr/bin/env python
|
2009-10-20 10:52:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-06-16 07:24:04 +00:00
|
|
|
##############################################################################
|
|
|
|
#
|
2008-12-14 16:46:47 +00:00
|
|
|
# OpenERP, Open Source Management Solution
|
2012-08-22 10:11:17 +00:00
|
|
|
# Copyright (C) 2004-2012 OpenERP SA (<http://www.openerp.com>)
|
2008-06-16 07:24:04 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2011-06-29 09:51:38 +00:00
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
2008-06-16 07:24:04 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2011-06-29 09:51:38 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2008-06-16 07:24:04 +00:00
|
|
|
#
|
2011-06-29 09:51:38 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2008-11-03 18:27:16 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-06-16 07:24:04 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
##############################################################################
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2010-05-18 15:41:23 +00:00
|
|
|
import errno
|
2006-12-07 13:41:40 +00:00
|
|
|
import logging
|
2008-11-25 10:35:00 +00:00
|
|
|
import logging.handlers
|
2010-04-21 10:12:00 +00:00
|
|
|
import os
|
2011-06-16 10:26:24 +00:00
|
|
|
import platform
|
|
|
|
import release
|
2010-05-18 15:41:23 +00:00
|
|
|
import socket
|
2008-12-18 23:54:33 +00:00
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
import time
|
2011-06-17 16:56:45 +00:00
|
|
|
import types
|
2011-06-16 10:26:24 +00:00
|
|
|
from pprint import pformat
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2011-02-07 12:57:23 +00:00
|
|
|
# TODO modules that import netsvc only for things from loglevels must be changed to use loglevels.
|
|
|
|
from loglevels import *
|
|
|
|
import tools
|
2011-07-14 11:08:09 +00:00
|
|
|
import openerp
|
2011-02-07 12:57:23 +00:00
|
|
|
|
2012-01-24 16:22:45 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2011-06-16 10:26:24 +00:00
|
|
|
|
|
|
|
|
2011-06-23 09:03:57 +00:00
|
|
|
#.apidoc title: Common Services: netsvc
|
|
|
|
#.apidoc module-mods: member-order: bysource
|
|
|
|
|
2011-09-26 15:14:03 +00:00
|
|
|
def abort_response(dummy_1, description, dummy_2, details):
|
|
|
|
# TODO Replace except_{osv,orm} with these directly.
|
2011-10-01 01:32:54 +00:00
|
|
|
raise openerp.osv.osv.except_osv(description, details)
|
2011-07-29 08:38:24 +00:00
|
|
|
|
2006-12-07 13:41:40 +00:00
|
|
|
class Service(object):
|
2012-12-09 18:46:32 +00:00
|
|
|
""" Base class for Local services
|
|
|
|
Functionality here is trusted, no authentication.
|
|
|
|
Workflow engine and reports subclass this.
|
2009-08-29 15:23:46 +00:00
|
|
|
"""
|
|
|
|
_services = {}
|
2011-07-28 14:19:10 +00:00
|
|
|
def __init__(self, name):
|
2009-08-29 15:23:46 +00:00
|
|
|
Service._services[name] = self
|
2008-08-12 14:44:56 +00:00
|
|
|
self.__name = name
|
|
|
|
|
2010-01-29 10:17:12 +00:00
|
|
|
@classmethod
|
|
|
|
def exists(cls, name):
|
|
|
|
return name in cls._services
|
2008-07-22 14:24:36 +00:00
|
|
|
|
2010-01-29 10:28:29 +00:00
|
|
|
@classmethod
|
|
|
|
def remove(cls, name):
|
2010-01-29 12:22:20 +00:00
|
|
|
if cls.exists(name):
|
2010-01-29 10:28:29 +00:00
|
|
|
cls._services.pop(name)
|
|
|
|
|
2011-07-28 12:28:56 +00:00
|
|
|
def LocalService(name):
|
2012-01-19 19:17:56 +00:00
|
|
|
# Special case for addons support, will be removed in a few days when addons
|
|
|
|
# are updated to directly use openerp.osv.osv.service.
|
|
|
|
if name == 'object_proxy':
|
|
|
|
return openerp.osv.osv.service
|
2011-07-29 08:38:24 +00:00
|
|
|
|
2012-01-19 19:17:56 +00:00
|
|
|
return Service._services[name]
|
2008-11-27 08:27:20 +00:00
|
|
|
|
2009-08-29 15:23:46 +00:00
|
|
|
class ExportService(object):
|
2011-07-29 11:20:19 +00:00
|
|
|
""" Proxy for exported services.
|
2009-08-29 15:23:46 +00:00
|
|
|
|
2011-07-29 11:20:19 +00:00
|
|
|
Note that this class has no direct proxy, capable of calling
|
|
|
|
eservice.method(). Rather, the proxy should call
|
2011-09-25 15:23:18 +00:00
|
|
|
dispatch(method, params)
|
2009-08-29 15:23:46 +00:00
|
|
|
"""
|
2011-07-29 11:20:19 +00:00
|
|
|
|
2009-08-29 15:23:46 +00:00
|
|
|
_services = {}
|
|
|
|
|
2011-07-28 14:19:10 +00:00
|
|
|
def __init__(self, name):
|
2009-08-29 15:23:46 +00:00
|
|
|
ExportService._services[name] = self
|
|
|
|
self.__name = name
|
2012-01-24 16:22:45 +00:00
|
|
|
_logger.debug("Registered an exported service: %s" % name)
|
2009-08-29 15:23:46 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def getService(cls,name):
|
|
|
|
return cls._services[name]
|
|
|
|
|
2011-07-29 14:56:09 +00:00
|
|
|
# Dispatch a RPC call w.r.t. the method name. The dispatching
|
|
|
|
# w.r.t. the service (this class) is done by OpenERPDispatcher.
|
2011-09-25 15:23:18 +00:00
|
|
|
def dispatch(self, method, params):
|
2009-10-03 09:00:03 +00:00
|
|
|
raise Exception("stub dispatch at %s" % self.__name)
|
2007-06-07 12:00:30 +00:00
|
|
|
|
2010-04-21 10:12:00 +00:00
|
|
|
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, _NOTHING, DEFAULT = range(10)
|
|
|
|
#The background is set with 40 plus the number of the color, and the foreground with 30
|
|
|
|
#These are the sequences need to get colored ouput
|
|
|
|
RESET_SEQ = "\033[0m"
|
|
|
|
COLOR_SEQ = "\033[1;%dm"
|
|
|
|
BOLD_SEQ = "\033[1m"
|
|
|
|
COLOR_PATTERN = "%s%s%%s%s" % (COLOR_SEQ, COLOR_SEQ, RESET_SEQ)
|
|
|
|
LEVEL_COLOR_MAPPING = {
|
|
|
|
logging.DEBUG: (BLUE, DEFAULT),
|
|
|
|
logging.INFO: (GREEN, DEFAULT),
|
|
|
|
logging.TEST: (WHITE, BLUE),
|
|
|
|
logging.WARNING: (YELLOW, DEFAULT),
|
|
|
|
logging.ERROR: (RED, DEFAULT),
|
|
|
|
logging.CRITICAL: (WHITE, RED),
|
|
|
|
}
|
|
|
|
|
2010-09-03 07:41:32 +00:00
|
|
|
class DBFormatter(logging.Formatter):
|
2010-04-21 10:12:00 +00:00
|
|
|
def format(self, record):
|
2012-01-26 11:01:35 +00:00
|
|
|
record.pid = os.getpid()
|
2010-09-06 14:06:45 +00:00
|
|
|
record.dbname = getattr(threading.currentThread(), 'dbname', '?')
|
2010-04-21 10:12:00 +00:00
|
|
|
return logging.Formatter.format(self, record)
|
|
|
|
|
2010-09-03 07:41:32 +00:00
|
|
|
class ColoredFormatter(DBFormatter):
|
|
|
|
def format(self, record):
|
|
|
|
fg_color, bg_color = LEVEL_COLOR_MAPPING[record.levelno]
|
|
|
|
record.levelname = COLOR_PATTERN % (30 + fg_color, 40 + bg_color, record.levelname)
|
|
|
|
return DBFormatter.format(self, record)
|
2010-04-21 10:12:00 +00:00
|
|
|
|
2006-12-07 13:41:40 +00:00
|
|
|
def init_logger():
|
2009-02-04 17:27:00 +00:00
|
|
|
from tools.translate import resetlocale
|
2009-02-05 13:40:49 +00:00
|
|
|
resetlocale()
|
2008-07-22 14:24:36 +00:00
|
|
|
|
2008-12-29 10:24:51 +00:00
|
|
|
# create a format for log messages and dates
|
2012-02-01 00:10:19 +00:00
|
|
|
format = '%(asctime)s %(pid)s %(levelname)s %(dbname)s %(name)s: %(message)s'
|
2009-07-07 10:02:07 +00:00
|
|
|
|
2008-12-26 15:05:51 +00:00
|
|
|
if tools.config['syslog']:
|
2008-12-22 23:04:18 +00:00
|
|
|
# SysLog Handler
|
|
|
|
if os.name == 'nt':
|
2010-03-11 05:51:33 +00:00
|
|
|
handler = logging.handlers.NTEventLogHandler("%s %s" % (release.description, release.version))
|
2008-12-22 23:04:18 +00:00
|
|
|
else:
|
2008-12-29 10:24:51 +00:00
|
|
|
handler = logging.handlers.SysLogHandler('/dev/log')
|
2010-04-21 11:11:18 +00:00
|
|
|
format = '%s %s' % (release.description, release.version) \
|
2010-09-03 07:41:32 +00:00
|
|
|
+ ':%(dbname)s:%(levelname)s:%(name)s:%(message)s'
|
2008-12-22 23:04:18 +00:00
|
|
|
|
2008-12-29 10:24:51 +00:00
|
|
|
elif tools.config['logfile']:
|
2008-12-22 23:04:18 +00:00
|
|
|
# LogFile Handler
|
2008-12-26 15:05:51 +00:00
|
|
|
logf = tools.config['logfile']
|
2008-07-22 14:24:36 +00:00
|
|
|
try:
|
2008-11-25 10:35:00 +00:00
|
|
|
dirname = os.path.dirname(logf)
|
2008-12-12 00:28:15 +00:00
|
|
|
if dirname and not os.path.isdir(dirname):
|
|
|
|
os.makedirs(dirname)
|
2009-11-24 14:44:05 +00:00
|
|
|
if tools.config['logrotate'] is not False:
|
2009-07-09 18:16:35 +00:00
|
|
|
handler = logging.handlers.TimedRotatingFileHandler(logf,'D',1,30)
|
2009-11-24 14:44:05 +00:00
|
|
|
elif os.name == 'posix':
|
|
|
|
handler = logging.handlers.WatchedFileHandler(logf)
|
|
|
|
else:
|
|
|
|
handler = logging.handlers.FileHandler(logf)
|
2010-10-05 12:37:30 +00:00
|
|
|
except Exception:
|
2009-01-30 16:55:51 +00:00
|
|
|
sys.stderr.write("ERROR: couldn't create the logfile directory. Logging to the standard output.\n")
|
2009-07-07 10:02:07 +00:00
|
|
|
handler = logging.StreamHandler(sys.stdout)
|
2008-07-22 14:24:36 +00:00
|
|
|
else:
|
2008-12-22 23:04:18 +00:00
|
|
|
# Normal Handler on standard output
|
2008-07-22 14:24:36 +00:00
|
|
|
handler = logging.StreamHandler(sys.stdout)
|
|
|
|
|
2010-04-21 10:12:00 +00:00
|
|
|
if isinstance(handler, logging.StreamHandler) and os.isatty(handler.stream.fileno()):
|
|
|
|
formatter = ColoredFormatter(format)
|
|
|
|
else:
|
2010-09-03 07:41:32 +00:00
|
|
|
formatter = DBFormatter(format)
|
2008-07-22 14:24:36 +00:00
|
|
|
handler.setFormatter(formatter)
|
|
|
|
|
2012-02-01 00:10:19 +00:00
|
|
|
# Configure handlers
|
2012-02-08 01:59:57 +00:00
|
|
|
default_config = [
|
|
|
|
'openerp.netsvc.rpc.request:INFO',
|
|
|
|
'openerp.netsvc.rpc.response:INFO',
|
2012-10-10 20:48:29 +00:00
|
|
|
'openerp.addons.web.http:INFO',
|
2012-02-08 01:59:57 +00:00
|
|
|
'openerp.sql_db:INFO',
|
|
|
|
':INFO',
|
|
|
|
]
|
|
|
|
|
2012-02-06 23:02:15 +00:00
|
|
|
if tools.config['log_level'] == 'info':
|
|
|
|
pseudo_config = []
|
|
|
|
elif tools.config['log_level'] == 'debug_rpc':
|
2012-02-08 01:59:57 +00:00
|
|
|
pseudo_config = ['openerp:DEBUG','openerp.netsvc.rpc.request:DEBUG']
|
2012-02-06 23:02:15 +00:00
|
|
|
elif tools.config['log_level'] == 'debug_rpc_answer':
|
2012-02-08 01:59:57 +00:00
|
|
|
pseudo_config = ['openerp:DEBUG','openerp.netsvc.rpc.request:DEBUG', 'openerp.netsvc.rpc.response:DEBUG']
|
2012-02-06 23:02:15 +00:00
|
|
|
elif tools.config['log_level'] == 'debug':
|
|
|
|
pseudo_config = ['openerp:DEBUG']
|
|
|
|
elif tools.config['log_level'] == 'test':
|
|
|
|
pseudo_config = ['openerp:TEST']
|
|
|
|
elif tools.config['log_level'] == 'warn':
|
|
|
|
pseudo_config = ['openerp:WARNING']
|
2012-02-08 01:59:57 +00:00
|
|
|
elif tools.config['log_level'] == 'error':
|
|
|
|
pseudo_config = ['openerp:ERROR']
|
|
|
|
elif tools.config['log_level'] == 'critical':
|
|
|
|
pseudo_config = ['openerp:CRITICAL']
|
2012-02-06 23:02:15 +00:00
|
|
|
elif tools.config['log_level'] == 'debug_sql':
|
|
|
|
pseudo_config = ['openerp.sql_db:DEBUG']
|
2012-02-10 13:44:46 +00:00
|
|
|
else:
|
|
|
|
pseudo_config = []
|
2012-02-08 01:59:57 +00:00
|
|
|
|
|
|
|
logconfig = tools.config['log_handler']
|
|
|
|
|
|
|
|
for logconfig_item in default_config + pseudo_config + logconfig:
|
2012-02-01 23:56:04 +00:00
|
|
|
loggername, level = logconfig_item.split(':')
|
2012-02-01 00:10:19 +00:00
|
|
|
level = getattr(logging, level, logging.INFO)
|
2012-02-01 23:56:04 +00:00
|
|
|
logger = logging.getLogger(loggername)
|
2012-02-01 00:10:19 +00:00
|
|
|
logger.handlers = []
|
|
|
|
logger.setLevel(level)
|
|
|
|
logger.addHandler(handler)
|
2012-02-01 23:56:04 +00:00
|
|
|
if loggername != '':
|
2012-02-01 00:19:41 +00:00
|
|
|
logger.propagate = False
|
2012-02-01 00:10:19 +00:00
|
|
|
|
2012-02-08 01:59:57 +00:00
|
|
|
for logconfig_item in default_config + pseudo_config + logconfig:
|
2012-02-06 23:02:15 +00:00
|
|
|
_logger.debug('logger level set: "%s"', logconfig_item)
|
2012-01-26 11:01:35 +00:00
|
|
|
|
2011-05-07 11:21:29 +00:00
|
|
|
# A alternative logging scheme for automated runs of the
|
|
|
|
# server intended to test it.
|
|
|
|
def init_alternative_logger():
|
|
|
|
class H(logging.Handler):
|
2012-01-19 19:17:56 +00:00
|
|
|
def emit(self, record):
|
|
|
|
if record.levelno > 20:
|
|
|
|
print record.levelno, record.pathname, record.msg
|
2011-05-07 11:21:29 +00:00
|
|
|
handler = H()
|
2012-01-24 10:45:58 +00:00
|
|
|
# Add the handler to the 'openerp' logger.
|
|
|
|
logger = logging.getLogger('openerp')
|
2011-05-07 11:21:29 +00:00
|
|
|
logger.addHandler(handler)
|
|
|
|
logger.setLevel(logging.ERROR)
|
|
|
|
|
2010-10-13 10:57:13 +00:00
|
|
|
def replace_request_password(args):
|
|
|
|
# password is always 3rd argument in a request, we replace it in RPC logs
|
|
|
|
# so it's easier to forward logs for diagnostics/debugging purposes...
|
|
|
|
if len(args) > 2:
|
2012-02-01 00:10:19 +00:00
|
|
|
args = list(args)
|
2010-10-13 10:57:13 +00:00
|
|
|
args[2] = '*'
|
2012-02-01 00:10:19 +00:00
|
|
|
return tuple(args)
|
2010-10-13 10:57:13 +00:00
|
|
|
|
2012-02-01 23:56:04 +00:00
|
|
|
def log(logger, level, prefix, msg, depth=None):
|
2012-02-01 00:10:19 +00:00
|
|
|
indent=''
|
|
|
|
indent_after=' '*len(prefix)
|
|
|
|
for line in (prefix+pformat(msg, depth=depth)).split('\n'):
|
2012-02-01 23:56:04 +00:00
|
|
|
logger.log(level, indent+line)
|
2012-02-01 00:10:19 +00:00
|
|
|
indent=indent_after
|
2008-12-18 18:13:41 +00:00
|
|
|
|
2011-09-25 15:23:18 +00:00
|
|
|
def dispatch_rpc(service_name, method, params):
|
2011-09-02 14:12:32 +00:00
|
|
|
""" Handle a RPC call.
|
|
|
|
|
|
|
|
This is pure Python code, the actual marshalling (from/to XML-RPC or
|
2011-09-02 15:02:26 +00:00
|
|
|
NET-RPC) is done in a upper layer.
|
2011-09-02 14:12:32 +00:00
|
|
|
"""
|
|
|
|
try:
|
2012-02-06 22:14:20 +00:00
|
|
|
rpc_request = logging.getLogger(__name__ + '.rpc.request')
|
|
|
|
rpc_response = logging.getLogger(__name__ + '.rpc.response')
|
|
|
|
rpc_request_flag = rpc_request.isEnabledFor(logging.DEBUG)
|
|
|
|
rpc_response_flag = rpc_response.isEnabledFor(logging.DEBUG)
|
|
|
|
if rpc_request_flag or rpc_response_flag:
|
2011-09-02 14:12:32 +00:00
|
|
|
start_time = time.time()
|
2012-02-06 22:14:20 +00:00
|
|
|
if rpc_request and rpc_response_flag:
|
|
|
|
log(rpc_request,logging.DEBUG,'%s.%s'%(service_name,method), replace_request_password(params))
|
2012-02-01 00:10:19 +00:00
|
|
|
|
2012-03-16 11:12:18 +00:00
|
|
|
threading.current_thread().uid = None
|
|
|
|
threading.current_thread().dbname = None
|
2011-09-25 15:23:18 +00:00
|
|
|
result = ExportService.getService(service_name).dispatch(method, params)
|
2012-02-01 00:10:19 +00:00
|
|
|
|
2012-02-06 22:14:20 +00:00
|
|
|
if rpc_request_flag or rpc_response_flag:
|
2011-09-02 14:12:32 +00:00
|
|
|
end_time = time.time()
|
2012-02-06 22:14:20 +00:00
|
|
|
if rpc_response_flag:
|
|
|
|
log(rpc_response,logging.DEBUG,'%s.%s time:%.3fs '%(service_name,method,end_time - start_time), result)
|
2012-02-01 00:10:19 +00:00
|
|
|
else:
|
2012-02-06 22:14:20 +00:00
|
|
|
log(rpc_request,logging.DEBUG,'%s.%s time:%.3fs '%(service_name,method,end_time - start_time), replace_request_password(params), depth=1)
|
2012-02-01 00:10:19 +00:00
|
|
|
|
2011-09-02 14:12:32 +00:00
|
|
|
return result
|
2011-09-26 15:14:03 +00:00
|
|
|
except openerp.exceptions.AccessError:
|
|
|
|
raise
|
|
|
|
except openerp.exceptions.AccessDenied:
|
|
|
|
raise
|
|
|
|
except openerp.exceptions.Warning:
|
|
|
|
raise
|
2011-09-27 10:22:46 +00:00
|
|
|
except openerp.exceptions.DeferredException, e:
|
2012-08-22 10:11:17 +00:00
|
|
|
_logger.exception(tools.exception_to_unicode(e))
|
2011-09-27 10:22:46 +00:00
|
|
|
post_mortem(e.traceback)
|
|
|
|
raise
|
2011-09-02 14:12:32 +00:00
|
|
|
except Exception, e:
|
2012-08-22 10:11:17 +00:00
|
|
|
_logger.exception(tools.exception_to_unicode(e))
|
2011-09-27 10:22:46 +00:00
|
|
|
post_mortem(sys.exc_info())
|
|
|
|
raise
|
|
|
|
|
|
|
|
def post_mortem(info):
|
|
|
|
if tools.config['debug_mode'] and isinstance(info[2], types.TracebackType):
|
|
|
|
import pdb
|
|
|
|
pdb.post_mortem(info[2])
|
2009-01-05 18:55:20 +00:00
|
|
|
|
2008-07-23 15:01:27 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|