Skip to content

Commit 60fdf52

Browse files
committed
PHPC-241: mongodb.debug improvements
1 parent 6cbacda commit 60fdf52

File tree

3 files changed

+46
-32
lines changed

3 files changed

+46
-32
lines changed

php_phongo.c

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
/* Stream wrapper */
4949
#include <main/php_streams.h>
5050
#include <main/php_network.h>
51+
/* Debug log writing */
52+
#include <main/php_open_temporary_file.h>
5153
/* Our Compatability header */
5254
#include "phongo_compat.h"
5355

@@ -60,7 +62,7 @@
6062
#undef MONGOC_LOG_DOMAIN
6163
#define MONGOC_LOG_DOMAIN "PHONGO"
6264

63-
#define PHONGO_DEBUG_INI "mongodb.debug_log"
65+
#define PHONGO_DEBUG_INI "mongodb.debug"
6466
#define PHONGO_DEBUG_INI_DEFAULT ""
6567

6668
ZEND_DECLARE_MODULE_GLOBALS(mongodb)
@@ -197,27 +199,51 @@ static void php_phongo_log(mongoc_log_level_t log_level, const char *log_domain,
197199
case MONGOC_LOG_LEVEL_DEBUG:
198200
case MONGOC_LOG_LEVEL_TRACE:
199201
{
200-
int fd = 0;
202+
int fd = -1;
203+
time_t t;
201204
char *dt = NULL;
202205

203-
if (!MONGODB_G(debug_log)) {
206+
if (!MONGODB_G(debug)) {
204207
return;
205208
}
206-
if (strcasecmp(MONGODB_G(debug_log), "off") == 0) {
209+
if (strcasecmp(MONGODB_G(debug), "off") == 0) {
207210
return;
208211
}
209-
if (strcasecmp(MONGODB_G(debug_log), "0") == 0) {
212+
if (strcasecmp(MONGODB_G(debug), "0") == 0) {
210213
return;
211214
}
212215

213216
#define PHONGO_DEBUG_LOG_FORMAT "[%s] %10s: %-8s> %s\n"
214217

215-
dt = php_format_date((char *)"Y-m-d\\TH:i:sP", strlen("Y-m-d\\TH:i:sP"), time(NULL), 1 TSRMLS_CC);
216-
if (strcasecmp(MONGODB_G(debug_log), "stderr") == 0) {
218+
time(&t);
219+
dt = php_format_date((char *)"Y-m-d\\TH:i:sP", strlen("Y-m-d\\TH:i:sP"), t, 1 TSRMLS_CC);
220+
221+
if (strcasecmp(MONGODB_G(debug), "stderr") == 0) {
217222
fprintf(stderr, PHONGO_DEBUG_LOG_FORMAT, dt, log_domain, mongoc_log_level_str(log_level), message);
218-
} else if (strcasecmp(MONGODB_G(debug_log), "stdout") == 0) {
223+
} else if (strcasecmp(MONGODB_G(debug), "stdout") == 0) {
219224
php_printf(PHONGO_DEBUG_LOG_FORMAT, dt, log_domain, mongoc_log_level_str(log_level), message);
220-
} else if ((fd = VCWD_OPEN_MODE(MONGODB_G(debug_log), O_CREAT | O_APPEND | O_WRONLY, 0644)) != -1) {
225+
} else if (MONGODB_G(debug_filename)) {
226+
fd = VCWD_OPEN_MODE(MONGODB_G(debug_filename), O_CREAT | O_APPEND | O_WRONLY, 0644);
227+
} else {
228+
char *prefix;
229+
int len;
230+
char *filename;
231+
232+
len = spprintf(&prefix, 0, "PHONGO-%ld", t);
233+
234+
if (strcasecmp(MONGODB_G(debug), "on") == 0 || strcasecmp(MONGODB_G(debug), "1") == 0) {
235+
fd = php_open_temporary_fd(NULL, prefix, &filename TSRMLS_CC);
236+
} else {
237+
fd = php_open_temporary_fd(MONGODB_G(debug), prefix, &filename TSRMLS_CC);
238+
}
239+
if (fd != -1) {
240+
MONGODB_G(debug_filename) = pestrdup(filename, 1);
241+
efree(filename);
242+
}
243+
efree(prefix);
244+
}
245+
246+
if (fd != -1) {
221247
char *tmp;
222248
int len;
223249

@@ -1720,7 +1746,7 @@ void _phongo_debug_bson(bson_t *bson)
17201746

17211747
/* {{{ INI entries */
17221748
PHP_INI_BEGIN()
1723-
STD_PHP_INI_ENTRY(PHONGO_DEBUG_INI, PHONGO_DEBUG_INI_DEFAULT, PHP_INI_ALL, OnUpdateString, debug_log, zend_mongodb_globals, mongodb_globals)
1749+
STD_PHP_INI_ENTRY(PHONGO_DEBUG_INI, PHONGO_DEBUG_INI_DEFAULT, PHP_INI_ALL, OnUpdateString, debug, zend_mongodb_globals, mongodb_globals)
17241750
PHP_INI_END()
17251751
/* }}} */
17261752

@@ -1733,7 +1759,8 @@ PHP_GINIT_FUNCTION(mongodb)
17331759
php_phongo_realloc,
17341760
php_phongo_free,
17351761
};
1736-
mongodb_globals->debug_log = NULL;
1762+
mongodb_globals->debug = NULL;
1763+
mongodb_globals->debug_filename = NULL;
17371764
mongodb_globals->bsonMemVTable = bsonMemVTable;
17381765

17391766
}
@@ -1833,7 +1860,11 @@ PHP_MSHUTDOWN_FUNCTION(mongodb)
18331860
/* {{{ PHP_GSHUTDOWN_FUNCTION */
18341861
PHP_GSHUTDOWN_FUNCTION(mongodb)
18351862
{
1836-
mongodb_globals->debug_log = NULL;
1863+
mongodb_globals->debug = NULL;
1864+
if (mongodb_globals->debug_filename) {
1865+
pefree(mongodb_globals->debug_filename, 1);
1866+
mongodb_globals->debug_filename = NULL;
1867+
}
18371868
}
18381869
/* }}} */
18391870

php_phongo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ extern zend_module_entry mongodb_module_entry;
5050
#endif
5151

5252
ZEND_BEGIN_MODULE_GLOBALS(mongodb)
53-
char *debug_log;
53+
char *debug;
54+
char *debug_filename;
5455
bson_mem_vtable_t bsonMemVTable;
5556
ZEND_END_MODULE_GLOBALS(mongodb)
5657

tests/utils/basic.inc

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,12 @@ def($servers);
2424
$consts = array(
2525
"DATABASE_NAME" => "phongo",
2626
"COLLECTION_NAME" => makeCollectionNameFromFilename($_SERVER["SCRIPT_FILENAME"]),
27-
"DEBUG_DIR" => sys_get_temp_dir() . "/PHONGO-TESTS/",
2827
);
2928
def($consts);
3029

3130
// These use values from constants defined above
3231
$consts = array(
33-
"NS" => DATABASE_NAME . "." . COLLECTION_NAME,
34-
"DEBUG_FILENAME" => DEBUG_DIR . DATABASE_NAME . "." . COLLECTION_NAME,
32+
"NS" => DATABASE_NAME . "." . COLLECTION_NAME,
3533
);
3634
def($consts);
3735

38-
39-
40-
if (!is_dir(DEBUG_DIR)) {
41-
mkdir(DEBUG_DIR, 0777, true);
42-
}
43-
44-
/* If the INI option hasn't been changed, then lets set it to the debug log */
45-
$ininame = "phongo.debug_log";
46-
$origin = ini_get($ininame);
47-
ini_restore($ininame);
48-
if ($ininame == $origin) {
49-
ini_set("phongo.debug_log", DEBUG_FILENAME);
50-
file_put_contents(DEBUG_FILENAME, sprintf("===> %s <=== %s\n", date(DATE_ISO8601), $_SERVER["SCRIPT_FILENAME"]), FILE_APPEND);
51-
}
52-
53-

0 commit comments

Comments
 (0)