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
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
6668ZEND_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 */
17221748PHP_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 )
17241750PHP_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 */
18341861PHP_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
0 commit comments