]> git.wh0rd.org - tt-rss.git/blobdiff - include/functions.php
pass logfile to child tasks if locking is possible, lock logfile before writing,...
[tt-rss.git] / include / functions.php
index 2d6716b8160768f6b296caf760638d556a6e3736..8ede14a0bfa7a188b036d8ac704aff70dd7c22a9 100644 (file)
         * @return void
         */
        function _debug($msg, $show = true) {
+               if (defined('SUPPRESS_DEBUGGING'))
+                       return false;
 
                $ts = strftime("%H:%M:%S", time());
                if (function_exists('posix_getpid')) {
                        $fp = fopen(LOGFILE, 'a+');
 
                        if ($fp) {
+                               $locked = false;
+
+                               if (function_exists("flock")) {
+                                       $tries = 0;
+
+                                       // try to lock logfile for writing
+                                       while ($tries < 5 && !$locked = flock($fp, LOCK_EX | LOCK_NB)) {
+                                               sleep(1);
+                                               ++$tries;
+                                       }
+
+                                       if (!$locked) {
+                                               fclose($fp);
+                                               return;
+                                       }
+                               }
+
                                fputs($fp, "[$ts] $msg\n");
+
+                               if (function_exists("flock")) {
+                                       flock($fp, LOCK_UN);
+                               }
+
                                fclose($fp);
                        }
                }