123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- /*
- * Copyright (C) 2019 Weber Yann
- *
- * This file is part of PyFCGI.
- *
- * PyFCGI is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * any later version.
- *
- * PyFCGI is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with PyFCGI. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef __LOGGER_H___
- #define __LOGGER_H___
-
- #include "config.h"
- #include <syslog.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #include <stdarg.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
-
- /**@file logger.h
- * @brief PyFCGI logging facility
- * @ingroup conf_logger
- */
- /**@defgroup conf_logger Logging configuration
- * @ingroup conf_internal
- */
-
- /**@defgroup log_facility Logger custom facilities */
- /**@ingroup log_facility */
- #define LOG_GLOBAL 0
- /**@ingroup log_facility */
- #define LOG_ACCESS 1
- /**@ingroup log_facility */
- #define LOG_INTERN 2
- /**@ingroup log_facility */
- #define LOG_WORKER 4
- /**@ingroup log_facility */
- #define LOG_MASTER 8
-
- #define SYSLOG_syslog syslog
- #define SYSLOG_vsyslog vsyslog
- #define SYSLOG_EMERG LOG_EMERG
- #define SYSLOG_ALERT LOG_ALERT
- #define SYSLOG_CRIT LOG_CRIT
- #define SYSLOG_ERR LOG_ERR
- #define SYSLOG_WARNING LOG_WARNING
- #define SYSLOG_NOTICE LOG_NOTICE
- #define SYSLOG_INFO LOG_INFO
- #define SYSLOG_DEBUG LOG_DEBUG
- const short SYSLOG_LVLS[8] = {LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
- LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG};
-
- #undef LOG_EMERG
- #undef LOG_ALERT
- #undef LOG_CRIT
- #undef LOG_ERR
- #undef LOG_WARNING
- #undef LOG_NOTICE
- #undef LOG_INFO
- #undef LOG_DEBUG
-
- /* This way we can mimic syslog and the or'ed level | facility */
- //#define LOG_EMERG (1 << 4)
- #define LOG_EMERG (2 << 4)
- #define LOG_ALERT (2 << 4)
- #define LOG_CRIT (3 << 4)
- #define LOG_ERR (4 << 4)
- #define LOG_WARNING (5 << 4)
- #define LOG_NOTICE (6 << 4)
- #define LOG_INFO (7 << 4)
- #define LOG_DEBUG (8 << 4)
-
- /**@brief Convert a PyFCGI loglevel in a syslog one */
- #define PYFCGI_SYSLOG_LVL(lvl) (SYSLOG_LVLS[(lvl & 0xF0) >> 4])
-
- /**@brief Macro checking if syslog is activated and log a message
- * @params int lvl the PyFCGI log level
- * @param char* fmt the message format
- * @see _vsyslog syslog
- */
- #define _syslog(lvl, ...) if(conf->flags & PYFCGI_LOG_FSYSLOG) { \
- syslog(PYFCGI_SYSLOG_LVL(lvl), __VA_ARGS__);\
- }
- #define _vsyslog(lvl, fmt, ap) if(conf->flags & PYFCGI_LOG_FSYSLOG) { \
- vsyslog(PYFCGI_SYSLOG_LVL(lvl), fmt, ap);\
- }
-
- /**@brief Macro checking if syslog is activated and log a message using PyFCGI
- * loglevels
- * @params int lvl the PyFCGI log level
- * @param char* fmt the message format
- * @see vsyslog _syslog
- */
-
-
- /**@defgroup conf_logger_flags Logger confifurations flags
- * @ingroup conf_logger */
- /**@ingroup cong_logger_flags */
- #define PYFCGI_LOG_FSYSLOG 1
- /**@brief Indicate if the logger should try to reopen on failure
- * @ingroup cong_logger_flags */
- #define PYFCGI_LOG_FRETRY 2
- /**@brief Exit if failure
- * @ingroup cong_logger_flags */
- #define PYFCG_LOG_FEXIT_ONFAIL 4
-
- /**@brief Log level mask
- * @ingroup conf_logger
- * Allow selection of loglevels using a bitwise mask 1 << LEVEL
- */
- typedef unsigned char logmask_t;
- typedef unsigned char loglvl_t;
- typedef struct pyfcgi_logger_s pyfcgi_logger_t;
-
- /**@brief Logger configuration
- * @ingroup conf_logger
- */
- struct pyfcgi_conf_logger_s
- {
- /**@brief Or combination of @ref PYFCGI_LOG_SYSLOG or
- * @ref PYFCGI_LOG_RETRY */
- short flags;
- char *syslog_ident;
- int syslog_facility;
- logmask_t syslog_loglvl;
- logmask_t syslog_logtyp;
-
- /**@brief PyFCGI internal ident, prefixes all log messages */
- char *ident;
- logmask_t facility;
-
- pyfcgi_logger_t *loggers;
- unsigned char logger_sz;
-
- char **format;
- unsigned char format_sz;
-
- /**@brief Internal pipe to tee(2) the message on loggers */
- int pipes[2];
- };
-
- /**@brief Informations on a logger
- * @ingroup conf_logger
- */
- struct pyfcgi_logger_s
- {
- char *filename;
- int fd;
- logmask_t loglvl;
- logmask_t logtyp;
-
- size_t fmt_id;
- };
-
- #include "conf.h"
-
- /**@brief Add a new logger
- * @param char* filename
- * @param logmask_t loglvl a mask indicating wich loglevels should be logged
- * @param logmask_t typemask a mask indicating wich facility should be logged
- * @param char* log format (or NULL for default format)
- */
- int pyfcgi_logger_add(const char*, logmask_t, logmask_t, const char*);
-
- /**@brief Add a new format
- * @param char *format
- * @param size_t* idx if not NULL, will contain the format index
- * @return 0 if OK
- */
- int pyfcgi_logger_add_format(const char*, size_t*);
-
- /**@brief Open a logger
- * @param pyfcgi_logger_t*
- * @return 0 if no errors
- */
- int pyfcgi_logger_open(pyfcgi_logger_t*);
-
- int pyfcgi_logger_set_ident(const char*);
-
- int pyfcgi_log(loglvl_t, const char*, ...);
- int vpyfcgi_log(loglvl_t, const char*, va_list);
-
- #endif
|