/src/php-src/ext/opcache/zend_accelerator_debug.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend OPcache | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright © The PHP Group and Contributors. | |
6 | | +----------------------------------------------------------------------+ |
7 | | | This source file is subject to the Modified BSD License that is | |
8 | | | bundled with this package in the file LICENSE, and is available | |
9 | | | through the World Wide Web at <https://www.php.net/license/>. | |
10 | | | | |
11 | | | SPDX-License-Identifier: BSD-3-Clause | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Authors: Andi Gutmans <andi@php.net> | |
14 | | | Zeev Suraski <zeev@php.net> | |
15 | | | Stanislav Malyshev <stas@zend.com> | |
16 | | | Dmitry Stogov <dmitry@php.net> | |
17 | | +----------------------------------------------------------------------+ |
18 | | */ |
19 | | |
20 | | #include <stdio.h> |
21 | | #include <stdlib.h> |
22 | | #include <stdarg.h> |
23 | | #include <time.h> |
24 | | #ifdef ZEND_WIN32 |
25 | | # include <process.h> |
26 | | #endif |
27 | | #include "ZendAccelerator.h" |
28 | | |
29 | | static void zend_accel_error_va_args(int type, const char *format, va_list args) |
30 | 101k | { |
31 | 101k | time_t timestamp; |
32 | 101k | char *time_string; |
33 | 101k | FILE * fLog = NULL; |
34 | | |
35 | 101k | if (type <= ZCG(accel_directives).log_verbosity_level) { |
36 | |
|
37 | 0 | timestamp = time(NULL); |
38 | 0 | time_string = asctime(localtime(×tamp)); |
39 | 0 | time_string[24] = 0; |
40 | |
|
41 | 0 | if (!ZCG(accel_directives).error_log || |
42 | 0 | !*ZCG(accel_directives).error_log || |
43 | 0 | strcmp(ZCG(accel_directives).error_log, "stderr") == 0) { |
44 | |
|
45 | 0 | fLog = stderr; |
46 | 0 | } else { |
47 | 0 | fLog = fopen(ZCG(accel_directives).error_log, "a"); |
48 | 0 | if (!fLog) { |
49 | 0 | fLog = stderr; |
50 | 0 | } |
51 | 0 | } |
52 | |
|
53 | | #ifdef ZTS |
54 | | fprintf(fLog, "%s (" ZEND_ULONG_FMT "): ", time_string, (zend_ulong)tsrm_thread_id()); |
55 | | #else |
56 | 0 | fprintf(fLog, "%s (%d): ", time_string, getpid()); |
57 | 0 | #endif |
58 | |
|
59 | 0 | switch (type) { |
60 | 0 | case ACCEL_LOG_FATAL: |
61 | 0 | fprintf(fLog, "Fatal Error "); |
62 | 0 | break; |
63 | 0 | case ACCEL_LOG_ERROR: |
64 | 0 | fprintf(fLog, "Error "); |
65 | 0 | break; |
66 | 0 | case ACCEL_LOG_WARNING: |
67 | 0 | fprintf(fLog, "Warning "); |
68 | 0 | break; |
69 | 0 | case ACCEL_LOG_INFO: |
70 | 0 | fprintf(fLog, "Message "); |
71 | 0 | break; |
72 | 0 | case ACCEL_LOG_DEBUG: |
73 | 0 | fprintf(fLog, "Debug "); |
74 | 0 | break; |
75 | 0 | } |
76 | | |
77 | 0 | vfprintf(fLog, format, args); |
78 | 0 | fprintf(fLog, "\n"); |
79 | |
|
80 | 0 | fflush(fLog); |
81 | 0 | if (fLog != stderr) { |
82 | 0 | fclose(fLog); |
83 | 0 | } |
84 | 0 | } |
85 | | /* perform error handling even without logging the error */ |
86 | 101k | switch (type) { |
87 | 0 | case ACCEL_LOG_ERROR: |
88 | 0 | zend_bailout(); |
89 | 0 | break; |
90 | 0 | case ACCEL_LOG_FATAL: |
91 | 0 | exit(-2); |
92 | 0 | break; |
93 | 101k | } |
94 | | |
95 | 101k | } |
96 | | |
97 | | void zend_accel_error(int type, const char *format, ...) |
98 | 101k | { |
99 | 101k | va_list args; |
100 | 101k | va_start(args, format); |
101 | 101k | zend_accel_error_va_args(type, format, args); |
102 | 101k | va_end(args); |
103 | 101k | } |
104 | | |
105 | | ZEND_NORETURN void zend_accel_error_noreturn(int type, const char *format, ...) |
106 | 0 | { |
107 | 0 | va_list args; |
108 | 0 | va_start(args, format); |
109 | 0 | ZEND_ASSERT(type == ACCEL_LOG_FATAL || type == ACCEL_LOG_ERROR); |
110 | 0 | zend_accel_error_va_args(type, format, args); |
111 | 0 | va_end(args); |
112 | | /* Should never reach this. */ |
113 | 0 | abort(); |
114 | 0 | } |