[sr-dev] git:master: app_python: expand log facilities

Ovidiu Sas osas at voipembedded.com
Tue Dec 4 00:33:57 CET 2012


Module: sip-router
Branch: master
Commit: acbeda46ac94e6c5c215a03bd803e10b2c7540db
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=acbeda46ac94e6c5c215a03bd803e10b2c7540db

Author: Konstantin M <evilzluk at gmail.com>
Committer: Ovidiu Sas <osas at voipembedded.com>
Date:   Mon Dec  3 18:26:36 2012 -0500

app_python: expand log facilities

---

 modules/app_python/Loggers.py              |   65 ++++++++++
 modules/app_python/README.TestCase-Loggers |   16 +++
 modules/app_python/python_iface.c          |  181 +++++++++++++++++++++++++++-
 modules/app_python/python_iface.h          |    1 +
 modules/app_python/python_mod.c            |   16 +--
 5 files changed, 264 insertions(+), 15 deletions(-)

diff --git a/modules/app_python/Loggers.py b/modules/app_python/Loggers.py
new file mode 100644
index 0000000..9f8b706
--- /dev/null
+++ b/modules/app_python/Loggers.py
@@ -0,0 +1,65 @@
+# -*- coding: utf-8 -*-
+
+"""
+    Script for logging test.
+    2012.12.03: Created by: Konstantin M. <evilzluk at gmail.com>
+"""
+
+import Router
+
+"""
+    Module Properties:
+       Log Levels:
+           L_ALERT
+           L_BUG
+           L_CRIT2
+           L_CRIT
+           L_ERR
+           L_WARN
+           L_NOTICE
+           L_INFO
+           L_DBG
+
+       Log Facilities:
+           DEFAULT_FACILITY
+
+    Module Methods:
+       LM_GEN1(self, int log_level, str msg)
+       LM_GEN2(self, int log_facility, int log_level, str msg)
+       LM_ALERT(self, str msg)
+       LM_CRIT(self, str msg)
+       LM_ERR(self, str msg)
+       LM_WARN(self, str msg)
+       LM_NOTICE(self, str msg)
+       LM_INFO(self, str msg)
+       LM_DBG(self, str msg)
+
+
+"""
+
+class Loggers:
+
+    def __init__(self):
+	pass
+
+    def __del__(self):
+	pass
+
+    def child_init(self, y):
+	return 0
+
+    def TestLoggers(self, msg, args):
+	Router.LM_GEN1(Router.L_INFO,                           	"Loggers.py:     LM_GEN1: msg: %s" % str(args))
+	Router.LM_GEN2(Router.L_INFO, Router.DEFAULT_FACILITY,	        "Loggers.py:     LM_GEN2: msg: %s" % str(args))
+	Router.LM_ALERT(                                        	"Loggers.py:    LM_ALERT: msg: %s" % str(args))
+	Router.LM_CRIT(                                         	"Loggers.py:     LM_CRIT: msg: %s" % str(args))
+	Router.LM_ERR(                                          	"Loggers.py:      LM_ERR: msg: %s" % str(args))
+	Router.LM_WARN(                                         	"Loggers.py:     LM_WARN: msg: %s" % str(args))
+	Router.LM_NOTICE(                                       	"Loggers.py:   LM_NOTICE: msg: %s" % str(args))
+	Router.LM_INFO(                                         	"Loggers.py:     LM_INFO: msg: %s" % str(args))
+	Router.LM_DBG(                                          	"Loggers.py:      LM_DBG: msg: %s" % str(args))
+	return 1
+
+def mod_init():
+    return Loggers()
+
diff --git a/modules/app_python/README.TestCase-Loggers b/modules/app_python/README.TestCase-Loggers
new file mode 100644
index 0000000..8eea4b2
--- /dev/null
+++ b/modules/app_python/README.TestCase-Loggers
@@ -0,0 +1,16 @@
+
+Example of using loggers in Python:
+
+loadmodule "app_python.so"
+modparam("app_python", "script_name", "/path/to/Loggers.py")
+modparam("app_python", "mod_init_function", "mod_init")
+modparam("app_python", "child_init_method", "child_init")
+
+...
+
+route
+{
+    python_exec("TestLoggers", "Test Message\n");
+    ...
+}
+
diff --git a/modules/app_python/python_iface.c b/modules/app_python/python_iface.c
index 7e571d8..36c7c23 100644
--- a/modules/app_python/python_iface.c
+++ b/modules/app_python/python_iface.c
@@ -27,9 +27,111 @@
 #include "../../route_struct.h"
 #include "python_exec.h"
 
-/* Return the number of arguments of the application command line */
-static PyObject*
-router_LM_ERR(PyObject *self, PyObject *args)
+
+/*
+ * Python method: LM_GEN1(self, int log_level, str msg)
+ */
+static PyObject *router_LM_GEN1(PyObject *self, PyObject *args)
+{
+    int log_level;
+    char *msg;
+
+    if (!PyArg_ParseTuple(args, "is:LM_GEN1", &log_level, &msg))
+        return NULL;
+
+    LM_GEN1(log_level, "%s", msg);
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+/*
+ * Python method: LM_GEN2(self, int log_facility, int log_level, str msg)
+ */
+static PyObject *router_LM_GEN2(PyObject *self, PyObject *args)
+{
+    int log_facility;
+    int log_level;
+    char *msg;
+
+    if(!PyArg_ParseTuple(args, "iis:LM_GEN2", &log_facility, &log_level, &msg))
+        return NULL;
+
+    LM_GEN2(log_facility, log_level, "%s", msg);
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+/*
+ * Python method: LM_ALERT(self, str msg)
+ */
+static PyObject *router_LM_ALERT(PyObject *self, PyObject *args)
+{
+    char *msg;
+
+    if(!PyArg_ParseTuple(args, "s:LM_ALERT", &msg))
+        return NULL;
+
+    LM_ALERT("%s", msg);
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+
+/*
+ * Python method: LM_CRIT(self, str msg)
+ */
+static PyObject *router_LM_CRIT(PyObject *self, PyObject *args)
+{
+    char *msg;
+
+    if(!PyArg_ParseTuple(args, "s:LM_CRIT", &msg))
+        return NULL;
+
+    LM_CRIT("%s", msg);
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+/*
+ * Python method: LM_WARN(self, str msg)
+ */
+static PyObject *router_LM_WARN(PyObject *self, PyObject *args)
+{
+    char *msg;
+
+    if(!PyArg_ParseTuple(args, "s:LM_WARN", &msg))
+        return NULL;
+
+    LM_WARN("%s", msg);
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+/*
+ * Python method: LM_NOTICE(self, str msg)
+ */
+static PyObject *router_LM_NOTICE(PyObject *self, PyObject *args)
+{
+    char *msg;
+
+    if(!PyArg_ParseTuple(args, "s:LM_NOTICE", &msg))
+        return NULL;
+
+    LM_NOTICE("%s", msg);
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+/*
+ * Python method: LM_ERR(self, str msg)
+ */
+static PyObject *router_LM_ERR(PyObject *self, PyObject *args)
 {
     char *msg;
 
@@ -42,8 +144,75 @@ router_LM_ERR(PyObject *self, PyObject *args)
     return Py_None;
 }
 
+/*
+ * Python method: LM_INFO(self, str msg)
+ */
+static PyObject *router_LM_INFO(PyObject *self, PyObject *args)
+{
+    char *msg;
+
+    if(!PyArg_ParseTuple(args, "s:LM_INFO", &msg))
+        return NULL;
+
+    LM_INFO("%s", msg);
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+/*
+ * Python method: LM_DBG(self, str msg)
+ */
+static PyObject *router_LM_DBG(PyObject *self, PyObject *args)
+{
+    char *msg;
+
+    if(!PyArg_ParseTuple(args, "s:LM_DBG", &msg))
+        return NULL;
+
+    LM_DBG("%s", msg);
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+
 PyMethodDef RouterMethods[] = {
-    {"LM_ERR", router_LM_ERR, METH_VARARGS,
-     "Pring error message."},
-    {NULL, NULL, 0, NULL}
+    {"LM_GEN1",		router_LM_GEN1,		METH_VARARGS, "Print GEN1 message."},
+    {"LM_GEN2",		router_LM_GEN2,		METH_VARARGS, "Print GEN2 message."},
+    {"LM_ALERT",	router_LM_ALERT,	METH_VARARGS, "Print alert message."},
+    {"LM_CRIT",		router_LM_CRIT,		METH_VARARGS, "Print critical message."},
+    {"LM_ERR",		router_LM_ERR,		METH_VARARGS, "Print error message."},
+    {"LM_WARN",		router_LM_WARN,		METH_VARARGS, "Print warning message."},
+    {"LM_NOTICE",	router_LM_NOTICE,	METH_VARARGS, "Print notice message."},
+    {"LM_INFO",		router_LM_INFO,		METH_VARARGS, "Print info message."},
+    {"LM_DBG",		router_LM_DBG,		METH_VARARGS, "Print debug message."},
+    {NULL, 		NULL, 			0, 		NULL}
 };
+
+/*
+ * Default module properties
+ */
+void RouterAddProperties(PyObject *m)
+{
+    /*
+    * Log levels
+    * Reference: dprint.h
+    */
+    PyModule_AddObject(m, "L_ALERT",  PyInt_FromLong((long)L_ALERT));
+    PyModule_AddObject(m, "L_BUG",    PyInt_FromLong((long)L_BUG));
+    PyModule_AddObject(m, "L_CRIT2",  PyInt_FromLong((long)L_CRIT2)); /* like L_CRIT, but adds prefix */
+    PyModule_AddObject(m, "L_CRIT",   PyInt_FromLong((long)L_CRIT));  /* no prefix added */
+    PyModule_AddObject(m, "L_ERR",    PyInt_FromLong((long)L_ERR));
+    PyModule_AddObject(m, "L_WARN",   PyInt_FromLong((long)L_WARN));
+    PyModule_AddObject(m, "L_NOTICE", PyInt_FromLong((long)L_NOTICE));
+    PyModule_AddObject(m, "L_INFO",   PyInt_FromLong((long)L_INFO));
+    PyModule_AddObject(m, "L_DBG",    PyInt_FromLong((long)L_DBG));
+
+    /*
+    * Facility
+    * Reference: dprint.h
+    */
+    PyModule_AddObject(m, "DEFAULT_FACILITY", PyInt_FromLong((long)DEFAULT_FACILITY));
+}
+
diff --git a/modules/app_python/python_iface.h b/modules/app_python/python_iface.h
index 8491ff4..965f97d 100644
--- a/modules/app_python/python_iface.h
+++ b/modules/app_python/python_iface.h
@@ -26,5 +26,6 @@
 #include <Python.h>
 
 extern PyMethodDef RouterMethods[];
+void RouterAddProperties(PyObject *);
 
 #endif
diff --git a/modules/app_python/python_mod.c b/modules/app_python/python_mod.c
index 185bf99..963aec7 100644
--- a/modules/app_python/python_mod.c
+++ b/modules/app_python/python_mod.c
@@ -82,12 +82,11 @@ struct module_exports exports = {
     child_init                      /* per-child init function */
 };
 
-static int
-mod_init(void)
+static int mod_init(void)
 {
     char *dname, *bname, *dname_src, *bname_src;
     int i;
-    PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
+    PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs, *m;
     PyThreadState *mainThreadState;
 
     if (script_name.len == 0) {
@@ -127,7 +126,7 @@ mod_init(void)
     PyEval_InitThreads();
     mainThreadState = PyThreadState_Get();
 
-    Py_InitModule("Router", RouterMethods);
+    m = Py_InitModule("Router", RouterMethods);
 
     if (python_msgobj_init() != 0) {
         LM_ERR("python_msgobj_init() has failed\n");
@@ -135,6 +134,8 @@ mod_init(void)
         return -1;
     }
 
+    RouterAddProperties(m);
+
     sys_path = PySys_GetObject("path");
     /* PySys_GetObject doesn't pass reference! No need to DEREF */
     if (sys_path == NULL) {
@@ -227,8 +228,7 @@ mod_init(void)
     return 0;
 }
 
-static int
-child_init(int rank)
+static int child_init(int rank)
 {
     PyObject *pFunc, *pArgs, *pValue, *pResult;
     int rval;
@@ -294,9 +294,7 @@ child_init(int rank)
     return rval;
 }
 
-static void
-mod_destroy(void)
+static void mod_destroy(void)
 {
-
     return;
 }




More information about the sr-dev mailing list