Skip to content

Commit 06281f7

Browse files
committed
Merge pull request #121 from bjori/PHPC-256-prod-debug-logs
PHPC-256: Production debug logs
2 parents cb14fad + 98dc85a commit 06281f7

File tree

5 files changed

+87
-63
lines changed

5 files changed

+87
-63
lines changed

php_phongo.c

Lines changed: 80 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
/* PHP array helpers */
5656
#include "php_array_api.h"
5757

58+
/* For our stream verifications */
59+
#include <openssl/x509.h>
60+
5861
/* Our Compatability header */
5962
#include "phongo_compat.h"
6063

@@ -206,62 +209,14 @@ static void php_phongo_log(mongoc_log_level_t log_level, const char *log_domain,
206209
case MONGOC_LOG_LEVEL_DEBUG:
207210
case MONGOC_LOG_LEVEL_TRACE:
208211
{
209-
int fd = -1;
210212
time_t t;
211213
char *dt = NULL;
212214

213-
if (!MONGODB_G(debug) || !strlen(MONGODB_G(debug))) {
214-
return;
215-
}
216-
if (strcasecmp(MONGODB_G(debug), "off") == 0) {
217-
return;
218-
}
219-
if (strcasecmp(MONGODB_G(debug), "0") == 0) {
220-
return;
221-
}
222-
223-
#define PHONGO_DEBUG_LOG_FORMAT "[%s] %10s: %-8s> %s\n"
224-
225215
time(&t);
226216
dt = php_format_date((char *)"Y-m-d\\TH:i:sP", strlen("Y-m-d\\TH:i:sP"), t, 0 TSRMLS_CC);
227217

228-
if (strcasecmp(MONGODB_G(debug), "stderr") == 0) {
229-
fprintf(stderr, PHONGO_DEBUG_LOG_FORMAT, dt, log_domain, mongoc_log_level_str(log_level), message);
230-
} else if (strcasecmp(MONGODB_G(debug), "stdout") == 0) {
231-
php_printf(PHONGO_DEBUG_LOG_FORMAT, dt, log_domain, mongoc_log_level_str(log_level), message);
232-
} else if (MONGODB_G(debug_filename)) {
233-
fd = VCWD_OPEN_MODE(MONGODB_G(debug_filename), O_CREAT | O_APPEND | O_WRONLY, 0644);
234-
} else {
235-
char *prefix;
236-
int len;
237-
char *filename;
238-
239-
len = spprintf(&prefix, 0, "PHONGO-%ld", t);
240-
241-
if (strcasecmp(MONGODB_G(debug), "on") == 0 || strcasecmp(MONGODB_G(debug), "1") == 0) {
242-
fd = php_open_temporary_fd(NULL, prefix, &filename TSRMLS_CC);
243-
} else {
244-
fd = php_open_temporary_fd(MONGODB_G(debug), prefix, &filename TSRMLS_CC);
245-
}
246-
if (fd != -1) {
247-
MONGODB_G(debug_filename) = pestrdup(filename, 1);
248-
efree(filename);
249-
}
250-
efree(prefix);
251-
}
252-
253-
if (fd != -1) {
254-
char *tmp;
255-
int len;
256-
257-
len = spprintf(&tmp, 0, PHONGO_DEBUG_LOG_FORMAT, dt, log_domain, mongoc_log_level_str(log_level), message);
258-
#ifdef PHP_WIN32
259-
php_flock(fd, 2);
260-
#endif
261-
php_ignore_value(write(fd, tmp, len));
262-
efree(tmp);
263-
close(fd);
264-
}
218+
fprintf(MONGODB_G(debug_fd), "[%s] %10s: %-8s> %s\n", dt, log_domain, mongoc_log_level_str(log_level), message);
219+
fflush(MONGODB_G(debug_fd));
265220
efree(dt);
266221
} break;
267222
}
@@ -1553,7 +1508,7 @@ void php_phongo_cursor_to_zval(zval *retval, php_phongo_cursor_t *cursor) /* {{{
15531508
/* }}} */
15541509

15551510

1556-
mongoc_uri_t *php_phongo_make_uri(const char *uri_string, bson_t *options TSRMLS_DC) /* {{{ */
1511+
mongoc_uri_t *php_phongo_make_uri(const char *uri_string, bson_t *options) /* {{{ */
15571512
{
15581513
bson_iter_t iter;
15591514
mongoc_uri_t *uri;
@@ -2280,9 +2235,78 @@ void _phongo_debug_bson(bson_t *bson)
22802235

22812236
/* {{{ M[INIT|SHUTDOWN] R[INIT|SHUTDOWN] G[INIT|SHUTDOWN] MINFO INI */
22822237

2238+
ZEND_INI_MH(OnUpdateDebug)
2239+
{
2240+
void ***ctx = NULL;
2241+
char *tmp_dir = NULL;
2242+
2243+
TSRMLS_SET_CTX(ctx);
2244+
2245+
/* Close any previously open log files */
2246+
if (MONGODB_G(debug_fd)) {
2247+
if (MONGODB_G(debug_fd) != stderr && MONGODB_G(debug_fd) != stdout) {
2248+
fclose(MONGODB_G(debug_fd));
2249+
}
2250+
MONGODB_G(debug_fd) = NULL;
2251+
}
2252+
2253+
if (!new_value_length
2254+
|| strcasecmp("0", new_value) == 0
2255+
|| strcasecmp("off", new_value) == 0
2256+
|| strcasecmp("no", new_value) == 0
2257+
|| strcasecmp("false", new_value) == 0
2258+
) {
2259+
mongoc_log_trace_disable();
2260+
mongoc_log_set_handler(NULL, NULL);
2261+
2262+
return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
2263+
}
2264+
2265+
2266+
if (strcasecmp(new_value, "stderr") == 0) {
2267+
MONGODB_G(debug_fd) = stderr;
2268+
} else if (strcasecmp(new_value, "stdout") == 0) {
2269+
MONGODB_G(debug_fd) = stdout;
2270+
} else if (
2271+
strcasecmp("1", new_value) == 0
2272+
|| strcasecmp("on", new_value) == 0
2273+
|| strcasecmp("yes", new_value) == 0
2274+
|| strcasecmp("true", new_value) == 0
2275+
) {
2276+
tmp_dir = NULL;
2277+
} else {
2278+
tmp_dir = new_value;
2279+
}
2280+
2281+
if (!MONGODB_G(debug_fd)) {
2282+
time_t t;
2283+
int fd = -1;
2284+
char *prefix;
2285+
int len;
2286+
char *filename;
2287+
2288+
time(&t);
2289+
len = spprintf(&prefix, 0, "PHONGO-%ld", t);
2290+
2291+
fd = php_open_temporary_fd(tmp_dir, prefix, &filename TSRMLS_CC);
2292+
if (fd != -1) {
2293+
MONGODB_G(debug_fd) = VCWD_FOPEN(filename, "a");
2294+
}
2295+
efree(filename);
2296+
efree(prefix);
2297+
close(fd);
2298+
}
2299+
2300+
mongoc_log_trace_enable();
2301+
mongoc_log_set_handler(php_phongo_log, ctx);
2302+
2303+
return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
2304+
}
2305+
2306+
22832307
/* {{{ INI entries */
22842308
PHP_INI_BEGIN()
2285-
{ 0, PHP_INI_ALL, (char *)PHONGO_DEBUG_INI, sizeof(PHONGO_DEBUG_INI), OnUpdateString, (void *) XtOffsetOf(zend_mongodb_globals, debug), (void *) &mglo, NULL, (char *)PHONGO_DEBUG_INI_DEFAULT, sizeof(PHONGO_DEBUG_INI_DEFAULT)-1, NULL, 0, 0, 0, NULL },
2309+
{ 0, PHP_INI_ALL, (char *)PHONGO_DEBUG_INI, sizeof(PHONGO_DEBUG_INI), OnUpdateDebug, (void *) XtOffsetOf(zend_mongodb_globals, debug), (void *) &mglo, NULL, (char *)PHONGO_DEBUG_INI_DEFAULT, sizeof(PHONGO_DEBUG_INI_DEFAULT)-1, NULL, 0, 0, 0, NULL },
22862310
PHP_INI_END()
22872311
/* }}} */
22882312

@@ -2295,8 +2319,7 @@ PHP_GINIT_FUNCTION(mongodb)
22952319
php_phongo_realloc,
22962320
php_phongo_free,
22972321
};
2298-
mongodb_globals->debug = NULL;
2299-
mongodb_globals->debug_filename = NULL;
2322+
mongodb_globals->debug_fd = NULL;
23002323
mongodb_globals->bsonMemVTable = bsonMemVTable;
23012324

23022325
}
@@ -2305,8 +2328,6 @@ PHP_GINIT_FUNCTION(mongodb)
23052328
/* {{{ PHP_MINIT_FUNCTION */
23062329
PHP_MINIT_FUNCTION(mongodb)
23072330
{
2308-
void ***ctx = NULL;
2309-
TSRMLS_SET_CTX(ctx);
23102331
(void)type; /* We don't care if we are loaded via dl() or extension= */
23112332

23122333

@@ -2316,7 +2337,6 @@ PHP_MINIT_FUNCTION(mongodb)
23162337
mongoc_init();
23172338
/* Initialize libbson */
23182339
bson_mem_set_vtable(&MONGODB_G(bsonMemVTable));
2319-
mongoc_log_set_handler(php_phongo_log, ctx);
23202340

23212341
/* Prep default object handlers to be used when we register the classes */
23222342
memcpy(&phongo_std_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
@@ -2399,9 +2419,9 @@ PHP_MSHUTDOWN_FUNCTION(mongodb)
23992419
PHP_GSHUTDOWN_FUNCTION(mongodb)
24002420
{
24012421
mongodb_globals->debug = NULL;
2402-
if (mongodb_globals->debug_filename) {
2403-
pefree(mongodb_globals->debug_filename, 1);
2404-
mongodb_globals->debug_filename = NULL;
2422+
if (mongodb_globals->debug_fd) {
2423+
fclose(mongodb_globals->debug_fd);
2424+
mongodb_globals->debug_fd = NULL;
24052425
}
24062426
}
24072427
/* }}} */

php_phongo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extern zend_module_entry mongodb_module_entry;
5252

5353
ZEND_BEGIN_MODULE_GLOBALS(mongodb)
5454
char *debug;
55-
char *debug_filename;
55+
FILE *debug_fd;
5656
bson_mem_vtable_t bsonMemVTable;
5757
ZEND_END_MODULE_GLOBALS(mongodb)
5858

@@ -135,7 +135,7 @@ void php_phongo_cursor_to_zval(zval *retval, php_phongo_cursor_t *cursor);
135135

136136
bool php_phongo_apply_rp_options_to_client(mongoc_client_t *client, bson_t *options TSRMLS_DC);
137137
bool php_phongo_apply_wc_options_to_client(mongoc_client_t *client, bson_t *options TSRMLS_DC);
138-
mongoc_uri_t *php_phongo_make_uri(const char *uri_string, bson_t *options TSRMLS_DC);
138+
mongoc_uri_t *php_phongo_make_uri(const char *uri_string, bson_t *options);
139139
mongoc_client_t *php_phongo_make_mongo_client(const mongoc_uri_t *uri, zval *driverOptions TSRMLS_DC);
140140
void php_phongo_objectid_new_from_oid(zval *object, const bson_oid_t *oid TSRMLS_DC);
141141
void php_phongo_cursor_id_new_from_id(zval *object, int64_t cursorid TSRMLS_DC);

src/MongoDB/Manager.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ PHP_METHOD(Manager, __construct)
8282
zval_to_bson(options, PHONGO_BSON_NONE, &bson_options, NULL TSRMLS_CC);
8383
}
8484

85-
if (!(uri = php_phongo_make_uri(uri_string, &bson_options TSRMLS_CC))) {
85+
if (!(uri = php_phongo_make_uri(uri_string, &bson_options))) {
8686
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Failed to parse MongoDB URI: '%s'", uri_string);
8787
bson_destroy(&bson_options);
8888

tests/server/server-executeCommand-002.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
--TEST--
22
MongoDB\Driver\Server::executeCommand() takes a read preference
3+
--XFAIL--
4+
See PHPC-441: slaveOk bit not set when using hint to query a secondary directly
35
--SKIPIF--
46
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("REPLICASET"); ?>
57
--FILE--

tests/server/server-executeQuery-005.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
--TEST--
22
MongoDB\Driver\Server::executeQuery() takes a read preference
3+
--XFAIL--
4+
See PHPC-441: slaveOk bit not set when using hint to query a secondary directly
35
--SKIPIF--
46
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("REPLICASET"); ?>
57
--FILE--

0 commit comments

Comments
 (0)