Browse Source

Continuing logger implementation

Yann Weber 4 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
 
122
 
123
 	conf = &PyFCGI_conf.logs;
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
 	len = 0;
127
 	len = 0;
131
 	if(conf->format_sz > 1)
128
 	if(conf->format_sz > 1)
133
 		len = vdprintf(conf->pipes[1], fmt, ap);
130
 		len = vdprintf(conf->pipes[1], fmt, ap);
134
 		if(len < 0)
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
 			return PYFCGI_FATAL;
136
 			return PYFCGI_FATAL;
143
 		}
137
 		}
144
 	}
138
 	}
147
 		len = vdprintf(conf->loggers[0].fd, fmt, ap);
141
 		len = vdprintf(conf->loggers[0].fd, fmt, ap);
148
 		if(len < 0)
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
 			return PYFCGI_FATAL;
147
 			return PYFCGI_FATAL;
157
 		}
148
 		}
158
 		return 0;
149
 		return 0;
162
 		ret = tee(conf->pipes[0], conf->loggers[i].fd, len, 0);
153
 		ret = tee(conf->pipes[0], conf->loggers[i].fd, len, 0);
163
 		if(ret < 0)
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
 			return PYFCGI_FATAL;
159
 			return PYFCGI_FATAL;
172
 		}
160
 		}
173
 	}
161
 	}
174
 	ret = splice(conf->pipes[0], NULL, conf->loggers[i].fd, NULL, len, 0);
162
 	ret = splice(conf->pipes[0], NULL, conf->loggers[i].fd, NULL, len, 0);
175
 	if(ret < 0)
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
 		return PYFCGI_ERR;
168
 		return PYFCGI_ERR;
184
 	}
169
 	}
185
 	if(ret < len)
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
 		do
175
 		do
194
 		{
176
 		{
195
 			len -= (ret<len)?ret:len;
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
 			if(ret < 0)
179
 			if(ret < 0)
198
 			{
180
 			{
181
+				_syslog(LOG_CRIT,
182
+				        "Logger pipe seems broken, unable to flush : %s",
183
+					strerror(errno));
199
 				return PYFCGI_ERR;
184
 				return PYFCGI_ERR;
200
 			}
185
 			}
201
 		}while(len);
186
 		}while(len);

+ 29
- 0
src/logger.h View File

31
 #include <sys/types.h>
31
 #include <sys/types.h>
32
 #include <sys/stat.h>
32
 #include <sys/stat.h>
33
 
33
 
34
+/**@file logger.h
35
+ * @brief PyFCGI logging facility
36
+ * @ingroup conf_logger
37
+ */
34
 /**@defgroup conf_logger Logging configuration
38
 /**@defgroup conf_logger Logging configuration
35
  * @ingroup conf_internal
39
  * @ingroup conf_internal
36
  */
40
  */
47
 /**@ingroup log_facility */
51
 /**@ingroup log_facility */
48
 #define LOG_MASTER 8
52
 #define LOG_MASTER 8
49
 
53
 
54
+#define SYSLOG_syslog syslog
55
+#define SYSLOG_vsyslog vsyslog
50
 #define SYSLOG_EMERG   LOG_EMERG
56
 #define SYSLOG_EMERG   LOG_EMERG
51
 #define SYSLOG_ALERT   LOG_ALERT
57
 #define SYSLOG_ALERT   LOG_ALERT
52
 #define SYSLOG_CRIT    LOG_CRIT
58
 #define SYSLOG_CRIT    LOG_CRIT
78
 #define LOG_INFO (7 << 4)
84
 #define LOG_INFO (7 << 4)
79
 #define LOG_DEBUG (8 << 4)
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
 /**@defgroup conf_logger_flags Logger confifurations flags
110
 /**@defgroup conf_logger_flags Logger confifurations flags
82
  * @ingroup conf_logger */
111
  * @ingroup conf_logger */
83
 /**@ingroup cong_logger_flags */
112
 /**@ingroup cong_logger_flags */

Loading…
Cancel
Save