mod_Logger.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. // Python includes
  23. #include <Python.h>
  24. #include "structmember.h"
  25. // Other/system includes
  26. #include <libgen.h>
  27. // router includes
  28. #include "../../str.h"
  29. #include "../../sr_module.h"
  30. // local includes
  31. #include "python_exec.h"
  32. #include "python_mod.h"
  33. #include "python_iface.h"
  34. #include "python_msgobj.h"
  35. #include "python_support.h"
  36. #include "mod_Router.h"
  37. #include "mod_Logger.h"
  38. /*
  39. * Python method: LM_GEN1(self, int log_level, str msg)
  40. */
  41. static PyObject *logger_LM_GEN1(PyObject *self, PyObject *args)
  42. {
  43. int log_level;
  44. char *msg;
  45. if (!PyArg_ParseTuple(args, "is:LM_GEN1", &log_level, &msg))
  46. return NULL;
  47. LM_GEN1(log_level, "%s", msg);
  48. Py_INCREF(Py_None);
  49. return Py_None;
  50. }
  51. /*
  52. * Python method: LM_GEN2(self, int log_facility, int log_level, str msg)
  53. */
  54. static PyObject *logger_LM_GEN2(PyObject *self, PyObject *args)
  55. {
  56. int log_facility;
  57. int log_level;
  58. char *msg;
  59. if(!PyArg_ParseTuple(args, "iis:LM_GEN2", &log_facility, &log_level, &msg))
  60. return NULL;
  61. LM_GEN2(log_facility, log_level, "%s", msg);
  62. Py_INCREF(Py_None);
  63. return Py_None;
  64. }
  65. /*
  66. * Python method: LM_ALERT(self, str msg)
  67. */
  68. static PyObject *logger_LM_ALERT(PyObject *self, PyObject *args)
  69. {
  70. char *msg;
  71. if(!PyArg_ParseTuple(args, "s:LM_ALERT", &msg))
  72. return NULL;
  73. LM_ALERT("%s", msg);
  74. Py_INCREF(Py_None);
  75. return Py_None;
  76. }
  77. /*
  78. * Python method: LM_CRIT(self, str msg)
  79. */
  80. static PyObject *logger_LM_CRIT(PyObject *self, PyObject *args)
  81. {
  82. char *msg;
  83. if(!PyArg_ParseTuple(args, "s:LM_CRIT", &msg))
  84. return NULL;
  85. LM_CRIT("%s", msg);
  86. Py_INCREF(Py_None);
  87. return Py_None;
  88. }
  89. /*
  90. * Python method: LM_WARN(self, str msg)
  91. */
  92. static PyObject *logger_LM_WARN(PyObject *self, PyObject *args)
  93. {
  94. char *msg;
  95. if(!PyArg_ParseTuple(args, "s:LM_WARN", &msg))
  96. return NULL;
  97. LM_WARN("%s", msg);
  98. Py_INCREF(Py_None);
  99. return Py_None;
  100. }
  101. /*
  102. * Python method: LM_NOTICE(self, str msg)
  103. */
  104. static PyObject *logger_LM_NOTICE(PyObject *self, PyObject *args)
  105. {
  106. char *msg;
  107. if(!PyArg_ParseTuple(args, "s:LM_NOTICE", &msg))
  108. return NULL;
  109. LM_NOTICE("%s", msg);
  110. Py_INCREF(Py_None);
  111. return Py_None;
  112. }
  113. /*
  114. * Python method: LM_ERR(self, str msg)
  115. */
  116. static PyObject *logger_LM_ERR(PyObject *self, PyObject *args)
  117. {
  118. char *msg;
  119. if(!PyArg_ParseTuple(args, "s:LM_ERR", &msg))
  120. return NULL;
  121. LM_ERR("%s", msg);
  122. Py_INCREF(Py_None);
  123. return Py_None;
  124. }
  125. /*
  126. * Python method: LM_INFO(self, str msg)
  127. */
  128. static PyObject *logger_LM_INFO(PyObject *self, PyObject *args)
  129. {
  130. char *msg;
  131. if(!PyArg_ParseTuple(args, "s:LM_INFO", &msg))
  132. return NULL;
  133. LM_INFO("%s", msg);
  134. Py_INCREF(Py_None);
  135. return Py_None;
  136. }
  137. /*
  138. * Python method: LM_DBG(self, str msg)
  139. */
  140. static PyObject *logger_LM_DBG(PyObject *self, PyObject *args)
  141. {
  142. char *msg;
  143. if(!PyArg_ParseTuple(args, "s:LM_DBG", &msg))
  144. return NULL;
  145. LM_DBG("%s", msg);
  146. Py_INCREF(Py_None);
  147. return Py_None;
  148. }
  149. PyMethodDef LoggerMethods[] = {
  150. {"LM_GEN1", (PyCFunction)logger_LM_GEN1, METH_VARARGS, "Print GEN1 message."},
  151. {"LM_GEN2", (PyCFunction)logger_LM_GEN2, METH_VARARGS, "Print GEN2 message."},
  152. {"LM_ALERT", (PyCFunction)logger_LM_ALERT, METH_VARARGS, "Print alert message."},
  153. {"LM_CRIT", (PyCFunction)logger_LM_CRIT, METH_VARARGS, "Print critical message."},
  154. {"LM_ERR", (PyCFunction)logger_LM_ERR, METH_VARARGS, "Print error message."},
  155. {"LM_WARN", (PyCFunction)logger_LM_WARN, METH_VARARGS, "Print warning message."},
  156. {"LM_NOTICE", (PyCFunction)logger_LM_NOTICE, METH_VARARGS, "Print notice message."},
  157. {"LM_INFO", (PyCFunction)logger_LM_INFO, METH_VARARGS, "Print info message."},
  158. {"LM_DBG", (PyCFunction)logger_LM_DBG, METH_VARARGS, "Print debug message."},
  159. {NULL, NULL, 0, NULL}
  160. };
  161. void init_mod_Logger(void)
  162. {
  163. logger_module = Py_InitModule("Router.Logger", LoggerMethods);
  164. PyDict_SetItemString(main_module_dict, "Logger", logger_module);
  165. /*
  166. * Log levels
  167. * Reference: dprint.h
  168. */
  169. PyModule_AddObject(logger_module, "L_ALERT", PyInt_FromLong((long)L_ALERT));
  170. PyModule_AddObject(logger_module, "L_BUG", PyInt_FromLong((long)L_BUG));
  171. PyModule_AddObject(logger_module, "L_CRIT2", PyInt_FromLong((long)L_CRIT2)); /* like L_CRIT, but adds prefix */
  172. PyModule_AddObject(logger_module, "L_CRIT", PyInt_FromLong((long)L_CRIT)); /* no prefix added */
  173. PyModule_AddObject(logger_module, "L_ERR", PyInt_FromLong((long)L_ERR));
  174. PyModule_AddObject(logger_module, "L_WARN", PyInt_FromLong((long)L_WARN));
  175. PyModule_AddObject(logger_module, "L_NOTICE", PyInt_FromLong((long)L_NOTICE));
  176. PyModule_AddObject(logger_module, "L_INFO", PyInt_FromLong((long)L_INFO));
  177. PyModule_AddObject(logger_module, "L_DBG", PyInt_FromLong((long)L_DBG));
  178. /*
  179. * Facility
  180. * Reference: dprint.h
  181. */
  182. PyModule_AddObject(logger_module, "DEFAULT_FACILITY", PyInt_FromLong((long)DEFAULT_FACILITY));
  183. Py_INCREF(logger_module);
  184. #ifdef WITH_EXTRA_DEBUG
  185. LM_ERR("Module 'Router.Logger' has been initialized\n");
  186. #endif
  187. }
  188. void destroy_mod_Logger(void)
  189. {
  190. Py_XDECREF(logger_module);
  191. #ifdef WITH_EXTRA_DEBUG
  192. LM_ERR("Module 'Router.Logger' has been destroyed\n");
  193. #endif
  194. }