Tests about a simple python3 fastcgi runner using libfcgi and the Python-C API.
python
c
wsgi
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

logger.h 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (C) 2019 Weber Yann
  3. *
  4. * This file is part of PyFCGI.
  5. *
  6. * PyFCGI is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * PyFCGI is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PyFCGI. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __LOGGER_H___
  20. #define __LOGGER_H___
  21. #include "config.h"
  22. #include <syslog.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <stdarg.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <stdio.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. /**@file logger.h
  33. * @brief PyFCGI logging facility
  34. * @ingroup conf_logger
  35. */
  36. /**@defgroup conf_logger Logging configuration
  37. * @ingroup conf_internal
  38. */
  39. /**@defgroup log_facility Logger custom facilities */
  40. /**@ingroup log_facility */
  41. #define LOG_GLOBAL 0
  42. /**@ingroup log_facility */
  43. #define LOG_ACCESS 1
  44. /**@ingroup log_facility */
  45. #define LOG_INTERN 2
  46. /**@ingroup log_facility */
  47. #define LOG_WORKER 4
  48. /**@ingroup log_facility */
  49. #define LOG_MASTER 8
  50. #define SYSLOG_syslog syslog
  51. #define SYSLOG_vsyslog vsyslog
  52. #define SYSLOG_EMERG LOG_EMERG
  53. #define SYSLOG_ALERT LOG_ALERT
  54. #define SYSLOG_CRIT LOG_CRIT
  55. #define SYSLOG_ERR LOG_ERR
  56. #define SYSLOG_WARNING LOG_WARNING
  57. #define SYSLOG_NOTICE LOG_NOTICE
  58. #define SYSLOG_INFO LOG_INFO
  59. #define SYSLOG_DEBUG LOG_DEBUG
  60. const short SYSLOG_LVLS[8] = {LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
  61. LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG};
  62. #undef LOG_EMERG
  63. #undef LOG_ALERT
  64. #undef LOG_CRIT
  65. #undef LOG_ERR
  66. #undef LOG_WARNING
  67. #undef LOG_NOTICE
  68. #undef LOG_INFO
  69. #undef LOG_DEBUG
  70. /* This way we can mimic syslog and the or'ed level | facility */
  71. //#define LOG_EMERG (1 << 4)
  72. #define LOG_EMERG (2 << 4)
  73. #define LOG_ALERT (2 << 4)
  74. #define LOG_CRIT (3 << 4)
  75. #define LOG_ERR (4 << 4)
  76. #define LOG_WARNING (5 << 4)
  77. #define LOG_NOTICE (6 << 4)
  78. #define LOG_INFO (7 << 4)
  79. #define LOG_DEBUG (8 << 4)
  80. /**@brief Convert a PyFCGI loglevel in a syslog one */
  81. #define PYFCGI_SYSLOG_LVL(lvl) (SYSLOG_LVLS[(lvl & 0xF0) >> 4])
  82. /**@brief Macro checking if syslog is activated and log a message
  83. * @params int lvl the PyFCGI log level
  84. * @param char* fmt the message format
  85. * @see _vsyslog syslog
  86. */
  87. #define _syslog(lvl, ...) if(conf->flags & PYFCGI_LOG_FSYSLOG) { \
  88. syslog(PYFCGI_SYSLOG_LVL(lvl), __VA_ARGS__);\
  89. }
  90. #define _vsyslog(lvl, fmt, ap) if(conf->flags & PYFCGI_LOG_FSYSLOG) { \
  91. vsyslog(PYFCGI_SYSLOG_LVL(lvl), fmt, ap);\
  92. }
  93. /**@brief Macro checking if syslog is activated and log a message using PyFCGI
  94. * loglevels
  95. * @params int lvl the PyFCGI log level
  96. * @param char* fmt the message format
  97. * @see vsyslog _syslog
  98. */
  99. /**@defgroup conf_logger_flags Logger confifurations flags
  100. * @ingroup conf_logger */
  101. /**@ingroup cong_logger_flags */
  102. #define PYFCGI_LOG_FSYSLOG 1
  103. /**@brief Indicate if the logger should try to reopen on failure
  104. * @ingroup cong_logger_flags */
  105. #define PYFCGI_LOG_FRETRY 2
  106. /**@brief Exit if failure
  107. * @ingroup cong_logger_flags */
  108. #define PYFCG_LOG_FEXIT_ONFAIL 4
  109. /**@brief Log level mask
  110. * @ingroup conf_logger
  111. * Allow selection of loglevels using a bitwise mask 1 << LEVEL
  112. */
  113. typedef unsigned char logmask_t;
  114. typedef unsigned char loglvl_t;
  115. typedef struct pyfcgi_logger_s pyfcgi_logger_t;
  116. /**@brief Logger configuration
  117. * @ingroup conf_logger
  118. */
  119. struct pyfcgi_conf_logger_s
  120. {
  121. /**@brief Or combination of @ref PYFCGI_LOG_SYSLOG or
  122. * @ref PYFCGI_LOG_RETRY */
  123. short flags;
  124. char *syslog_ident;
  125. int syslog_facility;
  126. logmask_t syslog_loglvl;
  127. logmask_t syslog_logtyp;
  128. /**@brief PyFCGI internal ident, prefixes all log messages */
  129. char *ident;
  130. logmask_t facility;
  131. pyfcgi_logger_t *loggers;
  132. unsigned char logger_sz;
  133. char **format;
  134. unsigned char format_sz;
  135. /**@brief Internal pipe to tee(2) the message on loggers */
  136. int pipes[2];
  137. };
  138. /**@brief Informations on a logger
  139. * @ingroup conf_logger
  140. */
  141. struct pyfcgi_logger_s
  142. {
  143. char *filename;
  144. int fd;
  145. logmask_t loglvl;
  146. logmask_t logtyp;
  147. size_t fmt_id;
  148. };
  149. #include "conf.h"
  150. /**@brief Add a new logger
  151. * @param char* filename
  152. * @param logmask_t loglvl a mask indicating wich loglevels should be logged
  153. * @param logmask_t typemask a mask indicating wich facility should be logged
  154. * @param char* log format (or NULL for default format)
  155. */
  156. int pyfcgi_logger_add(const char*, logmask_t, logmask_t, const char*);
  157. /**@brief Add a new format
  158. * @param char *format
  159. * @param size_t* idx if not NULL, will contain the format index
  160. * @return 0 if OK
  161. */
  162. int pyfcgi_logger_add_format(const char*, size_t*);
  163. /**@brief Open a logger
  164. * @param pyfcgi_logger_t*
  165. * @return 0 if no errors
  166. */
  167. int pyfcgi_logger_open(pyfcgi_logger_t*);
  168. int pyfcgi_logger_set_ident(const char*);
  169. int pyfcgi_log(loglvl_t, const char*, ...);
  170. int vpyfcgi_log(loglvl_t, const char*, va_list);
  171. #endif