示例#1
0
文件: logger.php 项目: nicolaisi/adei
 function LogMessage($msg, $req = NULL, $extra = NULL, $priority = LOG_WARNING, $source = NULL)
 {
     global $TMP_PATH;
     global $ADEI_SETUP;
     global $ADEI_SESSION;
     global $ADEI_RUNDIR;
     global $LOGGER_STORE_OBJECTS;
     $tod = gettimeofday();
     if ($req instanceof REQUEST) {
         $location = $req->GetLocationString();
     } else {
         if ($req instanceof READER) {
             $location = $req->req->GetLocationString();
         } else {
             if ($req instanceof CACHE) {
                 $location = $req->req->GetLocationString();
             } else {
                 if ($req instanceof EXPORT) {
                     $location = $req->req->GetLocationString();
                 } else {
                     if ($req instanceof DRAW) {
                         $location = $req->req->GetLocationString();
                     } else {
                         if ($req === NULL) {
                             $req = new REQUEST();
                             $location = $req->GetLocationString();
                         } else {
                             $location = false;
                         }
                     }
                 }
             }
         }
     }
     if (!$source) {
         $source = basename($_SERVER['SCRIPT_NAME'], ".php");
         if (preg_match("/services\\/(service).php\$/", $_SERVER['SCRIPT_NAME'], $m)) {
             $source = strtoupper($m[1]) . "(" . $_GET["service"] . ")";
         } else {
             if (preg_match("/(system|service|admin)\\/{$source}\\.php\$/", $_SERVER['SCRIPT_NAME'], $m) || preg_match("/(system|services|admin)\$/", $ADEI_RUNDIR, $m)) {
                 $source = strtoupper($m[1]) . "({$source})";
             } else {
                 $source = "ADEI({$source})";
             }
         }
     }
     if ($extra && is_array($extra)) {
         $info = $extra;
     } else {
         $info = array();
     }
     if ($_SERVER['REQUEST_URI']) {
         $info['request'] = $_SERVER['REQUEST_URI'];
     }
     if ($_GET) {
         $info['GET'] =& $_GET;
     }
     if ($_POST) {
         $info['POST'] =& $_POST;
     }
     if ($_SERVER) {
         $info['REQUEST_DETAILS'] =& $_SERVER;
     }
     if ($HTTP_RAW_POST_DATA) {
         $info['RAW_DATA'] =& $HTTP_RAW_POST_DATA;
     }
     if ($LOGGER_STORE_OBJECTS && $req && is_object($req)) {
         try {
             /* DSToDo: somehow handle PDO exception (not-serializable) and serialize
                everything besides it */
             $info['object'] = $this->EncodeObject($req);
         } catch (Exception $ex) {
             if (is_object($req->req)) {
                 try {
                     $info['object'] = $this->DecodeObject($req->req);
                 } catch (Exception $ex2) {
                 }
             }
         }
     }
     $pid = getmypid();
     if ($ADEI_SETUP) {
         $setup = $ADEI_SETUP;
     } else {
         $setup = "-";
     }
     if ($ADEI_SESSION) {
         $session = $ADEI_SESSION;
     } else {
         $session = "-";
     }
     if ($_SERVER['HTTP_X_FORWARDED_FOR']) {
         $client = $_SERVER['HTTP_X_FORWARDED_FOR'];
     } elseif ($_SERVER['REMOTE_ADDR']) {
         $client = $_SERVER['REMOTE_ADDR'];
     } else {
         $client = "-";
     }
     if ($location) {
         $msg = sprintf("%s. %s", $location, $msg);
     }
     if ($this->console === true || $priority <= $this->console) {
         if ($this->catch) {
             $this->LogOutput();
             echo "{$msg}\n";
             $this->SkipOutput();
         }
     }
     if (!is_dir("{$TMP_PATH}/log")) {
         if (!@mkdir("{$TMP_PATH}/log", 0777, true)) {
             return;
         }
         @chmod("{$TMP_PATH}/log", 0777);
     }
     $usec = str_pad($tod['usec'], 6, "0", STR_PAD_LEFT);
     $msg = preg_replace("/\n/", "[[BR]]", $msg);
     $info_str = urlencode(json_encode($info));
     #    $info_str = bin2hex(json_encode($info));
     $infolen = strlen($info_str);
     $msglen = strlen($msg);
     $msglen_str = sprintf("%d", $msglen);
     $fulllen = $msglen + $infolen + strlen(" \n");
     adeiSetSystemTimezone();
     $header = sprintf("%s %d %s %s %s %s %s %d %d", date("Y-m-d\\TH:i:s." . $usec . "0P", $tod['sec']), $priority, $setup, $source, $session, $pid, $client, $fulllen, $msglen);
     adeiRestoreTimezone();
     $header_size = strlen($header) + strlen("  ");
     $fname = sprintf("{$TMP_PATH}/log/%s.log", date("Ymd"));
     $umask = @umask(0);
     $f = @fopen($fname, "a+");
     if ($f) {
         if (flock($f, LOCK_EX)) {
             fprintf($f, "%" . LOGGER::SIZE_RECORD_LENGTH . "d %s %s %s\n", $header_size, $header, $msg, $info_str);
             flock($f, LOCK_UN);
         }
         fclose($f);
     }
     @umask($umask);
 }