ソースを参照

Using internal logger instead of syslog

Yann Weber 5年前
コミット
9eb5f1f42a
5個のファイルの変更103行の追加91行の削除
  1. 2
    0
      include/pyworker.h
  2. 1
    0
      include/responder.h
  3. 17
    8
      src/main.c
  4. 63
    63
      src/pyworker.c
  5. 20
    20
      src/responder.c

+ 2
- 0
include/pyworker.h ファイルの表示

@@ -78,6 +78,8 @@
78 78
 #include <sys/sem.h>
79 79
 #include <sys/wait.h>
80 80
 
81
+#include "logger.h"
82
+
81 83
 #define EXIT_PYERR 42
82 84
 #define WPIPER_SIG 30
83 85
 #define PYENTRY_FUNNAME "entrypoint"

+ 1
- 0
include/responder.h ファイルの表示

@@ -54,6 +54,7 @@
54 54
 #include <sys/ipc.h>
55 55
 #include <sys/sem.h>
56 56
 
57
+#include "logger.h"
57 58
 #include "pyworker.h"
58 59
 
59 60
 

+ 17
- 8
src/main.c ファイルの表示

@@ -40,6 +40,7 @@
40 40
 #include <sys/wait.h>
41 41
 
42 42
 #include "conf.h"
43
+#include "logger.h"
43 44
 #include "responder.h"
44 45
 
45 46
 #define IDENT_FMT "pyfcgi[%d]"
@@ -47,6 +48,8 @@
47 48
 
48 49
 #define EARLY_ERR(err_str) write(2, err_str, strlen(err_str))
49 50
 
51
+extern pyfcgi_conf_t PyFCGI_conf;
52
+
50 53
 int main(int argc, char **argv)
51 54
 {
52 55
 	char *ident, *py_entrypoint;
@@ -64,6 +67,12 @@ int main(int argc, char **argv)
64 67
 		return 1;
65 68
 	}
66 69
 
70
+PyFCGI_conf.context.pid = getpid();
71
+
72
+	pyfcgi_logger_init();
73
+	pyfcgi_logger_set_ident("pyfcgi(main)");
74
+	pyfcgi_logger_add("/tmp/pyfcgi.log", 0xFF, 0xFF, NULL);
75
+
67 76
 	ident_len = snprintf(NULL, 0, IDENT_FMT, getpid());
68 77
 	ident_len++;
69 78
 	ident = malloc(sizeof(char)*ident_len);
@@ -72,11 +81,11 @@ int main(int argc, char **argv)
72 81
 		EARLY_ERR("Unable to allocate memory for ident string...\n");
73 82
 		return -1;
74 83
 	}
75
-
76 84
 	snprintf(ident, ident_len, IDENT_FMT, getpid());
85
+	pyfcgi_logger_enable_syslog(ident);
86
+
77 87
 
78
-	openlog(ident, LOG_CONS | LOG_PERROR, LOG_DAEMON | LOG_USER);
79
-	syslog(LOG_INFO, "New server started");
88
+	pyfcgi_log(LOG_INFO, "New server started");
80 89
 
81 90
 	while(1)
82 91
 	{
@@ -84,7 +93,7 @@ int main(int argc, char **argv)
84 93
 		if(child == -1)
85 94
 		{
86 95
 			// TODO : Error
87
-			syslog(LOG_EMERG, "Failed to fork : '%s' sleeping %ds", strerror(errno), emerg_sleep);
96
+			pyfcgi_log(LOG_EMERG, "Failed to fork : '%s' sleeping %ds", strerror(errno), emerg_sleep);
88 97
 			sleep(emerg_sleep);
89 98
 			continue;
90 99
 		}
@@ -96,17 +105,17 @@ int main(int argc, char **argv)
96 105
 		waitpid(child, &child_ret, 0);
97 106
 		if(child_ret)
98 107
 		{
99
-			syslog(LOG_ERR, "Responder loop function exits with error code '%d'",
108
+			pyfcgi_log(LOG_ERR, "Responder loop function exits with error code '%d'",
100 109
 				WEXITSTATUS(child_ret));
101 110
 			if(WIFSIGNALED(child_ret))
102 111
 			{
103
-				syslog(LOG_ERR, "Responder loop function terminated by sig '%d'",
112
+				pyfcgi_log(LOG_ERR, "Responder loop function terminated by sig '%d'",
104 113
 					WTERMSIG(child_ret));
105 114
 			}
106 115
 		}
107 116
 		else
108 117
 		{
109
-			syslog(LOG_NOTICE, "Restarting main process after %d requests", MAX_REQS);
118
+			pyfcgi_log(LOG_NOTICE, "Restarting main process after %d requests", MAX_REQS);
110 119
 		}
111 120
 	}
112 121
 
@@ -143,7 +152,7 @@ int main(int argc, char **argv)
143 152
  *
144 153
  * @subsubsection main_how_use_syslog Logging, using syslog
145 154
  * 
146
- * Right now PyFCGI uses syslog() to log stuff using a pyfcgi ident.
155
+ * Right now PyFCGI uses pyfcgi_log() to log stuff using a pyfcgi ident.
147 156
  * PyFCGI logs can be filtered using /etc/rsyslog.d/pyfcgi.conf :
148 157
  * 
149 158
 <pre>

+ 63
- 63
src/pyworker.c ファイルの表示

@@ -47,7 +47,7 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
47 47
 	if(sigemptyset(&emptyset))
48 48
 	{
49 49
 		err = errno;
50
-		syslog(	LOG_ALERT, "sigemptyset fails in piper : %s",
50
+		pyfcgi_log(	LOG_ALERT, "sigemptyset fails in piper : %s",
51 51
 			strerror(err));
52 52
 		exit(err);
53 53
 	}
@@ -61,12 +61,12 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
61 61
 	if( !(piper_stack = malloc(PIPER_STACK_SZ)) )
62 62
 	{
63 63
 		err = errno;
64
-		syslog(LOG_ALERT, "Error while allocating piper stack : %s",
64
+		pyfcgi_log(LOG_ALERT, "Error while allocating piper stack : %s",
65 65
 		       strerror(err));
66 66
 		exit(err);
67 67
 	}
68 68
 
69
-	syslog(	LOG_INFO,
69
+	pyfcgi_log(	LOG_INFO,
70 70
 		"Worker %d started", wrk_id);
71 71
 
72 72
 	update_python_path(); // add cwd to python path
@@ -76,14 +76,14 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
76 76
 	piper_args.pystderr = pipe_err[0];
77 77
 
78 78
 	fetch_pyflush(&pystdout_flush, &pystderr_flush);
79
-	syslog(	LOG_INFO,
79
+	pyfcgi_log(	LOG_INFO,
80 80
 		"Worker[%d] Python started", wrk_id);
81 81
 	
82 82
 	//importing os
83 83
 	py_osmod = PyImport_ImportModule("os");
84 84
 	if(!py_osmod)
85 85
 	{
86
-		syslog(LOG_ALERT, "Unable to import os module");
86
+		pyfcgi_log(LOG_ALERT, "Unable to import os module");
87 87
 		log_expt(LOG_ALERT);
88 88
 		Py_Exit(EXIT_PYERR);
89 89
 	}
@@ -91,7 +91,7 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
91 91
 	// loading module
92 92
 	entry_fun = import_entrypoint(py_entrypoint);
93 93
 
94
-	syslog(	LOG_INFO,
94
+	pyfcgi_log(	LOG_INFO,
95 95
 		"Worker[%d] Waiting request with %s.%s()", wrk_id,
96 96
 		py_entrypoint, PYENTRY_FUNNAME);
97 97
 
@@ -100,7 +100,7 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
100 100
 	if(semop(semid, &sop, 1) < 0)
101 101
 	{
102 102
 		err = errno;
103
-		syslog(LOG_ERR,
103
+		pyfcgi_log(LOG_ERR,
104 104
 		       "Worker[%d] error incrementing the semaphore : %s",
105 105
 		       wrk_id, strerror(err));
106 106
 	}
@@ -108,7 +108,7 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
108 108
 	if(pipe(pipe_ctl) == -1)
109 109
 	{
110 110
 		err = errno;
111
-		syslog(LOG_ALERT,
111
+		pyfcgi_log(LOG_ALERT,
112 112
 		       "Worker[%d] Pipe fails for piper ctl : %s",
113 113
 		       wrk_id, strerror(err));
114 114
 		exit(err);
@@ -123,14 +123,14 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
123 123
 		if(semop(semid, &sop, 1) < 0)
124 124
 		{
125 125
 			err = errno;
126
-			syslog(LOG_ERR,
126
+			pyfcgi_log(LOG_ERR,
127 127
 			       "Worker[%d] error decrementing the semaphore : %s",
128 128
 			       wrk_id, strerror(err));
129 129
 		}
130 130
 
131 131
 		count++;
132 132
 		/*
133
-		syslog(	LOG_INFO,
133
+		pyfcgi_log(	LOG_INFO,
134 134
 			"Worker[%d] request %d", wrk_id, count);
135 135
 		*/
136 136
 		piper_args.out = out_stream;
@@ -150,7 +150,7 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
150 150
 		if(pid < 0)
151 151
 		{
152 152
 			err = errno;
153
-			syslog(LOG_ALERT,
153
+			pyfcgi_log(LOG_ALERT,
154 154
 			       "Worker[%d] req #%d Fork failed for piper : %s",
155 155
 			       wrk_id, count, strerror(err));
156 156
 			exit(err);
@@ -175,7 +175,7 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
175 175
 		else
176 176
 		{
177 177
 			/*
178
-			syslog(LOG_DEBUG, "Worker[%d] request %d funcall [OK]",
178
+			pyfcgi_log(LOG_DEBUG, "Worker[%d] request %d funcall [OK]",
179 179
 				wrk_id, count);
180 180
 			*/
181 181
 		}
@@ -189,23 +189,23 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
189 189
 		kill(pid, WPIPER_SIG); //indicate child python call ended
190 190
 /*
191 191
 gettimeofday(&stop, NULL);
192
-syslog(LOG_DEBUG, "W%dR%dtimer KIL %ld.%06ld us", wrk_id, count, stop.tv_sec - start.tv_sec, stop.tv_usec - start.tv_usec);
192
+pyfcgi_log(LOG_DEBUG, "W%dR%dtimer KIL %ld.%06ld us", wrk_id, count, stop.tv_sec - start.tv_sec, stop.tv_usec - start.tv_usec);
193 193
 */
194 194
 		waitpid(pid, &piper_status, 0);
195 195
 /*
196 196
 gettimeofday(&stop, NULL);
197
-syslog(LOG_DEBUG, "W%dR%dtimer W8  %ld.%06ld us", wrk_id, count, stop.tv_sec - start.tv_sec, stop.tv_usec - start.tv_usec);
197
+pyfcgi_log(LOG_DEBUG, "W%dR%dtimer W8  %ld.%06ld us", wrk_id, count, stop.tv_sec - start.tv_sec, stop.tv_usec - start.tv_usec);
198 198
 */
199 199
 		if(WIFSIGNALED(piper_status))
200 200
 		{
201
-			syslog(LOG_ERR,
201
+			pyfcgi_log(LOG_ERR,
202 202
 			       "Woker[%d] req #%d piper terminated by sig %d",
203 203
 			       wrk_id, count, WTERMSIG(piper_status));
204 204
 			exit(40);
205 205
 		}
206 206
 		else if(WEXITSTATUS(piper_status))
207 207
 		{
208
-			syslog(LOG_ERR,
208
+			pyfcgi_log(LOG_ERR,
209 209
 			       "Woker[%d] req #%d piper exited with error status %d",
210 210
 			       wrk_id, count, WEXITSTATUS(piper_status));
211 211
 			exit(41);
@@ -221,7 +221,7 @@ syslog(LOG_DEBUG, "W%dR%dtimer W8  %ld.%06ld us", wrk_id, count, stop.tv_sec - s
221 221
 		if(semop(semid, &sop, 1) < 0)
222 222
 		{
223 223
 			err = errno;
224
-			syslog(LOG_ERR,
224
+			pyfcgi_log(LOG_ERR,
225 225
 			       "Worker[%d] error incrementing the semaphore : %s",
226 226
 			       wrk_id, strerror(err));
227 227
 		}
@@ -233,7 +233,7 @@ syslog(LOG_DEBUG, "W%dR%dtimer W8  %ld.%06ld us", wrk_id, count, stop.tv_sec - s
233 233
 			stop.tv_usec += 1000000;
234 234
 			stop.tv_sec -= 1;
235 235
 		}
236
-		syslog(LOG_DEBUG, "Worker[%d] request %d END [OK] %lu bytes in %ld.%06lds",
236
+		pyfcgi_log(LOG_DEBUG, "Worker[%d] request %d END [OK] %lu bytes in %ld.%06lds",
237 237
 			wrk_id, count, rep_sz, stop.tv_sec, stop.tv_usec);
238 238
 	}
239 239
 	free(piper_stack);
@@ -269,14 +269,14 @@ int worker_piper(void *ptr)
269 269
 	if(sigaction(WPIPER_SIG, act, NULL))
270 270
 	{
271 271
 		err = errno;
272
-		syslog(	LOG_ALERT, "Unable to sigaction in piper : %s",
272
+		pyfcgi_log(	LOG_ALERT, "Unable to sigaction in piper : %s",
273 273
 			strerror(err));
274 274
 		exit(err);
275 275
 	}
276 276
 	write(ctl_pipe, " ", 1);
277 277
 
278 278
 
279
-	//syslog(LOG_DEBUG, "Worker[%d] req #%d piper", wrk_id, req_id);
279
+	//pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d piper", wrk_id, req_id);
280 280
 
281 281
 	fds[0].fd = pystderr;
282 282
 	fds[1].fd = pystdout;
@@ -290,7 +290,7 @@ int worker_piper(void *ptr)
290 290
 	while(cont)
291 291
 	{
292 292
 		poll_ret = poll(fds, nfds, 0);
293
-//syslog(LOG_DEBUG, "Worker[%d] req #%d poll_ret = %d", wrk_id, req_id, poll_ret);
293
+//pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d poll_ret = %d", wrk_id, req_id, poll_ret);
294 294
 		if(poll_ret < 0)
295 295
 		{
296 296
 			err = errno;
@@ -298,7 +298,7 @@ int worker_piper(void *ptr)
298 298
 			{
299 299
 				continue;
300 300
 			}
301
-			syslog( LOG_WARNING, "Poll error : %s",
301
+			pyfcgi_log( LOG_WARNING, "Poll error : %s",
302 302
 			        strerror(err));
303 303
 			fds[0].revents = fds[1].revents = 0;
304 304
 			continue;
@@ -311,19 +311,19 @@ int worker_piper(void *ptr)
311 311
 		if(poll_ret && (revents = fds[1].revents))
312 312
 		{
313 313
 			/*
314
-			syslog(LOG_DEBUG, "Worker[%d] req #%d STDOUT evt !",
314
+			pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d STDOUT evt !",
315 315
 			       wrk_id, req_id);
316 316
 			*/
317 317
 			poll_ret--;
318 318
 			if(revents & POLLIN)
319 319
 			{
320 320
 				/*
321
-				syslog(LOG_DEBUG, "Worker[%d] req #%d POLLIN STDOUT !",
321
+				pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d POLLIN STDOUT !",
322 322
 				       wrk_id, req_id);
323 323
 				*/
324 324
 				ret = read(pystdout, buf, PIPE_BUF);
325 325
 				/*
326
-				syslog(LOG_DEBUG, "Worker[%d] req #%d read(stdout) ret %d",
326
+				pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d read(stdout) ret %d",
327 327
 				       wrk_id, req_id, ret);
328 328
 				*/
329 329
 				if(ret < 0)
@@ -333,7 +333,7 @@ int worker_piper(void *ptr)
333 333
 					{
334 334
 						continue;
335 335
 					}
336
-					syslog( LOG_ERR,
336
+					pyfcgi_log( LOG_ERR,
337 337
 						"Error reading python stdout : %s",
338 338
 						strerror(err));
339 339
 					exit(err);
@@ -355,13 +355,13 @@ int worker_piper(void *ptr)
355 355
 		if(poll_ret && (revents = fds[0].revents))
356 356
 		{
357 357
 			/*
358
-			syslog(LOG_DEBUG, "Worker[%d] req #%d STDERR evt !",
358
+			pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d STDERR evt !",
359 359
 			       wrk_id, req_id);
360 360
 			*/
361 361
 			if(revents & POLLIN)
362 362
 			{
363 363
 				/*
364
-				syslog(LOG_DEBUG, "Worker[%d] req #%d POLLIN STDERR !",
364
+				pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d POLLIN STDERR !",
365 365
 				       wrk_id, req_id);
366 366
 				*/
367 367
 				while(1)
@@ -374,13 +374,13 @@ int worker_piper(void *ptr)
374 374
 						{
375 375
 							continue;
376 376
 						}
377
-						syslog( LOG_ERR,
377
+						pyfcgi_log( LOG_ERR,
378 378
 						        "Error reading python stdout : %s",
379 379
 							strerror(err));
380 380
 						exit(err);
381 381
 					}
382 382
 					buf[ret] = '\0';
383
-					syslog(LOG_ERR,
383
+					pyfcgi_log(LOG_ERR,
384 384
 					       "Worker[%d] Python error : %s",
385 385
 					       wrk_id, buf);
386 386
 					break;
@@ -391,7 +391,7 @@ int worker_piper(void *ptr)
391 391
 		fds[0].revents = fds[1].revents = 0;
392 392
 	}
393 393
 	/*
394
-	syslog(LOG_DEBUG, "Worker[%d] req #%d piper exiting...",
394
+	pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d piper exiting...",
395 395
 	       wrk_id, req_id);
396 396
 	*/
397 397
 	if(!closed)
@@ -406,7 +406,7 @@ int worker_piper(void *ptr)
406 406
 void worker_piper_sighandler(int signum)
407 407
 {
408 408
 	worker_piper_sigrcv = 1;
409
-	//syslog(LOG_DEBUG, "SIG");
409
+	//pyfcgi_log(LOG_DEBUG, "SIG");
410 410
 }
411 411
 
412 412
 int ctl_get_rep_sz(int ctl_pipe, size_t* rep_sz)
@@ -421,25 +421,25 @@ int ctl_get_rep_sz(int ctl_pipe, size_t* rep_sz)
421 421
 	if( (ret = poll(&fds, 1, 0)) < 0)
422 422
 	{
423 423
 		err = errno;
424
-		syslog(LOG_ERR, "Failed to poll ctl pipe : %s",
424
+		pyfcgi_log(LOG_ERR, "Failed to poll ctl pipe : %s",
425 425
 		       strerror(err));
426 426
 		return -1;
427 427
 	}
428 428
 	if(!ret)
429 429
 	{
430
-		syslog(LOG_ERR, "No data in ctl pipe for rep_sz...");
430
+		pyfcgi_log(LOG_ERR, "No data in ctl pipe for rep_sz...");
431 431
 		return -1;
432 432
 	}
433 433
 	ret = read(ctl_pipe, rep_sz, sizeof(size_t));
434 434
 	if(ret < 0)
435 435
 	{
436
-		syslog(LOG_ERR, "Error reading ctl pipe : %s",
436
+		pyfcgi_log(LOG_ERR, "Error reading ctl pipe : %s",
437 437
 		       strerror(errno));
438 438
 		return -1;
439 439
 	}
440 440
 	if(ret < sizeof(size_t))
441 441
 	{
442
-		syslog(LOG_ERR, "Incomplete read from ctl pipe, no rep_sz...");
442
+		pyfcgi_log(LOG_ERR, "Incomplete read from ctl pipe, no rep_sz...");
443 443
 		return -1;
444 444
 	}
445 445
 	return 0;
@@ -455,10 +455,10 @@ PyObject* import_entrypoint(char* py_entrypoint)
455 455
 	if(!entry_module)
456 456
 	{
457 457
 		//TODO syslog python error / traceback
458
-		syslog(	LOG_CRIT,
458
+		pyfcgi_log(	LOG_CRIT,
459 459
 			"Unable to import python file '%s'",
460 460
 			py_entrypoint);
461
-		syslog( LOG_INFO,
461
+		pyfcgi_log( LOG_INFO,
462 462
 			"%s", getcwd(NULL, 256));
463 463
 		log_expt(LOG_ERR);
464 464
 		sleep(1);
@@ -472,14 +472,14 @@ PyObject* import_entrypoint(char* py_entrypoint)
472 472
 	if(!entry_fun)
473 473
 	{
474 474
 		//TODO syslog python error / traceback
475
-		syslog(	LOG_CRIT,
475
+		pyfcgi_log(	LOG_CRIT,
476 476
 			"Unable to import object '%s' from '%s'",
477 477
 			PYENTRY_FUNNAME, py_entrypoint);
478 478
 		Py_Exit(2);
479 479
 	}
480 480
 	if(!PyCallable_Check(entry_fun))
481 481
 	{
482
-		syslog( LOG_CRIT,
482
+		pyfcgi_log( LOG_CRIT,
483 483
 			"When imported from '%s', '%s' was not a callable",
484 484
 			py_entrypoint, PYENTRY_FUNNAME);
485 485
 		Py_Exit(3);
@@ -493,7 +493,7 @@ static PyObject* _fetch_pyflush(const char *fdname)
493 493
 	pyfd = PySys_GetObject(fdname);
494 494
 	if(!pyfd)
495 495
 	{
496
-		syslog(LOG_ALERT, "Unable to fetch sys.%s", fdname);
496
+		pyfcgi_log(LOG_ALERT, "Unable to fetch sys.%s", fdname);
497 497
 		log_expt(LOG_ALERT);
498 498
 		Py_Exit(EXIT_PYERR);
499 499
 	}
@@ -501,13 +501,13 @@ static PyObject* _fetch_pyflush(const char *fdname)
501 501
 	Py_DECREF(pyfd);
502 502
 	if(!pyflush)
503 503
 	{
504
-		syslog(LOG_ALERT, "Unable to fetch sys.%s.flush", fdname);
504
+		pyfcgi_log(LOG_ALERT, "Unable to fetch sys.%s.flush", fdname);
505 505
 		log_expt(LOG_ALERT);
506 506
 		Py_Exit(EXIT_PYERR);
507 507
 	}
508 508
 	if(!PyCallable_Check(pyflush))
509 509
 	{
510
-		syslog(LOG_ALERT, "sys.%s.flush is not callable !",
510
+		pyfcgi_log(LOG_ALERT, "sys.%s.flush is not callable !",
511 511
 		       fdname);
512 512
 		Py_Exit(EXIT_PYERR);
513 513
 	}
@@ -581,7 +581,7 @@ void update_python_path()
581 581
 		}
582 582
 		else
583 583
 		{
584
-			syslog(LOG_ALERT, "Unable to fetch python path, got NULL.");
584
+			pyfcgi_log(LOG_ALERT, "Unable to fetch python path, got NULL.");
585 585
 		}
586 586
 		err_fmt = NULL;
587 587
 		err = 0;
@@ -622,7 +622,7 @@ update_python_path_err_cwd:
622 622
 update_python_path_err:
623 623
 	if(err_fmt)
624 624
 	{
625
-		syslog( LOG_ALERT, err_fmt, strerror(err));
625
+		pyfcgi_log( LOG_ALERT, err_fmt, strerror(err));
626 626
 	}
627 627
 	exit(err);
628 628
 }
@@ -657,7 +657,7 @@ void update_python_fd(int pipe_out[2], int pipe_err[2])
657 657
 		}
658 658
 		else
659 659
 		{
660
-			syslog(	LOG_ALERT,
660
+			pyfcgi_log(	LOG_ALERT,
661 661
 				"Unable to import python os module, got NULL.");
662 662
 		}
663 663
 		err_fmt = NULL;
@@ -673,7 +673,7 @@ void update_python_fd(int pipe_out[2], int pipe_err[2])
673 673
 		}
674 674
 		else
675 675
 		{
676
-			syslog(	LOG_ALERT,
676
+			pyfcgi_log(	LOG_ALERT,
677 677
 				"Unable to fetch os.fdopen() , got NULL.");
678 678
 		}
679 679
 		err_fmt = NULL;
@@ -683,7 +683,7 @@ void update_python_fd(int pipe_out[2], int pipe_err[2])
683 683
 	args = Py_BuildValue("is", pipe_out[1], "w");
684 684
 	if(!args)
685 685
 	{
686
-		syslog(	LOG_ERR, "Error building values with '%d', '%s' for stdout",
686
+		pyfcgi_log(	LOG_ERR, "Error building values with '%d', '%s' for stdout",
687 687
 			pipe_out[1], "w");
688 688
 		log_expt(LOG_ALERT);
689 689
 		err_fmt = NULL;
@@ -692,7 +692,7 @@ void update_python_fd(int pipe_out[2], int pipe_err[2])
692 692
 	new_fd = PyObject_CallObject(pyfdopen, args);
693 693
 	if(!new_fd || PyErr_Occurred())
694 694
 	{
695
-		syslog(	LOG_ERR, "Error calling fdopen(%d, '%s')",
695
+		pyfcgi_log(	LOG_ERR, "Error calling fdopen(%d, '%s')",
696 696
 			pipe_out[1], "w");
697 697
 		log_expt(LOG_ALERT);
698 698
 		err_fmt = NULL;
@@ -701,7 +701,7 @@ void update_python_fd(int pipe_out[2], int pipe_err[2])
701 701
 	Py_DECREF(args);
702 702
 	if(PySys_SetObject("stdout", new_fd))
703 703
 	{
704
-		syslog(LOG_ERR, "Unable to set sys.stdout");
704
+		pyfcgi_log(LOG_ERR, "Unable to set sys.stdout");
705 705
 		log_expt(LOG_ALERT);
706 706
 		goto update_python_fd_err_newfd;
707 707
 	}
@@ -710,7 +710,7 @@ void update_python_fd(int pipe_out[2], int pipe_err[2])
710 710
 	args = Py_BuildValue("is", pipe_err[1], "w");
711 711
 	if(!args)
712 712
 	{
713
-		syslog(	LOG_ERR, "Error building values with '%d', '%s' for stderr",
713
+		pyfcgi_log(	LOG_ERR, "Error building values with '%d', '%s' for stderr",
714 714
 			pipe_out[1], "w");
715 715
 		log_expt(LOG_ALERT);
716 716
 		err_fmt = NULL;
@@ -719,7 +719,7 @@ void update_python_fd(int pipe_out[2], int pipe_err[2])
719 719
 	new_fd = PyObject_CallObject(pyfdopen, args);
720 720
 	if(!new_fd || PyErr_Occurred())
721 721
 	{
722
-		syslog(	LOG_ERR, "Error calling fdopen(%d, '%s')",
722
+		pyfcgi_log(	LOG_ERR, "Error calling fdopen(%d, '%s')",
723 723
 			pipe_out[1], "w");
724 724
 		log_expt(LOG_ALERT);
725 725
 		err_fmt = NULL;
@@ -728,7 +728,7 @@ void update_python_fd(int pipe_out[2], int pipe_err[2])
728 728
 	Py_DECREF(args);
729 729
 	if(PySys_SetObject("stderr", new_fd))
730 730
 	{
731
-		syslog(LOG_ERR, "Unable to set sys.stderr");
731
+		pyfcgi_log(LOG_ERR, "Unable to set sys.stderr");
732 732
 		log_expt(LOG_ALERT);
733 733
 		goto update_python_fd_err_newfd;
734 734
 	}
@@ -751,7 +751,7 @@ update_python_fd_err_pipeout:
751 751
 update_python_fd_err:
752 752
 	if(err_fmt)
753 753
 	{
754
-		syslog(pri, err_fmt, strerror(err));
754
+		pyfcgi_log(pri, err_fmt, strerror(err));
755 755
 	}
756 756
 	exit(1);
757 757
 }
@@ -767,7 +767,7 @@ void update_pyenv(PyObject *py_osmod, char **environ)
767 767
 	pyenv = PyObject_GetAttrString(py_osmod, "environ");
768 768
 	if(!pyenv)
769 769
 	{
770
-		syslog(LOG_WARNING, "Unable to get os.environ");
770
+		pyfcgi_log(LOG_WARNING, "Unable to get os.environ");
771 771
 		log_expt(LOG_ALERT);
772 772
 	}
773 773
 	else
@@ -787,19 +787,19 @@ void update_pyenv(PyObject *py_osmod, char **environ)
787 787
 		}
788 788
 		if(!*value)
789 789
 		{
790
-			syslog(LOG_WARNING, "Droping environment value without value : '%s'",
790
+			pyfcgi_log(LOG_WARNING, "Droping environment value without value : '%s'",
791 791
 			       key);
792 792
 			cur++;
793 793
 			continue;
794 794
 		}
795 795
 		value++;
796 796
 		*(value-1) = '\0'; // dirty modification of **environ
797
-//syslog(LOG_DEBUG, "PySetEnv '%s'='%s'", key, value);
797
+//pyfcgi_log(LOG_DEBUG, "PySetEnv '%s'='%s'", key, value);
798 798
 		pykey = PyUnicode_DecodeLocale(key, "surrogateescape");
799 799
 		if(!pykey)
800 800
 		{
801 801
 			*(value-1) = '='; // **environ restore
802
-			syslog(LOG_ALERT, "Unable to parse environ key string '%s'",
802
+			pyfcgi_log(LOG_ALERT, "Unable to parse environ key string '%s'",
803 803
 			       key);
804 804
 			log_expt(LOG_ALERT);
805 805
 			Py_Exit(EXIT_PYERR);
@@ -808,14 +808,14 @@ void update_pyenv(PyObject *py_osmod, char **environ)
808 808
 		pyval = PyUnicode_DecodeFSDefault(value);
809 809
 		if(!pyval)
810 810
 		{
811
-			syslog(LOG_ALERT, "Unable to parse environ val string '%s'",
811
+			pyfcgi_log(LOG_ALERT, "Unable to parse environ val string '%s'",
812 812
 			       value);
813 813
 			log_expt(LOG_ALERT);
814 814
 			Py_Exit(EXIT_PYERR);
815 815
 		}
816 816
 		if(PyDict_SetItem(pyenv, pykey, pyval) == -1)
817 817
 		{
818
-			syslog(LOG_ERR, "Unable to set environ '%s'='%s'",
818
+			pyfcgi_log(LOG_ERR, "Unable to set environ '%s'='%s'",
819 819
 			       key, value);
820 820
 			log_expt(LOG_ERR);
821 821
 		}
@@ -831,7 +831,7 @@ void log_expt(int priority)
831 831
 {
832 832
 	if(!PyErr_Occurred())
833 833
 	{
834
-		syslog(priority, "No exception");
834
+		pyfcgi_log(priority, "No exception");
835 835
 		return;
836 836
 	}
837 837
 
@@ -847,7 +847,7 @@ void log_expt(int priority)
847 847
 	expt_str = PyObject_GetAttrString(expt, "__str__");
848 848
 	if(!expt_str)
849 849
 	{
850
-		syslog(LOG_ERR, "Unable to fetch __str__ from exception");
850
+		pyfcgi_log(LOG_ERR, "Unable to fetch __str__ from exception");
851 851
 	}
852 852
 	expt_val = PyObject_CallObject(expt_str, NULL);
853 853
 	expt_bytes = PyUnicode_AsUTF8String(expt_val);
@@ -866,5 +866,5 @@ void log_expt(int priority)
866 866
 	msg = malloc(sizeof(char) * msg_sz);
867 867
 	snprintf(msg, msg_sz, msg_fmt, type, val);
868 868
 
869
-	syslog(priority, msg);
869
+	pyfcgi_log(priority, msg);
870 870
 }

+ 20
- 20
src/responder.c ファイルの表示

@@ -44,7 +44,7 @@ int responder_loop(char *py_entrypoint, unsigned int max_reqs,
44 44
 	timeout.tv_nsec = 100000000;
45 45
 	idle = 0;
46 46
 
47
-	syslog(LOG_INFO, "Preparing workers");
47
+	pyfcgi_log(LOG_INFO, "Preparing workers");
48 48
 
49 49
 	init_context();
50 50
 
@@ -52,7 +52,7 @@ int responder_loop(char *py_entrypoint, unsigned int max_reqs,
52 52
 	if(!wrk_pids)
53 53
 	{
54 54
 		err = errno;
55
-		syslog(	LOG_ALERT,
55
+		pyfcgi_log(	LOG_ALERT,
56 56
 			"Unable to allocate memory for childs PID : %s",
57 57
 			strerror(err));
58 58
 		clean_exit(err);
@@ -94,7 +94,7 @@ int responder_loop(char *py_entrypoint, unsigned int max_reqs,
94 94
 			}
95 95
 			if(n == n_wrk)
96 96
 			{
97
-				syslog(LOG_WARNING,
97
+				pyfcgi_log(LOG_WARNING,
98 98
 				       "Child %d stopped but was notregistered",
99 99
 				       ret);
100 100
 				continue;
@@ -106,48 +106,48 @@ int responder_loop(char *py_entrypoint, unsigned int max_reqs,
106 106
 			if(ret < 0)
107 107
 			{
108 108
 				err = errno;
109
-				syslog(LOG_ALERT,
109
+				pyfcgi_log(LOG_ALERT,
110 110
 				       "Unable to dec sem after child exit : %s",
111 111
 				       strerror(err));
112 112
 				clean_exit(err);
113 113
 			}
114 114
 			if(status)
115 115
 			{
116
-				syslog(LOG_WARNING,
116
+				pyfcgi_log(LOG_WARNING,
117 117
 				       "Worker[%d] exited with status %d",
118 118
 				       n, status);
119 119
 			}
120 120
 			else
121 121
 			{
122
-				syslog(LOG_INFO,
122
+				pyfcgi_log(LOG_INFO,
123 123
 				       "Worker[%d] PID %d exited normally",
124 124
 				       n, wrk_pids[n]);
125 125
 			}
126 126
 			// child stopped, looking for it
127 127
 			if(wanted_n < n_wrk)
128 128
 			{	// need to shift the list and dec n_wrk
129
-				syslog(LOG_DEBUG, "GC Workers");
130
-syslog( LOG_DEBUG, "GC want %d have %d", wanted_n, n_wrk);
129
+				pyfcgi_log(LOG_DEBUG, "GC Workers");
130
+pyfcgi_log( LOG_DEBUG, "GC want %d have %d", wanted_n, n_wrk);
131 131
 				memmove(wrk_pids+n, wrk_pids+n+1,
132 132
 				        sizeof(pid_t) * (n_wrk - n));
133 133
 				n_wrk--;
134 134
 			}
135 135
 			else
136 136
 			{	// respawn on same slot
137
-				syslog(LOG_INFO, "respawn #%d", n);
137
+				pyfcgi_log(LOG_INFO, "respawn #%d", n);
138 138
 				wrk_pids[n] = spawn(py_entrypoint, n,
139 139
 				                    semid, max_reqs);
140 140
 				continue;
141 141
 			}
142 142
 		}
143 143
 		ret = semtimedop(semid, &sop, 1, &timeout);
144
-//syslog( LOG_DEBUG, "semtimeop ret=%d want %d have %d", ret, wanted_n, n_wrk);
144
+//pyfcgi_log( LOG_DEBUG, "semtimeop ret=%d want %d have %d", ret, wanted_n, n_wrk);
145 145
 		if(ret < 0)
146 146
 		{
147 147
 			err = errno;
148 148
 			if(err == EAGAIN)
149 149
 			{
150
-//syslog(LOG_DEBUG, "IDLE want %d have %d\t min=%d", wanted_n, n_wrk, min_wrk);
150
+//pyfcgi_log(LOG_DEBUG, "IDLE want %d have %d\t min=%d", wanted_n, n_wrk, min_wrk);
151 151
 				// workers idle
152 152
 				if(!idle)
153 153
 				{
@@ -159,13 +159,13 @@ syslog( LOG_DEBUG, "GC want %d have %d", wanted_n, n_wrk);
159 159
 				}
160 160
 				continue;
161 161
 			}
162
-			syslog(LOG_ERR, "Unable to read semaphore : %s",
162
+			pyfcgi_log(LOG_ERR, "Unable to read semaphore : %s",
163 163
 			       strerror(err));
164 164
 		}
165 165
 		if(!ret && n_wrk < max_wrk)
166 166
 		{
167 167
 			idle=0;
168
-			syslog( LOG_DEBUG,
168
+			pyfcgi_log( LOG_DEBUG,
169 169
 				"All workers busy, spawning a new one");
170 170
 			n = n_wrk;
171 171
 			n_wrk++;
@@ -179,11 +179,11 @@ syslog( LOG_DEBUG, "GC want %d have %d", wanted_n, n_wrk);
179 179
 	for(n_wrk=0; n_wrk != min_wrk; n_wrk++)
180 180
 	{
181 181
 		waitpid(wrk_pids[n_wrk], &status, 0);
182
-		syslog(LOG_DEBUG, "Child %d stopped with status %d",
182
+		pyfcgi_log(LOG_DEBUG, "Child %d stopped with status %d",
183 183
 		       wrk_pids[n_wrk], status);
184 184
 	}
185 185
 		//printf("Content-Type: text/html\r\n\r\nHello world !\n");
186
-	syslog(LOG_INFO,"Child workers stoped, stopping responder");
186
+	pyfcgi_log(LOG_INFO,"Child workers stoped, stopping responder");
187 187
 	exit(0);
188 188
 }
189 189
 
@@ -198,7 +198,7 @@ pid_t spawn(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
198 198
 	res = fork();
199 199
 	if(res == -1)
200 200
 	{
201
-		syslog(	LOG_ERR, "Fork fails for worker #%d : %s",
201
+		pyfcgi_log(	LOG_ERR, "Fork fails for worker #%d : %s",
202 202
 			wrk_id, strerror(errno));
203 203
 		return -1;
204 204
 	}
@@ -211,7 +211,7 @@ pid_t spawn(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
211 211
 	// busy. Let some time to this one to go up...
212 212
 	// TODO: find a better way to avoid spawning to max_wrk
213 213
 	nanosleep(&timeout, NULL);
214
-	syslog(	LOG_INFO,
214
+	pyfcgi_log(	LOG_INFO,
215 215
 		"Worker #%d spawned with PID %d", wrk_id, res);
216 216
 	return res;
217 217
 }
@@ -225,14 +225,14 @@ int new_semaphore(key_t semkey)
225 225
 	if(semid == -1)
226 226
 	{
227 227
 		err = errno;
228
-		syslog(	LOG_ALERT,
228
+		pyfcgi_log(	LOG_ALERT,
229 229
 			"Unable to create semaphore : %s",
230 230
 			strerror(err));
231 231
 		clean_exit(err);
232 232
 	}
233 233
 	if(pyfcgi_semid)
234 234
 	{
235
-		syslog(	LOG_WARNING,
235
+		pyfcgi_log(	LOG_WARNING,
236 236
 			"The semid context was not zero when calling new_semaphore, attempt to closeing it.");
237 237
 		semctl(pyfcgi_semid, 0, IPC_RMID);
238 238
 
@@ -245,7 +245,7 @@ void clean_exit(int status)
245 245
 {
246 246
 	if(pyfcgi_semid && semctl(pyfcgi_semid, 0, IPC_RMID) == -1)
247 247
 	{
248
-		syslog(	LOG_CRIT,
248
+		pyfcgi_log(	LOG_CRIT,
249 249
 			"Unable to delete semaphore before exiting.");
250 250
 	}
251 251
 	exit(status);

読み込み中…
キャンセル
保存