Skip to content

Commit 98dc85a

Browse files
committed
PHPC-256: Productions debug logs
1 parent e75191e commit 98dc85a

File tree

2 files changed

+77
-60
lines changed

2 files changed

+77
-60
lines changed

php_phongo.c

Lines changed: 76 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -209,62 +209,14 @@ static void php_phongo_log(mongoc_log_level_t log_level, const char *log_domain,
209209
case MONGOC_LOG_LEVEL_DEBUG:
210210
case MONGOC_LOG_LEVEL_TRACE:
211211
{
212-
int fd = -1;
213212
time_t t;
214213
char *dt = NULL;
215214

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

231-
if (strcasecmp(MONGODB_G(debug), "stderr") == 0) {
232-
fprintf(stderr, PHONGO_DEBUG_LOG_FORMAT, dt, log_domain, mongoc_log_level_str(log_level), message);
233-
} else if (strcasecmp(MONGODB_G(debug), "stdout") == 0) {
234-
php_printf(PHONGO_DEBUG_LOG_FORMAT, dt, log_domain, mongoc_log_level_str(log_level), message);
235-
} else if (MONGODB_G(debug_filename)) {
236-
fd = VCWD_OPEN_MODE(MONGODB_G(debug_filename), O_CREAT | O_APPEND | O_WRONLY, 0644);
237-
} else {
238-
char *prefix;
239-
int len;
240-
char *filename;
241-
242-
len = spprintf(&prefix, 0, "PHONGO-%ld", t);
243-
244-
if (strcasecmp(MONGODB_G(debug), "on") == 0 || strcasecmp(MONGODB_G(debug), "1") == 0) {
245-
fd = php_open_temporary_fd(NULL, prefix, &filename TSRMLS_CC);
246-
} else {
247-
fd = php_open_temporary_fd(MONGODB_G(debug), prefix, &filename TSRMLS_CC);
248-
}
249-
if (fd != -1) {
250-
MONGODB_G(debug_filename) = pestrdup(filename, 1);
251-
efree(filename);
252-
}
253-
efree(prefix);
254-
}
255-
256-
if (fd != -1) {
257-
char *tmp;
258-
int len;
259-
260-
len = spprintf(&tmp, 0, PHONGO_DEBUG_LOG_FORMAT, dt, log_domain, mongoc_log_level_str(log_level), message);
261-
#ifdef PHP_WIN32
262-
php_flock(fd, 2);
263-
#endif
264-
php_ignore_value(write(fd, tmp, len));
265-
efree(tmp);
266-
close(fd);
267-
}
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));
268220
efree(dt);
269221
} break;
270222
}
@@ -2283,9 +2235,78 @@ void _phongo_debug_bson(bson_t *bson)
22832235

22842236
/* {{{ M[INIT|SHUTDOWN] R[INIT|SHUTDOWN] G[INIT|SHUTDOWN] MINFO INI */
22852237

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+
22862307
/* {{{ INI entries */
22872308
PHP_INI_BEGIN()
2288-
{ 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 },
22892310
PHP_INI_END()
22902311
/* }}} */
22912312

@@ -2298,8 +2319,7 @@ PHP_GINIT_FUNCTION(mongodb)
22982319
php_phongo_realloc,
22992320
php_phongo_free,
23002321
};
2301-
mongodb_globals->debug = NULL;
2302-
mongodb_globals->debug_filename = NULL;
2322+
mongodb_globals->debug_fd = NULL;
23032323
mongodb_globals->bsonMemVTable = bsonMemVTable;
23042324

23052325
}
@@ -2308,8 +2328,6 @@ PHP_GINIT_FUNCTION(mongodb)
23082328
/* {{{ PHP_MINIT_FUNCTION */
23092329
PHP_MINIT_FUNCTION(mongodb)
23102330
{
2311-
void ***ctx = NULL;
2312-
TSRMLS_SET_CTX(ctx);
23132331
(void)type; /* We don't care if we are loaded via dl() or extension= */
23142332

23152333

@@ -2319,7 +2337,6 @@ PHP_MINIT_FUNCTION(mongodb)
23192337
mongoc_init();
23202338
/* Initialize libbson */
23212339
bson_mem_set_vtable(&MONGODB_G(bsonMemVTable));
2322-
mongoc_log_set_handler(php_phongo_log, ctx);
23232340

23242341
/* Prep default object handlers to be used when we register the classes */
23252342
memcpy(&phongo_std_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
@@ -2402,9 +2419,9 @@ PHP_MSHUTDOWN_FUNCTION(mongodb)
24022419
PHP_GSHUTDOWN_FUNCTION(mongodb)
24032420
{
24042421
mongodb_globals->debug = NULL;
2405-
if (mongodb_globals->debug_filename) {
2406-
pefree(mongodb_globals->debug_filename, 1);
2407-
mongodb_globals->debug_filename = NULL;
2422+
if (mongodb_globals->debug_fd) {
2423+
fclose(mongodb_globals->debug_fd);
2424+
mongodb_globals->debug_fd = NULL;
24082425
}
24092426
}
24102427
/* }}} */

php_phongo.h

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)