mod_Logger.c 6.0 KB

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