1 /*
2    +----------------------------------------------------------------------+
3    | Zend OPcache                                                         |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    |          Stanislav Malyshev <stas@zend.com>                          |
18    |          Dmitry Stogov <dmitry@php.net>                              |
19    +----------------------------------------------------------------------+
20 */
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <time.h>
26 #ifdef ZEND_WIN32
27 # include <process.h>
28 #endif
29 #include "ZendAccelerator.h"
30 
zend_accel_error(int type,const char * format,...)31 void zend_accel_error(int type, const char *format, ...)
32 {
33 	va_list args;
34 	time_t timestamp;
35 	char *time_string;
36 	FILE * fLog = NULL;
37 
38 	if (type <= ZCG(accel_directives).log_verbosity_level) {
39 
40 	timestamp = time(NULL);
41 	time_string = asctime(localtime(&timestamp));
42 	time_string[24] = 0;
43 
44 	if (!ZCG(accel_directives).error_log ||
45 		!*ZCG(accel_directives).error_log ||
46 		strcmp(ZCG(accel_directives).error_log, "stderr") == 0) {
47 
48 		fLog = stderr;
49 	} else {
50 		fLog = fopen(ZCG(accel_directives).error_log, "a+");
51 		if (!fLog) {
52 			fLog = stderr;
53 		}
54 	}
55 
56 #ifdef ZTS
57 		fprintf(fLog, "%s (" ZEND_ULONG_FMT "): ", time_string, (zend_ulong)tsrm_thread_id());
58 #else
59 		fprintf(fLog, "%s (%d): ", time_string, getpid());
60 #endif
61 
62 		switch (type) {
63 			case ACCEL_LOG_FATAL:
64 				fprintf(fLog, "Fatal Error ");
65 				break;
66 			case ACCEL_LOG_ERROR:
67 				fprintf(fLog, "Error ");
68 				break;
69 			case ACCEL_LOG_WARNING:
70 				fprintf(fLog, "Warning ");
71 				break;
72 			case ACCEL_LOG_INFO:
73 				fprintf(fLog, "Message ");
74 				break;
75 			case ACCEL_LOG_DEBUG:
76 				fprintf(fLog, "Debug ");
77 				break;
78 		}
79 
80 		va_start(args, format);
81 		vfprintf(fLog, format, args);
82 		va_end(args);
83 		fprintf(fLog, "\n");
84 
85 		fflush(fLog);
86 		if (fLog != stderr) {
87 			fclose(fLog);
88 		}
89 	}
90 	/* perform error handling even without logging the error */
91 	switch (type) {
92 		case ACCEL_LOG_ERROR:
93 			zend_bailout();
94 			break;
95 		case ACCEL_LOG_FATAL:
96 			exit(-2);
97 			break;
98 	}
99 
100 }
101