r82012 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82011‎ | r82012 | r82013 >
Date:12:34, 12 February 2011
Author:catrope
Status:resolved (Comments)
Tags:
Comment:
wmerrors: Add basic support for outputting backtraces. Because PHP is very unhelpful when it comes to formatting backtraces (the formatting code is inside debug_print_backtrace() and can't be used modularly other than by overwriting the global write function), I chose to obtain an array-formatted backtrace and format it using zend_print_zval_r_ex() instead, which results in print_r()-style output. Still TODO: output to UDP, and document my code
Modified paths:
  • /trunk/php/wmerrors/php_wmerrors.h (modified) (history)
  • /trunk/php/wmerrors/wmerrors.c (modified) (history)

Diff [purge]

Index: trunk/php/wmerrors/wmerrors.c
@@ -57,6 +57,7 @@
5858 {
5959 wmerrors_globals->message_file = NULL;
6060 wmerrors_globals->logging_file = NULL;
 61+ wmerrors_globals->logfile_stream = NULL;
6162 }
6263
6364 PHP_MINIT_FUNCTION(wmerrors)
@@ -143,9 +144,14 @@
144145 WMERRORS_G(old_error_cb)(type, error_filename, error_lineno, format, args);
145146 }
146147
 148+static int wmerrors_write_trace(const char *str, uint str_length) {
 149+ php_write((void *)str, str_length TSRMLS_CC);
 150+ php_stream_write(WMERRORS_G(logfile_stream), str, str_length TSRMLS_CC);
 151+}
 152+
147153 static void wmerrors_show_message(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args TSRMLS_DC)
148154 {
149 - php_stream *stream, *log_stream;
 155+ php_stream *stream;
150156 char *message, *p;
151157 int message_len;
152158 long maxlen = PHP_STREAM_COPY_ALL;
@@ -153,6 +159,7 @@
154160 int tmp1_len, tmp2_len;
155161 smart_str expanded = {0};
156162 va_list my_args;
 163+ zval *trace;
157164
158165 /* Is there a sane message_file? */
159166 if (!WMERRORS_G(message_file) || *WMERRORS_G(message_file) == '\0') {
@@ -167,10 +174,10 @@
168175 }
169176
170177 /* Try opening the logging file */
171 - log_stream = NULL;
 178+ WMERRORS_G(logfile_stream) = NULL;
172179 if (WMERRORS_G(logging_file) && *WMERRORS_G(logging_file) != '\0')
173180 {
174 - log_stream = php_stream_open_wrapper(WMERRORS_G(logging_file), "ab",
 181+ WMERRORS_G(logfile_stream) = php_stream_open_wrapper(WMERRORS_G(logging_file), "ab",
175182 ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
176183 }
177184
@@ -224,8 +231,8 @@
225232 /* Write the message out */
226233 if (expanded.c) {
227234 /*php_write(expanded.c, expanded.len TSRMLS_CC);*/
228 - if (log_stream) {
229 - php_stream_write(log_stream, expanded.c, expanded.len TSRMLS_CC);
 235+ if (WMERRORS_G(logfile_stream)) {
 236+ php_stream_write(WMERRORS_G(logfile_stream), expanded.c, expanded.len TSRMLS_CC);
230237 }
231238 php_write(expanded.c, expanded.len TSRMLS_CC);
232239 }
@@ -234,6 +241,13 @@
235242 smart_str_free(&expanded);
236243 efree(message);
237244 va_end(my_args);
 245+
 246+ /* Write a backtrace */
 247+ ALLOC_ZVAL(trace);
 248+ Z_UNSET_ISREF_P(trace);
 249+ Z_SET_REFCOUNT_P(trace, 0);
 250+ zend_fetch_debug_backtrace(trace, 0, 0 TSRMLS_CC);
 251+ zend_print_zval_r_ex(wmerrors_write_trace, trace, 4);
238252 }
239253
240254
Index: trunk/php/wmerrors/php_wmerrors.h
@@ -27,6 +27,7 @@
2828 char * logging_file;
2929 int recursion_guard;
3030 int enabled;
 31+ php_stream *logfile_stream;
3132 ZEND_END_MODULE_GLOBALS(wmerrors)
3233
3334

Follow-up revisions

RevisionCommit summaryAuthorDate
r82015Removed some leaks (internal to the request)....platonides14:49, 12 February 2011

Comments

#Comment by Platonides (talk | contribs)   14:52, 12 February 2011

Leaks zvals and the stream until the request cleanup.

Dereferences NULL when there's no file configured or it can't be open.

Fixed in r82015

Status & tagging log