core, lib, modules: restructured source code tree
[sip-router] / src / modules / app_python / mod_Logger.c
1 /**
2  * Copyright (C) 2012 Konstantin Mosesov
3  *
4  * This file is part of Kamailio, a free SIP server.
5  *
6  * This file is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version
10  *
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 // Python includes
24 #include <Python.h>
25 #include "structmember.h"
26
27 // Other/system includes
28 #include <libgen.h>
29
30 // router includes
31 #include "../../str.h"
32 #include "../../sr_module.h"
33
34 // local includes
35 #include "python_exec.h"
36 #include "app_python_mod.h"
37 #include "python_iface.h"
38 #include "python_msgobj.h"
39 #include "python_support.h"
40
41 #include "mod_Router.h"
42 #include "mod_Logger.h"
43
44 PyObject *_sr_apy_logger_module = NULL;
45
46 /*
47  * Python method: LM_GEN1(self, int log_level, str msg)
48  */
49 static PyObject *logger_LM_GEN1(PyObject *self, PyObject *args)
50 {
51         int log_level;
52         char *msg;
53
54         if (!PyArg_ParseTuple(args, "is:LM_GEN1", &log_level, &msg))
55                 return NULL;
56
57         LM_GEN1(log_level, "%s", msg);
58
59         Py_INCREF(Py_None);
60         return Py_None;
61 }
62
63 /*
64  * Python method: LM_GEN2(self, int log_facility, int log_level, str msg)
65  */
66 static PyObject *logger_LM_GEN2(PyObject *self, PyObject *args)
67 {
68         int log_facility;
69         int log_level;
70         char *msg;
71
72         if(!PyArg_ParseTuple(args, "iis:LM_GEN2", &log_facility, &log_level, &msg))
73                 return NULL;
74
75         LM_GEN2(log_facility, log_level, "%s", msg);
76
77         Py_INCREF(Py_None);
78         return Py_None;
79 }
80
81 /*
82  * Python method: LM_ALERT(self, str msg)
83  */
84 static PyObject *logger_LM_ALERT(PyObject *self, PyObject *args)
85 {
86         char *msg;
87
88         if(!PyArg_ParseTuple(args, "s:LM_ALERT", &msg))
89                 return NULL;
90
91         LM_ALERT("%s", msg);
92
93         Py_INCREF(Py_None);
94         return Py_None;
95 }
96
97
98 /*
99  * Python method: LM_CRIT(self, str msg)
100  */
101 static PyObject *logger_LM_CRIT(PyObject *self, PyObject *args)
102 {
103         char *msg;
104
105         if(!PyArg_ParseTuple(args, "s:LM_CRIT", &msg))
106                 return NULL;
107
108         LM_CRIT("%s", msg);
109
110         Py_INCREF(Py_None);
111         return Py_None;
112 }
113
114 /*
115  * Python method: LM_WARN(self, str msg)
116  */
117 static PyObject *logger_LM_WARN(PyObject *self, PyObject *args)
118 {
119         char *msg;
120
121         if(!PyArg_ParseTuple(args, "s:LM_WARN", &msg))
122                 return NULL;
123
124         LM_WARN("%s", msg);
125
126         Py_INCREF(Py_None);
127         return Py_None;
128 }
129
130 /*
131  * Python method: LM_NOTICE(self, str msg)
132  */
133 static PyObject *logger_LM_NOTICE(PyObject *self, PyObject *args)
134 {
135         char *msg;
136
137         if(!PyArg_ParseTuple(args, "s:LM_NOTICE", &msg))
138                 return NULL;
139
140         LM_NOTICE("%s", msg);
141
142         Py_INCREF(Py_None);
143         return Py_None;
144 }
145
146 /*
147  * Python method: LM_ERR(self, str msg)
148  */
149 static PyObject *logger_LM_ERR(PyObject *self, PyObject *args)
150 {
151         char *msg;
152
153         if(!PyArg_ParseTuple(args, "s:LM_ERR", &msg))
154                 return NULL;
155
156         LM_ERR("%s", msg);
157
158         Py_INCREF(Py_None);
159         return Py_None;
160 }
161
162 /*
163  * Python method: LM_INFO(self, str msg)
164  */
165 static PyObject *logger_LM_INFO(PyObject *self, PyObject *args)
166 {
167         char *msg;
168
169         if(!PyArg_ParseTuple(args, "s:LM_INFO", &msg))
170                 return NULL;
171
172         LM_INFO("%s", msg);
173
174         Py_INCREF(Py_None);
175         return Py_None;
176 }
177
178 /*
179  * Python method: LM_DBG(self, str msg)
180  */
181 static PyObject *logger_LM_DBG(PyObject *self, PyObject *args)
182 {
183         char *msg;
184
185         if(!PyArg_ParseTuple(args, "s:LM_DBG", &msg))
186                 return NULL;
187
188         LM_DBG("%s", msg);
189
190         Py_INCREF(Py_None);
191         return Py_None;
192 }
193
194 PyMethodDef LoggerMethods[] = {
195         {"LM_GEN1",             (PyCFunction)logger_LM_GEN1,            METH_VARARGS, "Print GEN1 message."},
196         {"LM_GEN2",             (PyCFunction)logger_LM_GEN2,            METH_VARARGS, "Print GEN2 message."},
197         {"LM_ALERT",    (PyCFunction)logger_LM_ALERT,           METH_VARARGS, "Print alert message."},
198         {"LM_CRIT",             (PyCFunction)logger_LM_CRIT,            METH_VARARGS, "Print critical message."},
199         {"LM_ERR",              (PyCFunction)logger_LM_ERR,             METH_VARARGS, "Print error message."},
200         {"LM_WARN",             (PyCFunction)logger_LM_WARN,            METH_VARARGS, "Print warning message."},
201         {"LM_NOTICE",   (PyCFunction)logger_LM_NOTICE,          METH_VARARGS, "Print notice message."},
202         {"LM_INFO",             (PyCFunction)logger_LM_INFO,            METH_VARARGS, "Print info message."},
203         {"LM_DBG",              (PyCFunction)logger_LM_DBG,             METH_VARARGS, "Print debug message."},
204         {NULL,          NULL,                   0,              NULL}
205 };
206
207 void init_mod_Logger(void)
208 {
209         _sr_apy_logger_module = Py_InitModule("Router.Logger", LoggerMethods);
210         PyDict_SetItemString(_sr_apy_main_module_dict, "Logger", _sr_apy_logger_module);
211
212         /*
213          * Log levels
214          * Reference: dprint.h
215          */
216         PyModule_AddObject(_sr_apy_logger_module, "L_ALERT",
217                         PyInt_FromLong((long)L_ALERT));
218         PyModule_AddObject(_sr_apy_logger_module, "L_BUG",
219                         PyInt_FromLong((long)L_BUG));
220         PyModule_AddObject(_sr_apy_logger_module, "L_CRIT2",
221                         PyInt_FromLong((long)L_CRIT2)); /* like L_CRIT, but adds prefix */
222         PyModule_AddObject(_sr_apy_logger_module, "L_CRIT",
223                         PyInt_FromLong((long)L_CRIT));  /* no prefix added */
224         PyModule_AddObject(_sr_apy_logger_module, "L_ERR",
225                         PyInt_FromLong((long)L_ERR));
226         PyModule_AddObject(_sr_apy_logger_module, "L_WARN",
227                         PyInt_FromLong((long)L_WARN));
228         PyModule_AddObject(_sr_apy_logger_module, "L_NOTICE",
229                         PyInt_FromLong((long)L_NOTICE));
230         PyModule_AddObject(_sr_apy_logger_module, "L_INFO",
231                         PyInt_FromLong((long)L_INFO));
232         PyModule_AddObject(_sr_apy_logger_module, "L_DBG",
233                         PyInt_FromLong((long)L_DBG));
234
235         /*
236          * Facility
237          * Reference: dprint.h
238          */
239         PyModule_AddObject(_sr_apy_logger_module, "DEFAULT_FACILITY",
240                         PyInt_FromLong((long)DEFAULT_FACILITY));
241
242         Py_INCREF(_sr_apy_logger_module);
243
244 #ifdef WITH_EXTRA_DEBUG
245         LM_ERR("Module 'Router.Logger' has been initialized\n");
246 #endif
247
248 }
249
250 void destroy_mod_Logger(void)
251 {
252         Py_XDECREF(_sr_apy_logger_module);
253
254 #ifdef WITH_EXTRA_DEBUG
255         LM_ERR("Module 'Router.Logger' has been destroyed\n");
256 #endif
257
258 }
259