BRISKIN5_ to BIN5_ constants prefix and Briskin5 to Bin5 class rename
[brisk.git] / web / Obj / brisk.phh
index cc7ff2c..32936fb 100644 (file)
@@ -808,39 +808,46 @@ class User {
     */
   }
 
-  function step_set($step) {
-    $this->step = $step;
-    
-    do {
-      if (validate_sess($this->sess) == FALSE)
-       break;
-      if (file_exists(PROXY_PATH) == FALSE)
-        mkdir(PROXY_PATH);
-      if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
-       break;
-      fwrite($fp, pack("LL",$this->step, $this->idx));
-      fclose($fp);
-
-      return (TRUE);
-    } while (0);
+  function step_set($step) 
+  {
+      $this->step = $step & 0x7fffffff;
+      
+      do {
+          if (validate_sess($this->sess) == FALSE)
+              break;
+          if (file_exists(PROXY_PATH) == FALSE)
+              mkdir(PROXY_PATH);
+          if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
+              break;
+          fwrite($fp, pack("LL",$this->step, $this->idx));
+          fclose($fp);
 
-    return (FALSE);
+          log_main("step_set [".$this->sess. "] [".$this->step."]"); 
+          
+          return (TRUE);
+      } while (0);
+      
+      return (FALSE);
   }
-
+  
   function step_inc($delta = 1) {
-    $this->step += $delta;
-    
-    if (validate_sess($this->sess)) {
-      if (file_exists(PROXY_PATH) == FALSE)
-        mkdir(PROXY_PATH);
-      $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
-      fwrite($fp, pack("LL", $this->step, $this->idx));
-      fclose($fp);
-
-      return (TRUE);
-    }
-    
-    return (FALSE);
+      $this->step += $delta;
+      /* modularization because unpack() not manage unsigned 32bit int correctly */
+      $this->step &= 0x7fffffff;
+      
+      if (validate_sess($this->sess)) {
+          if (file_exists(PROXY_PATH) == FALSE)
+              mkdir(PROXY_PATH);
+          $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
+          fwrite($fp, pack("LL", $this->step, $this->idx));
+          fclose($fp);
+
+          log_main("step_inc [".$this->sess. "] [".$this->step."]"); 
+          
+          return (TRUE);
+      }
+      
+      return (FALSE);
   }
 
   function reset() {
@@ -864,12 +871,12 @@ class User {
 
 
   // SHSPLIT save and load function for the User class.
-  function load_data($id
+  static function load_data($id, $sess
   {
-    GLOBAL $sess;
-
     // error_log("User::load_data BEGIN", 0);
 
+    log_main("load_data: id [".$id."] sess [".($sess == FALSE ? "FALSE" : $sess)."] ");
+
     $doexit = FALSE;
     do {
       if (($tok = @ftok(FTOK_PATH."/user".$id, "B")) == -1) {
@@ -888,7 +895,11 @@ class User {
       if ($shm = shm_attach($tok, $shm_sz)) {
        $user = @shm_get_var($shm, $tok);
        
-       log_only("bri ==  ".($user == FALSE ?   "FALSE" : "TRUE")."  bri ===  ".($user === FALSE ? "FALSE" : "TRUE")."  bri isset ".(isset($user) ?   "TRUE" : "FALSE"));
+        if ($sess != FALSE && $user->sess != $sess) {
+            $doexit = TRUE;
+            break;
+        }
+       log_only("user ==  ".($user == FALSE ?   "FALSE" : "TRUE")."  user ===  ".($user === FALSE ? "FALSE" : "TRUE")."  user isset ".(isset($user) ?   "TRUE" : "FALSE"));
        if (isset($user)) 
          log_only("bri count ".count($user));
        
@@ -896,7 +907,7 @@ class User {
          log_only("INIT MAIN DATA");
          
           // SHSPLIT FIXME: init_data for User class ??
-         $user =& User::create($id, "", "");
+         $user = User::create($id, "", "");
          if (@shm_put_var($shm, $tok, $user) == FALSE) {
               log_only("PUT_VAR FALLITA ".strlen(serialize($user)));
               log_only(serialize($user));
@@ -921,7 +932,7 @@ class User {
   }
   
 
-  function save_data(&$user, $id) 
+  function save_data($user, $id) 
   {
     GLOBAL $sess;
     
@@ -940,6 +951,7 @@ class User {
       // log_only("PUT_VAR DI ".strlen(serialize($user)));
       if (shm_put_var($shm, $tok, $user) != FALSE) {
        shm_detach($shm);
+       log_main("User[".$id."] saved.");
        return (TRUE);
       }
       if (shm_remove($shm) === FALSE) {
@@ -957,39 +969,39 @@ class User {
   }
 
 
+  static function step_get($sess)
+  {
+      $fp = FALSE;
+      do {
+          if (validate_sess($sess) == FALSE)
+              break;
+          
+          if (file_exists(PROXY_PATH) == FALSE)
+              mkdir(PROXY_PATH);
+          if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
+              break;
+          if (($s = fread($fp, 8)) == FALSE)
+              break;
+          if (strlen($s) != 8)
+              break;
+          $arr = unpack('Ls/Li', $s);
+          fclose($fp);
+          
+          // log_rd2("A0: ".$arr[0]."  A1: ".$arr[1]);
+          return ($arr);
+      } while (0);
+      
+      if ($fp != FALSE)
+          fclose($fp);
+      
+      log_rd2("STEP_GET [".$sess."]: return false ");
+      
+      return (FALSE);
+  }
 
 } // end class User
 
 
-function step_get($sess) {
-  $fp = FALSE;
-  do {
-    if (validate_sess($sess) == FALSE)
-      break;
-
-    if (file_exists(PROXY_PATH) == FALSE)
-      mkdir(PROXY_PATH);
-    if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
-      break;
-    if (($s = fread($fp, 8)) == FALSE)
-      break;
-    if (strlen($s) != 8)
-      break;
-    $arr = unpack('Ls/Li', $s);
-    fclose($fp);
-
-    // log_rd2("A0: ".$arr[0]."  A1: ".$arr[1]);
-    return ($arr);
-  } while (0);
-
-  if ($fp != FALSE)
-    fclose($fp);
-
-  log_rd2("STEP_GET: return false ");
-
-  return (FALSE);
-}
-
 function step_unproxy($sess) {
   log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
   if (file_exists(PROXY_PATH) == FALSE)
@@ -1003,7 +1015,7 @@ class Room {
   var $table;
   var $comm; // commands for many people
   var $step; // current step of the comm array
-  var $garbage_timeout;
+  // externalized var $garbage_timeout;
   var $shm_sz;
 
   function Room () {
@@ -1031,7 +1043,8 @@ class Room {
       else
         $this->table[$i]->auth_only = FALSE;
     }
-    $this->garbage_timeout = 0;
+    // externalized $this->garbage_timeout = 0;
+    Room::garbage_time_expire_set(0);
     $this->shm_sz = SHM_DIMS_MIN;
   }
 
@@ -1045,7 +1058,9 @@ class Room {
 
     /* Garbage collector degli utenti in timeout */
     $curtime = time();
-    if ($force || $this->garbage_timeout < $curtime) {
+
+    // externalized if ($force || $this->garbage_timeout < $curtime) {
+    if ($force || Room::garbage_time_is_expired($curtime)) {
       
       // FIXME BRISK4: include for each kind of table
       require_once("${G_base}briskin5/Obj/briskin5.phh");
@@ -1058,11 +1073,11 @@ class Room {
        if ($table_cur->player_n == PLAYERS_N) {
          log_main("PLAYERS == N TABLE ".$table_idx);
          
-         if (($sem = Briskin5::lock_data($table_idx)) != FALSE) { 
+         if (($sem = Bin5::lock_data($table_idx)) != FALSE) { 
            log_main("bin5 lock data success");
            
            $no_recovery = FALSE;
-           if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
+           if (($bri = Bin5::load_data($table_idx)) != FALSE) {
              if ($table_cur->table_token != $bri->table_token) {
                log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token."  Table: ".$bri->table_token);
                log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start."  Table: ".$bri->table_start);
@@ -1109,7 +1124,7 @@ class Room {
                $this->room_join_wakeup(&$user_cur, FALSE, 0); 
                $table_cur->table_token = "";
                 $table_cur->wakeup_time = $curtime + WAKEUP_TIME;
-               Briskin5::destroy_data($table_idx);
+               Bin5::destroy_data($table_idx);
              }
              else {
                log_main("gm:: save_data");
@@ -1118,9 +1133,9 @@ class Room {
                  $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
                }
              
-               Briskin5::save_data(&$bri);
+               Bin5::save_data(&$bri);
              }
-           } // else if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
+           } // else if (($bri = &Bin5::load_data($table_idx)) != FALSE) {
            else if ($no_recovery == FALSE) {
              log_crit("ERROR: table ".$table_idx." unrecoverable join");
 
@@ -1148,7 +1163,7 @@ class Room {
              $table_cur->table_token = "";
            }
 
-           Briskin5::unlock_data($sem);
+           Bin5::unlock_data($sem);
          } // bri::lock_data
        } //  if ($table_cur->player_n == PLAYERS_N) {
       } //  for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
@@ -1194,7 +1209,8 @@ class Room {
       }
       log_rd2("GARBAGE UPDATED!");
       
-      $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
+      // externalized $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
+      Room::garbage_time_expire_set(time() + GARBAGE_TIMEOUT);
       $ismod = TRUE;
     }
 
@@ -1920,9 +1936,9 @@ class Room {
                 $bri_user->comm[$bri_user->step % COMM_N] .= $to_tabl;
                 $bri_user->step_inc();
               }
-              Briskin5::save_data(&$bri);
+              Bin5::save_data(&$bri);
             }
-            Briskin5::unlock_data($sem);
+            Bin5::unlock_data($sem);
           } // bri::lock_data
         } //  if ($table_cur->player_n == PLAYERS_N) {
       } //  for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
@@ -2076,15 +2092,15 @@ class Room {
       if ($ghost_user->stat == "table" && $this->table[$table_idx]->player_n == PLAYERS_N) {
         // FIXME BRISK4: include for each kind of table
         require_once("${G_base}briskin5/Obj/briskin5.phh");
-        if (($brisem = Briskin5::lock_data($table_idx)) != FALSE) { 
-          if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
+        if (($brisem = Bin5::lock_data($table_idx)) != FALSE) { 
+          if (($bri = &Bin5::load_data($table_idx)) != FALSE) {
             if ($bri->the_end != TRUE) {
               $bri->user[$ghost_user->table_pos]->step_inc();
               $bri->user[$ghost_user->table_pos]->sess = $sess;
-              Briskin5::save_data(&$bri);
+              Bin5::save_data(&$bri);
             }
           }
-          Briskin5::unlock_data($brisem);
+          Bin5::unlock_data($brisem);
         }
       }
 
@@ -2267,7 +2283,7 @@ class Room {
           shm_detach($shm);
 
           for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
-              $room->user[$i] = User::load_data($i);
+              $room->user[$i] = User::load_data($i, FALSE);
           }
       }
       
@@ -2365,32 +2381,33 @@ class Room {
           User::save_data($user_park[$i], $i);
           $room->user[$i] = $user_park[$i];
       }
+      log_load("FINISH: ".($ret == TRUE ? "TRUE" : "FALSE"));
 
       return ($ret);
   }
 
-  function lock_data()
+  static function lock_data()
   {
-    GLOBAL $sess; 
-    
-    //  echo "LOCK: ".FTOK_PATH."/main";
-    //  exit;
-    if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
-      return (FALSE);
-    }
-    // echo "FTOK ".$tok."<br>";
-    if (($res = sem_get($tok)) == FALSE) {
-      return (FALSE);
-    }
-    if (sem_acquire($res)) {   
-      log_lock("LOCK room");
-      return ($res);
-    }
-    else
-      return (FALSE);
+      GLOBAL $sess; 
+      
+      //  echo "LOCK: ".FTOK_PATH."/main";
+      //  exit;
+      if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
+          return (FALSE);
+      }
+      // echo "FTOK ".$tok."<br>";
+      if (($res = sem_get($tok)) == FALSE) {
+          return (FALSE);
+      }
+      if (sem_acquire($res)) {   
+          log_lock("LOCK room");
+          return ($res);
+      }
+      else
+          return (FALSE);
   }
   
-  function unlock_data($res)
+  static function unlock_data($res)
   {
     GLOBAL $sess; 
     
@@ -2474,6 +2491,49 @@ class Room {
     return ($ret);
   }
 
+  static function garbage_time_is_expired($tm) 
+  {
+      $ret = TRUE;
+      $fp = FALSE;
+      do {
+          if (file_exists(PROXY_PATH) == FALSE)
+              mkdir(PROXY_PATH);
+          if (($fp = @fopen(PROXY_PATH."/room_garbage_time.expired", 'rb')) == FALSE)
+              break;
+          if (($s = fread($fp, 4)) == FALSE)
+              break;
+          if (strlen($s) != 4)
+              break;
+          $arr = unpack('Le', $s);
+          if ($arr['e'] > $tm)
+              $ret = FALSE;
+      } while (0);
+      
+      if ($fp != FALSE)
+          fclose($fp);
+      
+      log_rd2("END: return ".($ret ? "TRUE" : "FALSE"));
+      
+      return ($ret);
+  }
+
+  static function garbage_time_expire_set($tm) 
+  {
+      do {
+          if (file_exists(PROXY_PATH) == FALSE)
+              mkdir(PROXY_PATH);
+          if (($fp = @fopen(PROXY_PATH."/room_garbage_time.expired", 'wb')) == FALSE)
+              break;
+          fwrite($fp, pack("L",$tm));
+          fclose($fp);
+          
+          return (TRUE);
+      } while (0);
+      
+      return (FALSE);
+  }
+
+
 } // end class Room
 
 function make_seed()
@@ -2482,240 +2542,282 @@ function make_seed()
   return (float) $sec + ((float) $usec * 100000);
 }
 
+function btrace_line($ar)
+{
+    GLOBAL $G_btrace_pref_sub;
 
-function log_mop($step, $log) {
-  GLOBAL $sess, $PHP_SELF;
-
-  if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LMOP) == 0)
-    return;
-
-  if (isset($sess) == FALSE)
-    $ssess = "XXXX";
-  else
-    $ssess = $sess;
-
-  if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LMOP) == 0)
-    return;
-
-  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("LMOP: [%f] [%05d] [%s] [%s]\n", gettimeofday(TRUE), $step, $PHP_SELF, $log));
-    fclose($fp);
-  }
+    $ret = "";
+    for ($i = 0 ; $i < count($ar) ; $i++) {
+        $with_class = isset($ar[$i]['class']);
+        $with_file  = isset($ar[$i]['file']);
+        $ret .= sprintf("%s%s%s (%s:%d)", ($i == 0 ? "" : ", "), 
+                        ($with_class ?  $ar[$i]['class'].$ar[$i]['type'] : ""), 
+                        $ar[$i]['function'], ($with_file ? str_replace($G_btrace_pref_sub, "", $ar[$i]['file']) : ""), 
+                        ($with_file ? $ar[$i]['line'] : ""));
+    }
+    
+    return ($ret);
 }
 
-
-function log_only2($log) {
-  GLOBAL $sess, $PHP_SELF;
-
-  if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
-    return;
-
-  if (isset($sess) == FALSE)
-    $ssess = "XXXX";
-  else
-    $ssess = $sess;
-
-  if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
-    return;
-
-  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("ONL2: [%s] [%s] [%s]\n", $ssess, $PHP_SELF, $log));
-    fclose($fp);
-  }
+function log_mop($step, $log)
+{
+    GLOBAL $sess, $PHP_SELF;
+    
+    if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LMOP) == 0)
+        return;
+    
+    if (isset($sess) == FALSE)
+        $ssess = "XXXX";
+    else
+        $ssess = $sess;
+    
+    if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LMOP) == 0)
+        return;
+    
+    $btrace = btrace_line(debug_backtrace());
+    if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+        fwrite($fp, sprintf("LMOP: [%f] [%05d] [%s] [%s]\n", gettimeofday(TRUE), $step, $log, $btrace));
+        fclose($fp);
+    }
 }
 
-function log_crit($log) {
-  GLOBAL $sess, $PHP_SELF;
 
-  if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_CRIT) == 0)
-    return;
-
-  if (isset($sess) == FALSE)
-    $ssess = "XXXX";
-  else
-    $ssess = $sess;
-
-  if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_CRIT) == 0)
-    return;
-
-  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("CRIT: [%s] [%s] [%s]\n", $ssess, $PHP_SELF, $log));
-    fclose($fp);
-  }
+function log_only2($log)
+{
+    GLOBAL $sess, $PHP_SELF;
+    
+    if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
+        return;
+    
+    if (isset($sess) == FALSE)
+        $ssess = "XXXX";
+    else
+        $ssess = $sess;
+    
+    if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
+        return;
+    
+    $btrace = btrace_line(debug_backtrace());
+    if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+        fwrite($fp, sprintf("ONL2: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
+        fclose($fp);
+    }
 }
 
-function log_only($log) {
-  GLOBAL $sess, $PHP_SELF;
-
-  if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
-    return;
-
-  if (isset($sess) == FALSE)
-    $ssess = "XXXX";
-  else
-    $ssess = $sess;
-
-  if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
-    return;
-
-  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("ONLY: [%s] [%s] [%s]\n", $ssess, $PHP_SELF, $log));
-    fclose($fp);
-  }
+function log_crit($log)
+{
+    GLOBAL $sess, $PHP_SELF;
+    
+    if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_CRIT) == 0)
+        return;
+    
+    if (isset($sess) == FALSE)
+        $ssess = "XXXX";
+    else
+        $ssess = $sess;
+    
+    if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_CRIT) == 0)
+        return;
+    
+    $btrace = btrace_line(debug_backtrace());
+    if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+        fwrite($fp, sprintf("CRIT: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
+        fclose($fp);
+    }
 }
 
-function log_main($log) {
+function log_only($log)
+{
     GLOBAL $sess, $PHP_SELF;
-
-  if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
-    return;
-
-  if (isset($sess) == FALSE)
-    $ssess = "XXXX";
-  else
-    $ssess = $sess;
-
-  if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
-    return;
-
-  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("MAIN: [%s] [%s] [%s]\n", $ssess, $PHP_SELF, $log));
-    fclose($fp);
-  }
+    
+    if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
+        return;
+    
+    if (isset($sess) == FALSE)
+        $ssess = "XXXX";
+    else
+        $ssess = $sess;
+    
+    if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
+        return;
+    
+    $btrace = btrace_line(debug_backtrace());
+    if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+        fwrite($fp, sprintf("ONLY: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
+        fclose($fp);
+    }
 }
 
-function log_rd($log) {
-  GLOBAL $sess, $PHP_SELF;
-
-  if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
-    return;
-
-  if (isset($sess) == FALSE)
-    $ssess = "XXXX";
-  else
-    $ssess = $sess;
-
-  if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
-    return;
-
-  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("READ: [%s] [%s] [%s]\n", $ssess, $PHP_SELF, $log));
-    fclose($fp);
-  }
+function log_main($log)
+{
+    GLOBAL $sess, $PHP_SELF;
+    
+    if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
+        return;
+    
+    if (isset($sess) == FALSE)
+        $ssess = "XXXX";
+    else
+        $ssess = $sess;
+    
+    if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
+        return;
+    
+    $btrace = btrace_line(debug_backtrace());
+    if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+        fwrite($fp, sprintf("MAIN: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
+        fclose($fp);
+    }
 }
 
-function log_rd2($log) {
-  GLOBAL $sess, $PHP_SELF;
-
-  if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
-    return;
-
-  if (isset($sess) == FALSE)
-    $ssess = "XXXX";
-  else
-    $ssess = $sess;
-      
-  if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
-    return;
-
-  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("REA2: [%s] [%s] [%s]\n", $ssess, $PHP_SELF, $log));
-    fclose($fp);
-  }
+function log_rd($log)
+{
+    GLOBAL $sess, $PHP_SELF;
+    
+    if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
+        return;
+    
+    if (isset($sess) == FALSE)
+        $ssess = "XXXX";
+    else
+        $ssess = $sess;
+    
+    if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
+        return;
+    
+    $btrace = btrace_line(debug_backtrace());
+    if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+        fwrite($fp, sprintf("READ: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
+        fclose($fp);
+    }
 }
 
-function log_send($log) {
-  GLOBAL $sess, $PHP_SELF;
-
-  if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
-    return;
-
-  if (isset($sess) == FALSE)
-    $ssess = "XXXX";
-  else
-    $ssess = $sess;
-      
-  if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
-    return;
-
-  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("SEND: [%s] [%s] [%s]\n", $ssess, $PHP_SELF, $log));
-    fclose($fp);
-  }
+function log_rd2($log)
+{
+    GLOBAL $sess, $PHP_SELF;
+    
+    if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
+        return;
+    
+    if (isset($sess) == FALSE)
+        $ssess = "XXXX";
+    else
+        $ssess = $sess;
+    
+    if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
+        return;
+    
+    $btrace = btrace_line(debug_backtrace());
+    
+    if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+        fwrite($fp, sprintf("REA2: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
+        fclose($fp);
+    }
 }
 
-function log_lock($log) {
-  GLOBAL $sess, $PHP_SELF;
-
-  if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
-    return;
-
-  if (isset($sess) == FALSE)
-    $ssess = "XXXX";
-  else
-    $ssess = $sess;
-
-  if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
-    return;
-
-  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("LOCK: [%s] [%s] [%s]\n", $ssess, $PHP_SELF, $log));
-    fclose($fp);
-  }
+function log_send($log)
+{
+    GLOBAL $sess, $PHP_SELF;
+    
+    if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
+        return;
+    
+    if (isset($sess) == FALSE)
+        $ssess = "XXXX";
+    else
+        $ssess = $sess;
+    
+    if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
+        return;
+    
+    $btrace = btrace_line(debug_backtrace());
+    if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+        fwrite($fp, sprintf("SEND: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
+        fclose($fp);
+    }
 }
 
-function log_wr($log) {
-  GLOBAL $sess, $PHP_SELF;
-
-  if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
-    return;
-
-  if (isset($sess) == FALSE)
-    $ssess = "XXXX";
-  else
-    $ssess = $sess;
-      
-  if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
-    return;
-
-  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("WRIT: [%s] [%s] [%s]\n", $ssess, $PHP_SELF, $log));
-    fclose($fp);
-  }
+function log_lock($log)
+{
+    GLOBAL $sess, $PHP_SELF;
+    
+    if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
+        return;
+    
+    if (isset($sess) == FALSE)
+        $ssess = "XXXX";
+    else
+        $ssess = $sess;
+    
+    if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
+        return;
+    
+    $btrace = btrace_line(debug_backtrace());
+    if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+        fwrite($fp, sprintf("LOCK: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
+        fclose($fp);
+    }
 }
 
-function log_load($log) {
-  GLOBAL $sess, $PHP_SELF;
-
-  if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
-    return;
-
-  if (isset($sess) == FALSE)
-    $ssess = "XXXX";
-  else
-    $ssess = $sess;
+function log_wr($log)
+{
+    GLOBAL $sess, $PHP_SELF;
+    
+    if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
+        return;
+    
+    if (isset($sess) == FALSE)
+        $ssess = "XXXX";
+    else
+        $ssess = $sess;
+    
+    if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
+        return;
+    
+    $btrace = btrace_line(debug_backtrace());
+    if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+        fwrite($fp, sprintf("WRIT: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
+        fclose($fp);
+    }
+}
 
-  if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
-    return;
-      
-  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("LOAD: [%s] [%s] [%s]\n", $ssess, $PHP_SELF, $log));
-    fclose($fp);
-  }
+function log_load($log)
+{
+    GLOBAL $sess, $PHP_SELF;
+    
+    if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
+        return;
+    
+    if (isset($sess) == FALSE)
+        $ssess = "XXXX";
+    else
+        $ssess = $sess;
+    
+    if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
+        return;
+    
+    $btrace = btrace_line(debug_backtrace());
+    if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+        fwrite($fp, sprintf("LOAD: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
+        fclose($fp);
+    }
 }
 
-function log_auth($sess, $log) {
+function log_auth($sess, $log)
+{
     GLOBAL $PHP_SELF;
-  if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
-    return;
 
-  if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
-    return;
-
-  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("AUTH: [%s] [%d] [%s] [%s]\n", $sess, time(), $PHP_SELF, $log));
-    fclose($fp);
-  }
+    if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
+        return;
+    
+    if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
+        return;
+    
+    $btrace = btrace_line(debug_backtrace());
+    if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+        fwrite($fp, sprintf("LOAD: [%s] [%d] [%s] [%s]\n", $sess, time(), $log, $btrace));
+        fclose($fp);
+    }
 }