Browse Source

Continuing logger implementation

Yann Weber 5 years ago
parent
commit
164fe9dd98
2 changed files with 48 additions and 34 deletions
  1. 19
    34
      src/logger.c
  2. 29
    0
      src/logger.h

+ 19
- 34
src/logger.c View File

@@ -122,10 +122,7 @@ int vpyfcgi_log(loglvl_t lvl, const char *fmt, va_list ap)
122 122
 
123 123
 	conf = &PyFCGI_conf.logs;
124 124
 
125
-	if(conf->flags & PYFCGI_LOG_FSYSLOG)
126
-	{
127
-		vsyslog(SYSLOG_LVLS[(lvl & 0xF0) >> 4], fmt, ap);
128
-	}
125
+	_vsyslog(lvl, fmt, ap);
129 126
 
130 127
 	len = 0;
131 128
 	if(conf->format_sz > 1)
@@ -133,12 +130,9 @@ int vpyfcgi_log(loglvl_t lvl, const char *fmt, va_list ap)
133 130
 		len = vdprintf(conf->pipes[1], fmt, ap);
134 131
 		if(len < 0)
135 132
 		{
136
-			if(conf->flags & PYFCGI_LOG_FSYSLOG)
137
-			{
138
-				syslog(SYSLOG_ALERT, 
139
-				       "Unable to write to multiplexer pipe when trying to log : %s",
140
-				       strerror(errno));
141
-			}
133
+			_syslog(LOG_ALERT, 
134
+			       "Unable to write to multiplexer pipe when trying to log : %s",
135
+			       strerror(errno));
142 136
 			return PYFCGI_FATAL;
143 137
 		}
144 138
 	}
@@ -147,12 +141,9 @@ int vpyfcgi_log(loglvl_t lvl, const char *fmt, va_list ap)
147 141
 		len = vdprintf(conf->loggers[0].fd, fmt, ap);
148 142
 		if(len < 0)
149 143
 		{
150
-			if(conf->flags & PYFCGI_LOG_FSYSLOG)
151
-			{
152
-				syslog(SYSLOG_ALERT, 
153
-				       "Unable to write to single FD to '%s' when trying to log : %s",
154
-				       conf->loggers[0].filename, strerror(errno));
155
-			}
144
+			_syslog(LOG_ALERT, 
145
+			       "Unable to write to single FD to '%s' when trying to log : %s",
146
+			       conf->loggers[0].filename, strerror(errno));
156 147
 			return PYFCGI_FATAL;
157 148
 		}
158 149
 		return 0;
@@ -162,40 +153,34 @@ int vpyfcgi_log(loglvl_t lvl, const char *fmt, va_list ap)
162 153
 		ret = tee(conf->pipes[0], conf->loggers[i].fd, len, 0);
163 154
 		if(ret < 0)
164 155
 		{
165
-			if(conf->flags & PYFCGI_LOG_FSYSLOG)
166
-			{
167
-				syslog(SYSLOG_ALERT, 
168
-				       "Unable to splice to last logfile '%s' : %s",
169
-				       conf->loggers[i].filename, strerror(errno));
170
-			}
156
+			_syslog(LOG_ALERT, 
157
+			       "Unable to splice to last logfile '%s' : %s",
158
+			       conf->loggers[i].filename, strerror(errno));
171 159
 			return PYFCGI_FATAL;
172 160
 		}
173 161
 	}
174 162
 	ret = splice(conf->pipes[0], NULL, conf->loggers[i].fd, NULL, len, 0);
175 163
 	if(ret < 0)
176 164
 	{
177
-		if(conf->flags & PYFCGI_LOG_FSYSLOG)
178
-		{
179
-			syslog(SYSLOG_ALERT, 
180
-			       "Unable to splice to last logfile '%s' : %s",
181
-			       conf->loggers[i].filename, strerror(errno));
182
-		}
165
+		_syslog(LOG_ALERT, 
166
+		       "Unable to splice to last logfile '%s' : %s",
167
+		       conf->loggers[i].filename, strerror(errno));
183 168
 		return PYFCGI_ERR;
184 169
 	}
185 170
 	if(ret < len)
186 171
 	{
187
-		if(conf->flags & PYFCGI_LOG_FSYSLOG)
188
-		{
189
-			syslog(SYSLOG_WARNING, 
190
-			       "Unable to splice all data to logfile. Flushing pipe.");
191
-		}
172
+		_syslog(LOG_WARNING, 
173
+		       "Unable to splice all data to logfile. Flushing pipe.");
192 174
 
193 175
 		do
194 176
 		{
195 177
 			len -= (ret<len)?ret:len;
196
-			ret = read(conf->pipes[0], buf, len>512?512:len);
178
+			ret = read(conf->pipes[0], buf, (len>sizeof(buf))?sizeof(buf):len);
197 179
 			if(ret < 0)
198 180
 			{
181
+				_syslog(LOG_CRIT,
182
+				        "Logger pipe seems broken, unable to flush : %s",
183
+					strerror(errno));
199 184
 				return PYFCGI_ERR;
200 185
 			}
201 186
 		}while(len);

+ 29
- 0
src/logger.h View File

@@ -31,6 +31,10 @@
31 31
 #include <sys/types.h>
32 32
 #include <sys/stat.h>
33 33
 
34
+/**@file logger.h
35
+ * @brief PyFCGI logging facility
36
+ * @ingroup conf_logger
37
+ */
34 38
 /**@defgroup conf_logger Logging configuration
35 39
  * @ingroup conf_internal
36 40
  */
@@ -47,6 +51,8 @@
47 51
 /**@ingroup log_facility */
48 52
 #define LOG_MASTER 8
49 53
 
54
+#define SYSLOG_syslog syslog
55
+#define SYSLOG_vsyslog vsyslog
50 56
 #define SYSLOG_EMERG   LOG_EMERG
51 57
 #define SYSLOG_ALERT   LOG_ALERT
52 58
 #define SYSLOG_CRIT    LOG_CRIT
@@ -78,6 +84,29 @@ const short SYSLOG_LVLS[8] = {LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
78 84
 #define LOG_INFO (7 << 4)
79 85
 #define LOG_DEBUG (8 << 4)
80 86
 
87
+/**@brief Convert a PyFCGI loglevel in a syslog one */
88
+#define PYFCGI_SYSLOG_LVL(lvl) (SYSLOG_LVLS[(lvl & 0xF0) >> 4])
89
+
90
+/**@brief Macro checking if syslog is activated and log a message
91
+ * @params int lvl the PyFCGI log level
92
+ * @param char* fmt the message format
93
+ * @see _vsyslog syslog
94
+ */
95
+#define _syslog(lvl, ...) if(conf->flags & PYFCGI_LOG_FSYSLOG) { \
96
+	syslog(PYFCGI_SYSLOG_LVL(lvl), __VA_ARGS__);\
97
+}
98
+#define _vsyslog(lvl, fmt, ap) if(conf->flags & PYFCGI_LOG_FSYSLOG) { \
99
+	vsyslog(PYFCGI_SYSLOG_LVL(lvl), fmt, ap);\
100
+}
101
+
102
+/**@brief Macro checking if syslog is activated and log a message using PyFCGI
103
+ * loglevels
104
+ * @params int lvl the PyFCGI log level
105
+ * @param char* fmt the message format
106
+ * @see vsyslog _syslog
107
+ */
108
+
109
+
81 110
 /**@defgroup conf_logger_flags Logger confifurations flags
82 111
  * @ingroup conf_logger */
83 112
 /**@ingroup cong_logger_flags */

Loading…
Cancel
Save