Skip to content

Commit 972d7c7

Browse files
committed
stream: add initial stream errors API
1 parent b3061df commit 972d7c7

File tree

10 files changed

+794
-141
lines changed

10 files changed

+794
-141
lines changed

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,7 @@ PHP_ADD_SOURCES([main/streams], m4_normalize([
16951695
memory.c
16961696
mmap.c
16971697
plain_wrapper.c
1698+
stream_errors.c
16981699
streams.c
16991700
transports.c
17001701
userspace.c

ext/standard/basic_functions.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ PHP_MINIT_FUNCTION(basic) /* {{{ */
336336
#endif
337337
BASIC_MINIT_SUBMODULE(exec)
338338

339+
BASIC_MINIT_SUBMODULE(stream_errors)
339340
BASIC_MINIT_SUBMODULE(user_streams)
340341

341342
php_register_url_stream_wrapper("php", &php_stream_php_wrapper);

ext/standard/file.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ PHPAPI PHP_FUNCTION(ftell);
3434
PHPAPI PHP_FUNCTION(fseek);
3535
PHPAPI PHP_FUNCTION(fpassthru);
3636

37+
PHP_MINIT_FUNCTION(stream_errors);
3738
PHP_MINIT_FUNCTION(user_streams);
3839

3940
PHPAPI zend_result php_copy_file(const char *src, const char *dest);
@@ -100,7 +101,8 @@ typedef struct {
100101
php_stream_context *default_context;
101102
HashTable *stream_wrappers; /* per-request copy of url_stream_wrappers_hash */
102103
HashTable *stream_filters; /* per-request copy of stream_filters_hash */
103-
HashTable *wrapper_errors; /* key: wrapper address; value: linked list of char* */
104+
HashTable *wrapper_logged_errors; /* key: wrapper address; value: linked list of error entries */
105+
HashTable *wrapper_stored_errors; /* key: wrapper address; value: linked list of error entries */
104106
int pclose_wait;
105107
#ifdef HAVE_GETHOSTBYNAME_R
106108
struct hostent tmp_host_info;

main/php_streams.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ struct _php_stream {
248248
#endif
249249

250250
struct _php_stream *enclosing_stream; /* this is a private stream owned by enclosing_stream */
251+
252+
zend_llist *error_list;
251253
}; /* php_stream */
252254

253255
#define PHP_STREAM_CONTEXT(stream) \
@@ -539,6 +541,7 @@ PHPAPI ssize_t _php_stream_passthru(php_stream * src STREAMS_DC);
539541
#define php_stream_passthru(stream) _php_stream_passthru((stream) STREAMS_CC)
540542
END_EXTERN_C()
541543

544+
#include "streams/php_stream_errors.h"
542545
#include "streams/php_stream_transport.h"
543546
#include "streams/php_stream_plain_wrapper.h"
544547
#include "streams/php_stream_glob_wrapper.h"

main/streams/php_stream_errors.h

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#ifndef PHP_STREAM_ERRORS_H
2+
#define PHP_STREAM_ERRORS_H
3+
4+
#include "php.h"
5+
#include "php_streams.h"
6+
7+
BEGIN_EXTERN_C()
8+
9+
/* Error mode context options */
10+
#define PHP_STREAM_ERROR_MODE_ERROR 0
11+
#define PHP_STREAM_ERROR_MODE_EXCEPTION 1
12+
#define PHP_STREAM_ERROR_MODE_SILENT 2
13+
14+
/* Error store context options */
15+
#define PHP_STREAM_ERROR_STORE_AUTO 0
16+
#define PHP_STREAM_ERROR_STORE_NONE 1
17+
#define PHP_STREAM_ERROR_STORE_NON_TERM 2
18+
#define PHP_STREAM_ERROR_STORE_TERMINAL 3
19+
#define PHP_STREAM_ERROR_STORE_ALL 4
20+
21+
/* Error code definition for registration */
22+
typedef struct {
23+
int code;
24+
const char *name;
25+
} php_stream_error_code_def;
26+
27+
/* Stored error entry */
28+
typedef struct {
29+
zend_string *message;
30+
int code;
31+
const char *wrapper_name; /* Points to wrapper->wops->label, no need to duplicate */
32+
const char *param; /* Points to passed string, caller manages lifetime for storage */
33+
int severity;
34+
bool terminal;
35+
} php_stream_error_entry;
36+
37+
/* Sentinel for error code array termination */
38+
#define PHP_STREAM_ERROR_CODE_END {0, NULL}
39+
40+
/* Error code registration */
41+
PHPAPI void php_stream_wrapper_register_error_codes(
42+
php_stream_wrapper *wrapper,
43+
const php_stream_error_code_def *codes
44+
);
45+
46+
PHPAPI const char *php_stream_wrapper_get_error_name(
47+
php_stream_wrapper *wrapper,
48+
int code
49+
);
50+
51+
/* Main error reporting functions */
52+
PHPAPI void php_stream_wrapper_error(
53+
php_stream_wrapper *wrapper,
54+
php_stream_context *context,
55+
int options,
56+
int severity,
57+
bool terminal,
58+
int code,
59+
const char *fmt,
60+
...
61+
) ZEND_ATTRIBUTE_FORMAT(printf, 7, 8);
62+
63+
PHPAPI void php_stream_wrapper_error_param(
64+
php_stream_wrapper *wrapper,
65+
php_stream_context *context,
66+
int options,
67+
int severity,
68+
bool terminal,
69+
int code,
70+
const char *param,
71+
const char *fmt,
72+
...
73+
) ZEND_ATTRIBUTE_FORMAT(printf, 8, 9);
74+
75+
PHPAPI void php_stream_wrapper_error_param2(
76+
php_stream_wrapper *wrapper,
77+
php_stream_context *context,
78+
int options,
79+
int severity,
80+
bool terminal,
81+
int code,
82+
const char *param1,
83+
const char *param2,
84+
const char *fmt,
85+
...
86+
) ZEND_ATTRIBUTE_FORMAT(printf, 9, 10);
87+
88+
PHPAPI void php_stream_error(
89+
php_stream *stream,
90+
int severity,
91+
bool terminal,
92+
int code,
93+
const char *fmt,
94+
...
95+
) ZEND_ATTRIBUTE_FORMAT(printf, 5, 6);
96+
97+
/* Legacy wrapper error log - updated API */
98+
PHPAPI void php_stream_wrapper_log_error(
99+
const php_stream_wrapper *wrapper,
100+
int options,
101+
int severity,
102+
bool terminal,
103+
int code,
104+
const char *fmt,
105+
...
106+
) ZEND_ATTRIBUTE_FORMAT(printf, 6, 7);
107+
108+
PHPAPI void php_stream_wrapper_log_error_param(
109+
const php_stream_wrapper *wrapper,
110+
int options,
111+
int severity,
112+
bool terminal,
113+
int code,
114+
const char *param,
115+
const char *fmt,
116+
...
117+
) ZEND_ATTRIBUTE_FORMAT(printf, 7, 8);
118+
119+
PHPAPI void php_stream_display_wrapper_errors(
120+
php_stream_wrapper *wrapper,
121+
const char *path,
122+
const char *caption
123+
);
124+
125+
PHPAPI void php_stream_tidy_wrapper_error_log(php_stream_wrapper *wrapper);
126+
127+
void php_stream_display_wrapper_errors(php_stream_wrapper *wrapper, const char *path, const char *caption);
128+
void php_stream_tidy_wrapper_error_log(php_stream_wrapper *wrapper);
129+
130+
/* Convenience macros */
131+
#define php_stream_wrapper_warn(wrapper, context, options, code, ...) \
132+
php_stream_wrapper_error(wrapper, context, options, E_WARNING, true, code, __VA_ARGS__)
133+
134+
#define php_stream_wrapper_warn_nt(wrapper, context, options, code, ...) \
135+
php_stream_wrapper_error(wrapper, context, options, E_WARNING, false, code, __VA_ARGS__)
136+
137+
#define php_stream_wrapper_notice(wrapper, context, options, code, ...) \
138+
php_stream_wrapper_error(wrapper, context, options, E_NOTICE, false, code, __VA_ARGS__)
139+
140+
#define php_stream_wrapper_warn_param(wrapper, context, options, code, param, ...) \
141+
php_stream_wrapper_error_param(wrapper, context, options, E_WARNING, true, code, param, __VA_ARGS__)
142+
143+
#define php_stream_warn(stream, code, ...) \
144+
php_stream_error(stream, E_WARNING, true, code, __VA_ARGS__)
145+
146+
#define php_stream_warn_nt(stream, code, ...) \
147+
php_stream_error(stream, E_WARNING, false, code, __VA_ARGS__)
148+
149+
#define php_stream_notice(stream, code, ...) \
150+
php_stream_error(stream, E_NOTICE, false, code, __VA_ARGS__)
151+
152+
#define php_stream_fatal(stream, code, ...) \
153+
php_stream_error(stream, E_ERROR, true, code, __VA_ARGS__)
154+
155+
/* Legacy log variants */
156+
#define php_stream_wrapper_log_warn(wrapper, options, code, ...) \
157+
php_stream_wrapper_log_error(wrapper, options, E_WARNING, true, code, __VA_ARGS__)
158+
159+
#define php_stream_wrapper_log_warn_nt(wrapper, options, code, ...) \
160+
php_stream_wrapper_log_error(wrapper, options, E_WARNING, false, code, __VA_ARGS__)
161+
162+
#define php_stream_wrapper_log_notice(wrapper, options, code, ...) \
163+
php_stream_wrapper_log_error(wrapper, options, E_NOTICE, false, code, __VA_ARGS__)
164+
165+
END_EXTERN_C()
166+
167+
#endif /* PHP_STREAM_ERRORS_H */

0 commit comments

Comments
 (0)