diff options
author | Ted Ross <tross@apache.org> | 2013-07-05 17:17:45 +0000 |
---|---|---|
committer | Ted Ross <tross@apache.org> | 2013-07-05 17:17:45 +0000 |
commit | 9003c05edf8a7c6f6f199976470c255968d8bbe8 (patch) | |
tree | e37e2d79a4c92da724c52302d6c574862faac0b9 | |
parent | d244e9afa25c8a3c83841bbe8e15eaad6ef1ec35 (diff) | |
download | qpid-python-9003c05edf8a7c6f6f199976470c255968d8bbe8.tar.gz |
QPID-4968 - Added an adapter module for Python-to-Dispatch calls.
Implemented LogAdapter within the "dispatch" module to allow Python modules to
emit logs in Dispatch.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1500073 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | qpid/extras/dispatch/include/qpid/dispatch/python_embedded.h (renamed from qpid/extras/dispatch/src/python_embedded.h) | 33 | ||||
-rw-r--r-- | qpid/extras/dispatch/src/config.c | 2 | ||||
-rw-r--r-- | qpid/extras/dispatch/src/dispatch.c | 2 | ||||
-rw-r--r-- | qpid/extras/dispatch/src/py/config/parser.py | 5 | ||||
-rw-r--r-- | qpid/extras/dispatch/src/python_embedded.c | 247 |
5 files changed, 248 insertions, 41 deletions
diff --git a/qpid/extras/dispatch/src/python_embedded.h b/qpid/extras/dispatch/include/qpid/dispatch/python_embedded.h index 4b8948a555..76e168f238 100644 --- a/qpid/extras/dispatch/src/python_embedded.h +++ b/qpid/extras/dispatch/include/qpid/dispatch/python_embedded.h @@ -24,12 +24,45 @@ #include <qpid/dispatch/parse.h> #include <qpid/dispatch/iterator.h> +/** + * Initialize the embedded-python subsystem. This must be called before + * any other call into this module is invoked. + */ void dx_python_initialize(); + +/** + * Finalize the embedded-python subsystem. After this is called, there + * must be no further invocation of dx_python methods. + */ void dx_python_finalize(); + +/** + * Start using embedded python. This is called once by each module that plans + * to use embedded python capabilities. It must call dx_python_start before + * using any python components. + */ void dx_python_start(); + +/** + * Stop using embedded python. This is called once by each module after it is + * finished using embedded python capabilities. + */ void dx_python_stop(); +/** + * Convert a Python object to AMQP format and append to a composed_field. + * + * @param value A Python Object + * @param field A composed field + */ void dx_py_to_composed(PyObject *value, dx_composed_field_t *field); + +/** + * Convert a parsed field to a Python object + * + * @param field A parsed field + * @return A generated Python object + */ PyObject *dx_field_to_py(dx_parsed_field_t *field); #endif diff --git a/qpid/extras/dispatch/src/config.c b/qpid/extras/dispatch/src/config.c index 127a4e00fa..d92a1e71dc 100644 --- a/qpid/extras/dispatch/src/config.c +++ b/qpid/extras/dispatch/src/config.c @@ -17,7 +17,7 @@ * under the License. */ -#include "python_embedded.h" +#include <qpid/dispatch/python_embedded.h> #include "config_private.h" #include <qpid/dispatch/alloc.h> #include <qpid/dispatch/log.h> diff --git a/qpid/extras/dispatch/src/dispatch.c b/qpid/extras/dispatch/src/dispatch.c index 1cd7ca7ae2..27c988818b 100644 --- a/qpid/extras/dispatch/src/dispatch.c +++ b/qpid/extras/dispatch/src/dispatch.c @@ -17,7 +17,7 @@ * under the License. */ -#include "python_embedded.h" +#include <qpid/dispatch/python_embedded.h> #include <qpid/dispatch.h> #include <qpid/dispatch/server.h> #include <qpid/dispatch/ctools.h> diff --git a/qpid/extras/dispatch/src/py/config/parser.py b/qpid/extras/dispatch/src/py/config/parser.py index c921913d24..7619b91461 100644 --- a/qpid/extras/dispatch/src/py/config/parser.py +++ b/qpid/extras/dispatch/src/py/config/parser.py @@ -18,7 +18,8 @@ ## import json -from schema import config_schema +from schema import config_schema +from dispatch import LogAdapter, LOG_TRACE, LOG_ERROR, LOG_INFO class Section: """ @@ -198,10 +199,12 @@ class DXConfig: self.raw_config = None self.config = None self.schema = Schema() + self.log = LogAdapter('config.parser') def read_file(self): try: + self.log.log(LOG_INFO, "Reading Configuration File: %s" % self.path) cfile = open(self.path) text = cfile.read() cfile.close() diff --git a/qpid/extras/dispatch/src/python_embedded.c b/qpid/extras/dispatch/src/python_embedded.c index b88830f6c6..42a8221cea 100644 --- a/qpid/extras/dispatch/src/python_embedded.c +++ b/qpid/extras/dispatch/src/python_embedded.c @@ -17,15 +17,65 @@ * under the License. */ -#include "python_embedded.h" +#include <qpid/dispatch/python_embedded.h> #include <qpid/dispatch/threading.h> #include <qpid/dispatch/log.h> #include <qpid/dispatch/amqp.h> +#include <qpid/dispatch/alloc.h> + + +//=============================================================================== +// Control Functions +//=============================================================================== static uint32_t ref_count = 0; static sys_mutex_t *lock = 0; static char *log_module = "PYTHON"; +static void dx_python_setup(); + + +void dx_python_initialize() +{ + lock = sys_mutex(); +} + + +void dx_python_finalize() +{ + assert(ref_count == 0); + sys_mutex_free(lock); +} + + +void dx_python_start() +{ + sys_mutex_lock(lock); + if (ref_count == 0) { + Py_Initialize(); + dx_python_setup(); + dx_log(log_module, LOG_TRACE, "Embedded Python Interpreter Initialized"); + } + ref_count++; + sys_mutex_unlock(lock); +} + + +void dx_python_stop() +{ + sys_mutex_lock(lock); + ref_count--; + if (ref_count == 0) { + Py_Finalize(); + dx_log(log_module, LOG_TRACE, "Embedded Python Interpreter Shut Down"); + } + sys_mutex_unlock(lock); +} + + +//=============================================================================== +// Data Conversion Functions +//=============================================================================== static PyObject *parsed_to_py_string(dx_parsed_field_t *field) { @@ -67,43 +117,6 @@ static PyObject *parsed_to_py_string(dx_parsed_field_t *field) } -void dx_python_initialize() -{ - lock = sys_mutex(); -} - - -void dx_python_finalize() -{ - assert(ref_count == 0); - sys_mutex_free(lock); -} - - -void dx_python_start() -{ - sys_mutex_lock(lock); - if (ref_count == 0) { - Py_Initialize(); - dx_log(log_module, LOG_TRACE, "Embedded Python Interpreter Initialized"); - } - ref_count++; - sys_mutex_unlock(lock); -} - - -void dx_python_stop() -{ - sys_mutex_lock(lock); - ref_count--; - if (ref_count == 0) { - Py_Finalize(); - dx_log(log_module, LOG_TRACE, "Embedded Python Interpreter Shut Down"); - } - sys_mutex_unlock(lock); -} - - void dx_py_to_composed(PyObject *value, dx_composed_field_t *field) { if (PyBool_Check(value)) @@ -255,3 +268,161 @@ PyObject *dx_field_to_py(dx_parsed_field_t *field) return result; } + +//=============================================================================== +// Logging Object +//=============================================================================== + +typedef struct { + PyObject_HEAD + PyObject *module_name; +} LogAdapter; + + +static int LogAdapter_init(LogAdapter *self, PyObject *args, PyObject *kwds) +{ + const char *text; + if (!PyArg_ParseTuple(args, "s", &text)) + return -1; + + self->module_name = PyString_FromString(text); + return 0; +} + + +static void LogAdapter_dealloc(LogAdapter* self) +{ + Py_XDECREF(self->module_name); + self->ob_type->tp_free((PyObject*)self); +} + + +static PyObject* dx_python_log(PyObject *self, PyObject *args) +{ + int level; + const char* text; + + if (!PyArg_ParseTuple(args, "is", &level, &text)) + return 0; + + LogAdapter *self_ptr = (LogAdapter*) self; + char *logmod = PyString_AS_STRING(self_ptr->module_name); + + dx_log(logmod, level, text); + + Py_INCREF(Py_None); + return Py_None; +} + + +static PyMethodDef LogAdapter_methods[] = { + {"log", dx_python_log, METH_VARARGS, "Emit a Log Line"}, + {0, 0, 0, 0} +}; + +static PyMethodDef empty_methods[] = { + {0, 0, 0, 0} +}; + +static PyTypeObject LogAdapterType = { + PyObject_HEAD_INIT(0) + 0, /* ob_size*/ + "dispatch.LogAdapter", /* tp_name*/ + sizeof(LogAdapter), /* tp_basicsize*/ + 0, /* tp_itemsize*/ + (destructor)LogAdapter_dealloc, /* tp_dealloc*/ + 0, /* tp_print*/ + 0, /* tp_getattr*/ + 0, /* tp_setattr*/ + 0, /* tp_compare*/ + 0, /* tp_repr*/ + 0, /* tp_as_number*/ + 0, /* tp_as_sequence*/ + 0, /* tp_as_mapping*/ + 0, /* tp_hash */ + 0, /* tp_call*/ + 0, /* tp_str*/ + 0, /* tp_getattro*/ + 0, /* tp_setattro*/ + 0, /* tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /* tp_flags*/ + "Dispatch Log Adapter", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + LogAdapter_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)LogAdapter_init, /* tp_init */ + 0, /* tp_alloc */ + 0, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0, /* tp_weaklist */ + 0, /* tp_del */ + 0 /* tp_version_tag */ +}; + + +//=============================================================================== +// Message IO Object +//=============================================================================== + +typedef struct dx_python_io_adapter { + int x; +} dx_python_io_adapter; + +ALLOC_DECLARE(dx_python_io_adapter); +ALLOC_DEFINE(dx_python_io_adapter); + +//static PyObject* dx_python_send(PyObject *self, PyObject *args) +//{ +// return 0; +//} + + +//=============================================================================== +// Initialization of Modules and Types +//=============================================================================== + +static void dx_python_setup() +{ + // + // Add LogAdapter + // + LogAdapterType.tp_new = PyType_GenericNew; + if (PyType_Ready(&LogAdapterType) < 0) { + PyErr_Print(); + dx_log(log_module, LOG_ERROR, "Unable to initialize LogAdapter"); + assert(0); + } else { + PyObject *m = Py_InitModule3("dispatch", empty_methods, "Dispatch Adapter Module"); + + Py_INCREF(&LogAdapterType); + PyModule_AddObject(m, "LogAdapter", (PyObject*) &LogAdapterType); + + PyObject *LogTrace = PyInt_FromLong((long) LOG_TRACE); + Py_INCREF(LogTrace); + PyModule_AddObject(m, "LOG_TRACE", LogTrace); + + PyObject *LogError = PyInt_FromLong((long) LOG_ERROR); + Py_INCREF(LogError); + PyModule_AddObject(m, "LOG_ERROR", LogError); + + PyObject *LogInfo = PyInt_FromLong((long) LOG_INFO); + Py_INCREF(LogInfo); + PyModule_AddObject(m, "LOG_INFO", LogInfo); + } +} |